CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Shortest Paths Dijkstra’s Algorithm A Faster Algorithm Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 1 / 16
Shortest Paths ■ Problems ■ Properties Dijkstra’s Algorithm A Faster Algorithm Bellman-Ford Shortest Path Problems Wheeler Ruml (UNH) Class 16, CS 758 – 2 / 16
Problems single source/destination pair Shortest Paths single source, all destinations: harder? ■ Problems ■ Properties single destination, all sources Dijkstra’s Algorithm all-pairs A Faster Algorithm Bellman-Ford non-uniform weights? negative weights? negative-weight cycles? Wheeler Ruml (UNH) Class 16, CS 758 – 3 / 16
Properties optimal substructure Shortest Paths triangle inequality: δ ( s, v ) ≤ δ ( s, u ) + w ( u, v ) ■ Problems ■ Properties ‘relaxing’: constraint is met Dijkstra’s Algorithm A Faster Algorithm Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 4 / 16
Shortest Paths Dijkstra’s Algorithm ■ Dijkstra ■ Algorithm ■ Correctness ■ Running Time ■ Break A Faster Algorithm Dijkstra’s Algorithm Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 5 / 16
Edsger W. Dijkstra 1930–2002; Turing Award ’72 Shortest Paths invented RPN, self-stabilizing Dijkstra’s Algorithm ■ Dijkstra algorithms, semaphores ■ Algorithm The goto statement ■ Correctness ■ Running Time considered harmful ■ Break structured programming A Faster Algorithm (while!), formal verification Bellman-Ford “I mean, if 10 years from now, when you are doing something quick and dirty, you suddenly visualize that I am looking over your shoulders and say to yourself ‘Dijkstra would not have liked this,’ well, that would be enough immortality for me.” Wheeler Ruml (UNH) Class 16, CS 758 – 6 / 16
The Algorithm 1. for each vertex, v.d ← ∞ Shortest Paths Dijkstra’s Algorithm 2. s.d ← 0 ■ Dijkstra 3. Q ← all vertices ■ Algorithm ■ Correctness 4. while Q is not empty ■ Running Time 5. u ← remove vertex in Q with smallest d ■ Break A Faster Algorithm 6. for each edge ( u, v ) from u Bellman-Ford 7. if v.d > u.d + w ( u, v ) 8. v.d ← u.d + w ( u, v ) 9. v.π ← u correctness? running time? negative weights? Wheeler Ruml (UNH) Class 16, CS 758 – 7 / 16
Correctness Key property: popped vertices have d = δ Shortest Paths Proof by induction. Dijkstra’s Algorithm ■ Dijkstra Base case: s ■ Algorithm ■ Correctness Assumption: previously popped vertices have d = δ ■ Running Time ■ Break Inductive Step: proof by contradiction. Consider freshly A Faster Algorithm popped v . Assume its current path is too long. Bellman-Ford Since d = δ for all previously popped, then if v ’s predecessor along the optimal path were previously popped, then v.d would be correct. So there exists an unpopped vertex u along the shortest path. Let u be the first such vertex in the path. Note u.d = δ ( s, u ) . Since it is earlier on the optimal path, u.d = δ ( s, u ) ≤ δ ( s, v ) < v.d . But then we would have popped u instead of v : contradiction! Wheeler Ruml (UNH) Class 16, CS 758 – 8 / 16
Running Time O (( V + E ) lg V ) Shortest Paths O ( V lg V + E ) using a Fibonacci heap Dijkstra’s Algorithm ■ Dijkstra O ( V + E ) for compact distances using a bucket heap ■ Algorithm (‘monotone heap’) ■ Correctness ■ Running Time ■ Break A Faster Algorithm Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 9 / 16
Break asst 9 ■ Shortest Paths asst 10 ■ Dijkstra’s Algorithm ■ Dijkstra grad school ■ ■ Algorithm ■ Correctness ■ Running Time ■ Break A Faster Algorithm Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 10 / 16
Shortest Paths Dijkstra’s Algorithm A Faster Algorithm ■ DAGs Bellman-Ford A Faster Algorithm Wheeler Ruml (UNH) Class 16, CS 758 – 11 / 16
An Algorithm for DAGs 1. for each vertex, v.d ← ∞ Shortest Paths Dijkstra’s Algorithm 2. s.d ← 0 A Faster Algorithm 4. for each vertex u in topologically sorted order ■ DAGs 6. for each successor v Bellman-Ford 7. if v.d > u.d + w ( u, v ) 8. v.d ← u.d + w ( u, v ) 9. v.π ← u correctness? running time? edge weights? Wheeler Ruml (UNH) Class 16, CS 758 – 12 / 16
Shortest Paths Dijkstra’s Algorithm A Faster Algorithm Bellman-Ford ■ Psuedo-Code ■ Correctness ■ EOLQs Bellman-Ford Wheeler Ruml (UNH) Class 16, CS 758 – 13 / 16
Psuedo-Code 1. for each vertex, v.d ← ∞ Shortest Paths Dijkstra’s Algorithm 2. s.d ← 0 A Faster Algorithm 3. repeat | V | times Bellman-Ford 4. for each edge ( u, v ) ■ Psuedo-Code ■ Correctness 5. if v.d > u.d + w ( u, v ) ■ EOLQs 6. v.d ← u.d + w ( u, v ) 7. v.π ← u correctness? running time? how to make it faster? negative cycles? Wheeler Ruml (UNH) Class 16, CS 758 – 14 / 16
Correctness Path relaxation: If we relax all the edges along a shortest u, v Shortest Paths path in order, then v.d = δ ( u, v ) , even if other relaxations are Dijkstra’s Algorithm performed. Proof: induction on length of path A Faster Algorithm Bellman-Ford: Proof: Consider a shortest path. Its length will Bellman-Ford ■ Psuedo-Code be ≤ | V | − 1 . Each Bellman-Ford iteration considers all edges. ■ Correctness ■ EOLQs Wheeler Ruml (UNH) Class 16, CS 758 – 15 / 16
EOLQs For example: Shortest Paths Dijkstra’s Algorithm What’s still confusing? ■ A Faster Algorithm What question didn’t you get to ask today? ■ Bellman-Ford What would you like to hear more about? ■ ■ Psuedo-Code ■ Correctness Please write down your most pressing question about algorithms ■ EOLQs and put it in the box on your way out. Thanks! Wheeler Ruml (UNH) Class 16, CS 758 – 16 / 16
Recommend
More recommend