SlideShare a Scribd company logo
Disjoint-set Data Structures   원저자: vlad_D   발표자: 심준현
목   차 ,[object Object]
 The Problem
The Solution
Basic Functions
 LL Implementation
Root Tree Implementation
Practice,[object Object]
The Problem 3 1 2 Input 4 5 1 2 5 4 5 1
새로운 개념 대표값(Representatives): 어떤 집합의 고유한, 집합의 Key값 Disjoint Set이므로 해당 집합 안에 있기만 하면 자격 충분! 이번에는 편의상 최대값으로 설명
The Solution – Step 1 3 1 2 4 5 {1} {2} {3} {4} {5} 모든 원소가 자신이 속한 집합의 대표값
The Solution – Step 2 3 1 2 4 5 {1, 2} {3} {4} {5} 집합 1, 2가 합쳐진 집합의 대표값은2
The Solution – Step 3 3 1 2 4 5 {1, 2} {3} {4, 5} 집합 4, 5가 합쳐진 집합의 대표값은5
The Solution – Step 4 3 1 2 4 5 {1, 2, 4, 5} {3} 집합 {1, 2}, {4, 5}가 합쳐진 집합의 대표값은5
The Solution Q: 원소 a와 원소 b가 같은 집합에 있는지 확인하는 방법은? 3 1 2 4 5 A: 원소가 속한 집합의 대표값을 비교한다! 같으면 O 다르면 X
Basic Functions 1. CREATE_SET(x)  원소 x를 유일한 원소로 갖는 집합 생성 2. MERGE_SETS(x, y)  x가 속한 집합과 y가 속한 집합을 통합 3. FIND_SET(x) x가 속한 집합의 대표값 또는 그에 대한 포인터를 리턴
Basic Functions Pseudo Code Read N;  for (each person x from 1 to N)  CREATE-SET(x); for (each pair of friends (x y) )  if (FIND-SET(x) != FIND-SET(y))  	MERGE-SETS(x, y);
LL Implementation 원소 하나가 한 개의 노드 각 노드는대표 노드를 가리키는 포인터와 다음 노드를 가리키는 포인터를 갖는다. 1 1 2 2 {1} ← {2} Merge의 경우
LL Implementation 다수의 원소를 갖는 집합끼리의 Merge는? 한 집합의 대표노드를 그대로 다른 집합의 꼬리 노드에Append (즉, 앞 집합의 꼬리 노드의Next 포인터를 뒷 집합의 대표 노드로 이어줌) 뒷 집합에 속한 각 노드의 대표 노드 포인터를 모두 앞 노드의 대표 노드로 이어준다.
LL Implementation 결  과
LL Implementation 개선의 여지가 있다! 한 집합의 대표노드를 그대로 다른 집합의 꼬리 노드에Append 뒤에 붙는 집합의 원소 수가 더 작을 수록 T.C. 감소… 그렇다면? 대표 노드에subnode의 수를 기록!
LL Implementation LL Impl.  T.C. : O(M+NlogN), S.C.: O(N) BFS Impl. T.C. : O(M+N),       S.C.: O(M+N) M: FIND, MERGE, CREATE 호출 횟수 N: 원소의 개수 아직 뭔가 부족하다..
Root Trees Implementation 원소 하나 당 노드 하나 각 노드는 원소 값과 부모 노드에 대한 포인터를 갖는다. 루트 노드== 대표 노드 LL Impl.과의 차이점:  대표 노드 포인터와 Next 포인터가 사라지고 대신 Prev포인터(부모 포인터)가 생겼다.
Root Trees Impl. – Step 1 for(int i = 1; i <= 5; i++) CREATE_SET(i);
Root Trees Impl. – Step 2 MERGE_SETS(1, 2);
Root Trees Impl. – Step 3 MERGE_SETS(5, 4);
Root Trees Impl. – Step 4 (Final) MERGE_SETS(5, 1);
Root Trees Impl. 지금까지의 Computational Complexity는 LL Impl.과 동일 개선 방법 Union by rank 노드마다Subnode의 로그 깊이를 저장 Path compression 탐색 단계 하나 당 로그 깊이가 1씩 감소

More Related Content

More from skku_npc

Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
skku_npc
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
 
String Searching Algorithms
String Searching AlgorithmsString Searching Algorithms
String Searching Algorithms
skku_npc
 
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithms
skku_npc
 
Data Structures
Data StructuresData Structures
Data Structures
skku_npc
 
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorization
skku_npc
 
Mathematics
MathematicsMathematics
Mathematics
skku_npc
 
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
skku_npc
 
Binary Search
Binary SearchBinary Search
Binary Search
skku_npc
 
How to find a solution
How to find a solutionHow to find a solution
How to find a solution
skku_npc
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
skku_npc
 
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursion
skku_npc
 
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Games
skku_npc
 
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
skku_npc
 

More from skku_npc (14)

Maximum Flow
Maximum FlowMaximum Flow
Maximum Flow
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
String Searching Algorithms
String Searching AlgorithmsString Searching Algorithms
String Searching Algorithms
 
Line sweep algorithms
Line sweep algorithmsLine sweep algorithms
Line sweep algorithms
 
Data Structures
Data StructuresData Structures
Data Structures
 
Prime numbers, factorization
Prime numbers, factorizationPrime numbers, factorization
Prime numbers, factorization
 
Mathematics
MathematicsMathematics
Mathematics
 
Greedy is Good
Greedy is GoodGreedy is Good
Greedy is Good
 
Binary Search
Binary SearchBinary Search
Binary Search
 
