CS 473 Algorithms I Lecture X Disjoint Set

  • Slides: 44
Download presentation
CS 473 -Algorithms I Lecture X Disjoint Set Operations CS 473 Lecture X 1

CS 473 -Algorithms I Lecture X Disjoint Set Operations CS 473 Lecture X 1

Disjoint Set Operations A disjoint-set data structure • Maintains a collection of disjoint dynamic

Disjoint Set Operations A disjoint-set data structure • Maintains a collection of disjoint dynamic sets • Each set is identified by a representative which is some member of the set In some applications, • It doesn't matter which member is used as the representative • We only care that, Ø if we ask for the representative of a set twice without modifying the set between the requests, Pwe get the same answer both times CS 473 Lecture X 2

Disjoint Set Operations In other applications, There may be a prescribed rule for choosing

Disjoint Set Operations In other applications, There may be a prescribed rule for choosing the representative E. G. Choosing the smallest member in the set Each element of a set is represented by an object “x” MAKE-SET(x) creates a new set whose only member is x – Object x is the representative of the set – x is not already a member of any other set UNION(x, y) unites the dynamic sets that contain x&y – are assumed to be disjoint prior to the operation – The new representative is some member of CS 473 Lecture X 3

Disjoint Set Operations – Usually, the representative of either the new representative is chosen

Disjoint Set Operations – Usually, the representative of either the new representative is chosen as We destroy sets , removing them from the collection since we require the sets in the collection to be disjoint FIND-SET(x) returns a pointer to the representative of the unique set containing x We will analyze the running times in terms of two parameters Ø n : The number of MAKE-SET operations Ø m : The total number of MAKE-SET, UNION and FIND-SET operations CS 473 Lecture X 4

Disjoint Set Operations • Each union operation reduces the number of sets by one

Disjoint Set Operations • Each union operation reduces the number of sets by one since the sets are disjoint Ø Therefore, only one set remains after n - 1 union operations Ø Thus, the number of union operations is n– 1 • Also note that, m n always hold since MAKE-SET operations are included in the total number of operations CS 473 Lecture X 5

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) CONNECTED-COMPONENTS (G) for each vertex v V[G] do MAKE-SET(v) endfor each edge (u, v) E[G] do if FIND-SET(u) FIND-SET(v) then UNION(u, v) endif endfor end SAME-COMPONENT(u, v) if FIND-SET(u) = FIND-SET(v) then return TRUE else return FALSE endif end CS 473 Lecture X 6

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g Initial {a} CS 473 {b} h f j i {c} {d} {e} Lecture X {f} {g} {h} {i} {j} 7

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g h f i Initial {a} {b} (b, d) {a} {b, d} {c} CS 473 j {c} {d} {e} {f} {g} {h} {i} {j} Lecture X 8

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g Initial {a} {b} (b, d) (e, g) h f j i {e} {f} {g} {h} {i} {j} {a} {b, d} {c} {e} {f} {g} {h} {i} {j} {a} {e, g} {f} {h} {i} {j} CS 473 {c} {b, d} {c} {d} Lecture X 9

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g Initial {a} {b} (b, d) (e, g) (a, c) h f j i {e} {f} {g} {h} {i} {j} {a} {b, d} {c} {a, c} {b, d} {e, g} {f} CS 473 {c} {d} Lecture X {h} {i} {j} 10

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g Initial {a} {b} (b, d) (e, g) (a, c) (h, i) h f j i {e} {f} {g} {h} {i} {j} {a} {b, d} {c} {a, c} {b, d} {e, g} {f} CS 473 {c} {d} Lecture X {h} {i} {j} {h, i} {j} 11

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g h f i Initial {a} {b} (b, d) {a} {b, d} {c} (e, g) (a, c) (h, i) (a, b) {a} {b, d} {c} {a, c} {b, d} {a, b, c, d} CS 473 j {c} {d} {e} {f} {g} {h} {i} {j} {e, g} {f} {f} Lecture X {h} {i} {h, i} {j} {j} 12

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g h f i Initial {a} {b} (b, d) {a} {b, d} {c} (e, g) (a, c) (h, i) (a, b) (e, f) {a} {b, d} {c} {a, c} {b, d} {a, b, c, d} CS 473 j {c} {d} {e} {f} {g} {h} {i} {j} {e, g} {f} {e, f, g} Lecture X {h} {i} {h, i} {j} {j} {j} 13

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph

