19
All-Pairs Shortest Paths with Matrix Multiplication Chandler Burfield March 15, 2013 Chandler Burfield APSP with Matrix Multiplication March 15, 2013 1 / 19

All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

All-Pairs Shortest Paths with Matrix Multiplication

Chandler Burfield

March 15, 2013

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 1 / 19

Page 2: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Outline

1 Seidel’s algorithm

2 Faster Matrix MulitplicaltionStrassen’s AlgorithmSpeeding up Seidel’s Algorithm

3 More Advanced APSP Algorithms that Use Matrix Multiplication

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 2 / 19

Page 3: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

All-Pairs Shortest Paths

Problem

To find the shortest pathbetween all vertices v 2 Vfor a graph G = (V ,E ).

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 3 / 19

Page 4: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Definitions

A: the input to Seidel’s algorithm

Let A be the adjacency matrix, an n x n boolean matrix where a 1represents an edge between node i and node j in the graph G.

D: the output of Seidel’s algorithm

Let D be the distance matrix, an n x n integer matrix with dij

representingthe length of the shortest path from vertex i to vertex j in the graph G.

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 4 / 19

Page 5: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Seidel’s Algorithm

Seidel(A)Let Z = A · ALet B be an n x n 0-1 matrix,

where bij

=

⇢1 if i 6= j and (a

ij

= 1 or zij

> 0)0 otherwise

IF 8i , j , i 6= j bij

= 1 thenReturn D = 2B-A

Let T = Seidel(B)Let X = T · AReturn n x n matrix D,

where dij

=

⇢2t

ij

if xij

� tij

· degree(j)2t

ij

� 1 if xij

< tij

· degree(j)

Function

Seidel’s algorithm is an APSP algorithm that can be used on unweighted,undirected graphs.

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 5 / 19

Page 6: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Analysis of Seidel’s Algorithm

Seidel(A)Let Z = A · ALet B be an n x n 0-1 matrix,

where bij

=

⇢1 if i 6= j and (a

ij

= 1 or zij

> 0)0 otherwise

IF 8i , j , i 6= j bij

= 1 thenReturn D = 2B-A

Let T = Seidel(B)Let X = T · AReturn n x n matrix D,

where dij

=

⇢2t

ij

if xij

� tij

· degree(j)2t

ij

� 1 if xij

< tij

· degree(j)

Runtime

O(M(n)logn)

where M(n)denotes thetime necessaryto multiply twon x n integermatricies

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 6 / 19

Page 7: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Example for Seidel’s Algorithm

University of Tokyo: Advanced Algorithms Summer 2010

Lecture 8 — 10 JuneLecturer: Christian Sommer Scribe: Florian Wagner

8.1 Graph Representation in Memory

There are several possibilities to represent a graph G = (V, E) in memory. Let the set ofnodes be V = {1, 2, . . . , n} with edges E � V � V , and let m := |E|.

1

6

2

5

3

4

1. Adjacency matrix

The adjacency matrix of a graph G = (V, E) is a |V |� |V | matrix A, where each entryaij is equal to 1 if there exists an edge e = (vi, vj) 2 E and 0 otherwise. In case aweight function w : E � R is given, then aij = w

�vi, vj

�.

The adjacency matrix of the graph depicted above is

A =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������.

Note that, since the graph in the example is undirected, the resulting matrix is sym-metric. Also, since there are no self loops, each entry in the main diagonal is zero.

2. Edge List

In an edge list L, every edge e is stored as a tuple (vi, vj) in a sorted list. The edgelist of the graph depicted above is:

L =�(1, 2), (1, 6), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5), (5, 6)

While being a space-e�cient way to store the graph, the access times of an edge listare slower than the access times of an adjacency matrix (for most edges).

8-1

Algorithm 2: Seidel’s Algorithm (All Pairs Distances)

function APD(A)1

begin2

Z � AA3

let B be a boolean matrix with bij �⇢

1 if i 6= j and�aij = 1 or zij > 0

0 otherwise4

if 8i, j . i 6= j � bij = 1 then5

return D � 2B � A6

end7

else8

T � APD(B)9

X � TA10

return D with dij �⇢

2tij if xij � tij · deg(j)2tij � 1 otherwise11

end12

end13

In the following, we explain how Algorithm 2 works using the example graph from above.At the beginning, the algorithm computes the square graph. Following one edge in the squaregraph amounts to following a path of length one or two in the original graph. In order toobtain the adjacency matrix of the square graph, first, the matrix A is squared (one matrixmultiplication) and stored into Z in line 3. The graph and the matrix for the example are:

