/*! 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

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.

Let S be a finite set. A binary relation on S is simply a collection R of ordered pairs(x,y)SS. . For instance, S might be a set of people, and each such pair (x,y)R might mean 鈥 x knows y 鈥.

An equivalence relationis a binary relation which satisfies three properties:

  • Reflexivity: localid="1659006645990" (x,y)R for all XS
  • Symmetry: If (x,y)R then (y,x)R
  • Transitivity: if (x,y)R and (y,z)R then localid="1659006784500" (x,Z)R

For instance, the binary relation 鈥渉as the same birthday as鈥 is an equivalence relation, whereas 鈥渋s the father of鈥 is not, since it violates all three properties.

Show that an equivalence relation partition set S into disjoint groups S1,S2,,Sk (in other words, S=S1S2SkandSiSj=forallij ) such that:

  • Any two members of a group are related, that is, (x,y)R for any localid="1659006702579" (x,y)Si, for any i .
  • Members of different groups are not related, that is, for all ij, for all localid="1659006762355" xSi andySi, we have (x,Z)R.

(Hint: Represent an equivalence relation by an undirected graph.)

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?

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

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.

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.