You are given a tree T=(V,E) (in adjacency list format), along with a designated root node rV. Recall that u is said to be an ancestor of v in the rooted tree if the path from r to v in T passes through u.

You wish to reprocess the tree so that queries of the form “is u an ancestor v?” can be answered in constant time. The pre-processing itself should take linear time. How can this be done?

Short Answer

Expert verified

To reprocess the tree to fit the query “is u an ancestor v ”, by performing the Depth first search with pre and post numbering the tree T=(V,E).

Step by step solution

01

Explain the information given.

Consider the tree T=(V,E), in adjacency list format, along with a designated root node r . The node u is said to be an ancestor of v in the rooted tree if the path from r to v in T passes through .

02

Step 2: Explain how the reprocess is done and in linear time

Consider the tree T=V,E, in adjacency list format, along with a designated root node r. Perform a depth-first search on T=V,E, mark the root node as 1. Label the node L, if the left child is labelled L0and its right child is labelled L1.

Consider any two nodes u,v , If the node u is marked as the L0and the other node v that is labelled as L0. Then the u is the ancestor of v.

The search and the labelling can be done in constant time.

Therefore, the reprocess to fit the tree into the query “is u an ancestor v”, is done by performing the Depth first search with pre and post numbering and it has been done in constant time.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

You are given tree T=(V,E) along with a designated root node rV. The parent of any node Vr, denoted p(V), is defined to be the node adjacent to v in the path from r to v . By convention, p(r)=r. For k>1, define pk(v)pk-1(pv)andp1(v)=p(v)(so pk(v)is the k th ancestor of v ). Each vertex v of the tree has an associated non-negative integer label l(v). Given a linear-time algorithm to update the labels of all the vertices T according to the following rule: lnew(v)=l(plvv).

Infinite paths.Let G=(V,E) be a directed graph with a designated “start vertex” sV,asetVGV, a set of “good” vertices, and a set VBV of “bad” vertices. An infinite trace of is an infinite sequence of vertices viV such that (1)v0=s, and (2) for all i0, (vi,vi+1)E. That is, p is an infinite path in G starting at vertex s. Since the setV of vertices is finite, every infinite trace of Gmust visit some vertices infinitely often.

  1. If p is an infinite trace, let Inf(p)V be the set of vertices that occur infinitely often in p. Show that Inf(p) is a subset of a strongly connected component of G.
  2. Describe an algorithm that determines if role="math" G has an infinite trace.
  3. Describe an algorithm that determines if G has an infinite trace that visits some good vertex in VG infinitely often.
  4. Describe an algorithm that determines if role="math" localid="1659627728759" G has an infinite trace that visits some good vertex in VG infinitely often, but visits no bad vertex in VB infinitely often.

The police department in the city of Computopia has made all streets one-way. The mayor contends that there is still a way to drive legally from any intersection in the city to any other intersection, but the opposition is not convinced. A computer program is needed to determine whether the mayor is right. However, the city elections are coming up soon, and there is just enough time to run a linear-time algorithm.

a) Formulate this problem graph-theoretically, and explain why it can indeed be solved in linear time.

(b) Suppose it now turns out that the mayor’s original claim is false. She next claims something weaker: if you start driving from town hall, navigating one-way streets, then no matter where you reach, there is always a way to drive legally back to the town hall. Formulate this weaker property as a graph-theoretic problem, and carefully show how it too can be checked in linear time.

Give an efficient algorithm that takes as input a directed acyclic graph G=V,E, and two vertices s,tV, and outputs the number of different directed paths from S to t in G.

Give a linear-time algorithm to find an odd-length cycle in a directed graph. (Hint: First solve this problem under the assumption that the graph is strongly connected.)

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free