Question: Question 3 : Fast Ancestry It turns out that DFS is surprisingly powerful, especially if one keeps track of visit times for each vertex. Consider
Question : Fast Ancestry
It turns out that DFS is surprisingly powerful, especially if one keeps track of "visit times" for each vertex. Consider the following subroutine that uses a global variable t in addition to the reachable array, and two new arrays that maintain the start and finish times for each vertex:
boolean reachablen dimension #vertices n
int starttimen finishtimen
int t
Procedure TimeDFSvertex u:
set reachableu true
set starttimeu t
t t
for j in neighborsu:
if reachablej false:
TimeDFSj
end if
end for
finishtimeu t
t t
end Procedure
a Take a complete binary tree of depth so it has vertices draw the tree and write down the starttime and finishtime values for each of the vertices.
b Using the procedure above, solve the following problem from the textbook: given a tree with n vertices and root node r find a way to preprocess the tree so as to answer "ancestry" queries in O time. An ancestry query asks: is vertex u an ancestor of vertex v This should return true if u appears on the path from v to the root and false otherwise. As is standard, a tree is given as a set of vertices; for each vertex, we have the index of its parent, and a list of all its children. Of course, the parent of the root r is null.Important Give an informal explanation as to why your solution is correct.
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
