/*! 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} Q19E As in the previous problem, you ... [FREE SOLUTION] | 91影视

91影视

As in the previous problem, you are given a binary tree T=(V,E) with designated root node. In addition, there is an array x[.]with a value for each node in V Define a new array z[.]as follows: for each uV,

z[u]=the maximum of the x-values associated with u鈥檚 descendants.

Give a linear-time algorithm that calculates the entire z-array.

Short Answer

Expert verified

A linear-time algorithm is as follows.

Input: BinaryTree T=(v,E)

Output: z[.] for each uV

Initialize z{i} to 0,

Begin procedure DFS ( )

While traversal

if node pops out of the stack

update the z values of the node u at the top of the stack

z{u} =max (z{u},x{v})

return z{u}

Step by step solution

01

Explain Binary tree

Consider the binary tree T=(V,E) with the root node. A binary tree must have at most two children for every parent node.

02

Give a linear-time algorithm that calculates the entire z -array.

Consider the binary tree T=(V,E)with the designated root node. Array x{.} with a value for each each node in V is given.

Create new array z{.} for each uV,

z{u}= the maximum of the x -values associated with u鈥檚 descendants.

Considering the given information, a linear-time algorithm is as follows.

Input: BinaryTree T=(V,E)

Output: z{.} for each uV

Initialize z{i} to 0,

Begin procedure DFS()

While traversal

if node pops out of the stack

update the z value of the node u at the top of the stack

z{u}=max(z{u},x{v}

return z{u}

The above algorithm initializes the z array, and performs the depth first search. During traversal , if the node pops out of the stack , update the z value of the node u. This algorithm runs in linear time.

Therefore, a linear-time algorithm that calculates the entire z-array has been provided.

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

Two paths in a graph are called edge-disjointif they have no edges in common. Show that in any undirected graph, it is possible to pair up the vertices of odd degree and find paths between each such pair so that all these paths are edge-disjoint.

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.

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

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

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.