3. Show the final data structure that results from the following program. Also s
ID: 3607457 • Letter: 3
Question
3. Show the final data structure that results from the following program. Also show the answers returned by the two FIND operations below. 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) for i-1 to 18 do MAKE-SET (i) for i-1 to 17 step 2 do UNION (i+1,i) for i-3 to 15 step 4 do UNION(i,i+2) UNION (5,13) UNION (10,15) UNION (4,10) FIND (3) FIND (12) 4. 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)? A node r is at level h in a tree T if the shortest path from z to the root of T is of length h. the length of a path is given by the number of edges in the path.Explanation / Answer
(3)The result of the above program is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
0 1 1 3 4 5 6 7 8 9 10 11 12 1 3 1 4 15 16 17
3 4 5 6 7 8 9 10 11 12 13 14 15
3 3 5 6 7 8 9 10 11 12 13 14 15
5 5 5 6 7 8 9 10 11 12 13 14 15
13 13 13 6 7 8 9 10 11 12 13 14 15
13 13 13 6 7 8 9 15 11 12 13 14 15
15 15 15 6 7 8 9 15 11 12 15 14 15
15 15 15 6 7 8 9 15 11 12 15 14 15
15 15 15 6 7 8 9 15 11 12 15 14 15
And the result for FIND (3)=13 and FIND (12)=14
(4)The nonrecursive code for the Find() function is this
int Find(int x)
int y = p[x];
int p[x] = x;
while (x != y)
{ int z =p[y];
p[y] =x;
x=y ;
y = z;
}
root= x;
y =p[x] ;
p[x]= x;
while( x != y )
{ z= p[y];
p[y]= root;
x =y ;
y = z ;
}
return root;
}
The 1st while travel from x to tree root by reversing all parent pointers along, the 2nd while return this path and sets of all parent pointing to the root.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.