cse 421
play

CSE 421 Union Find DS Dijkstras Algorithm , Shayan Oveis Gharan 1 - PowerPoint PPT Presentation

CSE 421 Union Find DS Dijkstras Algorithm , Shayan Oveis Gharan 1 Union Find Data Structure Each set is represented as a tree of pointers, where every vertex is labeled with longest path ending at the vertex To check whether A,Q are in


  1. CSE 421 Union Find DS Dijkstra’s Algorithm , Shayan Oveis Gharan 1

  2. Union Find Data Structure Each set is represented as a tree of pointers, where every vertex is labeled with longest path ending at the vertex To check whether A,Q are in same connected component, follow pointers and check if root is the same. vv vv D,2 W,1 vv vv vv vv Q,0 A,1 P,0 B,0 vv C,0 {W,P,Q} {A,B,C,D}

  3. Union Find Data Structure Merge: To merge two connected components, make the root with the smaller label point to the root with the bigger label (adjusting labels if necessary). Runs in O(1) time vv D,2 vv D,2 vv W,1 vv vv A,1 W,1 vv vv A,1 B,0 vv B,0 vv vv P,0 Q,0 vv vv Q,0 P,0 vv vv C,0 C,0 At most one label must be adjusted vv W,2 vv vv D,1 W,1 vv D,1 vv Q,0 vv vv Q,0 A,0 vv A,0

  4. Depth vs Size Claim: If the label of a root is k, there are at least 2 " elements in the set. Therefore the depth of any tree in algorithm is at most log n vv D,2 vv vv A,1 W,1 vv B,0 So, we can check if #, % are in the same component in time &(log +) vv vv P,0 Q,0 vv C,0

  5. Depth vs Size: Correctness Claim: If the label of a root is k, there are at least 2 " elements in the set. Pf: By induction on k. Base Case (k = 0): this is true. The set has size 1. IH: Suppose the claim is true until some time t IS: If we merge roots with labels # $ > # & , the number of vertices only increases while the label stays the same. If # $ = # & , the merged tree has label # $ + 1 , and by induction, it has at least 2 " * + 2 " + = 2 " * ,$ elements.

  6. Kruskal’s Algorithm with Union Find Implementation. Use the union-find data structure. Build set ! of edges in the MST. • • Maintain a set for each connected component. • O(m log n) for sorting and O(m log n) for union-find Kruskal(G, c) { Sort edges weights so that c 1 £ c 2 £ ... £ c m . " ← ∅ foreach ( % ∈ ' ) make a set containing singleton {%} Find roots and compare for i = 1 to m Let (u,v) = e i if (u and v are in different sets) { " ← " È {* + } merge the sets containing % and , } Merge at the roots return " }

  7. Removing weight Distinction Assumption Suppose edge weights are not distinct, and Kruskal’s algorithm sorts edges so ! " # ≤ ! " % ≤ ⋯ ≤ ! " ' Suppose Kruskal finds tree ( of weight !(() , but the optimal solution ( ∗ has cost ! ( ∗ < ! ( . Perturb each of the weights by a very small amount so that . < ! " % . < ⋯ ≤ ! " ' . ! " # If the perturbation is small enough, ! . ( ∗ < !(() . However, this contradicts the correctness of Kruskal’s algorithm, since the algorithm will still find (, and Kruskal’s algorithm is correct if all weights are distinct.

  8. Single Source Shortest Path Given an (un)directed graph G=(V,E) with non-negative UW edge weights ! " ≥ 0 and a start vertex s Find length of shortest paths from s to each vertex in G Amazon

  9. Dijkstra’s Algorithm Maintain a set S of vertices whose shortest paths are known • initially S= { s } Maintaining current best lengths of paths that only go through S to each of the vertices in G • Path-lengths to elements of S will be right, to V-S they might not be right Repeatedly add vertex v to S that has the shortest path- length of any vertex in V-S • Update path lengths based on new paths through v

  10. Dijkstra’s Algorithm Dijkstra(G, c, s) { foreach (v Î V) d[v] ¬ ¥ //This is the key of node v ! " ← $ foreach (v Î V) insert v onto a priority queue Q Initialize set of explored nodes S ¬ {s} while (Q is not empty) { u ¬ delete min element from Q S ¬ S È { u } foreach (edge e = (u, v) incident to u) if ((v Ï S) and (d[u]+c e < d[v])) ! % ← ! & + ( ) Decrease key of v to d[v]. *+,)-. % ← & }

  11. Dijkstra’s Algorithm: Example s 0 2 4 ¥ ¥ 5 7 3 ¥ 1 4 ¥ 5 6 3 ¥ ¥ ¥ 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  12. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 ¥ 1 4 ¥ 5 6 3 ¥ ¥ ¥ 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  13. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 ¥ 1 4 ¥ 5 6 3 ¥ ¥ ¥ 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  14. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 ¥ 5 6 3 ¥ ¥ ¥ 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  15. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 ¥ 5 6 3 ¥ ¥ ¥ 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  16. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  17. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 ¥ 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  18. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 13 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  19. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 13 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  20. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 13 8 ¥ ¥ 4 ¥ 2 5 10 ¥

  21. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 13 8 ¥ 16 4 ¥ 2 5 10 ¥

  22. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 ¥ 8 8 1 9 13 8 ¥ 16 4 ¥ 2 5 10 ¥

  23. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 ¥ 16 4 10 2 5 10 ¥

  24. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 ¥ 16 4 10 2 5 10 ¥

  25. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 20

  26. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 20

  27. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 19

  28. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 19

  29. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 18

  30. Dijkstra’s Algorithm: Example s 0 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 14 16 4 10 2 5 10 18

  31. Disjkstra’s Algorithm: Correctness Prove by induction that throughout the algorithm, for any ! ∈ # , the path $ % in the shortest from s to u. Base Case: This is always true when # = ' . IH: Suppose |#| = * and the claim holds for S IS: Say + is the k+1-st vertex that we add to S. Let {u,v} be last edge on $ , . y s $ If $ x , is not the shortest path there is a path $ to s which is shorter. Consider the first time that P leaves S v (with edge {x,y}). u $ , S -> x has weight (at least) d(x) So, - $ ≥ / 0 + - 2,4 ≥ / + = - $ , . A contradiction.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend