2007-04-10 Linköpings Tekniska Högskola Institutionen för Datavetenskap Jan Maluszynski TDDB56 & TTIT33 DALG part of the exam 2007-04-10 Outline of the proposed solutions for selected problems. 1. (a) The ordering is: √ n log n n3 , 5n3/2 , nlog k = k log n , nk , k n The explanations are omitted, for similar example see TDDB56 homepage Lektioner 2006 Le1 Problem 2 (solutions). Note that k is a constant greater or equal 4, so log k > 3/2 √ The functions n3 (= n3/2 ) and 5n3/2 differ by a constant factor and have thus equal growth rate. The functions nlog k and k log n are equal for every n > 0, thus have the same growth rate. (2 p) (b) See TDDB56 homepage Lecture 2 Slide 10. Proper analysis of a recursive algorithm requires formulation and solution of a reccurrence equation. (2 p) 2. (4 p) (a) Stack (2) • ACEDB : yes Output(A), Push(B), Output(C), Push(D), Push(E), Output(Pop), Output(Pop), Output(Pop) • BCEAD : no. To get BCE first we have to push A and D. But then D must be popped before A so we cannot get AD. (b) (2) • ACEDB: no, if every element is enqueued at most once. To get ACE we have to enqueue B and D. But then B can only be dequeued before D, so we cannot obtain DB. However, if we allow an enqueued element to be dequeued and placed again in the queue we can obtain the sequence ACEDB by the following operations: Output(A), Enqueue(B), Output(C), Enqueue(D), Dequeue(Enqueue), Output(E), Output(Dequeue), Output(Dequeue) • BCEAD: yes. Enqueue(A), Output(B), Output(C), Enqueue(D), Output(E), Output(Dequeue), Output(Dequeue) 1 3. (4 p) (a) (3) • Ommitted: see the definitions in the textbook. (1) • The trees are: (2) 15 / \ 6 21 / \ / \ 5 10 18 25 15 / \ 6 21 / \ / \ 5 10 18 25 \ 12 15 / \ 6 21 / \ / \ 5 10 18 25 / \ 7 12 15 / \ 7 21 / \ / \ 6 10 18 25 / / \ 5 9 12 12 / \ 7 21 / \ / \ 6 10 18 25 / / 5 9 2 (b) (1) • A: no: the node marked * violates the heap requirement 1 / \ 2 1 / \ / \ 2 2 1 1 / \ / \ / \ 2 2 1* 2 1 1 • B: yes. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / \ / \ 8 9 10 11 12 13 4. Removal is handled by marking deleted objects (denoted “x”). (a) 0 15 1 2 3 4 5 6 0 15 1 8 2 3 4 5 6 14 0 15 1 8 2 3 4 5 6 14 0 x 1 8 2 3 4 5 6 14 0 x 1 8 2 3 32 4 5 6 14 0 x 1 8 2 3 32 4 4 5 6 14 0 7 1 8 2 3 32 4 4 5 6 0 15 1 2 3 4 5 6 0 15 1 2 8 3 4 5 6 14 0 15 1 2 8 3 4 5 6 (b) 3 (2 p) 14 0 x 1 2 8 3 4 5 6 14 0 x 1 2 8 3 32 4 5 6 14 0 x 1 2 8 3 32 4 4 5 6 14 0 x 1 2 8 3 32 4 4 5 7 6 5. (3 p) (a) See the textbook p.516 (1) (b) In-place QuickSort, HeapSort, BucketSort under the implementation discussed in the course book. The examples are not given. (2) 6. (3 p) (a) 1 2 4 6 5 3 or 1 3 4 5 6 2. There are more orders. (1 p) (b) 2 4 5 6 or 2 5 4 6. These are the only solutions. (1 p) (c) 1 2 3 4 5 6 or 1 3 2 4 5 6. (1 p) 4