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

Loading...

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



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Lecture 22: Matrix Operations and All-pair Shortest Paths II

Description:

Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng Matrix Multiplication Add and Multiply Rings: Commutative, Associative Distributive Other ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 28
Provided by: STeng
Learn more at: http://www.cs.bu.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Lecture 22: Matrix Operations and All-pair Shortest Paths II


1
Lecture 22 Matrix Operations and All-pair
Shortest Paths II
  • Shang-Hua Teng

2
  • Matrix Multiplication

3
Add and Multiply
  • Rings
  • Commutative, Associative
  • Distributive
  • Other rings

4
Matrix Multiplication Can be Defined on any Ring
5
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

6
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
7
Transitive Closure and Matrix Multiplication
  • Let A be the adjacency matrix of a graph G

A
8
Floyd-Warshall, Iteration 2
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
9
Transitive Closure and Matrix Multiplication
10
A Better Idea
11
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
12
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

13
Floyd-Warshall Example
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
14
Floyd-Warshall, Iteration 1
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
15
Floyd-Warshall, Iteration 3
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
16
Floyd-Warshall, Iteration 4
BOS
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
17
Floyd-Warshall, Iteration 5
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
18
Floyd-Warshall, Iteration 6
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
19
Floyd-Warshall, Conclusion
v
ORD
4
JFK
v
v
2
6
SFO
DFW
LAX
v
3
v
1
MIA
v
5
20
All Pair Shortest Paths
  • Using matrix multiplication
  • Using Dynamic Programming

21
Shortest Path and Matrix Multiplication
  • Let W be the weighted matrix of a graph G

1
-3
2
4
3
A
22
Shortest Paths and Matrix Multiplication
23
A Better Idea
24
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
25
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)
26
  • 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
27
  • 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
About PowerShow.com