7    VisuAlgo    Union-Find Disjoint Sets
Exploration Mode ▿

Examples

Initialize(N)

FindSet(i)

IsSameSet(i, j)

UnionSet(i, j)

Three disjoint sets

Four disjoint sets

2 Trees of Rank 1

2 Trees of Rank 2

2 Trees of Rank 3

1 Tree of Rank 4

Go

Go

i =

j =

Go

i =

j =

Go

About Team Terms of use
slow
fast
go to beginning previous frame pause play next frame go to end

About

VisuAlgo was conceptualised in 2011 by Dr Steven Halim as a tool to help his students better understand data structures and algorithms, by allowing them to learn the basics on their own and at their own pace.

VisuAlgo contains many advanced algorithms that are discussed in Dr Steven Halim's book ('Competitive Programming', co-authored with his brother Dr Felix Halim) and beyond. Today, some of these advanced algorithms visualization/animation can only be found in VisuAlgo.

Though specifically designed for National University of Singapore (NUS) students taking various data structure and algorithm classes (e.g. CS1010, CS1020, CS2010, CS2020, CS3230, and CS3230), as advocators of online learning, we hope that curious minds around the world will find these visualisations useful too.

VisuAlgo is not designed to work well on small touch screens (e.g. smartphones) from the outset due to the need to cater for many complex algorithm visualizations that require lots of pixels and click-and-drag gestures for interaction. The minimum screen resolution for a respectable user experience is 1024x768 and only the landing page is relatively mobile-friendly.

VisuAlgo is an ongoing project and more complex visualisations are still being developed.

The most exciting development is the automated question generator and verifier (the online quiz system) that allows students to test their knowledge of basic data structures and algorithms. The questions are randomly generated via some rules and students' answers are instantly and automatically graded upon submission to our grading server. This online quiz system, when it is adopted by more CS instructors worldwide, should technically eliminate manual basic data structure and algorithm questions from typical Computer Science examinations in many Universities. By setting a small (but non-zero) weightage on passing the online quiz, a CS instructor can (significantly) increase his/her students mastery on these basic questions as the students have virtually infinite number of training questions that can be verified instantly before they take the online quiz. The training mode currently contains questions for 12 visualization modules. We will soon add the remaining 8 visualization modules so that every visualization module in VisuAlgo have online quiz component.

Another active branch of development is the internationalization sub-project of VisuAlgo. We want to prepare a database of CS terminologies for all English text that ever appear in VisuAlgo system. This is a big task and requires crowdsourcing. Once the system is ready, we will invite VisuAlgo visitors to contribute, especially if you are not a native English speaker. Currently, we have also written public notes about VisuAlgo in various languages: zh, id, kr, vn, th.

Team

Project Leader & Advisor (Jul 2011-present)
Dr Steven Halim, Senior Lecturer, School of Computing (SoC), National University of Singapore (NUS)
Dr Felix Halim, Software Engineer, Google (Mountain View)

Undergraduate Student Researchers 1 (Jul 2011-Apr 2012)
Koh Zi Chun, Victor Loh Bo Huai

Final Year Project/UROP students 1 (Jul 2012-Dec 2013)
Phan Thi Quynh Trang, Peter Phandi, Albert Millardo Tjindradinata, Nguyen Hoang Duy

Final Year Project/UROP students 2 (Jun 2013-Apr 2014)
Rose Marie Tan Zhao Yun, Ivan Reinaldo

Undergraduate Student Researchers 2 (May 2014-Jul 2014)
Jonathan Irvin Gunawan, Nathan Azaria, Ian Leow Tze Wei, Nguyen Viet Dung, Nguyen Khac Tung, Steven Kester Yuwono, Cao Shengze, Mohan Jishnu

Final Year Project/UROP students 3 (Jun 2014-Apr 2015)
Erin Teo Yi Ling, Wang Zi

Final Year Project/UROP students 4 (Jun 2016-Apr 2017)
Truong Ngoc Khanh, John Kevin Tjahjadi, Gabriella Michelle

Acknowledgements
This project is made possible by the generous Teaching Enhancement Grant from NUS Centre for Development of Teaching and Learning (CDTL).

Terms of use

VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter, course webpage, blog review, email, etc.

