Download Lecture Notes on Shortest Path Algorithms | CS 673 and more Study notes Algorithms and Programming in PDF only on Docsity!
17-0: Computing Shortest Path
- Given a directed weighted graph G (all weights non-negative) and two vertices x and y, find the least-cost path from x to y in G. - Undirected graph is a special case of a directed graph, with symmetric edges
- Least-cost path may not be the path containing the fewest edges
- “shortest path” == “least cost path”
- “path containing fewest edges” = “path containing fewest edges”
17-1: Shortest Path Example
- Shortest path 6 = path containing fewest edges
A
B
C
D
E
- Shortest Path from A to E?
17-2: Shortest Path Example
- Shortest path 6 = path containing fewest edges
A
B
C
D
E
- Shortest Path from A to E:
17-3: Single Source Shortest Path
- To find the shortest path from vertex x to vertex y, we need (worst case) to find the shortest path from x to all other vertices in the graph - Why?
17-4: Single Source Shortest Path
- To find the shortest path from vertex x to vertex y, we need (worst case) to find the shortest path from x to all other vertices in the graph - To find the shortest path from x to y, we need to find the shortest path from x to all nodes on the path from x to y - Worst case, all nodes will be on the path
17-5: Single Source Shortest Path
- If all edges have unit weight ...
17-6: Single Source Shortest Path
- If all edges have unit weight,
- We can use Breadth First Search to compute the shortest path
- BFS Spanning Tree contains shortest path to each node in the graph
- Need to do some more work to create & save BFS spanning tree
- When edges have differing weights, this obviously will not work
17-7: Single Source Shortest Path
- General Idea for finding Single Source Shortest Path
- Start with the distance estimate to each node (except the source) as ∞
- Repeatedly relax distance estimate until you can relax no more
- To relax and edge (u, v)
- dist(v) > dist(u) + cost((u, v))
- Set dist(v) ← dist(u) + cost((u, v))
17-8: Single Source Shortest Path
- Dijkstra’s algorithm
- Remarkably similar to Prim’s MST algorith
- Pretty neat – algorithms are doing different things, but code is almost identical
17-9: Single Source Shortest Path
- Divide the vertices into two sets:
- Vertices whose shortest path from the initial vertex is known
- Why is it safe to add D, with cost 1?
17-13: Single Source Shortest Path
A B
C D E
F G
Node Distance
A B C D E F G
- Why is it safe to add D, with cost 1?
- Could we do better with a more roundabout path?
17-14: Single Source Shortest Path
A (^) B
C D E
F G
2
4 1 3 10 2 2
5 8 4 6
1
Node Distance A B C D E F G 0
1
- Why is it safe to add D, with cost 1?
- Could we do better with a more roundabout path?
- No – to get to any other node will cost at least 1
- No negative edge weights, can’t do better than 1
17-15: Single Source Shortest Path
A B
C D E
F G
Node Distance
A B C D E F G
- We can now add another vertex to our known list ...
17-16: Single Source Shortest Path
A B
C D E
F G
Node Distance
A B C D E F G
- How do we know that we could not get to B cheaper than by going through D?
17-17: Single Source Shortest Path
A B
C D E
F G
Node Distance A B C D E F G 0
- How do we know that we could not get to B cheaper than by going through D?
- Costs 1 to get to D
- Costs at least 2 to get anywhere from D
- Cost at least (1+2 = 3) to get to B through D
17-18: Single Source Shortest Path
A B
C D E
F G
Node Distance
A B C D E F G
17-22: Single Source Shortest Path
A B
C D E
F G
Node Distance A B C D E F G 0
- We now know the length of the shortest path from A to all other vertices in the graph
17-23: Dijkstra’s Algorithm
- Keep a table that contains, for each vertex
- Is the distance to that vertex known?
- What is the best distance we’ve found so far?
- Repeat:
- Pick the smallest unknown distance
- mark it as known
- update the distance of all unknown neighbors of that node
- Until all vertices are known
17-24: Dijkstra’s Algorithm Example
A
B
C
D
E
F
Node Known Distance A false 0 B false ∞ C false ∞ D false ∞ E false ∞ F false ∞ 17-25: Dijkstra’s Algorithm Example
A
B
C
D
E
F
Node Known Distance A true 0 B false 7 C false 5 D false ∞ E false ∞ F false 1 17-26: Dijkstra’s Algorithm Example
A
B
C
D
E
F
Node Known Distance A true 0 B false 7 C false 5 D false 8 E false 3 F true 1 17-27: Dijkstra’s Algorithm Example
A
B
C
D
E
F
Node Known Distance A true 0 B false 7 C false 4 D false 8 E true 3 F true 1 17-28: Dijkstra’s Algorithm Example
- We know the length of the shortest path
- We do not know what the shortest path is
- How can we modify Dijstra’s algorithm to compute the path?
17-32: Dijkstra’s Algorithm
- After Dijkstra’s algorithm is complete:
- We know the length of the shortest path
- We do not know what the shortest path is
- How can we modify Dijstra’s algorithm to compute the path?
- Store not only the distance, but the immediate parent that led to this distance
17-33: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A false 0 B false ∞ C false ∞ D false ∞ E false ∞ F false ∞ G false ∞ 17-34: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B false 5 A C false 3 A D false ∞ E false ∞ F false ∞ G false ∞ 17-35: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B false 5 A C true 3 A D false 4 C E false ∞ F false ∞ G false ∞
17-36: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B false 5 A C true 3 A D true 4 C E false 9 D F false 9 D G false 7 D 17-37: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B true 5 A C true 3 A D true 4 C E false 9 D F false 9 D G false 7 D 17-38: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B true 5 A C true 3 A D true 4 C E false 9 D F false 8 G G true 7 D 17-39: Dijkstra’s Algorithm Example
A (^) B
C D E
F G
1
3
2
5
1
4 2
(^531)
1
5 Node Known Dist Path A true 0 B true 5 A C true 3 A D true 4 C E false 9 D F true 8 G G true 7 D 17-40: Dijkstra’s Algorithm Example
- Total time: O(|V | lg |V | + |E| lg |V |) ∈ O(|E| lg |V |)
17-45: Dijkstra Running Time
- If minUnknownVertex(T) is calculated by inserting all vertices into a Fibonacci heap (using distances as key) updating the heap as the distances are changed - Each minUnknownVertex call takes amortized time Θ(lg |V |) - Called |V | times – total amortized time for all calls to minUnknownVertex: Θ(|V | lg |V |) - If statement is executed |E| times – each time takes amortized time O(1), since decrementing keys takes time O(1). - Total time: O(|V | lg |V | + |E|)
17-46: Negative Edges
- Does Dijkstra’s algorithm work when edge costs can be negative?
- What happens if there is a negative-weight cycle in the graph?
17-47: Bellman-Ford
- Bellman-Ford allows us to calculate shortest paths in graphs with negative edge weights, as long as there are no negative-weight cycles
- As a bonus, we will also be able to detect negative-weight cycles
17-48: Bellman-Ford
- For each node v, maintiain:
- A “distance estimate” from source to v, d[v]
- Parent of v, π[v], that gives this distance estimate
- Start with d[v] = ∞, π[v] = nil for all nodes
- Set d[source] = 0
- udpate estimates by “relaxing” edges
17-49: Bellman-Ford
- Relaxing an edge (u, v)
- See if we can get a better distance estimate for v by going thorugh u
Relax(u,v,w) if d[v] > d[u] + w(u, v) d[v] ← d[u] + w(u, v) π[v] ← u
17-50: Bellman-Ford
- Relax all edges edges in the graph (in any order)
- Repeat until relax steps cause no change
- After first relaxing, all optimal paths from source of length 1 are computed
- After second relaxing, all optimal paths from source of length 2 are computed
- after |V | − 1 relaxing, all optimal paths of length |V | − 1 are computed
- If some path of length |V | is cheaper than a path of length |V | − 1 that means ...
17-51: Bellman-Ford
- Relax all edges edges in the graph (in any order)
- Repeat until relax steps cause no change
- After first relaxing, all optimal paths from source of length 1 are computed
- After second relaxing, all optimal paths from source of length 2 are computed
- after |V | − 1 relaxing, all optimal paths of length |V | − 1 are computed
- If some path of length |V | is cheaper than a path of length |V | − 1 that means ...
17-52: Bellman-Ford
BellamanFord(G, s) Initialize d[], π[] for i ← 1 to |V | − 1 do for each edge (u, v) ∈ G do if d[v] > d[u] + w(u, v) d[v] ← d[u] + w(u, v) π[v] ← u for each edge (u, v) ∈ G do if d[v] > d[u] + w(u, v) return false return true
17-53: Bellman-Ford
- Running time:
- Each iteration requires us to relax all |E| edges
- Each single relaxation takes time O(1)
- |V | − 1 iterations (|V | if we are checking for negative weight cycles)
- Total running time O(|V | ∗ |E|)
17-54: Shortest Path/DAGs
- Finding Single Source Shorest path in a Directed, Acyclic graph
- Very easy! How can we do this quickly?
17-55: Shortest Path/DAGs
- How can we calculate Lm[i, j] recursively?
L(m)[i, j] = min
L(m−1)[i, j], min 1 ≤k≤n (L(m−1)[i, k] + wkj )
= min 1 ≤k≤n (L(m−1)[i, k] + wkj )
17-61: Multi-Source Shortest Path
- Create L(m+1)^ from L(m):
Extend-Shortest-Paths(L, W ) n ← rows[L] L′^ ← new n × n matrix for i ← 1 to n do for j ← 1 to n do L′[i, j] ← ∞ for k ← 1 to n do L′[i, j] ← min(L′[i, j], L[i, k] + W [k, j]) return L′
17-62: Multi-Source Shortest Path
- Need to calculate L(n−1)
- Why L(n−1), and not L(n)^ or L(n+1)?
All-Pairs-Shortest-Paths(W ) n ← rows[W ] L(1)^ ← W for m ← 2 to n − 1 do L(m)^ ←Extend-Shortest-Path(L(m−1), W ) return L(n−1)
17-63: Multi-Source Shortest Path
- We really don’t care about any of the L matrices except L(n−1)
- We can save some time by not calculating all of the intermediate matrices L(1)^... L(n−2)
- Note that Extend-Shortest-Path looks a lot like matrix multiplication
17-64: Multi-Source Shortest Path
Square-Matrix-Multiply(A, B) n ← rows[A] C ← new n × n matrix for i ← 1 to n do for j ← 1 to n do C[i, j] ← 0 for k ← 1 to n do C[i, j] ← C[i, j] + A[i, k] ∗ B[k, j]) return L′
- Replace min with +, + with ∗
17-65: Multi-Source Shortest Path
- Using our “Extend-Multiplication”
- Replace + with min, ∗ with +
L(1)^ = L(0)^ ∗ W = W
L(1)^ = L(1)^ ∗ W = W 2
L(2)^ = L(2)^ ∗ W = W 3
L(3)^ = L(3)^ ∗ W = W 4
L(n−1)^ = L(n−2)^ ∗ W = W n−^1
17-66: Multi-Source Shortest Path
L(1)^ = W
L(2)^ = W 2 = W ∗ W
L(4)^ = W 4 = W 2 ∗ W 2
L(8)^ = W 8 = W 4 ∗ W 4
L^2
⌈lg(n−1)⌉ = L^2
⌈lg(n−1)⌉ = L^2
⌈lg(n−1)⌉ (^) − 1 ∗ L^2
⌈lg(n−1)⌉ (^) − 1
- Since L(n−1)^ = L(n)^ = L(n+1)^ =.. ., it doesn’t matter if n is an exact power of 2 – we just need to get to at least L(n−1), not hit it exactly
17-67: Multi-Source Shortest Path
All-Pairs-Shortest-Paths(W ) n ← rows[W ] L(1)^ ← W m ← 1 while m < n − 1 do L(2m)^ ←Extend-Shortest-Path(L(m), L(m)) m → m ∗ 2 return L(m)
17-68: Multi-Source Shortest Path
- Each call to Extend-Shortest-Path takes time:
of calls to Extend-Shortest-Path:
- Total time:
- Shortest 1-path from 1 to 3: 7
- Shortest 2-path from 1 to 3: 6
- Shortest 3-path from 1 to 3: 6
- Shortest 4-path from 1 to 3: 6
- Shortest 5-path from 1 to 3: 4
17-74: Floyd’s Algorithm
- Shortest n-path = Shortest path
- Shortest 0-path:
- ∞ if there is no direct link
- Cost of the direct link, otherwise
17-75: Floyd’s Algorithm
- Shortest n-path = Shortest path
- Shortest 0-path:
- ∞ if there is no direct link
- Cost of the direct link, otherwise
- If we could use the shortest k-path to find the shortest (k + 1) path, we would be set
17-76: Floyd’s Algorithm
- Shortest k-path from v to u either goes through vertex k, or it does not
- If not:
- Shortest k-path = shortest (k − 1)-path
- If so:
- Shortest k-path = shortest k − 1 path from v to k, followed by the shortest k − 1 path from k to w
17-77: Floyd’s Algorithm
- If we had the shortest k-path for all pairs (v,w), we could obtain the shortest k + 1-path for all pairs
- For each pair v, w, compare:
- length of the k-path from v to w
- length of the k-path from v to k appended to the k-path from k to w
- Set the k + 1 path from v to w to be the minimum of the two paths above
17-78: Floyd’s Algorithm
- Let Dk[v, w] be the length of the shortest k-path from v to w.
- D 0 [v, w] = cost of arc from v to w (∞ if no direct link)
- Dk[v, w] = MIN(Dk− 1 [v, w], Dk− 1 [v, k] + Dk− 1 [k, w])
- Create D 0 , use D 0 to create D 1 , use D 1 to create D 2 , and so on – until we have Dn
17-79: Floyd’s Algorithm
- Use a doubly-nested loop to create Dk from Dk− 1
- Use the same array to store Dk− 1 and Dk – just overwrite with the new values
- Embed this loop in a loop from 1..k
17-80: Floyd’s Algorithm
Floyd(Edge G[], int D[][]) { int i,j,k
Initialize D, D[i][j] = cost from i to j
for (k=0; k (D[i,k] + D[k,j]))) D[i][j] = D[i][k] + D[k][j] }
17-81: Floyd’s Algorithm
- We’ve only calculated the distance of the shortest path, not the path itself
- We can use a similar strategy to the PATH field for Dijkstra to store the path
- We will need a 2-D array to store the paths: P[i][j] = last vertex on shortest path from i to j
17-82: Johnson’s Algorithm
- Yet another all-pairs shortest path algorithm
- Time O(|V |^2 lg |V | + |V | ∗ |E|)
- If graph is dense (|E| ∈ Θ(|V |^2 )) , no better than Floyd
- If graph is sparse, better than Floyd
- Basic Idea: Run Dijkstra |V | times
- Need to modify graph to remove negative edges
17-83: Johnson’s Algorithm
- Reweighing Graph
- Create a new weight function wˆ, such that:
- For all pairs of vertices u, v ∈ V , a path from u to v is a shortest path using w if and only if it is also a shortest path using wˆ.