2005-10-18 Linköpings Tekniska Högskola Institutionen för Datavetenskap Jan Maluszynski TDDB56 DALGOPT – Algoritmer och Optimering Kontrollskrivning DALG 2005-10-18, 14–18 Examinator Jour Maxpoäng Tillåtna hjälpmedel Jan Maluszynski Jan Maluszynski, 013-281483 eller 0707-524408 14p (som kan tillgodoräknas på de tre närmast följande ordinarie tentamina i kursen om så önskas - detaljerade anvisningar om detta se kurshemsidan.) – Miniräknare – Engelsk-Svensk ordbok – Goodrich, Tamassia: Data Structures and Algorithms in Java – Lewis, Denenberg: Data Structures and Their Algorithms – Cormen, Leiserson, Rivest: Introduction to Algorithms Generella instruktioner: • Läs igenom alla uppgifter innan du börjar. • Redovisa maximalt en uppgift per inlämnat ark. Består uppgiften av flera deluppgifter kan dessa redovisas på samma ark. Skriv namn och personnummer överst på varje ark. • Skriv tydligt. Oläsbara lösningsförslag beaktas icke. • Motivera tydligt alla steg i svaret/lösningen. Avsaknad av motivering kan medföra poängavdrag. • Uppgifterna är inte ordnade i svårighetsgrad. OBS! Notera följande: Detta är inte en ordinarie tentamen i kursen DALGOPT - det är endast en kontrollskrivning avseende stora delar av algoritmdelen av kursen. Uppgifter motsvarande de i denna kontrollskrivning kommer att återfinnas på alla ordinarie tentamina i kursen. Lycka till! 1 1. Complexity. (5 p) (a) The following 8 functions can be compared by their asymptotic growth. (2) n, 2n , n log n, n − n3 + 7n5 , n2 + log n, n2 , log n, n! Put them in a sequence f1 (n), ..., f8 (n) such that fi (n) is in O(fi+1 (n)) for i = 1, ..., 7. Justify your answer. (b) A function on natural numbers is recursively defined as follows: (1.5) F (0) = 0 F (1) = 1 F (n) = F (n − 2) + F (n − 1) for n > 1 Show that a recursive procedure based directly on this definition runs in time Ω(2n/2 ). (c) Write a pseudocode of an iterative algorithm that computes the function discussed above in time O(n). Justify that its complexity is as required. 2. Hashing. (1.5) (2 p) We use an array with indices 0 to 10 to implement a hash table of length 11. The keys of the inserted elements are integers. The hash value is calculated as the key value modulo the table length. Show the contents of the initially empty array after performing each of the following operations Insert(15, c), Insert(4, a), Insert(26, b), Delete(15), Insert(5, d), Insert(4, e) when the following technique is used: (a) open adressing with linear probing, where deletion is handled by re-hashing. (1) (b) open addressing with double hashing, where h2 (k) = 5 − (k mod 5) where deletion is handled by the deleted bit technique. (1) 3. Sorting and Selection (3 p) (a) For each of the following algorithms discuss what are the respective best case and worst case data and illustrate them by examples for n = 5: (1.5) • Insertion Sort • Quick Sort • Quick Select (b) Explain whether each of the following statements is true or false • The best case running time of Insertion Sort is O(n). • The worst case complexity of Quick Sort is O(n3 ). • The worst case complexity of Quick Select is O(n). A correct answer without justification or with wrong justification gives no credits. 2 (1.5) 4. Search Trees and Heap. The pre-order traversal of a binary search tree T returns the following sequence of keys (the left-most key is the one returned first). (4) 29, 22, 15, 8, 19, 25, 23, 26, 33, 32 (a) Draw the tree. Describe two different variants of deletion operation on the binary search trees and illustrate them on the example of deletion of the key 22 from T . (1) (b) Justify that T is an AVL-tree. Perform on T the AVL-tree insertion of the key 24. (1) (c) Show the (2,3)-tree obtained from the empty tree by consecutive insertions of the keys in the above sequence. (1) (d) Show the heap obtained from the empty heap by consecutive insertions of the keys in the above sequence. (1) 3