1

6

2

5

3

4

Z = AA =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������=

�������

2 0 1 1 2 00 4 1 2 1 21 1 2 1 2 01 2 1 3 1 12 1 2 1 3 00 2 0 1 0 2

�������

The value of zij indicates the number of distinct paths between i and j of length 2. Second,the matrix B (the adjacency matrix of the square graph) is calculated in line 4, having a 1in each entry if the nodes are either directly connected by an edge or if there is at least one

8-6

University of Tokyo: Advanced Algorithms Summer 2010

Lecture 8 — 10 JuneLecturer: Christian Sommer Scribe: Florian Wagner

8.1 Graph Representation in Memory

There are several possibilities to represent a graph G = (V, E) in memory. Let the set ofnodes be V = {1, 2, . . . , n} with edges E � V � V , and let m := |E|.

1

6

2

5

3

4

1. Adjacency matrix

The adjacency matrix of a graph G = (V, E) is a |V |� |V | matrix A, where each entryaij is equal to 1 if there exists an edge e = (vi, vj) 2 E and 0 otherwise. In case aweight function w : E � R is given, then aij = w

�vi, vj

�.

The adjacency matrix of the graph depicted above is

A =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������.

Note that, since the graph in the example is undirected, the resulting matrix is sym-metric. Also, since there are no self loops, each entry in the main diagonal is zero.

2. Edge List

In an edge list L, every edge e is stored as a tuple (vi, vj) in a sorted list. The edgelist of the graph depicted above is:

L =�(1, 2), (1, 6), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5), (5, 6)

While being a space-e�cient way to store the graph, the access times of an edge listare slower than the access times of an adjacency matrix (for most edges).

8-1

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 7 / 19

Page 8: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Example for Seidel’s Algorithm Continued

Algorithm 2: Seidel’s Algorithm (All Pairs Distances)

function APD(A)1

begin2

Z � AA3

let B be a boolean matrix with bij �⇢

1 if i 6= j and�aij = 1 or zij > 0

0 otherwise4

if 8i, j . i 6= j � bij = 1 then5

return D � 2B � A6

end7

else8

T � APD(B)9

X � TA10

return D with dij �⇢

2tij if xij � tij · deg(j)2tij � 1 otherwise11

end12

end13

In the following, we explain how Algorithm 2 works using the example graph from above.At the beginning, the algorithm computes the square graph. Following one edge in the squaregraph amounts to following a path of length one or two in the original graph. In order toobtain the adjacency matrix of the square graph, first, the matrix A is squared (one matrixmultiplication) and stored into Z in line 3. The graph and the matrix for the example are:

1

6

2

5

3

4

Z = AA =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������=

�������

2 0 1 1 2 00 4 1 2 1 21 1 2 1 2 01 2 1 3 1 12 1 2 1 3 00 2 0 1 0 2

�������

The value of zij indicates the number of distinct paths between i and j of length 2. Second,the matrix B (the adjacency matrix of the square graph) is calculated in line 4, having a 1in each entry if the nodes are either directly connected by an edge or if there is at least one

8-6

path of length two between these two nodes. For the example, B is:

B =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 01 1 1 0 1 11 1 1 1 0 11 1 0 1 1 0

�������

Apart from the entries of the main diagonal, only b36 and b63 are 0. Note that there is indeedno path of length one or two between nodes 3 and 6 of the graph.

Next, if all the entries of B (except for the diagonal) are 1, then all pairs of nodes in thegraph defined by A are connected by a path of length at most 2. It remains to distinguishpairs for which the distance is 1 from pairs for which the distance is 2. B is multiplied by2 and A is subtracted (line 6). This way, if the path has length 2, then the entry in A is 0and the resulting value will be 2. Otherwise, the value of A will be subtracted, yielding 1for this entry.

Otherwise, the algorithm is called recursively on matrix B in line 9. In the recursivecall, the algorithm starts with the square graph, computes the square of the square graph,etc. Let us get back to our example. We use subscript 2 for the names of the matrices inthe recursive call. The matrix A2 = B is squared:

Z2 = A2A2 = BB =

�������

5 4 3 4 4 34 5 3 4 4 33 3 4 3 3 44 4 3 5 4 34 4 3 4 5 33 3 4 3 3 4

�������

Next, B2 is computed:

B2 =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 11 1 1 0 1 11 1 1 1 0 11 1 1 1 1 0

