25
Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng

Lecture 21: Matrix Operations and All-pair Shortest Paths Shang-Hua Teng

  • View
    224

  • Download
    1

Embed Size (px)

Citation preview

Lecture 21:Matrix Operations and All-pair Shortest Paths

Shang-Hua Teng

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

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

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

Matrix Operations

• Matrix-vector operation– System of linear equations– Eigenvalues and Eigenvectors

• Matrix operations

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

2. Scalar Multiplication:

mnmm

n

n

mnmm

n

n

aaa

aaa

aaa

aaa

aaa

aaa

A

21

2 2221

11211

21

22221

11211

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

,

,

Add and Multiply

• Rings:

• Commutative, Associative

• Distributive

• Other rings

,,

,},1,0{

min,,

Matrix Multiplication Can be Defined on any Ring

tyConnectivi

,},1,0{

hMatrix witAdjacency

PathsShortest

min,,

hMatrix wit Weighted

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

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*

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

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

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

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

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)

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

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS