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

You are given a strongly connected directed graph G=(V,E) with positive edge weights along with a particularv0V . Give an efficient algorithm for finding shortest paths between all pairs of nodes, with the one restriction that these paths must all pass throughv0 .

Squares.Design and analyse an algorithm that takes as input an undirected graph G(V,E) and determines whether graph contains a simple cycle (that is, a cycle which doesn’t intersect itself) of length four. Its running time should be at mostO(V3) time.

You may assume that the input graph is represented either as an adjacency matrix or with adjacency lists, whichever makes your algorithm simpler.

Question: Often there are multiple shortest paths between two nodes of a graph. Give a linear-time algorithm for the following task.

Input: Undirected graph G = (V , E )with unit edge lengths; nodesu,vV

Output: The number of distinct shortest paths from utov.

Suppose we want to run Dijkstra’s algorithm on a graph whose edge weights are integers in the range 0,1,........,W, where Wis a relatively small number.
(a) Show how Dijkstra’s algorithm can be made to run in time

O(W|V|+|E|)

(b) Show an alternative implementation that takes time just .

O((|V|+|E|)logW)

You are given a directed graph G(V,E)with (possibly negative) weighted edges, along with a specific node sVand a tree T=(V,E'),E'E. Give an algorithm that checks whether T is a shortest-path tree for G with starting point s . Your algorithm should run in linear time.

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