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 3: 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 reachable[n]// dimension = #(vertices)= n
int start_time[n], finish_time[n]
int t =0
Procedure TimeDFS(vertex u):
set reachable[u]= true
set start_time[u]= t
t = t+1
for (j in neighbors(u)):
if (reachable[j]== false):
TimeDFS(j)
end if
end for
finish_time[u]= t
t = t+1
end Procedure
```
(a)[4] Take a complete binary tree of depth 3(so it has 7 vertices), draw the tree and write down the start_time and finish_time values for each of the vertices.
(b)[6] 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 pre-process the tree so as to answer "ancestry" queries in \( O(1)\) 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.]
Question 3 : Fast Ancestry It turns out that DFS

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!