An Application of Disjoint-Set Data Structures Determining the connected components of an undirected graph G=(V, E) a b e c d g h f i Initial {a} {b} (b, d) {a} {b, d} {c} (e, g) (a, c) (h, i) (a, b) (e, f) (b, c) {a} {b, d} {c} {a, c} {b, d} {a, b, c, d} CS 473 j {c} {d} {e} {f} {g} {h} {i} {j} {e, g} {f} {e, f, g} Lecture X {h} {i} {h, i} {j} {j} {j} 14

Linked-List Representation of Disjoint Sets • • Represent each set by a linked-list The

Linked-List Representation of Disjoint Sets • • Represent each set by a linked-list The first object in the linked-list serves as its set representative • Each object in the linked-list contains i. A set member ii. A pointer to the object containing the next set member iii. A pointer back to the representative Representative pointer MAKE-SET(x) : O(1) x Next Object Pointer / FIND-SET(x) : We return the representative pointer of x CS 473 Lecture X 15

Linked-List Representation of Disjoint Sets A Simple Implementation of Union : UNION(c, y) –

Linked-List Representation of Disjoint Sets A Simple Implementation of Union : UNION(c, y) – APPEND x's list to the end of y 's list – The representative of y 's list becomes the new representative – UPDATE the representative pointer of each object originally on x's list which takes time linear in the length of x's list c 's list • • / • NIL y 's list CS 473 • • Lecture X • 16

Linked-List Representation of Disjoint Sets A Simple Implementation of Union : UNION(c, y) c

Linked-List Representation of Disjoint Sets A Simple Implementation of Union : UNION(c, y) c 's • list • • • / • NIL y 's list • • CS 473 Lecture X • • • / 17

Analysis of the Simple Union Implementation • A sequence of m operations that requires

