Assignment 2 - Technische Universiteit Eindhoven

Transcription

Assignment 2 - Technische Universiteit Eindhoven
technische universiteit eindhoven
2IL50 Data Structures
Assignment 2
Spring 2015
Due at 23:59 on 22–02–2015
Requirements: Assignments have to be handed in by each student separately. Please write
your name and student number on the top of the first sheet that you hand in. Remember
that assignments have to be typeset in English and sent by email as .pdf to your instructor
on the due date. Always justify your answers!
Note: Whenever you are asked to describe an algorithm, you should present three things:
the algorithm, a proof of its correctness, and a derivation of its running time.
Exercise 1:
(a) [1 points] Prove that the solution of T (n) = T (dn/3e) + T (b2n/3c) + n with T (1) = 1
is T (n) = O(n log n).
√
√
(b) [1 points] Prove that the solution of T (n) = nT (b nc) + n with T (1) = T (2) = T (3) = 0
is T (n) = O(n log log n).
(c) [2 points] Can the master method be applied to T (n) = 4T (n/2) + n2 log n? Why or
why not? Give a tight asymptotic upper bound for this recurrence.
Note: You might want to study the material in Chapter 4.3 of the textbook. Using the
substitution method is a good option for this question.
Exercise 2: [3 points] Give asymptotic upper and lower bounds for T (n) in each of the
following recurrences. Assume that T (n) is constant for n ≤ 2. Make your bounds as tight
as possible, and justify your answers.
(a) T (n) = 7T (n/2) + n2
(b) T (n) = 7T (n/3) + n2
√
(c) T (n) = 2T (n/4) + n
Exercise 3:
(a) [2 points] Illustrate the execution of HeapSort on the following input:
(3, 11, 22, 37, 16, 24, 13) .
Show every step that changes the array. (One call of Max-Heapify counts as one step.)
(b) [1 points] Where in a max-heap might the smallest element reside, assuming that all
elements are distinct?
(c) [1 points] Is the sequence h30, 14, 25, 10, 1, 19, 22, 2, 4, 3i a max-heap?
(d) [2 points] Prove by induction that a heap with n vertices has exactly dn/2e leaves.
(e) [3 points] Show that there are at most dn/2h+1 e nodes of height h in any n-element
heap. (Note: if you assume that n is a power of two minus one – that is, n = 2k − 1 for
a positive integer k – you will receive only partial points.)
/department of mathematics and computer science
1
2IL50 Data Structures
technische universiteit eindhoven
Exercise 4: [4 points] Consider the following task scheduling problem: You are given n tasks
Ti , 1 ≤ i ≤ n. Each task has an (integer) arrival time ai , an (integer) priority pi , and an
(integer) length li . All arrival times and priorities are distinct. The processor schedules only
one task at a time. Whenever it schedules a new task, it schedules the task with the highest
priority among the tasks which have already arrived. Tasks cannot be interrupted once they
are running.
Describe an O(n log n) algorithm which computes at which time the processor has processed all tasks. Don’t forget to analyze the running time and prove the correctness of your
algorithm.
2
/department of mathematics and computer science