08 - Implementing Kruskal’s algorithm using union-find

08 - Implementing Kruskal’s algorithm using union-find

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
2/1/08 - Implementing Kruskal’s algorit... Lecture: Implementing Kruskal's algorithm using union-find. Reading: Chapter 4.6 Kruskal’s Algorithm Sort edges by increasing cost O(mlogn) T < Ø For each edge (u, v) in this order if u, v are not in same component of T T <- T {u} O(logn) per iteration endwhile n = # of vertices m = # of edges Sorting takes O(nlogn) = O(mlogn) We have m iterations of the “for” loop Testing for (u, v) path takes O(n) O(m, n) m < n 2 logm < 2lgn Union-Find data structure Supports 3 ops (1) MakeUnionFind(n) creates new data structure with n elements in separate pieces (2) Union(A, B): Merge pieces A, B (3) Find(u): Outputs name of piece containing u Implementation 8-1 8
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Assume elts and pieces identified by numbers {1, ..., n} Elements organized into trees via parent pointers Convention: In every subtree, a unique “root” element pointing to itself. Its ID number is the ID number of that piece. Invariant: Starting from any element, following parent pointers
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: eventually leads to the root. Union(A, B): Change root(A)’s parent to root(B) ±ind(u): ±ollow pointers to the root and output its ID # Path Compression: Reattach all these elements to the root Analysis Union runs in O(1) ±ind: O(n) O(logn) Union(A, B) iF size(A) < size(B) root(A) -> root(B) else root(B).size <- root(A).size + root(B).size root(B) -> root(A) Elements represented as ordered pairs (ID, size) with parent pointers Invariant: ∀ piece A, the “size” component oF (A) counts # oF elts in A 8-2 Claim: Starting from any element of A, following parent pointers eventually leads to the root(A), in at most 1+log(size(A)) Base Case: Size(A) = 1 Induction Step: Distance from u to root increases only when its piece merges with a piece at least as large. Distance increases by 1 => log(size(A)) increases by ≥ 1 O(m α (n)) [Chazelle, 90s] Minimal spanning tree 8-3...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern