Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.

Similar presentations


Presentation on theme: "Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng."— Presentation transcript:

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 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

6 Transitive Closure 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 A D C E B A D C E G G*

7 Transitive Closure and Matrix Multiplication Let A be the adjacency matrix of a graph G 12 34 1 -3 3 2 4 A

8 Floyd-Warshall, Iteration 2 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

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: k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

12 Floyd-Warshall’s Algorithm It should be obvious that the complexity is  (n 3 ) because of the 3 nested for-loops T[i, j] =1 if there is a path from vertex i to vertex j 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])

13 Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

14 Floyd-Warshall, Iteration 1 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

15 Floyd-Warshall, Iteration 3 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

16 Floyd-Warshall, Iteration 4 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

17 Floyd-Warshall, Iteration 5 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

18 Floyd-Warshall, Iteration 6 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

19 Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

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 A 12 34 1 -3-3 3 2 4

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: k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

25 Floyd-Warshall Algorithm D ij (k) = length of shortest path from i to j with intermediate vertices from {1, 2,..., k}:  (i, j)= D ij (n) Dynamic Programming: recurrence –D ij (0) = D ij –D ij (k) = min {D ij (k-1), D ik (k-1) + D kj (k-1) } intermediate nodes in {1, 2,..., k} j i D ik (k-1) D kj (k-1) k D ij (k-1)

26 The Floyd-Warshall algorithm = { Floyd-Warshall(W)  (n 3 ) 1n  rows[W] 2 D (0) = W 3for k  1 to n 4 do for i  1 to n 5do for j  1 to n 6 7return D (n) calculate D (0),D (1),D (2), D (3),D (4) and D (5) 26 2 1 3 5 4 3 4 -4 -5 7 8 2 1 if k = 0 if k  1

27 The matrix  can be constructed within Floyd-Warshall as follows: = { calculate  (0),  (1),  (2),  (3),  (4) and  (5) 27 2 1 3 5 4 3 4 -4 -5 7 8 2 1 Nil if i = j or w ij =  i if i  j and w ij < 


Download ppt "Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng."

Similar presentations


Ads by Google