1.38k likes | 1.56k Views
Graphs: MSTs and Shortest Paths. David Kauchak cs161 Summer 2009. Administrative. Grading final grade extra credit TA issues/concerns HW6 shorter will be due Wed. 8/12 before class Errors in class slides and notes Anonymized scores posted. A. B. C. E. D. Shortest paths.
E N D
Graphs: MSTsand Shortest Paths David Kauchak cs161 Summer 2009
Administrative • Grading • final grade • extra credit • TA issues/concerns • HW6 • shorter • will be due Wed. 8/12 before class • Errors in class slides and notes • Anonymized scores posted
A B C E D Shortest paths • What is the shortest path from a to d?
A B C E D Shortest paths • BFS
A B C E D Shortest paths • What is the shortest path from a to d? 2 3 3 2 1 1 4
A B C E D Shortest paths • We can still use BFS 2 3 3 2 1 1 4
D A B D E E B A C C Shortest paths • We can still use BFS 2 3 3 2 1 1 4
A B C E D Shortest paths • We can still use BFS
A B C E D Shortest paths • What is the problem?
A B C A B C Shortest paths • Running time is dependent on the weights 100 2 50 1 200 4
A B C A B C Shortest paths 100 50 200
A B C Shortest paths
A B C Shortest paths
A B C Shortest paths • Nothing will change as we expand the frontier until we’ve gone out 100 levels
Dijkstra’s algorithm prev keeps track of the shortest path
Single source shortest paths • All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms • Why?
A B C E D 3 3 1 2 1 1 4
A B C E D 3 3 1 2 1 1 4
A B C E D Heap A 0B CDE 3 3 0 1 2 1 1 4
A B C E D Heap B CDE 3 3 0 1 2 1 1 4
A B C E D Heap B CDE 3 3 0 1 2 1 1 4
A B C E D Heap C 1B DE 3 3 0 1 2 1 1 1 4
A B C E D Heap C 1B DE 3 3 0 1 2 1 1 1 4
A B C E D Heap C 1B 3DE 3 3 3 0 1 2 1 1 1 4
A B C E D Heap C 1B3DE 3 3 3 0 1 2 1 1 1 4
A B C E D Heap B3DE 3 3 3 0 1 2 1 1 1 4
A B C E D Heap B3DE 3 3 3 0 1 2 1 1 1 4
A B C E D Heap B3DE 3 3 3 0 1 2 1 1 1 4
A B C E D Heap B2DE 2 3 3 0 1 2 1 1 1 4
A B C E D Heap B2DE 2 3 3 0 1 2 1 1 1 4
A B C E D Heap B2E 5D 2 3 3 0 1 2 1 1 1 5 4
A B C E D Heap B2E 5D 2 3 3 0 1 Frontier? 2 1 1 1 5 4
A B C E D Heap B2E 5D 2 3 3 0 All nodes reachable from starting node within a given distance 1 2 1 1 1 5 4
A B C E D Heap E3D 5 2 5 3 3 0 1 2 1 1 1 3 4
A B C E D Heap D 5 2 5 3 3 0 1 2 1 1 1 3 4
A B C E D Heap 2 5 3 3 0 1 2 1 1 1 3 4
A B C E D Heap 2 5 3 0 1 1 1 1 3
Is Dijkstra’s algorithm correct? • Invariant:
Is Dijkstra’s algorithm correct? • Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v
Is Dijkstra’s algorithm correct? • Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v • The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex • Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path
Running time? 1 call to MakeHeap
Running time? |V| iterations
Running time? |V| calls
Running time? O(|E|) calls