page_title: Proof of O(log*n) time complexity of union–find
section_title: Proof
hierarchical_section_title: Proof of O(log*n) time complexity of union–find / Proof
caption_attribution_description: English: Proof of O(log*n) Union Find
context_page_description: In computer science, Union Find is an algorithm for doing certain operations on sets. This page is about proof of O(log*n) amortized time of Union Find
  Statement: If m operations, either Union or Find, are applied to n elements, the total run time is O(m log*n), where log* is the iterated logarithm.
context_section_description: Lemma 1: As the find function follows the path along to the root, the rank of node it encounters is increasing.
  Proof: claim that as Find and Union operations are applied to the data set, this fact remains true over time. Initially when each node is the root of its own tree, it's trivially true. The only case when the rank of a node might be changed is when the Union by Rank operation is applied. In this case, a tree with smaller rank will be attached to a tree with greater rank, rather than vice versa. And during the find operation, all nodes visited along the path will be attached to the root, which has larger rank than its children, so this operation won't change this fact either.
  Lemma 2: A node u which is root of a subtree with rank r has at least 2ʳ nodes.
  Proof:  Initially when each node is the root of its own tree, it's trivially true. Assume that a node u with rank r has at least 2ʳ nodes. Then when two trees with rank r Union by Rank and form a tree with rank r + 1, the new node has at least 2ʳ + 2ʳ = 2ʳ ⁺ ¹ nodes.
  Lemma 3: The maximum number of nodes of rank r is at most n/2ʳ.
  Proof:  From lemma 2, we know that a node u which is root of a subtree with rank r has at least 2ʳ nodes. We will get the maximum number of nodes of rank r when each node with rank r is the root of a tree that has exactly 2ʳ nodes. In this case, the number of nodes of rank r is n/2ʳ
  For convenience, we define "bucket" here: a bucket is a set that contains vertices with particular ranks.
  We create some buckets and put vertices into the buckets according to their ranks inductively. That is, vertices with rank 0 go into the zeroth bucket, vertices with rank 1 go into the first bucket, vertices with ranks 2 and 3 go into the second bucket. If the Bth bucket contains vertices with ranks from interval [r, 2ʳ − 1] = [r, R - 1] then the (B+1)st bucket will contain vertices with ranks from interval [R, 2ᴿ − 1].
  We can make two observations about the buckets.
  The total number of buckets is at most log*n
  Proof: When we go from one bucket to the next, we add one more two to the power, that is, the next bucket to [B, 2ᴮ − 1] will be [2ᴮ, 2²ᴮ − 1]
  The maximum number of elements in bucket [B, 2ᴮ – 1] is at most 2n/2ᴮ
  Proof: The maximum number of elements in bucket [B, 2ᴮ – 1] is at most n/2ᴮ + n/2ᴮ⁺¹ + n/2ᴮ⁺² + … + n/2²ᴮ – 1 ≤ 2n/2ᴮ
  Let F represent the list of "find" operations performed, and let
  Then the total cost of m finds is T = T₁ + T₂ + T₃
  Since each find operation makes exactly one traversal that leads to a root, we have T₁ = O(m).
  Also, from the bound above on the number of buckets, we have T₂ = O(mlog*n).
  For T₃, suppose we are traversing an edge from u to v, where u and v have rank in the bucket [B, 2ᴮ − 1] and v is not the root (at the time of this traversing, otherwise the traversal would be accounted for in T₁). 
  Fix u and consider the sequence v₁,v₂,...,vₖ that take the role of v in different find operations.
  Because of path compression and not accounting for the edge to a root, this sequence contains only different nodes and because of Lemma 1 we know that the ranks of the nodes in this sequence are strictly increasing.
  By both of the nodes being in the bucket we can conclude that the length k of the sequence (the number of times node u is attached to a different root in the same bucket) is at most the number of ranks in the buckets B, i.e. at most 2ᴮ − 1 − B < 2ᴮ.
  Therefore, 
  From Observations 1 and 2, we can conclude that 
  Therefore, T = T₁ + T₂ + T₃ = O(m log*n).
caption: Proof of 
    
      
        
          O
          (
          
            log
            
              ∗
            
          
          ⁡
          n
          )
        
      
      {\displaystyle O(\log ^{*}n)}
    
   Union Find
exif: {"Image PhotometricInterpretation": "2", "Image Orientation": "Horizontal (normal)", "Image XResolution": "72", "Image YResolution": "72", "Image ResolutionUnit": "Pixels/Inch", "Image ExifOffset": "102", "EXIF ExifImageWidth": "556", "EXIF ExifImageLength": "261"}