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

Section 4.5.2 describes a way of storing a complete binary tree of n nodes in an array indexed by 1, 2, K, n .

(a) Consider the node at position j of the array. Show that its parent is at position [j2]and its children are at 2 jand 2 j + 1 (if these numbers are n).

(b) What the corresponding indices when a complete d-ary tree is stored in an array?

Figure 4.16 shows pseudocode for a binary heap, modeled on an exposition by R.E. Tarjan. The heap is stored as an array , which is assumed to support two constant-time operations:

  • |h|, which returns the number of elements currently in the array;
  • h-1, which returns the position of an element within the array.

The latter can always be achieved by maintaining the values of h-1as an auxiliary array.

(c) Show that themakeheapprocedure takesO(n) time when called on a set of elements. What is the worst-case input? (Hint:Start by showing that the running time is at most 1=1nlog(ni)).

(d) What needs to be changed to adapt this pseudocode to d-ary heaps?

Short Answer

Expert verified

(a)It can be shown that the parent position is j2and its children are at2 jand 2j + 1.

(b) The corresponding indices are j = (dp + 2 - d, dp + 1 - d, dp - d, K, dp, dp + 1 ).

(c) Yes, the makeheap takes the given time and the worst case input is reverse order array.

(d) The bubbleup and the minimum child processes can be changed slightly to adapt the pseudocode to d-ary heaps.

Step by step solution

01

Explain binary tree

A tree with at most two children in every node is called as binary tree. A complete binary tree will have two child node for all the nodes on the same level.

02

Step 2:Show the position of the parent and its children

(a)

Consider that the node j is at the mthposition of the Kthlevel of the tree, that is j=2k=1-1+m. At parent node, m2=m+12.

Then the index p is ,

2k=2-1+m+12=2k=2+m-12

So, the position of the parent node and its children node are,

p=j2j=2p,2p+1 ……(1)

Therefore, Equation (1) shows the parent and its children’s position.

03

Calculate the corresponding indices.

(b)

Consider that the j is located at the mthposition of the kthlevel of the tree. Then position of the j and its parents node index p can be defined as follows.

role="math" localid="1659079911975" j=dk-1-1d-1+mp=dk-2-1d-1+d-1+md

Then, it can be simplified as,

j+d+2=dk-1-1d-1+d-1-m

Then the corresponding indices are as follows,

p=j+d-2dj=(dp+2-d,dp+1-d,dp-d,K,dp,dp+1).

Therefore, the corresponding indices when a complete d-ary is stored in an array has been calculated.

04

Show that the given runtime is achieved and calculate the worst-case input.

(c)

Consider the Figure 4.16 in the text book, and the two constant time operations.

Consider the vertex with the index i, and the height of the tree is logi. The distance from the base of the tree is defined as follows,

h-logi=logn-logi=logni

Let, T(n) be the time complexity of building heap can be defined as,

i-1nlogni=logi-1nni,

Let,

F(n)=i-1nni,F(n+1)=F(n)n+1nn

For, F(n)cn,where c is a constant, and substitute it into the recursive formula.

cnn+1nncn+1,because limnn+1nn=e, where ce.

Then, it is concluded that F(n)=O(en).

Therefore, the time complexity is,

T(n)=logF(n)=O(log(en))=O(n)

Therefore, the worst case input is reverse order array.

05

What needs to be changed to adapt this pseudocode to d-ary heaps.

(d)

Considering the index relationship of the parent and child nodes, the bubbleup and the minimum child processes can be changed slightly to adapt the pseudocode to d-ary heaps.

Therefore, the bubbleup and the minimum child processes can be changed slightly to adapt the pseudocode to d-ary heaps.

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

Suppose Dijkstra’s algorithm is run on the following graph, starting at node A.

a) Draw a table showing the intermediate distance values of all the nodes at each iteration of the algorithm.

b) Show the final shortest-path tree.

Here's a proposal for how to find the length of the shortest cycle in an undirected graph with unit edge lengths. When a back edge, say (v,w), is encountered during a depth-first search, it forms a cycle with the tree edges from wtov. The length of the cyclelevel[v]-level[w+1] is where the level of a vertex is its distance in the DFS tree from the root vertex. This suggests the following algorithm:

• Do a depth-first search, keeping track of the level of each vertex.

• Each time a back edge is encountered, compute the cycle length and save it if it is smaller than the shortest one previously seen. Show that this strategy does not always work by providing a counterexample as well as a brief (one or two sentence) explanation.

There is a network of roads G=(V,E) connecting a set of cities . Each road in E has an associated length Ie. There is a proposal to add one new road to this network, and there is a list E' of pairs of cities between which the new road can be built. Each such potential road localid="1659075853079" e'E' has an associated length. As a designer for the public works department you are asked to determine the road localid="1659075866764" e'E'whose addition to the existing network G would result in the maximum decrease in the driving distance between two fixed cities s and t in the network. Give an efficient algorithm for solving this problem.

Give an O|V|2algorithm for the following task.

Input:An undirected graph G=(V,E); edge lengths Ie>0;an edge eE.

Output:The length of the shortest cycle containing edge e

Just like the previous problem, but this time with the Bellman-Ford algorithm.

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