Download presentation
Presentation is loading. Please wait.
1
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng
2
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
3
Matrix of Graphs Adjacency Matrix: If A(i, j) = 1: edge exists Else A(i, j) = 0. 12 34 1 -3 3 2 4
4
Matrix of Graphs Weighted Matrix: If A(i, j) = w(i,j): edge exists Else A(i, j) = infty. 12 34 1 -3 3 2 4
5
Matrix Operations Matrix-vector operation –System of linear equations –Eigenvalues and Eigenvectors Matrix operations
6
1. Matrix Addition:
7
2. Scalar Multiplication:
8
3. Matrix Multiplication
9
Add and Multiply Rings: Commutative, Associative Distributive Other rings
10
Matrix Multiplication Can be Defined on any Ring
11
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
12
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*
13
Transitive Closure and Matrix Multiplication Let A be the adjacency matrix of a graph G 12 34 1 -3 3 2 4 A
14
Floyd-Warshall, Iteration 2 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
15
Transitive Closure and Matrix Multiplication
16
A Better Idea
17
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)
18
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])
19
Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
20
Floyd-Warshall, Iteration 1 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
21
Floyd-Warshall, Iteration 3 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
22
Floyd-Warshall, Iteration 4 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
23
Floyd-Warshall, Iteration 5 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS
24
Floyd-Warshall, Iteration 6 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS
25
Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.