0% found this document useful (0 votes)
132 views

Data Structure - Disjoint Set

The document discusses the disjoint set data structure, also known as union find. It can be used to keep track of disjoint (non-overlapping) sets that may be dynamically merged as needed. The key operations are findSet, which determines which set an element belongs to, and unionSet, which merges two sets. It provides sample C++ code to implement the data structure using parent pointers and path compression to improve efficiency. Examples of applications include minimum spanning trees, connected components in graphs, and network connections problems.

Uploaded by

sonygabriel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Data Structure - Disjoint Set

The document discusses the disjoint set data structure, also known as union find. It can be used to keep track of disjoint (non-overlapping) sets that may be dynamically merged as needed. The key operations are findSet, which determines which set an element belongs to, and unionSet, which merges two sets. It provides sample C++ code to implement the data structure using parent pointers and path compression to improve efficiency. Examples of applications include minimum spanning trees, connected components in graphs, and network connections problems.

Uploaded by

sonygabriel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Disjoint Set Data Structure

IOI Training 2009

Disjoint Set Data Structure


Disjoint-Set DS (Union Find)
Given several disjoint sets initially Combine them when needed! UVa: 459 (Graph Connectivity), 793 (Network Connections), 10227 (Forests), 11503 (Virtual Friends)
The next few slides are adapted from past IOI/ACM ICPC training materials by A/P Ken Sung or others

Overview

Operation Union

Operation Find

Applications
Kruskal Minimum Spanning Tree algorithm Finding Connected Components in Graph etc

How to implement?
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); }

Parent = A

Parent = B

Parent = C

Parent = D

Parent = E

unionSet(A, B)
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); }

Parent = B

Now both A and B become one set, identified by both nodes having the same parent ID

Parent = B

B A

Parent = C

Parent = D

Parent = E

unionSet(A, C)
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); } Parent = C

Parent = C

C B

Now both A, B, C become one set, identified by all three nodes having the same parent ID
Parent = D

Parent = B

Parent = E

unionSet(D, B)
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); } Parent = C

Parent = C

C B

Parent = C

Parent = B

Parent = E

findSet(A)
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); } Parent = C

Parent = C

C B

Parent = C

D
A

Parent = E

Parent = C This is called: Path Compression!

sameSet(A, E)
#define MAX_N 1000 // as needed int pset[MAX_N]; void initSet(){ for(int i = 0; i < MAX_N; i++) pset[i] = i; } int findSet(int i){ return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); } void unionSet(int i, int j){ pset[findSet(i)] = findSet(j); } int sameSet(int i, int j){ return findSet(i) == findSet(j); } Parent = C

Parent = C

C B

Parent = C

D
A

Parent = E

Parent = C

As their parent ID is different, A and E are not in the same set!

Disjoint Set (Union Find)


Okay, thats the basics
No union-by-rank or other detailed analysis

Further Reference:
Introductions to Algorithms, p505-509, ch21.3 Algorithm Design, p151-157, ch4.6

No STL for this DS


We need to use the library shown previously
The code is short anyway

You might also like