08 - Implementing Kruskal’s algorithm using union-find

08 - Implementing Kruskal’s algorithm using union-find

Info iconThis 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-fnd. Reading: Chapter 4.6 Kruskal’s Algorithm Sort edges by increasing cost O(mlogn) T < Ø For each edge (u, v) in this order i± u, v are not in same component o± T T <- T {u} O(logn) per iteration endwhile n = # o± vertices m = # o± edges Sorting takes O(nlogn) = O(mlogn) We have m iterations o± the “±or” loop Testing ±or (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 o± piece containing u Implementation 8-1 8
Background image of page 1

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

View Full DocumentRight Arrow Icon
Assume elts and pieces identifed 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
Background image of page 2
Background 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

This note was uploaded on 10/02/2008 for the course CS 482 taught by Professor Kleinberg during the Spring '08 term at Cornell.

Page1 / 3

08 - Implementing Kruskal’s algorithm using union-find

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online