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

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’s 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’s 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 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

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?

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.

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 “metagraph” (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

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.

On page 102, we defined the binary relation “connected” on the set of vertices of a directedgraph. Show that this is an equivalence relation(see Exercise 3.29), and conclude that it partitions the vertices into disjoint strongly connected components.

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