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