# Question: In the depth determination problem we maintain a forest

In the depth-determination problem, we maintain a forest F = (Ti) of rooted trees under three operations:

• MAKE-TREE (v) creates a tree whose only node is v.

• FIND-DEPTH (v) returns the depth of node v within its tree.

• GRAFT(r, v) makes node r, which is assumed to be the root of a tree, become the child of node v, which is assumed to be in a different tree than r but may or may not itself be a root.

The key idea is to maintain in each node v a "pseudo distance" d[v], which is defined so that the sum of the pseudo distances along the path from v to the root of its set Si equals the depth of v in Ti. That is, if the path from v to its root in Si is v0, v1, ..., vk, where v0 = v and vk is Si's root, then the depth of v in Ti is .

b. Give an implementation of MAKE-TREE.

c. Show how to modify FIND-SET to implement FIND-DEPTH. Your implementation should perform path compression, and its running time should be linear in the length of the find path. Make sure that your implementation updates pseudo distances correctly.

d. Show how to implement GRAFT(r, v), which combines the sets containing r and v, by modifying the UNION and LINK procedures. Make sure that your implementation updates pseudo distances correctly. Note that the root of a set Si is not necessarily the root of the corresponding tree Ti.

e. Give a tight bound on the worst-case running time of a sequence of m MAKE-TREE, FIND-DEPTH, and GRAFT operations, n of which are MAKE-TREE operations.

d. Show how to implement GRAFT(r, v), which combines the sets containing r and v, by modifying the UNION and LINK procedures. Make sure that your implementation updates pseudo distances correctly. Note that the root of a set Si is not necessarily the root of the corresponding tree Ti.

e. Give a tight bound on the worst-case running time of a sequence of m MAKE-TREE, FIND-DEPTH, and GRAFT operations, n of which are MAKE-TREE operations.

• MAKE-TREE (v) creates a tree whose only node is v.

• FIND-DEPTH (v) returns the depth of node v within its tree.

• GRAFT(r, v) makes node r, which is assumed to be the root of a tree, become the child of node v, which is assumed to be in a different tree than r but may or may not itself be a root.

The key idea is to maintain in each node v a "pseudo distance" d[v], which is defined so that the sum of the pseudo distances along the path from v to the root of its set Si equals the depth of v in Ti. That is, if the path from v to its root in Si is v0, v1, ..., vk, where v0 = v and vk is Si's root, then the depth of v in Ti is .

b. Give an implementation of MAKE-TREE.

c. Show how to modify FIND-SET to implement FIND-DEPTH. Your implementation should perform path compression, and its running time should be linear in the length of the find path. Make sure that your implementation updates pseudo distances correctly.

d. Show how to implement GRAFT(r, v), which combines the sets containing r and v, by modifying the UNION and LINK procedures. Make sure that your implementation updates pseudo distances correctly. Note that the root of a set Si is not necessarily the root of the corresponding tree Ti.

e. Give a tight bound on the worst-case running time of a sequence of m MAKE-TREE, FIND-DEPTH, and GRAFT operations, n of which are MAKE-TREE operations.

d. Show how to implement GRAFT(r, v), which combines the sets containing r and v, by modifying the UNION and LINK procedures. Make sure that your implementation updates pseudo distances correctly. Note that the root of a set Si is not necessarily the root of the corresponding tree Ti.

e. Give a tight bound on the worst-case running time of a sequence of m MAKE-TREE, FIND-DEPTH, and GRAFT operations, n of which are MAKE-TREE operations.

**View Solution:**## Answer to relevant Questions

Consider the problem of neatly printing a paragraph on a printer. The input text is a sequence of n words of lengths l1, l2, ..., ln, measured in characters. We want to print this paragraph neatly on a number of lines that ...Not just any greedy approach to the activity-selection problem produces a maximum-size set of mutually compatible activities. Give an example to show that the approach of selecting the activity of least duration from those ...A sequence of n operations is performed on a data structure. The ith operation costs i if i is an exact power of 2, and 1 otherwise. Use aggregate analysis to determine the amortized cost per operation.Suppose that all edge weights in a graph are integers in the range from 1 to |V|. How fast can you make Prim’ s algorithm run? What if the edge weights are integers in the range from 1 to W for some constant W?Define a read-only operator that, given a point with Cartesian coordinates x and y, returns the point with Cartesian coordinates f(x) and g(y), where f and g are predefined operators.Post your question