How to find a solution
How to find a solutionHow to find a solution
How to find a solution
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
An introduction to recursion
An introduction to recursionAn introduction to recursion
An introduction to recursion
 
Algorithm Games
Algorithm GamesAlgorithm Games
Algorithm Games
 
Introduction to Graphs
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
 

disjoint-set data structures

  • 1. Disjoint-set Data Structures 원저자: vlad_D 발표자: 심준현
  • 2.
  • 8.
  • 9. The Problem 3 1 2 Input 4 5 1 2 5 4 5 1
  • 10. 새로운 개념 대표값(Representatives): 어떤 집합의 고유한, 집합의 Key값 Disjoint Set이므로 해당 집합 안에 있기만 하면 자격 충분! 이번에는 편의상 최대값으로 설명
  • 11. The Solution – Step 1 3 1 2 4 5 {1} {2} {3} {4} {5} 모든 원소가 자신이 속한 집합의 대표값
  • 12. The Solution – Step 2 3 1 2 4 5 {1, 2} {3} {4} {5} 집합 1, 2가 합쳐진 집합의 대표값은2
  • 13. The Solution – Step 3 3 1 2 4 5 {1, 2} {3} {4, 5} 집합 4, 5가 합쳐진 집합의 대표값은5
  • 14. The Solution – Step 4 3 1 2 4 5 {1, 2, 4, 5} {3} 집합 {1, 2}, {4, 5}가 합쳐진 집합의 대표값은5
  • 15. The Solution Q: 원소 a와 원소 b가 같은 집합에 있는지 확인하는 방법은? 3 1 2 4 5 A: 원소가 속한 집합의 대표값을 비교한다! 같으면 O 다르면 X
  • 16. Basic Functions 1. CREATE_SET(x) 원소 x를 유일한 원소로 갖는 집합 생성 2. MERGE_SETS(x, y) x가 속한 집합과 y가 속한 집합을 통합 3. FIND_SET(x) x가 속한 집합의 대표값 또는 그에 대한 포인터를 리턴
  • 17. Basic Functions Pseudo Code Read N; for (each person x from 1 to N) CREATE-SET(x); for (each pair of friends (x y) ) if (FIND-SET(x) != FIND-SET(y)) MERGE-SETS(x, y);
  • 18. LL Implementation 원소 하나가 한 개의 노드 각 노드는대표 노드를 가리키는 포인터와 다음 노드를 가리키는 포인터를 갖는다. 1 1 2 2 {1} ← {2} Merge의 경우
  • 19. LL Implementation 다수의 원소를 갖는 집합끼리의 Merge는? 한 집합의 대표노드를 그대로 다른 집합의 꼬리 노드에Append (즉, 앞 집합의 꼬리 노드의Next 포인터를 뒷 집합의 대표 노드로 이어줌) 뒷 집합에 속한 각 노드의 대표 노드 포인터를 모두 앞 노드의 대표 노드로 이어준다.
  • 21. LL Implementation 개선의 여지가 있다! 한 집합의 대표노드를 그대로 다른 집합의 꼬리 노드에Append 뒤에 붙는 집합의 원소 수가 더 작을 수록 T.C. 감소… 그렇다면? 대표 노드에subnode의 수를 기록!
  • 22. LL Implementation LL Impl. T.C. : O(M+NlogN), S.C.: O(N) BFS Impl. T.C. : O(M+N), S.C.: O(M+N) M: FIND, MERGE, CREATE 호출 횟수 N: 원소의 개수 아직 뭔가 부족하다..
  • 23. Root Trees Implementation 원소 하나 당 노드 하나 각 노드는 원소 값과 부모 노드에 대한 포인터를 갖는다. 루트 노드== 대표 노드 LL Impl.과의 차이점: 대표 노드 포인터와 Next 포인터가 사라지고 대신 Prev포인터(부모 포인터)가 생겼다.
  • 24. Root Trees Impl. – Step 1 for(int i = 1; i <= 5; i++) CREATE_SET(i);
  • 25. Root Trees Impl. – Step 2 MERGE_SETS(1, 2);
  • 26. Root Trees Impl. – Step 3 MERGE_SETS(5, 4);
  • 27. Root Trees Impl. – Step 4 (Final) MERGE_SETS(5, 1);
  • 28. Root Trees Impl. 지금까지의 Computational Complexity는 LL Impl.과 동일 개선 방법 Union by rank 노드마다Subnode의 로그 깊이를 저장 Path compression 탐색 단계 하나 당 로그 깊이가 1씩 감소
  • 29. Root Trees Impl. – Steps 1 & 2 1/0 2/0 1/1 2/0 5/0 4/0 5/1 4/0
  • 30. Root Trees Impl. – Step 3 1/1 1/2 2/0 2/0 5/1 4/0 5/1 4/0
  • 31. Root Trees Impl. – Final State 1/2 5/1 2/0 4/0
  • 32. Root Trees Impl. – Pseudo Code Let P[x] = the parent of node x. CREATE-SET(x) P[x] = x rank[x] = 0 MERGE-SETS(x, y) PX = FIND-SET(X) PY =FIND-SET(Y) If (rank[PX] > rank[PY]) P[PY] = PX Else P[PX] = PY If (rank[PX] == rank[PY]) rank[PY]++ FIND-SET(x) If (x != P[x]) P[x] = FIND-SET(P[X]) Return P[X]
  • 33. Practice Problems grafixMask Problem 각 좌표를 하나의 노드로 본다 각 노드에서 그 상하좌우 노드에 대한 판별 작업 후 막혀 있지 않으면 MERGE_SETS(Node1, Node2) 처리해서 통합