1 / 52

CSE 421 Algorithms

CSE 421 Algorithms. Richard Anderson Dijkstra’s algorithm. 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”

tyme
Download Presentation

CSE 421 Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 421Algorithms Richard Anderson Dijkstra’s algorithm

  2. 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 1 u u 2 1 3 5 s x s x 4 3 3 v v

  3. Construct Shortest Path Tree from s 2 d d 1 a a 5 4 4 4 e e -3 c c s -2 s 3 3 2 6 g g b b 3 7 f f

  4. 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

  5. Assume all edges have non-negative cost 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)) 4 3 y 1 u 1 1 0 1 4 s x 2 2 2 2 v 2 3 5 z

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

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

  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. Proof • Let v be a vertex in V-S with minimum d[v] • Let Pv 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 = Psx + c(x,y) + Pyv • Len(Psx) + c(x,y) >= d[y] • Len(Pyv) >= 0 • Len(P) >= d[y] + 0 >= d[v] y x s u v

  10. Negative Cost Edges • Draw a small example a negative cost edge and show that Dijkstra’s algorithm fails on this example

  11. Bottleneck Shortest Path • Define the bottleneck distance for a path to be the maximum cost edge along the path u 5 6 s x 2 5 4 3 v

  12. Compute the bottleneck shortest paths 6 d d 6 a a 5 4 4 4 e e -3 c c s -2 s 3 3 2 6 g g b b 4 7 f f

  13. How do you adapt Dijkstra’s algorithm to handle bottleneck distances • Does the correctness proof still apply?

  14. Who was Dijkstra? • What were his major contributions?

  15. http://www.cs.utexas.edu/users/EWD/ • Edsger Wybe Dijkstra was one of the most influential members of computing science's founding generation. Among the domains in which his scientific contributions are fundamental are • algorithm design • programming languages • program design • operating systems • distributed processing • formal specification and verification • design of mathematical arguments

  16. Shortest Paths • Negative Cost Edges • Dijkstra’s algorithm assumes positive cost edges • For some applications, negative cost edges make sense • Shortest path not well defined if a graph has a negative cost cycle a 6 4 -4 4 e -3 c s -2 3 3 2 6 g b f 4 7

  17. Negative Cost Edge Preview • Topological Sort can be used for solving the shortest path problem in directed acyclic graphs • Bellman-Ford algorithm finds shortest paths in a graph with negative cost edges (or reports the existence of a negative cost cycle).

  18. Dijkstra’s AlgorithmImplementation and Runtime 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)) y u a HEAP OPERATIONS n Extract Mins m Heap Updates s x v b z Edge costs are assumed to be non-negative

  19. Bottleneck Shortest Path • Define the bottleneck distance for a path to be the maximum cost edge along the path u 5 6 s x 2 5 4 3 v

  20. Compute the bottleneck shortest paths 6 d d 6 a a 5 4 4 4 e e -3 c c s -2 s 3 3 2 5 g g b b 7 7 f f

  21. Dijkstra’s Algorithmfor Bottleneck Shortest Paths S = {}; d[s] = negative infinity; 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], max(d[v], c(v, w))) 3 y 4 u a 1 2 4 3 s x 1 1 2 v 5 3 b 4 z

  22. Minimum Spanning Tree • Introduce Problem • Demonstrate three different greedy algorithms • Provide proofs that the algorithms work

  23. Minimum Spanning Tree 15 t a 6 9 14 3 4 e 10 13 c s 11 20 5 17 2 7 g b 8 f 22 u 12 1 16 v

  24. Extend a tree by including the cheapest out going edge Add the cheapest edge that joins disjoint components Delete the most expensive edge that does not disconnect the graph Greedy Algorithms for Minimum Spanning Tree 4 b c 20 5 11 8 a d 7 22 e

  25. Greedy Algorithm 1Prim’s Algorithm • Extend a tree by including the cheapest out going edge 15 t a 6 14 3 9 4 e 10 13 c s 11 20 5 Construct the MST with Prim’s algorithm starting from vertex a Label the edges in order of insertion 17 2 7 g b 8 f 22 u 12 1 16 v

  26. Greedy Algorithm 2Kruskal’s Algorithm • Add the cheapest edge that joins disjoint components 15 t a 6 14 3 9 4 e 10 13 c s 11 20 5 17 2 7 Construct the MST with Kruskal’s algorithm Label the edges in order of insertion g b 8 f 22 u 12 1 16 v

  27. Greedy Algorithm 3Reverse-Delete Algorithm • Delete the most expensive edge that does not disconnect the graph 15 t a 6 14 3 9 4 e 10 13 c s 11 20 5 17 2 7 Construct the MST with the reverse-delete algorithm Label the edges in order of removal g b 8 f 22 u 12 1 16 v

  28. Why do the greedy algorithms work? • For simplicity, assume all edge costs are distinct • Let S be a subset of V, and suppose e = (u, v) is the minimum cost edge of E, with u in S and v in V-S • e is in every minimum spanning tree

  29. Proof • Suppose T is a spanning tree that does not contain e • Add e to T, this creates a cycle • The cycle must have some edge e1 = (u1, v1) with u1 in S and v1 in V-S • T1 = T – {e1} + {e} is a spanning tree with lower cost • Hence, T is not a minimum spanning tree

  30. Optimality Proofs • Prim’s Algorithm computes a MST • Kruskal’s Algorithm computes a MST

  31. Reverse-Delete Algorithm • Lemma: The most expensive edge on a cycle is never in a minimum spanning tree

  32. Dealing with the assumption of no equal weight edges • Force the edge weights to be distinct • Add small quantities to the weights • Give a tie breaking rule for equal weight edges

  33. Dijkstra’s Algorithmfor Minimum Spanning Trees 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], c(v, w)) 3 y 4 u a 1 2 4 3 s x 1 1 2 v 5 3 b 4 z

  34. Minimum Spanning Tree Undirected Graph G=(V,E) with edge weights 15 t a 6 9 14 3 4 e 10 13 c s 11 20 5 17 2 7 g b 8 f 22 u 12 1 16 v

  35. [Prim] Extend a tree by including the cheapest out going edge [Kruskal] Add the cheapest edge that joins disjoint components [ReverseDelete] Delete the most expensive edge that does not disconnect the graph Greedy Algorithms for Minimum Spanning Tree 4 b c 20 5 11 8 a d 7 22 e

  36. Why do the greedy algorithms work? • For simplicity, assume all edge costs are distinct

  37. Edge inclusion lemma • Let S be a subset of V, and suppose e = (u, v) is the minimum cost edge of E, with u in S and v in V-S • e is in every minimum spanning tree of G • Or equivalently, if e is not in T, then T is not a minimum spanning tree e S V - S

  38. e is the minimum cost edge between S and V-S Proof • Suppose T is a spanning tree that does not contain e • Add e to T, this creates a cycle • The cycle must have some edge e1 = (u1, v1) with u1 in S and v1 in V-S • T1 = T – {e1} + {e} is a spanning tree with lower cost • Hence, T is not a minimum spanning tree S V - S e

  39. Optimality Proofs • Prim’s Algorithm computes a MST • Kruskal’s Algorithm computes a MST • Show that when an edge is added to the MST by Prim or Kruskal, the edge is the minimum cost edge between S and V-S for some set S.

  40. Prim’s Algorithm S = { }; T = { }; while S != V choose the minimum cost edge e = (u,v), with u in S, and v in V-S add e to T add v to S

  41. Prove Prim’s algorithm computes an MST • Show an edge e is in the MST when it is added to T

  42. Kruskal’s Algorithm Let C = {{v1}, {v2}, . . ., {vn}}; T = { } while |C| > 1 Let e = (u, v) with u in Ci and v in Cj be the minimum cost edge joining distinct sets in C Replace Ci and Cj by Ci U Cj Add e to T

  43. Prove Kruskal’s algorithm computes an MST • Show an edge e is in the MST when it is added to T

  44. Reverse-Delete Algorithm • Lemma: The most expensive edge on a cycle is never in a minimum spanning tree

  45. Dealing with the assumption of no equal weight edges • Force the edge weights to be distinct • Add small quantities to the weights • Give a tie breaking rule for equal weight edges

  46. Application: Clustering • Given a collection of points in an r-dimensional space, and an integer K, divide the points into K sets that are closest together

  47. Distance clustering • Divide the data set into K subsets to maximize the distance between any pair of sets • dist (S1, S2) = min {dist(x, y) | x in S1, y in S2}

  48. Divide into 2 clusters

  49. Divide into 3 clusters

  50. Divide into 4 clusters

More Related