Skip to content

Commit 5456b27

Browse files
committed
UF bug fix, exercises (comp crashed; git probs)
1 parent cf41afe commit 5456b27

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

src/algorithms/controllers/unionFindFind.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737

3838
// restoring the visualisers
3939
chunker.add(
40-
`while n != parent[n]`,
40+
`Find(n)`,
4141
(vis, parentArray) => {
4242
vis.array.set([N_ARRAY, parentArray], 'unionFind');
4343
vis.tree.setNTree(realNodes, realEdges, nodes, edges);
@@ -231,7 +231,7 @@ export default {
231231
vis.tree.addEdge(grandparent, n);
232232
vis.tree.layout();
233233
},
234-
[n, parentArr[parentArr[n]]]
234+
[n, parentArr[n]]
235235
);
236236

237237
chunker.add(

src/algorithms/controllers/unionFindUnion.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// XXX maybe get rid of some of the animation steps where there are
2+
// multiple steps for a single line of code (generally using the last
3+
// step is sufficient)
14
/* eslint-disable eqeqeq */
25
/* eslint-disable no-plusplus */
36
/* eslint-disable no-param-reassign */

src/algorithms/explanations/UFExp.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Extra information is maintained for each subset so Union can reduce the
3232
height and when Find traverses a path from a node to the root, we take
3333
the opportunity to reduce the length of the path for future calls to Find
3434
(the tree height is reduced and the "width" is increased by having more
35-
children for some nodes).
35+
children for some nodes - see below).
3636

3737
Interestingly, the extra information used to reduce the height is only
3838
approximate and the method used to reduce path lengths is not as thorough
@@ -42,4 +42,19 @@ nodes point directly to the root. It has been shown that no matter how
4242
large the set is, Find has takes very close to constant time on average
4343
(the inverse of the Ackerman function to be precise). In this animation
4444
we allow path compression to be disabled so you can experiment to see
45-
how much this aspect of the algorithm reduces tree height.
45+
how much this aspect of the algorithm reduces tree height.
46+
47+
## Path compression in Find
48+
49+
The method of path compression used here is that whenever we are at a
50+
node, searching upwards to find the root of the tree, we change the parent
51+
of the node to be the (previous) grandparent.
52+
53+
## Union by rank
54+
55+
When two trees are joined in a Union operation, we want to minimise the
56+
height of the resulting tree. This can be done by making the shorter
57+
tree a subtree of the taller tree. Here we maintain the *rank* of each
58+
tree, which is an approximation (an upper bound) on the height. Each
59+
tree starts with rank 1 and when two trees of equal rank are joined, the
60+
rank of the resulting tree is incremented.

src/algorithms/extra-info/UFInfo.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,26 @@
44

55
<a href="https://www.geeksforgeeks.org/introduction-to-disjoint-set-data-structure-or-union-find-algorithm/" target="_blank"><span style="color:blue">**geeksforgeeks.com**</span></a>
66

7-
<a href="https://cp-algorithms.com/data_structures/disjoint_set_union.html" target="_blank"><span style="color:blue">**cp-algorithms.com**</span></a>
7+
<a href="https://cp-algorithms.com/data_structures/disjoint_set_union.html" target="_blank"><span style="color:blue">**cp-algorithms.com**</span></a>
8+
9+
## Exercises/Exploration
10+
11+
Rather than using the *rank* of each tree, we could use the precise
12+
*height* of the tree. What are the potential advantages and
13+
disadvantages of this?
14+
15+
The path compression used here approximately halves the length of the
16+
path from a node to the root. Instead, it would be possible to reduce
17+
the path length to at most one. How could this be done and what are
18+
the potential advantages and disadvantages of this?
19+
20+
Find a sequence of union operations that results in all 10 elements
21+
being in the same set but maximizes the height of the resulting tree.
22+
Does path compression make a difference in this case?
23+
24+
Find a sequence of union operations that results in all 10 elements
25+
being in the same set but maximizes the height of the resulting tree
26+
when path compression is off but gives an almost optimal result with
27+
path compression on. How many extra elements would be needed to have the
28+
result optimal with path compression?
29+

0 commit comments

Comments
 (0)