Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
19 January 2021

Disjoint Sets

by mervyn

Disjoint-Set Data Structure

A disjoint-set data structure maintains a collection \(C={S_{1}, S_{2}, ..., S_{k}}\) of disjoint dynamic sets. Each set is identified by a representative which is some member of the set.

Operations

Connected Components

Given a graph:

concomp

For each edge (x, y), perform union-set (x, y)

union

Note that, connected components can be computed more efficiently by using DFS

Implementation

Alternative implementation: Let each disjoint set be represented by a tree

uni2

Define an array

parent -= new int [n];
for(int i=0; i<n; i++){
  parent[i]=i;
}

If parent[i]==i, then i is a root node

int Find_Set(int x){
  while(parent[x] !=x){
    x=parent[x];
  }
}
return x;

Complexity: \(T_{find}(n)=O(h)\)

void Union_Set(int x, int y){
  x=Find_Set(x);
  y=Find_Set(y);

  if(x != y){
    parent[y]=x;
  }
}

Complexity: \(T_{union}(n)=2T_{find}(n)+\Theta(1)=O(h)\)

Optimization

To optimize both find-set and union-set, we must minimize the height of the tree

Worst-Case scenario

The worst-case disjoint set. Attaching the tree with less height to the root of the tree with greater height, the worst case must occur when both trees are equal in height - where all the trees to be combined are always the same. uniworst

These are binomial trees, defining Pascal’s triangle

\[\binom{n}{m}= \begin{cases} 1, & m=0 \text{ or } m=n\\ \binom{n-1}{m}+\binom{n-1}{m-1}, & 0<m<n \end{cases}=\frac{n!}{m!(n-m)!}\]

pascal

Suppose we have a worst-case tree of height h

Best-Case scenario

All elements point to the same entry with a resulting height of \(\Theta(1)\): unibest

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 12-1 서로소 집합 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: