Loading...

PPT – Lecture 22: Matrix Operations and All-pair Shortest Paths II PowerPoint presentation | free to download - id: 68523b-NDA0O

The Adobe Flash plugin is needed to view this content

Lecture 22 Matrix Operations and All-pair

Shortest Paths II

- Shang-Hua Teng

- Matrix Multiplication

Add and Multiply

- Rings
- Commutative, Associative
- Distributive
- Other rings

Matrix Multiplication Can be Defined on any Ring

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

closurei, j 1 - All-pair shortest paths shortest paths between

every pair of vertices - Doing better than Bellman-Ford O(V2E)
- They are very similar

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

- Let A be the adjacency matrix of a graph G

A

Floyd-Warshall, Iteration 2

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5

Transitive Closure and Matrix Multiplication

A Better Idea

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 its not already in)

i

j

Uses only vertices numbered 1,,k-1

Uses only vertices numbered 1,,k-1

k

Floyd-Warshalls Algorithm

A is the original matrix, T is the transitive

matrix T ? A for(k1n) for(j1n)

for(i1n) Ti, j Ti, j OR

(Ti, k AND Tk, j)

- It should be obvious that the complexity is ?(n3)

because of the 3 nested for-loops - Ti, j 1 if there is a path from vertex i to

vertex j

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

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5

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

BOS

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5

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

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5

Floyd-Warshall, Conclusion

v

ORD

4

JFK

v

v

2

6

SFO

DFW

LAX

v

3

v

1

MIA

v

5

All Pair Shortest Paths

- Using matrix multiplication
- Using Dynamic Programming

Shortest Path and Matrix Multiplication

- Let W be the weighted matrix of a graph G

1

-3

2

4

3

A

Shortest Paths and Matrix Multiplication

A Better Idea

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 its not already in)

i

j

Uses only vertices numbered 1,,k-1

Uses only vertices numbered 1,,k-1

k

Floyd-Warshall Algorithm

- Dij(k) length of shortest path from i to j with

intermediate vertices from 1, 2, ..., k - ?(i, j) Dij(n)
- Dynamic Programming recurrence
- Dij(0) Dij
- Dij(k) min Dij(k-1) , Dik(k-1) Dkj(k-1)
- intermediate nodes in 1,

2, ..., k

Dkj(k-1)

k

Dik(k-1)

j

i

Dij(k-1)

- The Floyd-Warshall algorithm
- Floyd-Warshall(W) ?(n3)
- 1 n ? rowsW
- 2 D(0) W
- 3 for k? 1 to n
- 4 do for i? 1 to n
- 5 do for j ? 1 to n
- 6
- 7 return D(n)
- calculate D(0) ,D(1) ,D(2), D(3) ,D(4) and

D(5) - 26

if

k 0

if k ? 1

2

3 4

7

8

2

1 3

1

-4 -5

5 4

- The matrix ? can be constructed within

Floyd-Warshall as follows - calculate ?(0) , ?(1) , ?(2), ?(3) , ?(4)

and ?(5) - 27

Nil if i j or wij ?

i if i ? j and wij lt ?

2

3 4

7

8

2

1 3

1

-4 -5

5 4