Analysis of the Simple Union Implementation • A sequence of m operations that requires • Suppose that we have n objects Operation MAKE-SET(c 1) CS 473 Number of Objects Updated 1 P {c } time and let m = 2 n - 1 Updated Objects (Denoted By ‘P’) 1 Lecture X 18

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2) CS 473 Number

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2) CS 473 Number of Objects Updated 1 1 {c. P 1} {c 2 P } Lecture X Updated Objects (Denoted By ‘P’) 19

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . CS

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . CS 473 Number of Objects Updated 1 1 {c. P 1} {c 2 P } Updated Objects (Denoted By ‘P’) . . . Lecture X 20

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . MAKE-SET(cn)

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . MAKE-SET(cn) CS 473 Number of Objects Updated 1 1. . . 1 {c. P 1} {c 2 P } Updated Objects (Denoted By ‘P’) P { c n} Lecture X 21

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number of Objects Updated 1 1. . . {c. P 1} {c 2 P } MAKE-SET(cn) 1 P c { } UNION(c 1, c 2) 1 { c 1} CS 473 Updated Objects (Denoted By ‘P’) n Lecture X {c 2} c {c. P 1, 2} 22

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . MAKE-SET(cn)

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . MAKE-SET(cn) UNION(c 1, c 2) UNION(c 2, c 3) CS 473 Number of Objects Updated 1 1. . . {c. P 1} {c 2 P } Updated Objects (Denoted By ‘P’) P 1 { c n} 1 { c 1} 2 {c 1, c 2} Lecture X {c 2} {c 3} P c { 1, c 2} c. Pc } {c. P 1, 2, 3 23

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number of Objects Updated 1 1. . . Updated Objects (Denoted By ‘P’) {c. P 1} {c 2 P } P MAKE-SET(cn) 1 { c n} UNION(c 1, c 2) 1 { c 1} 2 {c 1, c 2} 3 {c 1, c 2, c 3} UNION(c 2, c 3) UNION(c 3, c 4) CS 473 Lecture X c c } {P 1, 2 {c 2} {c 3} {c 4} c c c } {PP 1, 2, 3 PPP {c 1, c 2, c 3, c 4} 24

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number of Objects Updated 1 1. . . Updated Objects (Denoted By ‘P’) {c. P 1} {c 2 P } 1 c } {P n UNION(c 1, c 2) 1 { c 1} UNION(c 2, c 3) 2 {c 1, c 2} UNION(c 3, c 4) 3 {c 1, c 2, c 3} MAKE-SET(cn) . . CS 473 . . Lecture X P {c 2} {c 1, c 2} {c 3} {c 4} c c c } {PP 1, 2, 3 c c c {c. PPP 1, 2, 3, 4} 25

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number

Analysis of the Simple Union Implementation Operation MAKE-SET(c 1) MAKE-SET(c 2). . . Number of Objects Updated 1 1. . . {c. P 1} {c 2 P } 1 c } {P n UNION(c 1, c 2) 1 { c 1} UNION(c 2, c 3) 2 UNION(c 3, c 4) 3 MAKE-SET(cn) . . UNION(c c n-1, n) CS 473 . . n-1 Updated Objects (Denoted By ‘P’) P PP {c 1, c 2} {c 3} {c 1, c 2, c 3} PPP {c c c } {c 2} 1, 2, 3 {c 1, c 2, . . , cn-1} Lecture X {c 1, c 2} 4 1, 2, 3, 4 {cn} {c 1, c 2, . . , cn-1, cn, } PP P 26

Analysis of the Simple Union Implementation • The total number of representative pointer updates

Analysis of the Simple Union Implementation • The total number of representative pointer updates MAKE-SET operations UNION operations since Ø Thus, on the average, each operation requires Q(m) time Ø That is, the amortized time of an operation is Q(m) CS 473 Lecture X 27

A Weighted-Union Heuristic • The simplementation is inefficient because Ø We may be appending

A Weighted-Union Heuristic • The simplementation is inefficient because Ø We may be appending a longer list to a shorter list during a UNION operation so that we must update the representative pointer of each member of the longer list Weighted Union Heuristic – Maintain the length of each list – Always append the smaller list to the longer list With ties broken arbitrarily ‼ A single UNION can still take (m) time if both sets have (m) members CS 473 Lecture X 28

Weighted Union Heuristic Theorem: A sequence of m MAKE-SET, UNION & FINDSET operations, n

Weighted Union Heuristic Theorem: A sequence of m MAKE-SET, UNION & FINDSET operations, n of which are MAKE-SET operations, takes O(m+nlgn) time Proof: Try to compute an upper bound on the number of representative pointer updates for each object in a set of size n Consider a fixed object x – Each time x’s R-PTR was updated, x was a member of the smaller set P {x} {v}→ { c , v} 1 -st update |Sx| 2 P {x, v} {w 1, w 2} → { c , P v , w 1, w 2} 2 -nd update |Sx| 4 {x, v, w } {z , z , z } → { P , w , z , z }; |S | 4 c , v. PPP 1 2 3 -rd update |S| CS 473 1 2 3 4 x 8 Lecture X 29

Weighted Union Heuristic • For any k n, after x’s R-PTR has been updated

Weighted Union Heuristic • For any k n, after x’s R-PTR has been updated times the resulting set must have at least k members Ø R-PTR of each object can be updated at most time over all UNION operations Analysis of The Weighted-Union Heuristic • The figure below illustrates a worst case sequence for a set with n = 16 objects • The total number of R-PTR updates lg n CS 473 Lecture X 30

Analysis of The Weighted-Union Heuristic 1 2 CS 473 3 4 5 6 7

Analysis of The Weighted-Union Heuristic 1 2 CS 473 3 4 5 6 7 8 9 Lecture X 1 0 1 1 1 2 1 3 1 4 1 5 1 6 31

Analysis of The Weighted-Union Heuristic 1 , 2 1 2 CS 473 3 ,

Analysis of The Weighted-Union Heuristic 1 , 2 1 2 CS 473 3 , 4 3 4 5, 6 5 6 7, 8 7 8 9 , 10 9 Lecture X 1 0 11 , 12 1 1 1 2 13 , 14 1 3 1 4 15 , 16 1 5 1 6 32

Analysis of The Weighted-Union Heuristic 1 , 2 , 3, 4 1 , 2

Analysis of The Weighted-Union Heuristic 1 , 2 , 3, 4 1 , 2 1 2 CS 473 5 , 6 , 7, 8 3 , 4 3 4 5, 6 5 6 9 , 10 , 11, 12 7, 8 7 8 9 , 10 9 Lecture X 1 0 11 , 12 1 1 1 2 13 , 14 , 15, 13 , 14 1 3 1 4 16 15 , 16 1 5 1 6 33

Analysis of The Weighted-Union Heuristic 9 , 10 , 11 , 12 , 13,

Analysis of The Weighted-Union Heuristic 9 , 10 , 11 , 12 , 13, 14, 15, 16 1 , 2 , 3 , 4 , 5, 6, 7, 8 1 , 2 , 3, 4 1 , 2 1 2 CS 473 5 , 6 , 7, 8 3 , 4 3 4 5, 6 5 6 9 , 10 , 11, 12 7, 8 7 8 9 , 10 9 Lecture X 1 0 11 , 12 1 1 1 2 13 , 14 , 15, 13 , 14 1 3 1 4 16 15 , 16 1 5 1 6 34

Analysis of The Weighted-Union Heuristic 1 , 2 , 3 , 4 , 5

Analysis of The Weighted-Union Heuristic 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9, 10, 11, 12, 13, 14, 15, 16 9 , 10 , 11 , 12 , 13, 14, 15, 16 1 , 2 , 3 , 4 , 5, 6, 7, 8 1 , 2 , 3, 4 1 , 2 1 2 CS 473 5 , 6 , 7, 8 3 , 4 3 4 5, 6 5 6 9 , 10 , 11, 12 7, 8 7 8 9 , 10 9 Lecture X 1 0 11 , 12 1 1 1 2 13 , 14 , 15, 13 , 14 1 3 1 4 16 15 , 16 1 5 1 6 35

Analysis of The Weighted-Union Heuristic • Each MAKE-SET & FIND-SET operation takes O(1) time,

Analysis of The Weighted-Union Heuristic • Each MAKE-SET & FIND-SET operation takes O(1) time, and there are O(m) of them Ø The total time for the entire sequence = O(m + nlgn) Disjoint Set Forests In a faster implementation, we represent sets by rooted trees – Each node contains one member – Each tree represents one set – Each member points only to its parent – The root of each tree contains the representative – Each root is its own parent CS 473 Lecture X 36

Disjoint Set Forests x 1 x 2 x 4 y 1 x 3 y

Disjoint Set Forests x 1 x 2 x 4 y 1 x 3 y 2 UNION(x, y) x 2 y 3 y 2 x 1 y 3 x 4 CS 473 Lecture X 37

Disjoint Set Forests Straightforward Implementation MAKE-SET : Simply creates a tree with just one

Disjoint Set Forests Straightforward Implementation MAKE-SET : Simply creates a tree with just one node : O(1) FIND-SET : Follows parent pointers until the root node is found The nodes visited on this path toward the root constitute the FIND-PATH UNION : Makes the root of one tree to point to the other one Heuristics To Improve the Running Time • Straightforward implementation is no faster than ones that use the linked-list representation • A sequence of n – 1 UNIONs, following a sequence of n MAKE-SETs, may create a tree, which is just a linear chain of n nodes CS 473 Lecture X 38

Heuristics To Improve the Running Time First Heuristic : UNION by Rank • Similar

Heuristics To Improve the Running Time First Heuristic : UNION by Rank • Similar to the weighted-union used for the linked-list representation • The idea is to make the root of the tree with fewer nodes point to the root of the tree with more nodes • Rather than explicitly keeping the size of the subtree rooted at each node We maintain a rank – that approximates the logarithm of the subtree size – and is also an upperbound on the height of the node • During a UNION operation – make the root with smaller rank to point to the root with larger rank CS 473 Lecture X 39

Heuristics To Improve the Running Time Second Heuristic : Path Compression • Use it

Heuristics To Improve the Running Time Second Heuristic : Path Compression • Use it during the FIND-SET operations • Make each node on the FIND-PATH to point directly to the root f e d c b a CS 473 FIND-SET(b) Lecture X 40

Heuristics To Improve the Running Time Path Compression During FIND-SET(b) Operation f b c

Heuristics To Improve the Running Time Path Compression During FIND-SET(b) Operation f b c d e a CS 473 Lecture X 41

Pseudocodes For the Heuristics Implementation of UNION-BY-RANK Heuristic p[x] : Pointer to the parent

Pseudocodes For the Heuristics Implementation of UNION-BY-RANK Heuristic p[x] : Pointer to the parent of the node x rank[x] : An upperbound on the height of node x in the tree MAKE-SET(x) p[x] ← x rank[x] ← 0 end UNION(x, y) LINK(FIND-SET(x), FIND-SET(y)) end LINK(x, y) if rank[x] > rank[y] then p[y] ← x else p[x] ← y if rank[x] = rank[y] then rank[y] = rank[y] + 1 endif end CS 473 Lecture X 42

Implementation of UNION-BY-RANK Heuristic – When a singleton set is created by a MAKE-SET

Implementation of UNION-BY-RANK Heuristic – When a singleton set is created by a MAKE-SET the initial rank of the single node in the tree is zero – Each FIND-SET operation leaves all ranks unchanged – When applying a UNION to two trees, we make the root of tree with higher rank the parent of the root of lower rank Ties are broken arbitrarily CS 473 Lecture X 43

Implementation of the Path-Compression Heuristic The FIND-SET procedure with Path-Compression Iterative Version FIND-SET(x) y←x

Implementation of the Path-Compression Heuristic The FIND-SET procedure with Path-Compression Iterative Version FIND-SET(x) y←x while y p[y] do y ← p[y] endwhile root ← y while x p[x] do parent ← p[x] ← root x ← parent endwhile return root end CS 473 Recursive Version FIND-SET(x) if x p[x] then p[x] ← FIND-SET(p[x]) endif return p[x] end Lecture X 44