Você está na página 1de 3

Computer Science CSC263H February 3, 2011

St. George Campus University of Toronto

Solutions for Homework Assignment #1

Answer to Question 1. (20 marks)


a. (6 marks) If the input array is 1, 2, 3 then Build-Max-Heap transforms it to 3, 2, 1 while Build-by-
Inserts transforms it to 3, 1, 2.
b. (14 marks) Let T (n) denote the worst-case running time of Build-by-Inserts on an input array A
of size n. We must show that T (n) is Θ(n log2 n). To do so, we must show that T (n) is both O(n log2 n)
and Ω(n log2 n).

• T (n) is O(n log2 n). To show this upper bound on T (n), note that on every input array A of size
n the loop of Build-by-Inserts is executed exactly n − 1 times. Each time this loop is executed,
it inserts an element into a heap that has at most n elements. Thus, there is a constant c such
that every insertion takes at most c log2 n time, and so the n − 1 iterations of the loop take at most
cn log2 n time. We conclude that T (n) is O(n log2 n).

• T (n) is Ω(n log2 n). To show this lower bound on T (n), consider the execution of Build-by-Inserts
on an input array A of n elements such that the n elements of A appear in increasing order. In this
execution, for each j:

1. The jth iteration of the loop inserts an element to a heap that already contains j elements.
2. The jth iteration of the loop inserts an element that is larger than any other presently in the
heap, and so this element must “percolate up” from the bottom level to the root of the heap.

So there is a constant c such that forPevery j, the jth iteration takes at least cblog2 jc time. Thus,
n−1
the n − 1 iterations take at least c j=1 blog2 jc time. It turns out that, for all n ≥ 28 = 256,
Pn−1 1
j=1 blog2 jc ≥ 16 n log2 n (we include the proof at the end of this solution set). We conclude that,
for every n ≥ 256, there is an input array A of length n for which BuildNewHeap takes at least
c
16 n log2 n time. Thus, T (n) is Ω(n log2 n).

Answer to Question 2. (20 marks)


a. (8 marks) A binomial heap H with n vertices consists of α(n) trees. Let Ti , 1 ≤ i ≤ α(n), denote
the trees of H. A tree Ti with ni vertices has ni − 1 edges. So the total number of edges in H is
Pi=α(n) Pi=α(n)
i=1 (ni − 1) = ( i=1 ni ) − α(n) = n − α(n)
b. (12 marks) Binomial heap H has n nodes before the insertions. By Part (a), it has n − α(n) edges
before the insertions. After k consecutive insertions, H has n + k nodes, hence it now has (n + k) − α(n + k)
edges. So the number of new edges created during the k consecutive insertions is:
[(n + k) − α(n + k)] − [n − α(n)] = k + α(n) − α(n + k) ≤ k + α(n) edges.
As we explained in class, the number of pairwise comparisons between the elements of H needed to
execute k consecutive insertions is equal to the number of new edges created during these insertions (each
new edge is the result of a pairwise comparison, and each pairwise comparison creates a new edge in H).
So k consecutive insertions require at most k + α(n) comparisons. By definition α(n) is the number of 1’s
in the binary representation of n, therefore, α(n) ≤ blog2 nc + 1. So k consecutive insertions require at
most k + blog2 nc + 1 comparisons. Note that if k > log2 n, k is the dominant factor in k + blog2 nc + 1.
So, when k > log2 n, k consecutive insertions require just O(k) pairwise comparisons (a constant number
of comparisons per insertion on the average).

1
Answer to Question 3. (20 marks)
a. (12 marks) Describe your algorithm in clear and concise English.

First use the Build-Min-Heap(A) procedure to transform array A into a min-heap.


Then successively execute Heap-Extract-Min(A) m times.
This outputs the sequence of the m smallest integers of A, in increasing order.
b. (4 marks) Explain why the worst-case time complexity of your algorithm is O(n).

The worst-case time complexity of Build-Min-Heap(A) is O(n) (CLRS section 6.3).


The worst-case time complexity of executing Heap-Extract-Min(A) m times is O(m log2 n).
So the worst-case time complexity of the algorithm, denoted T (n), is O(n + m log2 n).
√ √ √
We have n + m log2 n ≤ n + n log2 n ≤ n + n n = 2n. Thus, T (n) is O(n).
c. (4 marks) Is the worst-case time complexity of your algorithm Θ(n)? Justify your answer.

Since Build-Min-Heap(A) looks at every element of A,


the worst-case time complexity of Build-Min-Heap(A) is Ω(n).
Thus, T (n) is Ω(n).
By part (b) above, T (n) is also O(n).
Thus, T (n) is Θ(n).

2
Let S(n) = n−1 1
P
j=1 blog2 jc. In Question 1 (b), we claimed that for all n ≥ 256, S(n) ≥ 16 n log2 n. We now
prove this result.
We have: S(n) ≥ n−1
P Pn−1 Pn
j=1 (log2 j − 1) ≥ ( j=1 log2 j) − n = ( j=1 log2 j) − log2 n − n.

So: S(n) ≥ L(n) − log2 n − n where L(n) = nj=1 log2 j.


P

L(n) = log2 1 + log2 2 + . . . + log2 n [by definition]


≥ log2 dn/2e + log2 (dn/2e + 1) + . . . + log2 (dn/2e + bn/2c) [dropping the first dn/2e − 1
terms from the previous sum]
≥ log2 dn/2e + log2 dn/2e + . . . + log2 dn/2e [by monotonicity of log]
| {z }
bn/2c + 1 times

= (bn/2c + 1) · log2 dn/2e


1 n
≥ n log2 [because bn/2c + 1 ≥ n/2
2 2
and by monotonicity of log]
1
= n (log2 n − log2 2) [because for all b, x and y,
2
logb (x/y) = logb x − logb y]
1 
= n log2 n − 1
2
1 1
= n log2 n − n
2 2
1
≥ n log2 n [for all n ≥ 4, because for
4 n n
all such n, ≤ log2 n]
2 4
1
Thus, for n ≥ 4, we have S(n) ≥ 4 n log2 n − log2 n − n.
It remains to show that for n > 28 , 41 n log2 n − log2 n − n ≥ 161
n log2 n. This holds because for n > 28 ,
1 1 3 3 3
4 n log2 n − 16 n log2 n = 16 n log2 n ≥ 16 8n = 2 n ≥ n + log2 n.

Você também pode gostar