----------------------------------------------------------------------------- COMP 731 - Data Struc & Algorithms - Lecture 13 - Friday, July 21, 2000 ----------------------------------------------------------------------------- Today: o Union - Find analysis Review: Kruskal3 (G,w) // G=(V,E), w: E -> R; n=|V|, m=|E| 1 Sort E by edge weights, w, and relabel so that w(e_1) < ... < w(e_m) // e_i = {v_i, w_i} 2 for i = 1 to m do T[i] = NO MAKESETS(n) // make disjoint sets with elements 1, ..., n count = 0; for i = 1 to m do if count==n-1 then break; name_1 = FIND(v_i) name_2 = FIND(w_i) if name_1==name_2 then UNION (name_1, name_2) T[i] = YES return T Implementation: Have the students help you devise a list representation. Assume that the elements are numbers 1..n, like they are in Kruskal's algorithm. Assume that initially we will MAKESET(i) for i = 1, ... n. 1 2 3 4 5 6 7 8 9 10 11 12 13 ----------------------------------------------------- | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| list L ----------------------------------------------------- Implementation 1: "Exhaustive Union" MAKESETS(n) Allocate an array A[1..n] for i=1 to n do A[i]=i; FIND(x): return A[x] UNION (a, b) : for i = 1 to n do if L[i] == b then L[i]=a; > FIND O(1) - UNION O(n) Implementation 2: "Lazy Union" FIND(x): repeat x = L[x] until x==L[x] // chase the pointer to the root return x UNION(a,b) L[b] = a Implementation 3: "Union-by-rank heuristic" 1 2 3 4 5 6 7 8 9 10 11 12 13 ----------------------------------------------------- | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| L[i].parent | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0| 0| 0| 0| L[i].rank ----------------------------------------------------- UNION (a,b) if L[b].rank < L[a].rank then L[b].parent = a else if A[a].rank < L[b].rank then L[a].parent = b else else L[a].parent = b L[a].rank++ FIND(x): repeat x = L[x].parent until x==L[x].parent return x Analysis: prove by induction that A tree of rank r has at least 2^r vertices. Conclude: the maximal rank is O(lg n). Conclude: FIND runs in O(lg n) time. Implementation 4: "Collapsing Find heuristic" Have everyone along the search path point to the root. FIND (x) // Find the root; call it a a = x repeat a = L[a] until a==L[a] // Re-walk the path, making every vertex along it point to a repeat p = L[x] L[x] = a x = p until L[x]==x Analysis of collapsing Find: next time! Table: |Exhaustive |Generic |Union-by-rank|Collapsing-Find|Union-by-rank | |Union |Union/Find| | | and | | | | | |Collapsing-Find | ----------------------------------------------------------------------- | | | | | | MAKESETS | O(n) | O(n) | O(n) | | | | | | | | | ----------------------------------------------------------------------- | | | | | | UNION | O(n) | O(1) | O(1) | | | | | | | | | ----------------------------------------------------------------------- | | | | | | FIND | O(1) | O(n) | O(lg n) | | | | | | | | | ----------------------------------------------------------------------- Kruskal: O(n^2+mlg n) O(mn) O(mlg n)