You are on page 1of 3

Cover page for

CSC263 Homework #1
(fill and attach this page to your homework)

Submitted by:

(1) Family Name: (2) Family Name:

Given Name: Given Name:

Student Number: Student Number:

Graded Homework should be returned in Tutorial: (tutorial room number)

By virtue of submitting this homework I/we acknowledge that I am/we are aware of the policy on
homework collaboration for this course.

1
Computer Science CSC263H January 20, 2011
St. George Campus University of Toronto

Homework Assignment #1
Due: February 3, 2011, by 5:30 pm
(in the drop box for this course in Bahen 2220)

1. On the cover page of your assignment, in addition to your name(s), you must write the location of
the tutorial where you want the graded homework to be returned.

2. If you do not know the answer to a question, and you write “I (We) do not know the answer to this
question”, you will receive 20% of the marks of that question. If you just leave a question blank
with no such statement, you get 0 marks for that question.

3. For any question, you may use data structures from class without describing how the operations
on these data structures are implemented. You may also use any result (e.g., a worst-case time
complexity bound) that we covered in class, or is in the course textbook, by referring to it.

4. Unless we explicitly state otherwise, you should justify your answers. Your paper will be marked
based on the correctness and completeness of your answers, and the clarity, precision and conciseness
of your presentation.

Question 1. (20 marks) Let A be an array containing n integers. Section 6.3 of our textbook (CLRS)
describes a procedure, called Build-Max-Heap(A), that transforms array A into a max-heap in O(n)
time. That procedure works “bottom-up”, using Max-Heapify repeatedly.
Another way of transforming A into a max-heap is to insert the elements of A into the heap one at a
time. Specifically, the algorithm is as follows:

Build-by-Inserts(A)
A.heap-size := 1
for i := 2..n do
Max-Heap-Insert(A, A[i])

a. (6 marks) Give an example of an input array A for which the two procedures Build-Max-Heap and
Build-by-Inserts produce different outputs. Keep your example as small as possible.
b. (14 marks) Let T (n) be the worst-case time complexity of Build-by-Inserts for an input array A of
size n. Prove that T (n) is Θ(n log n). (Recall that the worst-case time complexity of Build-Max-Heap
is O(n), and therefore Build-Max-Heap is more efficient than Build-by-Inserts.)

Question 2. (20 marks) This question is about the worst-case cost of successively inserting k elements
into a binomial heap (alias binomial queue) of size n.
a. (8 marks) Prove that a binomial heap with n elements has exactly n − α(n) edges, where α(n) is the
number of 1’s in the binary representation of n.

2
b. (12 marks) Consider the worst-case total cost of successively inserting k new elements into a binomial
heap H of size |H| = n. In this question, we measure the worst-case cost of inserting a new element into H
as the maximum number of pairwise comparisons between elements of the binomial heap that is required
to do this insertion. In class we proved that for k = 1 (i.e., inserting one element) the worst-case cost is
O(log n). Show that if k > log n, then the worst-case total cost of successively inserting k elements into H
is only O(k). In other words, if k > log n then the average cost of an insertion (i.e., the worst-case total
cost divided by k) is bounded by a constant.
Hint: Note that the cost of each one of the k consecutive insertions varies — some can be expensive,
other are cheaper. Relate the cost of each insertion, i.e., the number of pairwise comparisons that it
requires, with the number of extra edges that it forms. Then use part (a).

Question 3. (20 marks)


In the following, A[1..n] denotes an array of n distinct integers in arbitrary order. Give a simple algorithm

that takes any A[1..n] and any integer m such that 1 ≤ m ≤ n, and outputs the sequence of the m
smallest integers of the array A, in increasing order. Your algorithm’s worst-case time complexity should
be O(n).
Example: for inputs A = [5, 7, 4, 12, 0, 15, 6, 3, 10] and m = 3, your algorithm should output 0, 3, 4.
Do not make assumptions on the size or range of the integers in the array A.
a. (12 marks) Describe your algorithm in clear and concise English. The simpler your algorithm is, the
more marks it gets.
b. (4 marks) Explain why the worst-case time complexity of your algorithm is O(n).
c. (4 marks) Is the worst-case time complexity of your algorithm Θ(n)? Justify your answer.

You might also like