Download presentation
Presentation is loading. Please wait.
Published byFrederick Wheeler Modified over 9 years ago
1
CSE 421 Algorithms Richard Anderson Lecture 8 Optimal Caching Dijkstra’s algorithm
2
Today’s Lecture Optimal Caching (Section 4.3) Dijkstra’s Algorithm (Section 4.4)
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
Single Source Shortest Path Problem Given a graph and a start vertex s –Determine distance of every vertex from s –Identify shortest paths to each vertex Express concisely as a “shortest paths tree” Each vertex has a pointer to a predecessor on shortest path s v x u 1 2 5 3 4 s v x u 1 3 3
9
Construct Shortest Path Tree from s a b c s e g f d 4 2 -3 2 1 5 4 -2 3 3 6 3 7 4 a b c s e g f d
10
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
11
Dijkstra’s Algorithm S = {}; d[s] = 0; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], d[v] + c(v, w)) s u v z y x 1 4 3 2 3 2 1 2 1 0 1 2 2 5 4 Assume all edges have non-negative cost
12
Simulate Dijkstra’s algorithm (strarting 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
13
Dijkstra’s Algorithm as a greedy algorithm Elements committed to the solution by order of minimum distance
14
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
15
Proof Let v be a vertex in V-S with minimum d[v] Let P v be a path of length d[v], with an edge (u,v) Let P be some other path to v. Suppose P first leaves S on the edge (x, y) –P = P sx + c(x,y) + P yv –Len(P sx ) + c(x,y) >= d[y] –Len(P yv ) >= 0 –Len(P) >= d[y] + 0 >= d[v] s y v x u
16
Negative Cost Edges Draw a small example a negative cost edge and show that Dijkstra’s algorithm fails on this example
17
Bottleneck Shortest Path Define the bottleneck distance for a path to be the maximum cost edge along the path s v x u 6 5 5 3 4 2
18
Compute the bottleneck shortest paths a b c s e g f d 4 2 -3 6 6 5 4 -2 3 4 6 3 7 4 a b c s e g f d
19
How do you adapt Dijkstra’s algorithm to handle bottleneck distances Does the correctness proof still apply?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.