13.7k views
2 votes
Suppose you are given the following array-based representation of a forest of up trees. The forest originally consisted of 12 single-element sets, but over the structure's lifetime, it has changed:

0 1 2 3 4 5 6 7 8 9 10 11
arr 8 -1 0 -2 -1 -1 -1 -1 -4 8 -1 3
1 rows X 12 columns
We say that a Union operation "succeeds" if it combines two originally different sets, or that it "fails" if both arguments to the Union operation are the same set.
How many successful Union operations must have occurred in the history of the structure above? (integer)
What is the height of the tallest up tree in the example above? (integer)
Does it appear that the Disjoint Sets implementation that produced the table above use path compression?
(a) Yes
(b) No
suppose that over the lifetime of a disjoint sets structure holding 582 elements there are 118 successful union operations. how many uptrees exist in the structure?

1 Answer

3 votes

Final answer:

There were 5 successful Union operations, the tallest up tree has a height of 3, path compression does not seem to be used, and there are 464 uptrees after 118 successful union operations on 582 elements.

Step-by-step explanation:

To answer the questions about the disjoint set operations given the array-based representation of a forest of up trees, we need to analyze the array provided and understand how union operations work.

For the first question, the number of successful Union operations can be determined by counting the number of non-root elements in the array, as each successful Union operation would have connected an element to a root of another element (or tree). In the array given (8 -1 0 -2 -1 -1 -1 -1 -4 8 -1 3), the negative numbers represent the roots of the uptrees, and the value indicates the negative size of the set. The positive numbers are pointers to a parent node. Counting the non-root elements gives us 5 successful union operations (elements at indexes 0, 2, 8, 9, and 11).

For the height of the tallest up tree, we must trace the path of pointers from an element to its root. The element at index 0 points to index 8, which has a value of -4. This indicates that there is a chain of pointers 4 elements long, inclusive of the root, which makes the height of this up tree 3 (since we don't include the root in the height measurement).

Regarding whether path compression was used, since we do not see multiple elements directly pointing to the root, it's not evident that path compression has occurred, so the answer is No.

Finally, if there are 582 elements and 118 successful union operations, we take the total number of elements and subtract the number of unions to find the number of uptrees: 582 - 118 = 464 uptrees remain.

User DominikM
by
7.6k points