Lecture 21 matrix operations and all pair shortest paths
Download
1 / 25

Lecture 21: Matrix Operations and All-pair Shortest Paths - PowerPoint PPT Presentation


  • 186 Views
  • Uploaded on

Lecture 21: Matrix Operations and All-pair Shortest Paths. Shang-Hua Teng. Matrix Basic. Vector: array of numbers; unit vector Inner product, outer product, norm Matrix: rectangular table of numbers, square matrix; Matrix transpose All zero matrix and all one matrix Identity matrix

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 21: Matrix Operations and All-pair Shortest Paths' - josef


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 21 matrix operations and all pair shortest paths

Lecture 21:Matrix Operations and All-pair Shortest Paths

Shang-Hua Teng


Matrix basic
Matrix Basic

  • Vector: array of numbers; unit vector

  • Inner product, outer product, norm

  • Matrix: rectangular table of numbers, square matrix; Matrix transpose

  • All zero matrix and all one matrix

  • Identity matrix

  • 0-1 matrix, Boolean matrix, matrix of graphs


Matrix of graphs

1

2

4

3

Matrix of Graphs

Adjacency Matrix:

  • If A(i, j) = 1: edge exists

    Else A(i, j) = 0.

1

2

-3

4

3


Matrix of graphs1

1

2

4

3

Matrix of Graphs

Weighted Matrix:

  • If A(i, j) = w(i,j): edge exists

    Else A(i, j) = infty.

1

2

-3

4

3


Matrix operations
Matrix Operations

  • Matrix-vector operation

    • System of linear equations

    • Eigenvalues and Eigenvectors

  • Matrix operations





Add and multiply
Add and Multiply

  • Rings:

  • Commutative, Associative

  • Distributive

  • Other rings



Two graph problems
Two Graph Problems

  • Transitive closure: whether there exists a path between every pair of vertices

    • generate a matrix closure showing all transitive closures

    • for instance, if a path exists from i to j, then closure[i, j] =1

  • All-pair shortest paths: shortest paths between every pair of vertices

    • Doing better than Bellman-Ford O(|V|2|E|)

  • They are very similar


Transitive closure
Transitive Closure

D

E

  • Given a digraph G, the transitive closure of G is the digraph G* such that

    • G* has the same vertices as G

    • if G has a directed path from u to v (u  v), G* has a directed edge from u to v

  • The transitive closure provides reachability information about a digraph

B

G

C

A

D

E

B

C

A

G*


Transitive closure and matrix multiplication

1

1

2

2

-3

4

4

3

3

Transitive Closure and Matrix Multiplication

  • Let A be the adjacency matrix of a graph G

A


Floyd warshall iteration 2
Floyd-Warshall, Iteration 2

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5




Even better idea dynamic programming floyd warshall
Even Better idea: Dynamic Programming; Floyd-Warshall

  • Number the vertices 1, 2, …, n.

  • Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices:

Uses only vertices numbered 1,…,k

(add this edge if it’s not already in)

i

j

Uses only vertices

numbered 1,…,k-1

Uses only vertices

numbered 1,…,k-1

k


Floyd warshall s algorithm
Floyd-Warshall’s Algorithm

A is the original matrix, T is the transitive matrix

T  A

for(k=1:n)

for(j=1:n)

for(i=1:n)

T[i, j] = T[i, j] OR

(T[i, k] AND T[k, j])

  • It should be obvious that the complexity is (n3) because of the 3 nested for-loops

  • T[i, j] =1 if there is a path from vertex i to vertex j


Floyd warshall example
Floyd-Warshall Example

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall iteration 1
Floyd-Warshall, Iteration 1

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall iteration 3
Floyd-Warshall, Iteration 3

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall iteration 4
Floyd-Warshall, Iteration 4

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall iteration 5

BOS

Floyd-Warshall, Iteration 5

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall iteration 6

BOS

Floyd-Warshall, Iteration 6

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


Floyd warshall conclusion

BOS

Floyd-Warshall, Conclusion

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5


ad