Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.

Similar presentations


Presentation on theme: "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10."— Presentation transcript:

1 CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10

2 Extensions What if the objective is to minimize the sum of the lateness? –EDF does not seem to work If the tasks have release times and deadlines, and are non-preemptable, the problem is NP- complete

3 Optimal Caching Caching problem: –Maintain collection of items in local memory –Minimize number of items fetched

4 Caching example A, B, C, D, A, E, B, A, D, A, C, B, D, A

5 Optimal Caching If you know the sequence of requests, what is the optimal replacement pattern? Note: it is rare to know what the requests are in advance – but we still might want to do this –Some specific applications, the sequence is known –Competitive analysis, compare performance on an online algorithm with an optimal offline algorithm

6 Farthest in the future algorithm Discard element used farthest in the future A, B, C, A, C, D, C, B, C, A, D

7 Correctness Proof Sketch –Start with Optimal Solution O –Convert to Farthest in the Future Solution F-F –Look at the first place where they differ –Convert O to evict F-F element There are some technicalities here to ensure the caches have the same configuration...

8 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

9 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 Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48. s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6

10 Dijkstra's Algorithm Dijkstra's algorithm –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 –Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) =  (v) shortest path to some u in explored part, followed by a single edge (u, v) s v u d(u) S e

11 Simulate Dijkstra’s algorithm (starting from s) on the graph 1 2 3 4 5 Round Vertex Added sabcd bd c a 1 1 1 23 4 6 1 3 4 s

12 Dijkstra’s Algorithm as a greedy algorithm Elements committed to the solution by order of minimum distance

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

14 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? s t v

15 Dijkstra's Algorithm: Proof of Correctness Invariant: For each node u  S, d(u) is the length of the shortest s-u 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, and let u-v be the chosen edge The shortest s-u path plus (u, v) is an s-v path of length  (v) Consider any s-v path P. 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 P is already too long as soon as it leaves S (P)  (P') + (x,y)  d(x) + (x, y)   (y)   (v) nonnegative weights inductive hypothesis defn of  (y) Dijkstra chose v instead of y S s y v x P u P'

16 Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain Next node to explore = node with minimum  (v) When exploring v, for each incident edge e = (v, w), update Efficient implementation: Maintain a priority queue of unexplored nodes, prioritized by  (v)


Download ppt "CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10."

Similar presentations


Ads by Google