�������

This time, all the entries of B2 are 1, except for the main diagonal. That is, the matrixdescribes a complete graph Kn:

1

6

2

5

3

4

8-7

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 8 / 19

Page 9: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Example for Seidel’s Algorithm Continued

path of length two between these two nodes. For the example, B is:

B =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 01 1 1 0 1 11 1 1 1 0 11 1 0 1 1 0

�������

Apart from the entries of the main diagonal, only b36 and b63 are 0. Note that there is indeedno path of length one or two between nodes 3 and 6 of the graph.

Next, if all the entries of B (except for the diagonal) are 1, then all pairs of nodes in thegraph defined by A are connected by a path of length at most 2. It remains to distinguishpairs for which the distance is 1 from pairs for which the distance is 2. B is multiplied by2 and A is subtracted (line 6). This way, if the path has length 2, then the entry in A is 0and the resulting value will be 2. Otherwise, the value of A will be subtracted, yielding 1for this entry.

Otherwise, the algorithm is called recursively on matrix B in line 9. In the recursivecall, the algorithm starts with the square graph, computes the square of the square graph,etc. Let us get back to our example. We use subscript 2 for the names of the matrices inthe recursive call. The matrix A2 = B is squared:

Z2 = A2A2 = BB =

�������

5 4 3 4 4 34 5 3 4 4 33 3 4 3 3 44 4 3 5 4 34 4 3 4 5 33 3 4 3 3 4

�������

Next, B2 is computed:

B2 =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 11 1 1 0 1 11 1 1 1 0 11 1 1 1 1 0

�������

This time, all the entries of B2 are 1, except for the main diagonal. That is, the matrixdescribes a complete graph Kn:

1

6

2

5

3

4

8-7

path of length two between these two nodes. For the example, B is:

B =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 01 1 1 0 1 11 1 1 1 0 11 1 0 1 1 0

�������

Apart from the entries of the main diagonal, only b36 and b63 are 0. Note that there is indeedno path of length one or two between nodes 3 and 6 of the graph.

Next, if all the entries of B (except for the diagonal) are 1, then all pairs of nodes in thegraph defined by A are connected by a path of length at most 2. It remains to distinguishpairs for which the distance is 1 from pairs for which the distance is 2. B is multiplied by2 and A is subtracted (line 6). This way, if the path has length 2, then the entry in A is 0and the resulting value will be 2. Otherwise, the value of A will be subtracted, yielding 1for this entry.

Otherwise, the algorithm is called recursively on matrix B in line 9. In the recursivecall, the algorithm starts with the square graph, computes the square of the square graph,etc. Let us get back to our example. We use subscript 2 for the names of the matrices inthe recursive call. The matrix A2 = B is squared:

Z2 = A2A2 = BB =

�������

5 4 3 4 4 34 5 3 4 4 33 3 4 3 3 44 4 3 5 4 34 4 3 4 5 33 3 4 3 3 4

�������

Next, B2 is computed:

B2 =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 11 1 1 0 1 11 1 1 1 0 11 1 1 1 1 0

�������

This time, all the entries of B2 are 1, except for the main diagonal. That is, the matrixdescribes a complete graph Kn:

1

6

2

5

3

4

8-7

path of length two between these two nodes. For the example, B is:

B =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 01 1 1 0 1 11 1 1 1 0 11 1 0 1 1 0

�������

Apart from the entries of the main diagonal, only b36 and b63 are 0. Note that there is indeedno path of length one or two between nodes 3 and 6 of the graph.

Next, if all the entries of B (except for the diagonal) are 1, then all pairs of nodes in thegraph defined by A are connected by a path of length at most 2. It remains to distinguishpairs for which the distance is 1 from pairs for which the distance is 2. B is multiplied by2 and A is subtracted (line 6). This way, if the path has length 2, then the entry in A is 0and the resulting value will be 2. Otherwise, the value of A will be subtracted, yielding 1for this entry.

Otherwise, the algorithm is called recursively on matrix B in line 9. In the recursivecall, the algorithm starts with the square graph, computes the square of the square graph,etc. Let us get back to our example. We use subscript 2 for the names of the matrices inthe recursive call. The matrix A2 = B is squared:

Z2 = A2A2 = BB =

�������

5 4 3 4 4 34 5 3 4 4 33 3 4 3 3 44 4 3 5 4 34 4 3 4 5 33 3 4 3 3 4

�������

Next, B2 is computed:

