1.0 - Dynamic All-Pairs Shortest Paths Algorithms

Want to find the shortest path between all pairs of nodes in a graph.

1.1 - Recursive Definition of All-Pairs

1.1.1 - Naive, Slow Solution

1.1.2 - Improved Solution

This improved time complexity comes from the realisation that we don’t need to compute all $n-1$ matrices - we can compute the shortest paths containing 4 edges, from the set of shortest paths that contain 2 edges, and so on.

This means that we only compute a logarithmic amount of these shortest paths lgn\lg n vs nn.

faster_apsp(n): // L[m, i, j] is the weight of the shortest path from i to j of at most m edges L = new int[2(n-1) - 1][n][n] L[1] = weights m = 1 while m < n - 1 d = L[m] d' = L[2m] for i = 1 to n for j = 1 to n for k = 1 to n d'[i, j] = min(d'[i, j], d[i, k] + d[k, j]) m = 2m

1.2 - Floyd Warshall Algorithm

Store $\text{shortestPath}(i, j, k)$ at $d_{ij}^{(k)}$. That is $d_{ij}^{(k)}$ is the weight of t he shortest path from $i$ to $j$ using only intermediate vertices $1\cdots k$

1 floyd-warshall(W)2     n=W.rows3     D(0)=W4     for k=1 to n5         let D(k)=(dij(k)) be a new n×n matrix6         for i=1 to n7            for j=1 to n8                 dij(k)=min(dijk1,dikk1+dkjk1)9 return D(n)\def\t{\ \ \ \ } 1\ \text{floyd-warshall}(W)\\ 2\ \t n = W.\text{rows}\\ 3\ \t D^{(0)} = W\\ 4\ \t \bold{for\ } k = 1 \bold{\ to\ } n\\ 5\ \t\t \text{let } D^{(k)}=(d^{(k)}_{ij}) \text{ be a new } n \times n \text{ matrix}\\ 6\ \t\t\bold{for\ } i = 1 \bold{\ to \ } n\\ 7 \t\t\t \bold{for\ } j = 1\bold{\ to\ } n\\ 8 \ \t\t\t\t d^{(k)}_{ij}= \min(d^{k-1}_{ij}, d^{k-1}_{ik} + d^{k-1}_{kj})\\ 9 \ \bold{return\ } D^{(n)}

1.2.1 - Floyd Warshall Algorithm Example

1.3 - Aside: Johnson’s Algorithm