Introduction To Algorithms, Third Edition (international Edition)
Introduction To Algorithms, Third Edition (international Edition)
3rd Edition
ISBN: 9780262533058
Author: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
Publisher: TRILITERAL
bartleby

Concept explainers

Question
Book Icon
Chapter 6, Problem 1P

(a)

Program Plan Intro

To show the difference between the output of the array when it is passed as an array for BUILD-MAX-HEAP and BUILD-MAX-HEAP'.

(a)

Expert Solution
Check Mark

Explanation of Solution

Given Information: A heap that has n-elements.

Explanation:

Pseudo code for BUILD-MAX-HEAP is given below:

    BUILD-MAX-HEAP( A )
    A.heapsize = A.length
    for i = A.length/2 downto 1
    MAX-HEAPIFY( A,i )

Consider an array A= {1, 2, 3, 4, 5, 6}. If the above algorithm is used, A.heapsize =6 and i =6/2=3.

The for-loop will iterate from 3 to 1.

At i=3 , MAX-HEAPIFY will be called with arguments as array A and i=3 . The following pseudo code for MAX-HEAPIFY is as follows:

    MAX-HEAPIFY( A,i )
    l = LEFT( i )
    r = RIGHT( i )
    ifl <= A.heapsize and A[l]> A[i]
    largest = l
      Else
    largest = i
      ifr <= A.heapsize and A[r]> A[largest]
    largest=r
    iflargest != i
    exchange A [ i ] with A [ largest ]
    MAX-HEAPIFY( A,largest )

Currently at i=3 , A [3]=3 and have the following tree:

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  1

At the end of the iteration:

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  2

At i=2 , A [2]=2 and after executing the algorithm, the following tree is evolved:

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  3

At i=1 , A [1]=1 and after executing the algorithm, the following tree is evolved:

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  4

Resultant array after BUILD-MAX-HEAP is A= {6, 5, 3, 4, 2, 1}

However, according to the question, the algorithm for BUILD-MAX-HEAP ', there is an alteration in A.heapsize, which is by default 1. Moreover, the value of i starts from 2 and goes on till A.length that is 6.

At i=2 , A [2]=2. The tree is given below-

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  5

Look at the algorithm of BUILD-MAX-HEAP' . It is using MAX-HEAP-INSERT. So, it becomes-

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  6

At i=3 , A [3]=3 and after using MAX-HEAP-INSERT

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  7

At i=4 , A [4]=4 and after using MAX-HEAP-INSERT

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  8

At i=5 , A [5]=5 and after using MAX-HEAP-INSERT

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  9

Finally at i=6,A [6]=6 and after using MAX-HEAP-INSERT

Introduction To Algorithms, Third Edition (international Edition), Chapter 6, Problem 1P , additional homework tip  10

Resultant array after BUILD-MAX-HEAP' is A= {6, 4, 5, 1, 3, 2}

Hence, the resultant array after BUILD-MAX-HEAP and BUILD-MAX-HEAP' is not the same.

(b)

Program Plan Intro

To determine the worst case scenario for BUILD-MAX-HEAP' while entering n-elements.

(b)

Expert Solution
Check Mark

Explanation of Solution

The BUILD-MAX-HEAP' has MAX-HEAP-INSERT in it, which has worst case of Θ(log n). The call to this function is done for n-1 times, since it is not considering the parent node here.

The worst case for MAX-HEAP-INSERT happens when an array is sorted in ascending order.

Each insert step takes maximum Θ(log n ) steps, and since it is done for n times, it takes a worst case of Θ(nlog n ). Each iteration in the new block is actually taking more time as the older version as it has more iteration in it. The new block will work in even work in even worse case as it has the usage of other algorithm as well.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
True or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?
Can you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Fo
HistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole