Michelle Bodnar, Andrew Lohr May 22, 2017: H h+1 H 1 I 0 I H
Michelle Bodnar, Andrew Lohr May 22, 2017: H h+1 H 1 I 0 I H
Michelle Bodnar, Andrew Lohr May 22, 2017: H h+1 H 1 I 0 I H
Exercise 6.1-1
Exercise 6.1-2
Exercise 6.1-3
If there largest element in the subtee were somewhere other than the root,
it has a parent that is in the subtree. So, it is larger than it’s parent, so, the
heap property is violated at the parent of the maximum element in the subtree
Exercise 6.1-4
The smallest element must be a a leaf node. Suppose that node x contains
the smallest element and x is not a leaf. Let y denote a child node of x. By
the max-heap property, the value of x is greater than or equal to the value of
y. Since the elements of the heap are distinct, the inequality is strict. This
contradicts the assumption that x contains the smallest element in the heap.
Exercise 6.1-5
Yes, it is. The index of a child is always greater than the index of the parent,
so the heap property is satisfied at each vertex.
1
Exercise 6.1-6
Exercise 6.1-7
It suffices to show that the elements with no children are exactly indexed
by {bn/2c + 1, . . . , n}. Suppose that we had an i in this range. It’s childeren
would be located at 2i and 2i + 1 but both of these are ≥ 2bn/2c + 2 > n and so
are not in the array. Now, suppose we had an element with no kids, this means
that 2i and 2i + 1 are both > n, however, this means that i > n/2. This means
that i ∈ {bn/2c + 1, . . . , n}.
Exercise 6.2-1
27 17 3 16 13 10 1 5 7 12 4 8 9 0
27 17 10 16 13 3 1 5 7 12 4 8 9 0
27 17 10 16 13 9 1 5 7 12 4 8 3 0
Exercise 6.2-2
Algorithm 1 MIN-HEAPIFY(A,i)
1: l = LEF T (i)
2: r = RIGHT (i)
3: if l ≤ A.heap − size and A[l] < A[i] then
4: smallest = l
5: else smallest = i
6: end if
7: if r ≤ A.heap − size and A[r] < A[smallest] then
8: smallest = r
9: end if
10: if smallest 6= i then
11: exchange A[i] with A[smallest]
12: MIN-HEAPIFY(A, smallest)
13: end if
Exercise 6.2-3
The array remains unchanged since the if statement on line line 8 will be
false.
2
Exercise 6.2-4
If i > A.heap − size/2 then l and r will both exceed A.heap − size so the
if statement conditions on lines 3 and 6 of the algorithm will never be satisfied.
Therefore largest = i so the recursive call will never be made and nothing will
happen. This makes sense because i necessarily corresponds to a leaf node, so
MAX–HEAPIFY shouldn’t alter the heap.
Exercise 6.2-5
Iterative Max Heapify(A, i)
Exercise 6.2-6
Consider the heap resulting from A where A[1] = 1 and A[i] = 2 for
2 ≤ i ≤ n. Since 1 is the smallest element of the heap, it must be swapped
through each level of the heap until it is a leaf node. Since the heap has height
blg nc, MAX-HEAPIFY has worst-case time Ω(lg n).
Exercise 6.3-1
5 3 17 10 84 19 6 22 9
5 3 17 22 84 19 6 10 9
5 3 19 22 84 17 6 10 9
5 84 19 22 3 17 6 10 9
84 5 19 22 3 17 6 10 9
84 22 19 5 3 17 6 10 9
84 22 19 10 3 17 6 5 9
3
Exercise 6.3-2
Exercise 6.3-3
All the nodes of height h partition the set of leaves into sets of size between
2h−1 + 1 and 2h , where all but one is size 2h . Just by putting all the children
of each in their own part of trhe partition. Recall from 6.1-2 that the heap has
height blg(n)c, so, by looking at the one element of this height (the root), we
get that there are at most 2blg(n)c leaves. Since each of the vertices of height h
partitions this into parts of size at least 2h−1 + 1, and all but one corresponds
to a part of size 2h , we can let k denote the quantity we wish to bound, so,
so
n + 2h n l n m
k≤ ≤ ≤
2h+1 + 2h + 1 2h+1 2h+1
Exercise 6.4-1
4
5 13 2 25 7 17 20 8 4
5 13 20 25 7 17 2 8 4
5 25 20 13 7 17 2 8 4
25 5 20 13 7 17 2 8 4
25 13 20 5 7 17 2 8 4
25 13 20 8 7 17 2 5 4
4 13 20 8 7 17 2 5 25
20 13 4 8 7 17 2 5 25
20 13 17 8 7 4 2 5 25
5 13 17 8 7 4 2 20 25
17 13 5 8 7 4 2 20 25
2 13 5 8 7 4 17 20 25
13 2 5 8 7 4 17 20 25
13 8 5 2 7 4 17 20 25
4 8 5 2 7 13 17 20 25
8 4 5 2 7 13 17 20 25
8 7 5 2 4 13 17 20 25
4 7 5 2 8 13 17 20 25
7 4 5 2 8 13 17 20 25
2 4 5 7 8 13 17 20 25
5 4 2 7 8 13 17 20 25
2 4 5 7 8 13 17 20 25
4 2 5 7 8 13 17 20 25
2 4 5 7 8 13 17 20 25
Exercise 6.4-2
Suppose that at the start of the ith iteration of of the for loop of lines 2-5,
the subarray A[1..i] is a max-heap containing the i smallest elements of A[1..n]
and the subarray A[i + 1..n] contains the n − i largest elements of A[1..n] in
sorted order. Since A[1..i] is a max-heap, A[1] is the largest element in A[1..i].
Thus it is the (n − (i − 1))th largest element from the original array since the
n − i largest elements are assumed to be at the end of the array. Line 3 swaps
A[1] with A[i], so A[i..n] contain the n − i + 1 largest elements of the array,
and A[1..i − i] contains the i − 1 smallest elements. Finally, MAX-HEAPIFY
is called on A, 1. Since A[1..i] was a max-heap prior to the iteration and only
the elements in positions 1 and i were swapped, the left and right subtrees of
node 1, up to node i − 1, will be max-heaps. The call to MAX-HEAPIFY will
place the element now located at node 1 into the correct position and restore the
5
max-heap property so that A[1..i − 1] is a max-heap. This concludes the next
iteration, and we have verified each part of the loop invariant. By induction,
the loop invariant holds for all iterations.
After the final iteration, the loop invariant says that the subarray A[2..n]
contains the n − 1 largest elements of A[1..n], sorted. Since A[1] must be the
nth largest element, the whole array must be sorted as desired.
Exercise 6.4-3
There will be n iterations of the for loop in HEAPSORT, each taking Θ(lg(n))
time because the element that was at position i was the smallest and so will have
blg(n)c steps when doing MAX-HEAPIFY on line 5. So, it will be Θ(n lg(n))
time.
If it’s already sorted in decreasing order, then the call on line one will only
take Θ(n) time, since it was already a max heap to begin with. This is because
elements in earlier positions correspond to begin further up in the heap, and
since the array is descending, those have larger values. So, each of the MAX-
HEAPIFY calls in the BUILD-MAX-HEAP procedure will only take constant
time since the check on line 8 of MAX-HEAPIFY will always be false. It will
still take n lg(n) peel off the elements from the heap and re-heapify. This is
because each time we swap the head of the heap with the last element, we now
have the smallest element at the head, so it will have to have as many recursive
calls of MAX-HEAPIFY as the depth of the heap, which is about lg(n).
Exercise 6.4-4
Exercise 6.4-5
6
Since the call on line one could possibly take only linear time (if the input
was already a max-heap for example), we will focus on showing that the for loop
takes n log n time. This is the case because each time that the last element is
placed at the beginning to replace the max element being removed, it has to
go through every layer, because it was already very small since it was at the
bottom level of the heap before.
Exercise 6.5-1
The following sequence of pictures shows how the max is extracted from the
heap.
1. Original heap:
15
13 9
5 12 8 7
4 0 6 2 1
13 9
5 12 8 7
4 0 6 2
7
13
1 9
5 12 8 7
4 0 6 2
13
12 9
5 1 8 7
4 0 6 2
13
12 9
5 6 8 7
4 0 1 2
Exercise 6.5-2
The following sequence of pictures shows how 10 is inserted into the heap,
8
then swapped with parent nodes until the max-heap property is restored. The
node containing the new key is heavily shaded.
1. Original heap:
15
13 9
5 12 8 7
4 0 6 2 1
13 9
5 12 8 7
4 0 6 2 1 −∞
15
13 9
5 12 8 7
4 0 6 2 1 10
9
4. Since the parent key is smaller than 10, the nodes are swapped:
15
13 9
5 12 10 7
4 0 6 2 1 8
5. Since the parent node is smaller than 10, the nodes are swapped:
15
13 10
5 12 9 7
4 0 6 2 1 8
Exercise 6.5-3
Heap-Minimum(A)
1: return A[1]
Heap-Extract-Min(A)
Heap-decrease-key(A,i,key)
Min-Heap-Insert(A,key)
Exercise 6.5-4
10
1: if A.heap-size < 1 then
2: Error “heap underflow”
3: end if
4: min = A[1]
5: A[1] = A[A.heap − size]
6: A.heap − size − −
7: Min-heapify(A,1)
8: return min
occur. However, we could have assigned any value less than or equal to key to
A[A.heap − size] and the algorithm would still work.
Exercise 6.5-5
Initially, we have a heap and then only change the value at i to make it
larger. This can’t invalidate the ordering between i and it’s children, the only
other thing that needs to be related to i is that i is less than it’s parent, which
may be false. Thus we have the invariant is true at initialization. Then, when
we swap i with its parent if it is larger, since it is larger than it’s parent, it must
also be larger than it’s sibling, also, since it’s parent was initially above its kids
in the heap, we know that it’s parent is larger than it’s kids. The only relation
in question is then the new i and it’s parent. At termination, i is the root, so it
has no parent, so the heap property must be satisfied everywhere.
Exercise 6.5-6
Replace A[i] by key in the while condition, and replace line 5 by “A[i] =
A[P AREN T (i)].” After the end of the while loop, add the line A[i] = key.
Since the key value doesn’t change, there’s no sense in assigning it until we
know where it belongs in the heap. Instead, we only make the assignment of
1: A.heap − size + +
2: A[A.heap − size] = ∞
3: Heap-Decrease-Key(A,A.heap-size,key)
11
the parent to the child node. At the end of the while loop, i is equal to the
position where key belongs since it is either the root, or the parent is at least
key, so we make the assignment.
Exercise 6.5-7
Have a field in the structure that is just a count of the total number of
elements ever added. When adding an element, use the current value of that
counter as the key.
Exercise 6.5-8
The algorithm works as follows: Replace the key of the node to be deleted
by ∞, ie a value which will be interpreted as greater than all other keys stored
in the max-heap. Calling HEAP-INCREASE-KEY will float that node to the
top of the max-heap. We then replace the value of the root node with the value
of the last element in the heap, known to be smaller than A[1] by the max-heap
property. We update the size of the heap, then call MAX-HEAPIFY to restore
the max-heap property. This has running time O(lg n) since INCREASE-KEY
runs in O(lg n) and the number of times MAX-HEAPIFY is recursively called
is as most the height of the heap, which is blg nc.
Algorithm 2 HEAP-DELETE(A,i)
1: HEAP-INCREASE-KEY(A, i, ∞)
2: A[1] = A[A.heap-size]
3: A.heap-size = A.heap-size - 1
4: MAX-HEAPIFY(A,1)
Exercise 6.5-9
Construct a min heap from the heads of each of the k lists. Then, to find
the next element in the sorted array, extract the minimum element (in O lg(k)
time). Then, add to the heap the next element from the shorter list from which
the extracted element originally came (also O(lg(k)) time). Since finding the
next element in the sorted list takes only at most O(lg(k)) time, to find the
whole list, you need O(n lg(k)) total steps.
Problem 6-1
12
Problem 6-2
a. It will suffice to show how to access parent and child nodes. In a d-ary array,
PARENT(i) = bi/dc, and CHILD(k, i) = di − d + 1 + k, where CHILD(k, i)
gives the k th child of the node indexed by i.
Algorithm 4 DMAX-HEAPIFY(A,i)
1: largest = i
2: for k = 1 to d do
3: if CHILD(k, i) ≤ A.heap − size and A[CHILD(k, i)] > A[i] then
4: if A[CHILD(k, i)] > largest then
5: largest = A[CHILD(k, i)]
6: end if
7: end if
8: end for
9: if largest 6= i then
10: exchange A[i] with A[largest]
11: DMAX-HEAPIFY(A, largest)
12: end if
13
when we call PARENT, we mean it as defined in part (a).
Algorithm 5 INSERT(A,key)
1: A.heap − size = A.heap − size + 1
2: A[A.heap − size] = key
3: i = A.heap − size
4: while i > 1 and A[P AREN T (i) < A[i] do
5: exchange A[i] with A[P AREN T (i)]
6: i = P AREN T (i)
7: end while
Algorithm 6 INCREASE-KEY(A,i,key)
1: if key < A[i] then
2: error “new key is smaller than current key ”
3: end if
4: A[i] = key
5: while i > 1 and A[P AREN T (i) < A[i] do
6: exchange A[i] with A[P AREN T (i)]
7: i = P AREN T (i)
8: end while
Problem 6-3
2 3 4 5
8 9 12 14
a.
16 ∞ ∞ ∞
∞ ∞ ∞ ∞
b. For every i, j, Y [1, 1] ≤ Y [i, 1] ≤ Y [i, j]. So, if Y [1, 1] = ∞, we know that
Y [i, j] = ∞ for every i, j. This means that no elements exist. If Y is full, it
has no elements labeled ∞, in particular, the element Y [m, n] is not labeled
∞.
c. Extract-Min(Y,i,j), extracts the minimum value from the young tableau Y 0
obtained by Y 0 [i0 , j 0 ] = Y [i0 + i − 1, j 0 + j − 1]. Note that in running this
algorithm, several accesses may be made out of bounds for Y , define these
to return ∞. No store operations will be made on out of bounds locations.
Since the largest value of i + j that this can be called with is n + m, and
14
1: min = Y [i, j]
2: if Y [i, j + 1] = Y [i + 1, j] = ∞ then
3: Y [i, j] = ∞
4: return min
5: end if
6: if Y [i, j + 1] < Y [i + 1, j] then
7: Y [i, j] = Y [i, j + 1]
8: Y [i, j + 1] = min
9: return Extract-min(y,i,j+1)
10: else
11: Y [i, j] = Y [i + 1, j]
12: Y [i + 1, j] = min
13: return Extract-min(y,i+1,j)
14: end if
this quantity must increase by one for each call, we have that the runtime is
bounded by n + m.
1: i = m, j = n
2: Y [i, j] = key
3: while Y [i − 1, j] > Y [i, j] or Y [i, j − 1] > Y [i, j] do
4: if Y [i − 1, j] < Y [i, j − 1] then
5: Swap Y [i, j] and Y [i, j − 1]
6: j−−
7: else
8: Swap Y [i, j] and Y [i − 1, j]
9: i−−
10: end if
11: end while
bounded by 2 below, we know that this program has runtime O(n + m).
e. Place the n2 elements into a Young Tableau by calling the algorithm from
part d on each. Then, call the algorithm from part c n2 to obtain the numbers
in increasing order. Both of these operations take time at most 2n ∈ O(n),
and are done n2 times, so, the total runtime is O(n3 )
15
i=j=1
while Y [i, j] < key and i < m do
Check(Y,key,i,j)
i++
end while
while i > 1 and j < n do
Check(i,j)
if Y [i, j] < key then
j++
else
i–
end if
end while
return false
16