1 / 20

Disjoint Set Neil Tang 02/26/2008

Disjoint Set Neil Tang 02/26/2008. Class Overview. Disjoint Set and An Application Basic Operations Linked-list Implementation Array Implementation Union-by-Size and Union-by-Height(Rank) Find with Path Compression Worst-Case Time Complexity. Disjoint Set.

tim
Download Presentation

Disjoint Set Neil Tang 02/26/2008

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. Disjoint Set Neil Tang02/26/2008 CS223 Advanced Data Structures and Algorithms

  2. Class Overview • Disjoint Set and An Application • Basic Operations • Linked-list Implementation • Array Implementation • Union-by-Size and Union-by-Height(Rank) • Find with Path Compression • Worst-Case Time Complexity CS223 Advanced Data Structures and Algorithms

  3. Disjoint Set • Given a set of elements, we can have a collection S = {S1, S2, ... Sk} of disjoint dynamic (sub) sets. • Representative of a set: We choose one element of a set to identify the set, e.g., we use the root of a tree to identify a tree, or the head element of a linked list to access the linked list. • Usually, we want to find out if two elements belong to the same set. CS223 Advanced Data Structures and Algorithms

  4. a d f h c b e g i An Application • Given an undirected graph G = (V, E) • We may want to find all connected components, whether the graph is connected or whether two given nodes belong to the same connected component. CS223 Advanced Data Structures and Algorithms

  5. Basic Operations • find(x): find which disjoint set x belongs to • Union(x,y): Union set x and set y. CS223 Advanced Data Structures and Algorithms

  6. head f tail nil a b c tail nil find(b) a b c f tail nil Linked-list Implementation union(f, b) CS223 Advanced Data Structures and Algorithms

  7. Array Implementation • Assume that all the elements are numbered sequentially from 0 to N-1. CS223 Advanced Data Structures and Algorithms

  8. Array Implementation CS223 Advanced Data Structures and Algorithms

  9. Array Implementation CS223 Advanced Data Structures and Algorithms

  10. Union Operation Time complexity: O(1) CS223 Advanced Data Structures and Algorithms

  11. Find Operation Time complexity: O(N) CS223 Advanced Data Structures and Algorithms

  12. Union-by-Size • Make the smaller tree a subtree of the larger and break ties arbitrarily. CS223 Advanced Data Structures and Algorithms

  13. Union-by-Height (Rank) • Make the shallow tree a subtree of the deeper and break ties arbitrarily. CS223 Advanced Data Structures and Algorithms

  14. Size and Height 0 1 2 3 4 5 6 7 CS223 Advanced Data Structures and Algorithms

  15. Union-by-Height (Rank) Time complexity: O(logN) CS223 Advanced Data Structures and Algorithms

  16. Worst-Case Tree CS223 Advanced Data Structures and Algorithms

  17. Find with Path Compression CS223 Advanced Data Structures and Algorithms

  18. Find with Path Compression CS223 Advanced Data Structures and Algorithms

  19. Find with Path Compression • Fully compatible with union-by-size. • Not compatible with union-by-height. • Union-by-size is usually as efficient as union-by-height. CS223 Advanced Data Structures and Algorithms

  20. Worst-Case Time Complexity • If both union-by-rank and path compression heuristics are used, the worst-case running time for any sequence of M union/find operations is O(M * (M,N)), where (M, N) is the inverse Ackermann function which grows even slower than logN. • For any practical purposes, (M, N) < 4. CS223 Advanced Data Structures and Algorithms

More Related