Question: [ CLRS , Problem 2 1 - 3 , pages 5 8 4 - 5 8 5 ] Off - line Least Common Ancestors. The
CLRS Problem pages Offline Least Common Ancestors. The least common ancestor of two nodes u and v in a rooted tree T is the node w that is an ancestor of both u and v and that has the greatest depth in T with the above property. In the offline least common ancestor problem, we are given a rooted tree T and an arbitrary set P ui vi im of unordered pairs of nodes in T and we wish to determine the least common ancestor of each pair ui vi in P
To solve the offline least common ancestors problem, the following procedure performs a tree walk of T with the initial call LCArootT Each node is assumed to be coloured WHITE prior to the walk.
LCAu
MakeSetu
ancestorFindu u
for each child v of u in T do
LCAv
UnionFindu Findv
ancestorFindu u
endfor
colouru BLACK
for each node v such that uv in P do
if colourv BLACK
then print The least common ancestor of u and v is ancestorFindv
end
a Argue that line is executed exactly once for each pair uv in P
b Argue that at the time of the call LCAu the number of sets in the forest of uptrees data structure is equal to the depth of u in T
c Prove that LCA correctly prints the least common ancestor for each pair uv in P
d Analyze the running time of LCA, assuming that we use the implementation with forest of up trees with balanced union and path compression.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
