Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithm Analysis Lecture 8

Similar presentations


Presentation on theme: "Data Structures and Algorithm Analysis Lecture 8"— Presentation transcript:

1 Data Structures and Algorithm Analysis Lecture 8
CSCI 256 Data Structures and Algorithm Analysis Lecture 8 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

2 Recall Greedy Analysis Strategies
Greedy algorithm stays ahead: Show that after each step of the greedy algorithm, its solution is at least as good as any other algorithm's Exchange argument: Gradually transform any solution to the one found by the greedy algorithm without hurting its quality

3 Shortest Path Problem Directed graph G = (V, E)
Source s, destination t Length e = length of edge e Shortest path problem: find shortest directed path from s to t cost of path = sum of edge costs in path 2 23 3 9 s Cost of path s t = = 48. 18 14 2 6 6 30 4 19 5 11 15 5 6 20 16 t 7 44

4 Dijkstra's Algorithm choose Dijkstra's algorithm: determines the length of the shortest path from source node s to each other node in the graph: Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u Initialize S = { s }, d(s) = 0 while for each unexplored node v find shortest path that can be constructed by travelling along a path through S to some u in S followed by the edge(u,v) e v d(u) u S s

5 Letting d(u) be the length of the shortest path from s to u, for u in S (i.e., u is explored node)
Then for each v’ find (v’) = min (d(u) + le ) (u in S for which exists e= (u,v’)) Find the v with the minimum (v); add v to S and define d(v) = (v)

6 Simulate Dijkstra’s algorithm (starting from s) on the graph
Vertex Added Round s a b c d 1 1 2 3 4 5 c a 1 3 2 1 s 4 4 6 1 b d 3

7 Dijkstra’s Algorithm as a greedy algorithm
Heuristic: Elements committed to the solution by order of minimum distance from s

8 Correctness Proof Elements in S have the correct label
Key to proof: when v is added to S, it has the correct distance label y x s u v

9 Warmup If P is a shortest path from s to v, and if t is on the path P, the segment from s to t is a shortest path between s and t WHY? v t s

10 Induction statement: For any set S of size n in an arbitrary graph G, if s is in S then for any u in S, d(u), found by Dijkstra’s algorithm, is the length of the shortest path from s to u.

11 Dijkstra's Algorithm: Proof of Correctness
For each node v  S, d(v) is the length of the shortest s-v path Pf. (by induction on |S|) Base case: |S| = 1 is trivial Inductive hypothesis: Assume true for |S| = k  1 Let v be next node added to S, using the edge u-v By Dijkstra, d(v) (= (v) ) is the length of the s-v path found, is shorter than (y) for any path with one edge out of S to y for any y. Consider any other s-v path P (the blue path). We'll see that it's no shorter than (v) Let x-y be the first edge in P that leaves S, and let P' be the subpath to x We can see that  (P)  (v) P  (P)   (P') +  (x,y)  d(x) +  (x, y)  (y)  (v) P' x y nonnegative weights inductive hypothesis defn of (y) Dijkstra chose v instead of y s S u v

12 Implementation: There are n-1 iterations of the while loop, each adding a new node to S. One might then consider each v not in S and go through all the edges between u in S and v to determine the min (d(u) + le). With m edges, computing these can take O(m) time so running time is O(mn). We can do better

13 Dijkstra's Algorithm: Implementation
For each unexplored node, explicitly maintain Efficient implementation: Maintain a priority queue of unexplored nodes, prioritized (key value) by (v) Next node to explore = node v with minimum (v) When exploring v, for each incident edge e = (v,w), update Full discussion with tips on how to manage the priority queue is found in the text.

14 Using a priority queue, Dijkstra’s Algorithm can be implemented in a graph with n nodes and m edges to run in O(m) time plus the time for n ExtractMin and m ChangeKey Operations. Since each priority queue operation can be made to run in O(log n), the overall time is O(m log n) (assuming m > n).


Download ppt "Data Structures and Algorithm Analysis Lecture 8"

Similar presentations


Ads by Google