B2 =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 11 1 1 0 1 11 1 1 1 0 11 1 1 1 1 0

�������

This time, all the entries of B2 are 1, except for the main diagonal. That is, the matrixdescribes a complete graph Kn:

1

6

2

5

3

4

8-7

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 9 / 19

Page 10: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Example for Seidel’s Algorithm ContinuedThe recursion ends, B2 is multiplied by 2 and A2 = B is subtracted. The resulting matrixis returned to the first call of the function and stored in T :

T =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 21 1 1 0 1 11 1 1 1 0 11 1 2 1 1 0

�������

Note that, according to T , the distance from node 3 to node 6 is 2, meaning that, since Tcorresponds to the distance in the square graph, the distance could be either 4 or also (likein this example) 3. It remains to distinguish two cases: whether the actual distance fromnode i to node j is 2tij or 2tij � 1. In order to distinguish these two cases, the matrix T ismultiplied with A and stored in X:

X = AT =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 21 1 1 0 1 11 1 1 1 0 11 1 2 1 1 0

�������=

�������

2 3 2 3 3 11 4 1 2 2 23 3 2 2 4 22 3 1 3 2 22 3 2 2 3 11 5 2 4 2 2

�������

Finally, in line 11 the resulting matrix D is returned, containing, for each of its entries, eitherthe original value or the decrement of it. The following four propositions shall explain thisformula.

Proposition 8.1. If the distance dij is even, the resulting distance is 2tij and 2tij � 1otherwise.

Proof: If dij is even, i.e. dij = 2s and the shortest path is �v0, v1, . . . , v2s� then the path�v0, v2, v4, . . . , v2s�2, v2s� is the shortest path in the modified graph resulting from the squarematrix. If the distance dij is odd, i.e. dij = 2s � 1, then the shortest path in the modifiedgraph is �v0, v2, v4, . . . , v2s�4, v2s�2, v2s�1�. �

Next a trivial claim:

Proposition 8.2. Let i and j be a pair of distinct vertices in G. For any neighbor k of j,we have

dij � 1 dik dij + 1

Proof: By taking one step the distance can only change by 1 or it remains the same,depending on whether the neighbor k is the first node on a shortest path from i to j or not(see figure below).

8-8

The recursion ends, B2 is multiplied by 2 and A2 = B is subtracted. The resulting matrixis returned to the first call of the function and stored in T :

T =

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 21 1 1 0 1 11 1 1 1 0 11 1 2 1 1 0

�������

Note that, according to T , the distance from node 3 to node 6 is 2, meaning that, since Tcorresponds to the distance in the square graph, the distance could be either 4 or also (likein this example) 3. It remains to distinguish two cases: whether the actual distance fromnode i to node j is 2tij or 2tij � 1. In order to distinguish these two cases, the matrix T ismultiplied with A and stored in X:

X = AT =

�������

0 1 0 0 0 11 0 1 1 1 00 1 0 1 0 00 1 1 0 1 00 1 0 1 0 11 0 0 0 1 0

�������

�������

0 1 1 1 1 11 0 1 1 1 11 1 0 1 1 21 1 1 0 1 11 1 1 1 0 11 1 2 1 1 0

�������=

�������

2 3 2 3 3 11 4 1 2 2 23 3 2 2 4 22 3 1 3 2 22 3 2 2 3 11 5 2 4 2 2

�������

Finally, in line 11 the resulting matrix D is returned, containing, for each of its entries, eitherthe original value or the decrement of it. The following four propositions shall explain thisformula.

Proposition 8.1. If the distance dij is even, the resulting distance is 2tij and 2tij � 1otherwise.

Proof: If dij is even, i.e. dij = 2s and the shortest path is �v0, v1, . . . , v2s� then the path�v0, v2, v4, . . . , v2s�2, v2s� is the shortest path in the modified graph resulting from the squarematrix. If the distance dij is odd, i.e. dij = 2s � 1, then the shortest path in the modifiedgraph is �v0, v2, v4, . . . , v2s�4, v2s�2, v2s�1�. �

Next a trivial claim:

Proposition 8.2. Let i and j be a pair of distinct vertices in G. For any neighbor k of j,we have

dij � 1 dik dij + 1

Proof: By taking one step the distance can only change by 1 or it remains the same,depending on whether the neighbor k is the first node on a shortest path from i to j or not(see figure below).

8-8

i

k j

j

Furthermore, there exists a neighbor k of i such that dik = dij � 1

Proposition 8.3.

dij is even � tik � tij for all neighbors k of j

