/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q20E You are given tree T=(V,E) along... [FREE SOLUTION] | 91影视

91影视

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 91影视!

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

A bipartite graph is a graph G=(V,E)whose vertices can be partitioned into two sets (V=V1V2andV1V2=) such that there are no edges between vertices in the same set (for instance, if , then there is no edge between and ).

(a) Give a linear-time algorithm to determine whether an undirected graph is bipartite.

(b) There are many other ways to formulate this property. For instance, an undirected graph is bipartite if and only if it can be colored with just two colors. Prove the following formulation:

an undirected graph is bipartite if and only if it contains no cycles of odd length.

(c) At most how many colors are needed to color in an undirected graph with exactly one odd length?

Give a linear-time algorithm for the following task.
Input: A directed acyclic graph G

Does G contain a directed path that touches every vertex exactly once?

Run the strongly connected components algorithm on the following directed graphs G. When doing DFS on GR: whenever there is a choice of vertices to explore, always pick the one that is alphabetically first.

In each case answer the following questions.

(a) In what order are the strongly connected components (SCCs) found?

(b) Which are source SCCs and which are sink SCCs?

(c) Draw the 鈥渕etagraph鈥 (each meta-node is an SCC of G).

(d) What is the minimum number of edges you must add to this graph to make it strongly connected

Perform a depth-first search on the following graph; whenever there鈥檚 a choice of vertices, pick the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the pre and post number of each vertex.

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.

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.