If you are a data structure and algorithm student/instructor, you are allowed to use this website directly for your classes. If you take screen shots (videos) from this website, you can use the screen shots (videos) elsewhere as long as you cite the URL of this website (http://visualgo.net) and/or list of publications below as reference. However, you are NOT allowed to download VisuAlgo (client-side) files and host it on your own website as it is plagiarism. As of now, we do NOT allow other people to fork this project and create variants of VisuAlgo. Using the offline copy of (client-side) VisuAlgo for your personal usage is fine.

Note that VisuAlgo's online quiz component is by nature has heavy server-side component and there is no easy way to save the server-side scripts and databases locally. Currently, the general public can only use the 'training mode' to access these online quiz system. Currently the 'test mode' is a more controlled environment for using these randomly generated questions and automatic verification for a real examination in NUS. Other interested CS instructor should contact Steven if you want to try such 'test mode'.

List of Publications

This work has been presented briefly at the CLI Workshop at the ACM ICPC World Finals 2012 (Poland, Warsaw) and at the IOI Conference at IOI 2012 (Sirmione-Montichiari, Italy). You can click this link to read our 2012 paper about this system (it was not yet called VisuAlgo back in 2012).

This work is done mostly by my past students. The most recent final reports are here: Erin, Wang Zi, Rose, Ivan.

Bug Reports or Request for New Features

VisuAlgo is not a finished project. Dr Steven Halim is still actively improving VisuAlgo. If you are using VisuAlgo and spot a bug in any of our visualization page/online quiz tool or if you want to request for new features, please contact Dr Steven Halim. His contact is the concatenation of his name and add gmail dot com.

The Union-Find Disjoint Sets (UFDS) data structure is used to model a collection of disjoint sets, which is able to efficiently (i.e. in nearly constant time) determine which set an item belongs to, test if two items belong to the same set, and union two disjoint sets into one when needed. It can be used to find connected components in an undirected graph, and can hence be used as part of Kruskal Minimum Spanning Tree (MST) algorithm.

X Esc
Next

View the visualisation of Union-Find Disjoint Sets here! Each tree represents a disjoint set (thus a collection of disjoint sets form a forest of trees).


The root of the tree is the representative item of this disjoint set. Now stop and look at the currently visualized trees. How many disjoint sets are there? What are the members of each disjoint set? What is the representative item of each disjoint set?


We can record this forest of trees with an array p of size N items where p[i] records the parent of item i and if p[i] = i, then i is the root of this tree and also the representative item of the set that contains item i. Once again, look at the visualization above and determine the values inside this array p.


When the rank of a vertex i is greater than 0, VisuAlgo will show the value of rank[i] as a red text below vertex i. rank[i] is the upperbound of the height of subtree rooted at vertex i. You will notice that after 'path-compression' heuristic compresses some path, the rank values no longer reflect the true height of that subtree.

X Esc
Prev
Next

There are five available UFDS operations in this visualization page:
Examples, Initialize(N), FindSet(i), IsSameSet(i, j), and UnionSet(i, j).


Examples: List of example UFDS structures with various special characteristics for your starting point. Notice that none of the example contains a 'very tall' tree. Do you understand the reason?

X Esc
Prev
Next

Initialize(N): Create N disjoint sets, all with p[i] = i and rank 0 (these rank values are initially not shown).


Due to the limitation of screen size, we set 1 ≤ N ≤ 16.

X Esc
Prev
Next

FindSet(i): From vertex i, recursively go up the tree (that is, from vertex i, we go to vertex p[i]) until we find the root--the representative item, p[i] = i--of this tree/disjoint set.


Note that we employ path-compression heuristic after each call of FindSet(i) as now every single vertex along the path from vertex i to the root know that the root is their representative item and can point to it directly in O(1).


For an extreme example, choose: Examples, 1 Tree of Rank 4, then execute FindSet(8) to see multiple path-compression in action and notice that rank values at some vertices will become wrong.

X Esc
Prev
Next

IsSameSet(i, j): Check if FindSet(i) == FindSet(j) or not.


Note that FindSet function is called inside IsSameSet function, so path-compression heuristic is also indirectly used.


For an extreme example, choose: Examples, 1 Tree of Rank 4, then execute IsSameSet(0, 8) to see path-compression in action.


This function is used extensively in Kruskal's MST algorithm.

X Esc
Prev
Next

UnionSet(i, j): If item i and j come from two disjoint sets initially, we link the representative item of the smaller tree/disjoint set to the representative item of the taller tree/disjoint set. Do you realize why this heuristic can help maintaining short trees?


This is union-by-rank heuristic in action and will cause the resulting tree to be relatively short. Only if the two trees are equally tall before union (by comparing their rank values heuristically), then the rank of the resulting tree will increase by one unit.


Also note that FindSet function is called inside UnionSet function, so path-compression heuristic is also indirectly used. Each time path-compression heuristic compresses a path, at least one rank values will be incorrect. We do not bother fixing these rank values as they are only used as guiding heuristic for UnionSet function.

X Esc
Prev
Next

As the action is being carried out, each step will be described in the status panel.

X Esc
Prev
Next

You can also follow the pseudocode highlights to trace the algorithm.

X Esc
Prev
Next

Control the animation with the player controls! Keyboard shortcuts are:

Spacebar: play/pause/replay
Left/right arrows: step backward/step forward
-/+: decrease/increase speed
X Esc
Prev
Next

Return to 'Exploration Mode' to start exploring!


Note that if you notice any bug in this visualization or if you want to request for a new visualization feature, do not hesitate to drop an email to the project leader: Dr Steven Halim via his email address: stevenhalim at gmail dot com.

X Esc
Prev