Honors Track: Competitive Programming & Problem Solving Avoiding negative edges Steven Ge
Content Negative Cycles Dijkstra’s Algorithm with negative edges Adding constant Bellman-Ford Algorithm Single source shortest path Johnson’s Algorithm All pair shortest path Suurballe’s Aglorithm Cheapest multiple, non-overlapping paths
Negative Cycles Graph 1 1 2 S D -10 Shortest Path?
Negative Cycles Graph 1 1 2 S D -10 Shortest Path? No Shortest Path!
Dijkstra’s with negative edges Graph A 10 -100 1 S B -2 D
Dijkstra’s with negative edges Shortest Path A 10 -100 1 S B 1 D Total length: -89
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: D B A A 10 -100 1 ∞ S B 1 D ∞
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: B A A 10 -100 1 ∞ S B 1 D ∞
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: B A A 10 -100 1 ∞ S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: A A 10 -100 1 ∞ S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: A A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: D A A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: A A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: A A 10 -100 1 1 S B 1 D No expansion on D
Dijkstra’s with negative edges Dijkstra’s with closed set ∞ PQ: A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set 10 PQ: B A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D
Dijkstra’s with negative edges Dijkstra’s with closed set 10 PQ: A 10 -100 1 1 S B 1 D B is in closed set, so no more expansions 0 > -89 Incorrect output, so don’t bother
Dijkstra’s with negative edges Dijkstra’s without closed set A 10 -100 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: T B A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T ∞
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: B A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T ∞
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: GgwND B A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: B A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: B A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: A A 10 -100 1 ∞ S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: A A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: T A A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: A A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set ∞ PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: B A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 1 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: T A 10 -100 1 -90 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: GgwND A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89
Dijkstra’s with negative edges Dijkstra’s without closed set 10 PQ: A 10 -100 1 -90 S B 1 Gigantic graph with Node D T -89 Correct output Takes very long
Dijkstra’s with negative cycles Graph PQ: A 1 1 2 ∞ S A B D ∞ ∞ -10
Dijkstra’s with negative cycles Graph PQ: 1 1 2 ∞ S A B D ∞ ∞ -10
Dijkstra’s with negative cycles Graph PQ: 1 1 2 ∞ S A B D ∞ 1 -10
Dijkstra’s with negative cycles Graph PQ: B 1 1 2 ∞ S A B D ∞ 1 -10
Dijkstra’s with negative cycles Graph PQ: 1 1 2 ∞ S A B D ∞ 1 -10
Dijkstra’s with negative cycles Graph PQ: 1 1 2 ∞ S A B D 1 2 -10
Dijkstra’s with negative cycles Graph PQ: A D 1 1 2 ∞ S A B D 1 2 -10
Dijkstra’s with negative cycles Graph PQ: D 1 1 2 ∞ S A B D 1 2 -10
Dijkstra’s with negative cycles Graph PQ: D 1 1 2 ∞ S A B D -8 2 -10
Dijkstra’s with negative cycles Graph PQ: B D 1 1 2 ∞ S A B D -8 2 -10
Dijkstra’s with negative cycles Graph PQ: D 1 1 2 ∞ S A B D -8 2 -10
Dijkstra’s with negative cycles Graph PQ: D 1 1 2 ∞ S A B D -8 -7 -10
Dijkstra’s with negative cycles Graph PQ: A D 1 1 2 ∞ S A B D -8 -7 -10 Etc.
Adding Constant Graph 1 1 1 -100 10 10
Adding Constant Shortest Path Add constant 100 and run Dijkstra’s? 1 1 -100 10 10 Add constant 100 and run Dijkstra’s?
Adding Constant Shortest Path Add constant 100 and run Dijkstra’s? 101 101 101 110 110 Add constant 100 and run Dijkstra’s? Difference in amount of edges
Bellman-Ford Similar to Dijkstra’s No priority queue Slower than Dijkstra’s O(V*E) More versatile than Dijkstra’s (can detect negative cycles) Used in Johnson’s Algorithm
Bellman-Ford Steps: 1) Initialize graph 2) Relax all edges v-1 times (v = amount vertices) In other words: For each vertex, check if the distance to current vertex + edge distance < distance to child vertex 3) Check for negative cycles For each edge, if the distance to current vertex + edge distance < distance to child vertex, return error
Bellman-Ford Graph A 100 50 -100 10 20 S B D 20 1 20 C
Bellman-Ford ∞ ∞ ∞ ∞ Step 1 ∞ S A B C D A 100 50 -100 10 20 S B D 20 1 ∞ S A B C D ∞ A 100 50 -100 ∞ 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ ∞ ∞ ∞ Step 2 ∞ S A B C D A 100 50 -100 10 20 S B D 20 1 ∞ S A B C D ∞ A 100 50 -100 ∞ 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ ∞ ∞ Step 2 100 ∞ S A B C D 100 A 100 50 -100 10 20 S B 100 ∞ S A B C D 100 A 100 50 -100 ∞ 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ ∞ ∞ Step 2 100 ∞ S A B C D 100 A 100 50 -100 10 20 S B 100 ∞ S A B C D 100 A 100 50 -100 ∞ 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ ∞ Step 2 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ ∞ Step 2 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20 100 10 ∞ S A B C D 100 A 100 50 -100 10 10 20 ∞ S B D 20 1 20 C ∞
Bellman-Ford ∞ Step 2 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20 ∞ S B D 20 1 20 C 1
Bellman-Ford ∞ Step 2 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20 100 10 1 ∞ S A B C D 100 A 100 50 -100 10 10 20 ∞ S B D 20 1 20 C 1
Bellman-Ford Step 2 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 S B D 150 20 1 20 C 1
Bellman-Ford Step 2 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 100 10 1 150 S A B C D 100 A 100 50 -100 10 10 20 S B D 150 20 1 20 C 1
Bellman-Ford Step 2 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 150 20 1 20 C 1
Bellman-Ford Step 2 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 150 S A B C D 100 A 100 50 -100 10 20 S B D 150 20 1 20 C 1
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1 After the second iteration, the distances don’t change This also means no negative cycles
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2
Bellman-Ford ∞ ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D ∞ ∞ ∞
Bellman-Ford ∞ ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D ∞ ∞ ∞
Bellman-Ford ∞ ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D ∞ ∞ ∞
Bellman-Ford ∞ ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D ∞ ∞ ∞
Bellman-Ford ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 1 1 1 1 S D ∞ ∞ 1
Bellman-Ford ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D ∞ ∞ 1
Bellman-Ford ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D ∞ ∞ 1
Bellman-Ford ∞ ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D ∞ ∞ 1
Bellman-Ford ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D ∞ 1 2
Bellman-Ford ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 2 1 1 1 S D ∞ 1 2
Bellman-Ford ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D ∞ 1 2
Bellman-Ford ∞ Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D ∞ 1 2
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3
Bellman-Ford Justification: Step 2 needs to be done at most v-1 times, because the optimal path has at most v-1 edges, otherwise it contains a cycle Step 3 follows from 2 Iteration: 3 1 1 1 S D 1 2 3 Contains cycle, because optimal path contains at most all the nodes All pairs, O(V2 * E)
Johnson’s Algorithm Reweight all the edges effectively Makes use of both Dijkstra’s and Bellman-Ford
Johnson’s Algorithm Steps: 1) Add new node q to graph 2) Connect node q to all other nodes in graph (directed towards other nodes) 3) Initialize graph with q as source 4) Run Bellman-Ford 5) Remove q and reweight the edges from u to v with the following formula Wnew(u, v) = Wold(u, v) + h(u) – h(v) 6) Run Dijkstra’s 7) Use the formula to get the original weight Wold(u, v) = Wnew(u, v) - h(u) + h(v)
Johnson’s Algorithm Graph A 100 50 -100 10 20 S B D 20 1 20 C
Johnson’s Algorithm Step 1 A 100 50 -100 10 20 S B D 20 1 20 C q
Johnson’s Algorithm Step 2 A 100 50 -100 10 20 S B D 20 1 20 C q
Johnson’s Algorithm Step 3 + 4 A 100 50 -100 10 -100 20 -80 S B D 20 1 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q
Johnson’s Algorithm Step 3 + 4 A 100 50 -100 10 -100 20 -80 S B D 20 1 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q
Johnson’s Algorithm Step 5 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C q
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v)
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v)
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 100 + 0 – 0 = 100
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 10 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 10 + 0 – -100 = 110
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 110 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 10 + 0 – -100 = 110
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 110 -100 20 -80 S B D 20 1 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 1 + 0 – -80 = 81
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 1 + 0 – -80 = 81
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 -100 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) -100 + 0 – -100 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) -100 + 0 – -100 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 110 -100 20 -80 S B D 20 81 20 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 20 + -100 – -80 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 20 + -100 – -80 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 50 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 50 + 0 – -80 = 130
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 130 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 50 + 0 – -80 = 130
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 130 110 -100 20 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 20 + -100 – -80 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 130 110 -100 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 20 + -100 – -80 = 0
Johnson’s Algorithm Step 5 Wnew(u, v) = Wold(u, v) + h(u) – h(v) A 100 130 110 -100 -80 S B D 20 81 -80 C Wnew(u, v) = Wold(u, v) + h(u) – h(v) 20 + -80 – -80 = 20
Johnson’s Algorithm Step 6 A 100 130 110 -100 -80 S B D 20 81 -80 C
Johnson’s Algorithm Step 6, shortest path to A A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 100 – 0 + 0 = 100
Johnson’s Algorithm Step 6, shortest path to A 100 A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 100 – 0 + 0 = 100
Johnson’s Algorithm Step 6, shortest path to B 100 A 100 130 110 -100 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) (100 + 0) – 0 + -100 = 0
Johnson’s Algorithm Step 6, shortest path to B 100 A 100 130 110 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) (100 + 0) – 0 + -100 = 0
Johnson’s Algorithm Step 6, shortest path to C 100 A 100 130 110 -80 S B D 20 81 -80 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 81 – 0 + -80 = 1
Johnson’s Algorithm Step 6, shortest path to C 100 A 100 130 110 -80 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) 81 – 0 + -80 = 1
Johnson’s Algorithm Step 6, shortest path to D 100 A 100 130 110 -80 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) (100 + 0 + 0) – 0 + -80 = 20
Johnson’s Algorithm Step 6, shortest path to D 100 A 100 130 110 20 S B D 20 81 1 C Wold(u, v) = Wnew(u, v) - h(u) + h(v) (100 + 0 + 0) – 0 + -80 = 20
Bellman-Ford Step 2 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 100 1 20 S A B C D 100 A 100 50 -100 10 20 S B D 20 20 1 20 C 1
Johnson’s Algorithm Using node q guaranties that all edges and nodes are checked Node q does not affect any properties of the graph Single source: Dijkstra: O((V + E) * log V) Bellman-Ford: O(V * E) Johnson: O(V * E) All pairs: Dijkstra: O((V2 + E) log V) Bellman-Ford: O(V2 * E) Johnson: O((V2 + E) log V)
Johnson’s Algorithm Correctness reweighting path: Claim: Every path with the same source and destination will be shifted equally Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. (Wold (S, V1) + Wold (V1, V2) + … + Wold (Vn, D) + h(S) – h(D)) = Wnew(S, D) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Every path with the same source and destination will be shifted equally Let P be a path from S to D (Wold (S, V1) + h(S) – h(V1)) + (Wold (V1, V2) + h(V1) – h(V2)) + … (Wold (Vn, D) + h(Vn) – h(D)) = Wnew(S, D) + h(Vi) and – h(Vi) cancel each other out. (Wold (S, V1) + Wold (V1, V2) + … + Wold (Vn, D) + h(S) – h(D)) = Wnew(S, D) Each path from S to T gets shifted a fixed amount, unaffected by the amount of edges Each path from S to T gets shifted the same amount. (Only with h(S) – h(D)) So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting edge: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Length of a path ≥ length of a shortest path Wold(u, v) + h(u) ≥ h(v) Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Johnson’s Algorithm Correctness reweighting: Claim: Wnew(u, v) = Wold(u, v) + h(u) – h(v) always creates non-negative edges Pick a random edge (u, v) from the original graph Use the q node from step 2 h(u) = length of a shortest path from q to u h(v) = length of a shortest path from q to v Length of a path from q to u to v is: Wold(u, v) + h(u) Length of a path ≥ length of a shortest path Wold(u, v) + h(u) ≥ h(v) Rewrite Wold(u, v) + h(u) – h(v) ≥ 0 Wnew ≥ 0 Each path from S to T gets shifted the same amount. So the shortest path of the set of paths from S to T will be the same in the new graph as in the old graph.
Suurballe’s Algorithm Finding disjoint paths in a network The paths have the shortest length compared to other set of paths with the same amount.
Suurballe’s Algorithm Steps: 1) Find the shortest path with Dijksta’s 2) Create a shortest path tree T from source S with Dijkstra’s 3) Use: Wnew(u, v) = Wold(u, v) + h(u) – h(v) to reweight the edges and reverse the edges of the shortest path 4) Rerun Dijkstra’s to find the second path 5) Remove the overlapping edges from the first and second path 6) connect the ends of the graphs together
Suurballe’s Algorithm Graph: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C
Suurballe’s Algorithm Step 1: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C
Suurballe’s Algorithm Step 2: 23 20 A E 8 23 5 15 S B D 28 12 3 C 12
Suurballe’s Algorithm Step 3: 99 23 20 A E 73 8 17 23 5 33 15 S B D 20 28 12 3 21 C 12
Suurballe’s Algorithm Step 3: 79 23 20 A E 68 25 25 15 S B D 33 28 5 C 12
Suurballe’s Algorithm Step 2 for comparison: Notice that all the edges in this graph becomes 0 Because of the formula 23 20 A E 8 23 5 15 S B D 28 12 3 C 12
Suurballe’s Algorithm Step 3: 79 A E 68 25 25 S B D 33 5 C
Suurballe’s Algorithm Step 4: 79 A E 68 25 25 S B D 33 5 C
Suurballe’s Algorithm Step 5: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C
Suurballe’s Algorithm Step 5: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C
Suurballe’s Algorithm Step 6: 99 A E 73 8 17 23 5 33 S B D 20 12 3 21 C
Suurballe’s Algorithm Extra constraint: Paths cannot go through the same node
Suurballe’s Algorithm Extra constraint: Paths cannot go through the same node
Suurballe’s Algorithm Extra constraint: Paths cannot go through the same node Split the node in out
Suurballe’s Algorithm Justification: Reweighting technique proven at Johnson’s Reversed edges of the original graph used as backward edges Similar to finding a minimum flow from S to D
Conclusion Single source: Graph without negative edges: Dijkstra’s algorithm Graph with negative edges: Bellman-Ford All pairs: Graph with negative edges: Johnson’s algorithm Finding multiple, non-overlapping shortest paths in a graph Suurballe’s algorithm
References Bellman-Ford: https://www.youtube.com/watch?v=W2fKGISUAtM Johnson’s: https://www.youtube.com/watch?v=b6LOHvCzmkI Suurballe’s: https://books.google.nl/books?id=SIkfR0lAN1wC&lpg=PA86&ots=LRIAi0ii6e https://books.google.nl/books?id=QsLMBQAAQBAJ&pg=PA70&lpg=PA70 http://www.eecs.yorku.ca/course_archive/2007-08/F/6590/Notes/surballe_alg.pdf Incorrect naming, but maybe nice to look at: http://www.macfreek.nl/memory/Disjoint_Path_Finding NWERC 2012 A - Admiral