dij is odd � tik tij for all neighbors k of j, and

tik < tij for at least one neighbor k of j

Proof: If dij is even, i.e. dij = 2l, then tij = l. There also exists a k such that dkj � 2l � 1.

tkj � dkj

2� l � 1

2

Distances are integral, so tkj � tij. The proof for an odd dij is analogous. �

Proposition 8.4. Let �(i) be the set of neighbors of node i.�

k2�(i)

tkj � tij deg(i) � dij is even

k2�(i)

tkj < tij deg(i) � dij is odd

Thus, the resulting matrix D for this example is:

D =

�������

0 1 2 2 2 11 0 1 1 1 22 1 0 1 2 32 1 1 0 1 22 1 2 1 0 11 2 3 2 1 0

�������

In an unweighted, connected graph, the maximum distance is at most n. In each recursive callof Seidel’s algorithm, distances are doubled. The recursion depth is thus at most logarithmicin n. Therefore, the total running time is the required time to multiply two integer matrices,multiplied by O(log n).

8-9

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 10 / 19

Page 11: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Correctness of Seidel’s Algorithm

Lemma 1:

If the distance dij

is even then the resulting distance is 2tij

and 2tij

-1otherwise.

Lemma 2:

Let i and j be a pair of distinct vertices in G. For any neighbor k of j , wehave d

ij

-1 dik

dij

+ 1. More over, there exists a neighbor k of j withdik

= dij

+ 1

Lemma 3:

If dij

is even then tik

� tij

for all neighbors of k and jIf d

ij

is odd then tik

tij

for all neighbors of k of j ,tik

< tij

for at least one neighbor k of j

Lemma 4:

Let �(j) be the set of neighbors of node j , thendij

is even i↵P

k2�(j)

tik

� tij

· degree(j)dij

is odd i↵P

k2�(j)

tik

< tij

· degree(j)Chandler Burfield APSP with Matrix Multiplication March 15, 2013 11 / 19

Page 12: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Square Matrix Multiplication

Naively square matrix multiplication takes O(n3) time.

Strassen’s algorithm can preform it in O(n2.807)

The fastest algorithm works in O(n2.373)

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 12 / 19

Page 13: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Naive Matrix Multiplication Algorithm

!

This can be done in O(n3) time.

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 13 / 19

Page 14: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Naive Matrix Multiplication for 2x2 Matricies

!

Analysis of Algorithm

Recurrence Relation: T (n) = 8T (n/2) + O(n2)

Runtime: O(nlog8/log2) = O(n3)

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 14 / 19

Page 15: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Strassen’s Algorithm for 2x2 Matrices

2 x 2 Matrices:

!

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 15 / 19

Page 16: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Strassen’s Algorithm

Strassen for n x n Matrices

We can think of an n x n matrix as a 2 x 2 matrix whose elements are n

2

xn

2

matrices and apply the 2x2 algorithm recursively.

Analysis of Algorithm

Recurrence Relation:T (n) = 7T (n/2) + O(n2)

Runtime:O(nlog7/log2) = O(n2.807)

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 16 / 19

Page 17: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Fast Matrix Multiplication and Seidel’s Algorithm

Recall that Seidel’s Algorithm runs in O(M(n)logn).

Using Strassen’s algorithm instead of the naive algorithm, wedecrease the runtime from O(n3logn) to O(n2.81logn)).

If we used the fastest algorithm available to date, we could achieve aruntime of O(n2.373logn).

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 17 / 19

Page 18: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

Other APSP Algorithms Involving Matrix Multiplication

Consider integer edge weights on the set {0,1,.., M}.

APSP in weighted, undirected graphs: Shoshan-Zwick ’99, O(Mn2.38)

APSP in weighted, directed graphs: Zwick ’98, O(M0.68n2.58)

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 18 / 19

Page 19: All-Pairs Shortest Paths with Matrix Multiplicationmath.mit.edu/~rothvoss/18.304.1PM/Presentations/2-Chandler-slideslect2.pdf · All-Pairs Shortest Paths Problem To find the shortest

APSP Algorithms with Integer Weights {0, 1, 2,..., M}that Use Matrix Multiplication

Graph Algorithm Runtime

Unweighted and Undirected Seidel ’95 n2.38

Weighted and Undirected Shoshan-Zwick ’99 Mn2.38

Weighted and Directed Zwick ’98 M0.68n2.58

Chandler Burfield APSP with Matrix Multiplication March 15, 2013 19 / 19