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 nodexand 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!