Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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).

Short Answer

Expert verified

Depth-first search algorithm can be used to update the labels of all vertices T according to the rule lnewv=lplvv.

Step by step solution

01

Explain Depth-first search

Depth-first search is the linear time algorithm that gives information about a graph. A graph is considered in the form of an adjacency list. Neighbors of the vertices can be found by exploring the vertices and marking the visited vertices.

02

Give a linear-time algorithm to update the labels of all the vertices T  

Consider the binary tree T =(v,E) with the designated root node rV. The parent of any node vr, denoted p (v), is the node adjacent to v in the path from r to v . By convention p (r)=r .

For k>1 ,define pkv=pk-1pvand p1v=pv. Each vertex v of the tree has an associated non-negative integer label lv.

Consider the following algorithm,that updates the labels of all vertices Taccording to the rule lnewv=lplvv.

Input: TV,E

Output: Inewv

Procedure:

perform DFS onTV,E

Declare DFS on stack

During traversal

if node v is pushed onto the stack

update Inewv=1stack.atmaxstack,size-1v,1

at stack.atI

take out the vertex at ith position

set bottom element to be in the first position

return Inewv

The above algorithm considers the stack to perform DFS. During traversal if node is pushed onto the stack, then the label of the vertex at stack is updated. Then the vertex is moved to the bottom and the bottom element is moved to the first position.

Therefore, Depth-first search algorithm has been used to update the labels of all vertices T according to the rule Inewv=IpIvv.

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

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.

Question:Undirected vs. directed connectivity.

(a) Prove that in any connected undirected graph G =(V , E)there is a vertexvV whose removal leaves G connected. (Hint: Consider the DFS search tree for G.)

(b) Give an example of a strongly connected directed graph G(V ,E)such that, for everyvV, removing v from G leaves a directed graph that is not strongly connected.

(c) In an undirected graph with two connected components it is always possible to make the graph connected by adding only one edge. Give an example of a directed graph with two strongly connected components 0 such that no addition of one edge can make the graph strongly connected.

Pouring water.

We have three containers whose sizes are 10 pints, 7 pints, and 4 pints, respectively. The 7-pint and 4-pint containers start out full of water, but the 10-pint container is initially empty. We are allowed one type of operation: pouring the contents of one container into another, stopping only when the source container is empty or the destination container is full. We want to know if there is a sequence of pouring’s that leaves exactly 2 pints in the 7- or 4-pint container.

(a) Model this as a graph problem: give a precise definition of the graph involved and state the specific question about this graph that needs to be answered.

(b) What algorithm should be applied to solve the problem?

(c) Find the answer by applying the algorithm.

Give an efficient algorithm which takes as input a directed graph G(V,E)and determines whether or not there is a vertexsV from which all other vertices are reachable.

Rewrite the explore procedure (Figure 3.3) so that it is non-recursive (that is, explicitly use a stack). The calls to pre visit and post visit should be positioned so that they have the same effect as in the recursive procedure.

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