Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
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, Chapter 6, Problem 1P , additional homework tip  1

At the end of the iteration:

Introduction to Algorithms, 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, 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, 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, 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, Chapter 6, Problem 1P , additional homework tip  6

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

Introduction to Algorithms, Chapter 6, Problem 1P , additional homework tip  7

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

Introduction to Algorithms, Chapter 6, Problem 1P , additional homework tip  8

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

Introduction to Algorithms, Chapter 6, Problem 1P , additional homework tip  9

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

Introduction to Algorithms, 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
A 4-ary max heap is like a binary max heap, but instead of 2 children, nodes have 4 children. A 4-ary heap can be represented by an array as shown in Figure (up to level 2). Write down MAX_HEAPIFY(A,i) function for a 4-ary max-heap that restores the heap property for ith node.
Suppose we have p max heaps, with q elements in each. We wish to combine these into a single heap. Each of the following sub-parts describes one approach merging the heaps, and you are asked to derive the big O running time. MUST show derivation   We create a new, empty heap H. From each of the input heaps, P, until P is empty we repeatedly remove the highest priority element using the heap delete method, and insert it into H using the heap insert method. What is the worst-case big O running time? Show derivation.  We create a new, empty linked list L. For each of the input heaps, P, we iterate over its internal array and insert each element at the front of L. We then transfer the elements of L into an array, and heapify the array using repeated sift downs. What is the worst-case running time? Show derivation.  We divide the p input heaps into p/2 pairs, and apply the algorithm from part (b) on each pair, resulting in p/2 heaps. We then repeat this process until we are left with a…
Suppose we want to use the Heapsort algorithm to sort a large list of numbers. Our first step is to convert the input list to a heap, and then run BUILD-MAX-HEAP, which applies MAX-HEAPIFY on all the nodes in the heap, starting at the bottom and moving towards the top. For example, if A = [4,1,3,2,16,9,10,14,8,7], then BUILD-MAX-HEAP(A) makes a total of 7 swaps, and returns the array [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]. The swaps are made in this order: (14, 2), (10, 3), (16, 1), (7, 1), (16, 4), (14, 4), (8, 4). Suppose A = [2,4,6,8,10,12,14,1,3,5,7,9,11,13,15].Determine the total number of swaps made by BUILD-MAX-HEAP(A).
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education