![Page 1: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/1.jpg)
Lecture 21:Matrix Operations and All-pair Shortest Paths
Shang-Hua Teng
![Page 2: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/2.jpg)
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
![Page 3: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/3.jpg)
Matrix of GraphsAdjacency Matrix:• If A(i, j) = 1: edge exists
Else A(i, j) = 0.
0101
0000
1100
00101 2
34
1
-3
3
2 4
![Page 4: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/4.jpg)
Matrix of GraphsWeighted Matrix:• If A(i, j) = w(i,j): edge exists
Else A(i, j) = infty.
032
0
430
101 2
34
1
-3
3
2 4
![Page 5: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/5.jpg)
Matrix Operations
• Matrix-vector operation– System of linear equations– Eigenvalues and Eigenvectors
• Matrix operations
![Page 6: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/6.jpg)
1. Matrix Addition:
mnmnmm
nn
nn
mnmm
n
n
mnmm
n
n
baba
bababa
bababa
BA
bbb
bbb
bbb
B
a aa
a aa
a aa
A
,
,
11
2222222121
1112121111
21
22221
11211
21
22221
11211
![Page 7: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/7.jpg)
2. Scalar Multiplication:
mnmm
n
n
mnmm
n
n
aaa
aaa
aaa
aaa
aaa
aaa
A
21
2 2221
11211
21
22221
11211
![Page 8: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/8.jpg)
3. Matrix Multiplication
n
i ipmiimi
n
i ipi
n
i ii
n
i ii
n
i ipi
n
i ii
n
i ii
npnn
p
p
mnmm
n
n
baba
bababa
bababa
BA
bbb
bbb
bbb
B
aaa
aaa
aaa
A
1
n
1=i 1
1 21 221 12
1 11 211 11
21
22221
11211
21
22221
11211
,
,
![Page 9: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/9.jpg)
Add and Multiply
• Rings:
• Commutative, Associative
• Distributive
• Other rings
,,
,},1,0{
min,,
![Page 10: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/10.jpg)
Matrix Multiplication Can be Defined on any Ring
tyConnectivi
,},1,0{
hMatrix witAdjacency
PathsShortest
min,,
hMatrix wit Weighted
![Page 11: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/11.jpg)
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
![Page 12: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/12.jpg)
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*
![Page 13: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/13.jpg)
Transitive Closure and Matrix Multiplication
• Let A be the adjacency matrix of a graph G
•
0101
0000
1100
00101 2
34
1
-3
3
2 4
A
?,},1,0{by defined is
tionmultiplicamatrix the where, isWhat 2
A
)(2kjik
kijAAA
![Page 14: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/14.jpg)
Floyd-Warshall, Iteration 2
JFK
BOS
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
![Page 15: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/15.jpg)
Transitive Closure and Matrix Multiplication
algorithm time
?... isWhat
? isWhat
? isWhat
j toi connects that
edges twopath with a exists thereiff 1
4
12
4
3
2
VO
AAA
A
A
A
n
ij
![Page 16: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/16.jpg)
A Better Idea
algorithm timelog
? isWhat
? isWhat
? compute toNeed
? isWhat
...let general,In
Let
3
22
1
2
22
VVO
AA
AA
A
A
AAAA
AAA
kk
n
k
kk
![Page 17: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/17.jpg)
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 verticesnumbered 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)
![Page 18: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/18.jpg)
Floyd-Warshall’s Algorithm
• 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
A is the original matrix, T is the transitive matrix T Afor(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])
![Page 19: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/19.jpg)
Floyd-Warshall Example
JFK
BOS
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
![Page 20: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/20.jpg)
Floyd-Warshall, Iteration 1
JFK
BOS
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
![Page 21: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/21.jpg)
Floyd-Warshall, Iteration 3
JFK
BOS
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
![Page 22: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/22.jpg)
Floyd-Warshall, Iteration 4
JFK
BOS
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
![Page 23: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/23.jpg)
Floyd-Warshall, Iteration 5
JFK
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
BOS
![Page 24: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/24.jpg)
Floyd-Warshall, Iteration 6
JFK
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
BOS
![Page 25: Lecture 21: Matrix Operations and All-pair Shortest Paths](https://reader036.vdocuments.us/reader036/viewer/2022062323/56815931550346895dc66755/html5/thumbnails/25.jpg)
Floyd-Warshall, Conclusion
JFK
MIA
ORD
LAXDFW
SFO
v2
v1v3
v4
v5
v6
v7
BOS