Download Docs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Linked list wikipedia , lookup

Binary search tree wikipedia , lookup

Interval tree wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Transcript
Introduction to the Disjoint Set Data Structure
Suppose you have a group of things. These things might be anything from
people to places to sports to nodes on a graph. For this example, let's use
people and we'll say that these people all belong to a math club. The math
club is divided into several different teams and each person is a member of
exactly one team. How could you quickly determine who was on who's
team? That is where a Disjoint Set Data Structure comes into play.
A disjoint set data structure groups elements into disjoint sets so that
elements in the same group are in the same set of the data structure and
elements in different groups are in different sets of the data structure. There
are two basic operations that are performed on disjoint sets. One is
the find operation that finds which set a particular element belongs to. Find
can also be used to determine if two elements are in the same set. If find(1) is
equal to find(2), then 1 and 2 are in the same set. The other operation is
a union operation which joins two sets into one.
Disjoint Sets are often represented as either linked lists or tree structures.
The problem with linked lists is they have a poor Big-oh runtime of O(n) for the
find operation. Additionally, a union operation must perform two find
operations so the Big-oh runtime of a union operation is also bad. Tree
structures on the other hand can be implemented in such a way as to reduce
the Big-oh runtime to O(log n) which can be further reduced to what is
effectively a constant runtime.
Disjoint Set
This applet demonstrates a disjoint set data structure implemented by using
tree structures.
To watch a short demonstration video click here.
Standard Operations:
Create nodes by entering the number of values to be created and
pressing Add.
Union two nodes by entering the values of the nodes in the two provided
boxes pressing Union.
Find by entering a value and pressing Find.
Optimizations:
Notice, you can control the implementation of the data-structure to include
union by rank, union by size, or path compression. These are the
optimizations which reduce runtime.

Intermediate steps are displayed with nodes of interest highlighted
in red.

If "Union by Rank" is enabled, unifying two sets will result in the
shallowest set. That is, the shallower set will be merged into the deeper
set. Enabling this option will also display each node's rank in red.

If "Union by Size" is enabled, unifying two sets will result in the smaller
set being merged into the larger set. Enabling this option will also
display each node's size in red.

If "Path Compression" is enabled, finding the representative for a set will
result in all nodes along the find path being set as direct children of the
representative. SInce path compression is an optimization done with
find, it can be done in conjunction with either union otimization.
Preset Tests:
There are three tests that can be run to evaluate the runtime perfomance of
the different optimization techniques. Press Run to start the test. If elements
already exist, the test will be run with the current number of elements in the
data structure. Otherwise the test will create 100 elements on which to
perform the operations.
Other Options:
Clear - Clears the data structure.
Undo - undoes the previous operation.
Results - Shows the number of operations performed.
Print - Prints the data structure.
Comparing Implementation Strategies
To facilitate comparision between the different techniques we have created
several tests that can be run. If elements already exist, the test will be run
with the current number of elements in the data structure. Otherwise the test
will create 100 elements on which to perform the operations. In the following
descriptions NUM_ELEMENTS will represent the number of elements used in
the test.
Test Descriptions
Regular:
1. Repeatedly unions all even nodes with the next larger odd. Then all evens
with each other. Then all mutiples of 5 with each other, pairwise. Then all
multiples of 13 , pairwise.
2. Does a Find for each node.
Random:
1. Unions NUM_ELEMENTS random pairs of nodes.
2. Does a Find for each node.
Repeater:
1. Unions 10 nodes chosen at random with node 5. Unions 10 nodes chosen
at random with node 10. Unions 10 nodes chosen at random with node 15.
Continues unioning 10 nodes chosen at random with the next multiple of 5
until NUM_ELEMENTS unions have been performed.
3. Does a Find for each node.
Click Run to run each test using the same optimization technique such as
none, union by rank, etc. After each test is conlcuded clickResults to see the
results for that test. Then click Reset to reset the results and then run the
next test.
#1) How did the results compare? Did one technique always outperform the
other techinques for every test? Why or why not?
Superstar
Consider the Path Compression technique.
Run the tests for Path Compression and look at the result of perfoming the
union operations.
Click Clear to clear the data structure
Create 6 elements.
Union with Path compression the following sequence
01
21
32
43
50
#2 Do you see a way that the Path Compression technique could be
improved? If so, describe the technique.