CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks
CS 473Lecture X2 Analysis Of Union By Rank With Path Compression When we use both union-by-rank and path-compression the worst case running time is O(mα(m,n)) where α(m,n) is the very slowly growing inverse of the Ackerman’s function. In any conceivable application of disjoint-set data structure α(m,n) ≤ 4. Thus, we can view the running time as linear as in practical situations.
CS 473Lecture X3 Analysis Of Union By Rank With Path Compression We shall examine the function α to see just how it slowly grows. Then, rather than presenting the very complex proof of O(mα(m,n)) we shall offer a simpler proof of a slightly weaker upper bound O(mlg*n) on the running time. We will prove an O(mlg*n) bound on the running time of the disjoint set operations with union-by-rank & path-compression in order to prove this bound. In order to prove this bound: We first prove some simple properties of ranks.
CS 473Lecture X4 Properties of Ranks LEMMA 1: For all nodes x (a) rank[x] ≤ rank[p[x]] If x ≠ p[x] then rank[x] ≤ rank[p[x]] (b) rank[x] is initially 0 Increases through time until x ≠ p[x], then, it does not change value (c) rank[p[x]] is a monotonically increasing function of time
CS 473Lecture X5 Properties of Ranks Proof of (b) and (c) is trivial After a MAKE-SET(x) operation x is a root node & rank[x] = 0 Rank of only root nodes may increase during a LINK operation A non-root node may never become a root node Q.E.D Proof of (a) is by induction on the number of LINK & FIND-SET operations.
CS 473Lecture X6 Inductive Proof Of Lemma 1 BASIS: True before the first LINK & FIND-SET operations since rank[x] = rank[p[x]] = 0 for all x’s INDUCTIVE STEP: Assume that the lemma holds before performing the operation LINK(x,y). Let rank denote the rank just before the LINK operation. Let rank' denote the rank just after the LINK operation.
CS 473Lecture X7 Inductive Proof Of Lemma 1 CASE:rank[x] ≠ rank[y] Assume without loss of generality that rank[x] < rank[y] Node y becomes the root of the tree formed by the link No rank change occursrank'[x] = rank[x] rank'[y] = rank[y] Only the parent of x changes from p[x] = x to p[x] = y but rank'[x] < rank'[y = p[x]] due to case assumption
CS 473Lecture X8 Inductive Proof Of Lemma 1 CASE:rank[x] = rank[y] Node y becomes the root of the new tree Only the rank of node y changes as rank'[y] = rank[y] + 1 Only the parent of x changes from p[x] = x to p[y] = y rank'[y] = rank[y] + 1 = rank[x] + 1 = rank'[x] + 1 so rank'[y = p[x]] > rank'[x]
CS 473Lecture X9 Proving the Time Bound We will use the aggregate method of amortized analysis to prove the O(mlg*n) time bound. We will assume that we invoke the LINK operations rather than the UNION operation i.e. assume that appropriate FIND-SET operations are performed. The following lemma shows that, even if we count the extra FIND-SET operations the asymptotic running time remains unchanged.
CS 473Lecture X10 Proving the Time Bound LEMMA: Suppose we convert a sequence S' of m' MAKE-SET, UNION and FIND-SET operations into a sequence S of m MAKE-SET, LINK and FIND-SET operations by turning each UNION into two FIND-SET operations followed by a LINK. If sequence S runs in O(mlg*n) time, then, sequence S' runs in O(m'lg*n) time.
CS 473Lecture X11 Proving the Time Bound PROOF: We have m' ≤ m ≤ 3m' since each union operation in S' is converted to 3 operations in sequence S. Since m = O(m') O(mlg*n) for the converted sequence S implies O(m'lg*n) for the original sequence S' of m' operations.
CS 473Lecture X12 Proving the Time Bound THEOREM: A sequence of m MAKE-SET, LINK and FIND-SET operations, n of which are MAKE-SET operations, can be performed on a disjoint-set with union-by-rank and path-compression in worst case time O(mlg*n). PROOF: We assess charges corresponding to the actual cost of each set operation then, compute the total number of charges assessed. Once the entire sequence of operations has been performed, this total gives the actual cost of all the set operations.
CS 473Lecture X13 Proving the Time Bound The charges assessed to MAKE-SET and LINK operations are simpler one charge per operation. Since these operations each take O(1) actual time, the charges assessed are equal to the actual costs. Before discussing charges assessed to the FIND-SET operations we partition node ranks into blocks by putting rank r into block lg*r for r = 0,1,…,. Recall that is the maximum rank.
CS 473Lecture X14 Proving the Time Bound The blocks are numbered as B j for j = 0,1,2,…,j max where j max = lg*(lg n) = lg*n – 1 For notational convenience, we define for integers j ≥ -1
CS 473Lecture X15 Proving the Time Bound Then, for j = 0,1,…, (lg*n – 1) the j-th block B j consists of the set of ranks {B(j-1) + 1, B(j-1) + 2, …, B(j)} Note that two ranks r 1 & r 2 belong to the same block iff lg*r 1 = lg*r 2 Note that B(j) = 2B(j-1) for j ≥ 0
CS 473Lecture X16 Proving the Time Bound Examples to the block concept Let n = 64K = 2 16 = r max = = = 16 ; r = {0,1,2,3,…,16} j max = lg*n - 1 = lg - 1 = (3 + 1) - 1 = 3 ; j = {0,1,2,3} B 0 = {B(-1)+1,…,B(0)} = {,…,1} = {0,1} B 1 = {B(0)+1,…,B(1)} = {1+1,…,2} = {2} B 2 = {B(1)+1,…,B(2)} = {2+1,…,2 2 } = {3,4} B 3 = {B(2)+1,…,B(3)} = {2 2 +1,…, } = {5,…,16} = {5,6,7,8,…,16}
CS 473Lecture X17 Proving the Time Bound Examples to the block concept Let n = 2 64K = r max = = = 64K = 2 16 ; r = {0,1,2,3,…,65536} j max = lg*n - 1 = lg - 1 = (4 + 1) - 1 = 4 ; j = {0,1,2,3,4} B 0 = {0,1} B 1 = {2} B 2 = {3,4} B 3 = {5,6,7,8,…,16} B 4 = {17,18,19,20,21,…,65536}
CS 473Lecture X18 Proving the Time Bound We use two types of charges for a find-set operation: Block Charges and Path Charges Consider the FIND-SET(x 0 ) operation Assume that find pathwhere x l is the root of the tree containing node x 0 p[x i ] = x i+1 for i = 0,1,2,3,…l-1 We assess one block charge to the last node with rank in block j on the find path to the child of the root, i.e.
CS 473Lecture X19 Proving the Time Bound Because ranks strictly increase along any find path we effectively assess block charge to each node where p[x i ] = x l (x i is the root or its child) lg*(rank[x i ]) < lg*(rank[x i+1 ]) We assess one path charge to each node on the find path for which we do not assess a block charge.
CS 473Lecture X20 Proving the Time Bound FACT: Once a node other than the root or its child is assessed block charges, it will never again be assessed path charges. PROOF: Each time path compression occurs, the rank of a node for x i which p[x i ] ≠ x l remains the same but, a new parent of x i has a rank > rank of its old parent. The difference between the ranks of x i & its parent is a monotonically increasing function of time.
CS 473Lecture X21 Proving the Time Bound Once x i & p[x i ] have ranks in different blocks they will always have ranks in different blocks. So, x i will never again be assessed a path charge. Since we charge once for each node visited in each FIND-SET total number of charges assessed = total number of nodes visited in all FIND-SET operations. Thus, total number of charges represents actual cost of all FIND-SETs we wish to show that this total is O(mlg*n)
CS 473Lecture X22 Proving the Time Bound Bound on the number of block charges At most one block charge assessed for each block number on the FP plus one block charge for the child of the root. Since block numbers change from 0 to lg*n – 1 at most lg*n+1 block charges assessed for each FIND-SET Thus, at most m(lg*n + 1) block charges assessed over all FIND-SET operations.
CS 473Lecture X23 Proving the Time Bound Bound on the number of path charges If a node x i is assessed a path charge, then p[x i ] ≠ x l so that x i will be assigned a new parent during path compression. Moreover, x i ’s new parent has a higher rank than its old parent. Suppose that, node x’s rank is in block j.
CS 473Lecture X24 Proving the Time Bound How many times can x i be assigned a new parent and thus assessed a path charge before x i is assigned a parent whose rank is in a different block after which x i will never again be assessed a path charge. This number of times is maximized if x i has the lowest rank in its block, namely B(j-1) + 1 and its parents’ rank successively on values B(j-1) + 2, B(j-1) + 3,…,B(j) We conclude that a vertex can be assessed at most B(j) – B(j – 1) – 1 path charges while its rank is in block j.
CS 473Lecture X25 Proving the Time Bound Bound on the number of path charges…continued Hence, we should bound the number of nodes N(j) whose ranks are in block j. By Lemma 4: For j = 0;
CS 473Lecture X26 Proving the Time Bound For j ≥ 1; N(j)≤ = =≤ = == Thus, for all integers j ≥ 0.
CS 473Lecture X27 Proving the Time Bound Bound on the number of path charges…continued We can bound the total number of path charges P(n) by summing over all blocks the product of The maximum number of nodes with ranks in the block, and The maximum number of path charges per node in that block
CS 473Lecture X28 Proving the Time Bound P(n) ≤ = ≤= = Thus, the total number of charges incurred by FIND-SET operations is O(m(lg*n + 1) + nlg*n) = O(mlg*n) since m ≥ n Since there are O(n) MAKE-SET & LINK operations with one charge each, the total time is O(mlg*n).
CS 473Lecture X29 Properties of Ranks Define size(x) to be the number of nodes in the tree rooted at x, including node x itself LEMMA 2: For all tree roots x size(x) 2 rank[x] PROOF: By induction on the number of LINK operations NOTE: FIND-SET operations change neither the rank not the size of a tree
CS 473Lecture X30 Properties of Ranks BASIS: True for the first link since size(x)=1 & rank[x]=0 initially INDUCTION: Assume that lemma holds before performing LINK(x,y) Let: rank & size denote the rank & size just before the LINK operation rank' & size' denote the rank & size just after the LINK operation
CS 473Lecture X31 Properties of Ranks CASE: rank[x] rank[y] rank'[x] rank[x] & rank'[y] rank[y] Assume without loss of generality that: rank[x] < rank[y] Node y becomes the root of the tree formed by the LINK operation
CS 473Lecture X32 Properties of Ranks size'(y) size(x) + size(y) 2 rank[x] + 2 rank[y] DUE TO INDUCTION 2 rank[y] 2 rank'[y] since rank'[y] rank[y] in this case No ranks or size changes for any nodes other than y.
CS 473Lecture X33 Properties of Ranks CASE: rank[x] rank[y] node y becomes the root of the new tree size'(y) size(x) + size(y) 2 rank[x] + 2 rank[y] 2 rank[y]+1 2 rank'[y]
CS 473Lecture X34 Properties of Ranks INDUCTIVE STEP: Assume that the lemma holds before performing the operation FIND-SET(x) Let rank denote the rank just before the FIND-SET operation and rank' denote the rank just after the FIND-SET operation Consider the find-path of node x: FP x {a 0 =x, a 1, a 2,..., a q }
CS 473Lecture X35 Properties of Ranks WHERE: a i for i 1, 2,..., q are the ancestors of node x a 0 a 1 p[a 0 x], a 2 p[a 1 ],..., a i p[a i-1 ] ... a q p[a q-1 ] and a q is the root of the tree containing node x Only the parents of node x and in FP x change as p[x] a q and p[d i ] a q for i 1, 2,....q-1
CS 473Lecture X36 Properties of Ranks a 9-1 a 9 a i a i-1 x = a 0 Ta i Sa i a = x 0 a i-1 a i a 9-2 a 9-1 Ta i = Sa i a 9
CS 473Lecture X37 Properties of Ranks Due to induction; rank[a 0 =x] < rank[a 1 ] < … < rank[a q-2 ] < rank[a q-1 ] < rank[a q ] However, FIND-SET operation does not make any rank update. Therefore rank'[y] = rank[y] for any node rank'[x] < rank'[a q =p[x]] rank'[a 1 ] < rank'[a q =p[a 1 ]. rank'[a q-2 ] < rank'[a q =p[a q-2 ]]
CS 473Lecture X38 Properties of Ranks Analysis of FIND-SET(x) operations…continued Let denote the tree rooted at a i just before the FIND-SET, and denote the tree rooted at a i just before the FIND-SET Tree rooted at nodes a 2, a 3,…,a q-1,a q change after FIND-SET, i.e. = for i = 2, 3, 4,…,q Hence, height of these nodes may change after FIND-SET. In fact, their height may decrease after a FIND-SET.
CS 473Lecture X39 Properties of Ranks Since, FIND-SET operation does not update rank values; (old) rank values are still upper bounds on the heights of trees rooted at these nodes after FIND-SET. In fact, only the rank value of the root node a q is important since rank value only of this node may be checked during a future LINK operation. Hence, a tree with greater height may be linked to a tree with smaller height during a LINK operation. However, the important point is the following fact.
CS 473Lecture X40 Properties of Ranks FACT: If size(a q ) ≥ 2 rank[ a q] before FIND-SET(x) operation then size'(a q ) ≥ 2 rank'[ a q] after FIND-SET(x) operation where a q is the root of tree containing node x PROOF: Trivial since FIND-SET operation does not change the size and the rank of the tree.
CS 473Lecture X41 Properties of Ranks Consider the Following Sequence MAKE-SET(x 1 ). MAKE-SET(x 8 ) rank= UNION(x 2, x 1 )rank[x 1 ]=rank[x 2 ]=1 UNION(x 4, x 3 ) UNION(x 6, x 5 ) UNION(x 8, x 7 ) x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8
CS 473Lecture X42 Properties of Ranks Consider the Following Sequence UNION(x 3, x 1 )rank[x 1 ]=rank[x 5 ]=2 UNION(x 7, x 5 ) x 2 x 1 x 3 x 4 x 6 x 5 x 7 x 8
CS 473Lecture X43 Properties of Ranks Consider the Following Sequence UNION(x 8, x 4 )x 5 ← FIND-SET(x 8 ) x 1 ← FIND-SET(x 4 ) LINK(x 5,x 1 )
CS 473Lecture X44 Properties of Ranks Consider the last UNION operation UNION(x 8,x 4 ) size(x 1 ) = 4;size(x 5 ) = 4;size'(x 1 ) = = 8 rank(x 1 ) = 2;rank(x 5 ) = 2;rank'(x 1 ) = 3 height(x 1 ) = 2;height(x 5 ) = 2;height'(x 1 ) = 2 Hence, rank'[x 1 ] is a loose upper bound on the height of. However, rank'[x 1 ] is a tight lower bound on the log of size of.
CS 473Lecture X45 Properties of Ranks LEMMA 3: For any integer r ≥ 0 there are at most n/2r nodes of rank r PROOF: Trivial for r = 0; there are at most n/2 0 = n nodes of rank 0 Fix a particular value of r > 0 Suppose that when we assign a rank r to a node x we attach a label x to all nodes in the tree rooted at x Lemma 2 assures that at least 2 r nodes are labeled each time since size(x) ≥ 2 r for a node x with rank r. Note that, a new rank > 0 is assigned to a node
CS 473Lecture X46 Properties of Ranks Only we link two trees of equal rank during a UNION rank[x]=r-1 rank[y]=r-1 rank[y]=r size[x] nodes are labeled with x size[y] ≥2 r-1 size[y] ≥2 r-1 size[x] = size[T x ] ≥ 2 r T x y T y x y x
CS 473Lecture X47 Properties of Ranks Consider the nodes in T x Whenever their roots change due to a LINK during a future UNION Lemma 1 ensures that the rank of the new root > r. Thus, each node is labeled at most once when its root is first assigned the rank r. Since there are n nodes there are at most n labeled nodes with at least 2 r labels assigned for each node of rank r. If there were more than n/2 r nodes of rank r then more than 2 r ∙n/2 r = n nodes would be labeled. CONTRADICTION
CS 473Lecture X48 Properties of Ranks COROLLARY: Every node has rank at most PROOF: If we let max. rank r > lg n due to Lemma 3, there are at most n / 2 r nodes of rank r. However in this case n/2 r < n/2lg n = 1 Hence, the number of nodes of rank r > lg n is < 1 Q.E.D.