I'm working with an union-find
algorithm. In the first part of my program, the algorithm computes a partition of a big set E
.
After that, I want to retrieve all the members of the set S
, which contains a given node x
.
Until now, naively, I was testing membership of all elements of E
to the set S
. But yesterday I was reading "Introduction to Algorithms" (by CLRS, 3rd edition, ex. 21.3-4), and, in the exercises, I found that:
Suppose that we wish to add the operation
PRINT-SET(x)
, which is given a nodex
and prints all the members ofx
's set, in any order. Show how we can add just a single attribute to each node in a disjoint-set forest so thatPRINT-SET(x)
takes time linear in the number of members ofx
's set, and the asymptotic running times of the other operations are unchanged.
"linear in the number of members of x
's set" would be a great improvement for my problem! So, I'm trying to solve this exersice... and after some unsuccessful tries, I'm asking help on Stack Overflow!
Recall that union-find is implemented as upside-down tree, where for each set S = {v1, v2, ..., vn}, you have vn - 1 edges, that ultimately have the same root (or sink).
Now, whenever you add an edge (vi, vj) to this tree, add another edge (using the new attribute) (vj, vi). And when you remove a node, remove the attribute as well.
Note that the new edge is separated from the old one. You use it only when printing all elements in a set. And modify it whenever any original edge is modified in the original algorithm.
Note that this attribute is actually a list of nodes, but the total number of elements in all lists combined is still n - 1.
This will give you a second tree, but not upside down. Now, using the root, and doing some tree-traversal (using e.g. BFS or DFS), you can print all elements.
I managed to write another algorithm without the use of lists (it is somewhat more convenient to use with my programming language, i.e. ANSI-C).
I did that with the help of
I found this thread after my first post, my apologies.
In pseudo-code (not yet tested) :