# Write a nonrecursive version of find-set with path compression

To implement highest ancestor compression without recursion you need to maintain your own list. Make one pass up the chain to get pointers to the sets that need their parent pointers changed and also to learn the root. Let's begin with an initial disjoint-set forest. We're going to color all the edges in the forest one of three colors: The colors are given as follows: Any edge from a node u to a node v, where the number of nodes in u's subtree is less than half the number of nodes in v's subtree, is colored blue.

Every other edge is red.

## Intro to Algorithms: CHAPTER DATA STRUCTURES FOR DISJOINT SETS

To understand blue and red edges, imagine that you're not going from some node up to the root, but from the root down to some node. Every time you follow a blue path, the number of nodes in the current subtree decreases by at least a factor of two.

That leaves red edges as everything else. So now imagine that you start doing a find operation beginning at some node.

Look at the path that you'll take up to the root. You're going to go through some number of red edges, some number of blue edges, and some number of gold edges. The reason that this coloring scheme is interesting is that something magic happens when you do a find.

Remember, every node on the path is going to have its parent pointer changed to be the ultimate representative in the partition. Imagine that you use the potential function that you described in the question. Every time you follow a red edge other than one that already points to the root directlyyou change the node so that it points directly to the root. No node's weight increases, since we only move edges from descendants up to the root. Now, this analysis completely ignores the costs of doing a union operation, and that's important.

The good news is that it's not a problem.Write a nonrecursive version of FIND with path compression, with same asymptotic running time as the procedure seen in class. What is the asymptotic running time of your nonrecursive FIND when called at a node which is at level h of the tree it belongs to (give the running time in terms of h)? @Hemant- Iterative and recursive DFS's have the same time and space complexities, though the recursive version typically uses stack space while the iterative version uses heap space.

Search for "iterative DFS" for some good pseudocode to use as a starting point. – templatetypedef Nov 10 '13 at Use a disjoint-set forest with union by rank and path compression (use the procedures Make-set and Union and Find with union-by-rank and path-compression as seen in class) Write a nonrecursive version of Find with path compression.

(1 point) Write a nonrecursive version of find-set with path compression. 7. (2 points) Show the data structure that results and the answers returned by the find-set operations in the following program.

Using the trivial algorithm yields a running time of O(n 3). – 2 Write a nonrecursive version of FIND-SET with path compression Proof: The following algorithm does the job using pointer reversal: Algorithm 11 FIND-SET(x) Input: The node x.