Link opings Tekniska H ogskola MAI/IDA Kaj Holmberg/Jan Maluszynski 2007-04-10 TDDB56 DALGOPT { Algoritmer och Optimering Tentamen 2007-04-10, 8{13 Examinator Jour Maxpoang Tillatna hjalpmedel Jan Maluszynski / Kaj Holmberg Jan Maluszynski, 013-281483 eller 0707-524408 Kaj Holmberg, 0730-869790 40p (varav max 20p kan tillgodoraknas fr an kontrollskrivningen i Oktober 2006) { Miniraknare { Engelsk-Svensk ordbok eller motsvarande for andra spr ak (dvs oversattningstod mellan valfria spr ak) { Lewis, Denenberg: Data Structures and Their Algorithms { Goodrich, Tamassia: Data Structures and Algorithms in Java { Cormen, Leiserson, Rivest: Introduction to Algorithms { Holmberg: Kombinatorisk optimering med linjarprogrammering (kompendium) { Papadimitriou, Steiglitz: Combinatorial Optimization: Algorithms and Complexity Det ar till atet med anteckningar i boken i normallt omfatning. Generella instruktioner: Las igenom alla uppgifter innan du borjar. Redovisa maximalt en uppgift per inlamnat ark. Best ar uppgiften av era deluppgifter kan dessa redovisas p a samma ark. Skriv namn och personnummer overst p a varje ark. Skriv tydligt. Olasbara losningsforslag beaktas icke. Motivera tydligt alla steg i svaret/losningen. Avsaknad av motivering kan medfora poangavdrag. Uppgifterna ar inte ordnade i sv arighetsgrad. Losningar till algoritmuppgifter behover inte skrivas p a engelska. OBS! Notera foljande: Detta ar en ordinarie tentamen i kursen DALGOPT och den best ar av tv a delar: 1. Algoritm delen som motsvarar kontrollskrivningen (20p) 2. Optimering delen (20p) Resultatet fr an kontrollskrivningen kan nyttjas p a denna tentamina om s a onskas. Gor d a s a har: Lamna inte in losningar p a n agon av uppgifterna i Algoritm delen, och vi kommer automatiskt att inkludera resultatet av din kontrollskrivning nar vi summerar poangen. Lycka till! 1 Algoritmer Uppgift 1. Complexity (2p) (2p) a) Consider the following functions, where k 4 is a constant, and log is the logarithm of base 2. Order them according to their growth rate, i.e. put them in the sequence f1 ; :::; f7 such that fi 2 O(fi+1 ) for i = 1; :::6. Does the set include functions with equal growth rate? p nlog k ; k n ; 5n3=2 ; n log n; nk ; n3 ; k log n Justify your answer. b) Write a recursive procedure for computing factorial and analyze its complexity. Uppgift 2. Stacks and Queues The sequence of characters A B C D E is read once, character-by-character from left to right. Each character read may be rst placed in a temporary memory or sent directly to the output. The reading operations may be interleaved with operations removing characters from the memory and sending them to the output. In this way we obtain at the output a permutation of the input sequence (the left-most character of the permutation is the character sent rst to the output). Consider the permutations A C E D B and B C E A D. For each of them check whether it is possible to obtain it if the temporary memory is: (2p) (2p) a) stack c) queue If the answer is \yes" show the sequence of the respective operations performed to obtain it. If the answer is \no" explain why no sequence of the operations can generate the required permutation. Uppgift 3. Trees (3p) a) Consider the following binary tree T 15 6 5 21 10 18 25 (1p) Justify that T is a binary search tree and that T is also an AVL tree. (2p) Show the trees: T1 = Insert(12; T ), T2 = Insert(7; T1), T3 = Insert(9; T2), T4 = (1p) Remove(15; T3), where Insert and Remove are operations on AVL trees. b) Consider the following arrays: A: 1 B: 1 2 1 2 2 1 1 2 2 1 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 Which of them, if any, can represent a heap? Justify your answer by showing the respective trees. 2 Uppgift 4. Hashing We use an array with indices 0 to 6 to implement a hash table of size 7. 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); Insert(8); Insert(14); Remove(15); Insert(32); Insert(4); Insert(7) when the following technique is used: (1p) (1p) a) open addressing with linear probing, b) open addressing with double hashing, where h2 (k ) = 5 (k mod 5) Explain how removal is handled in your solution. Uppgift 5. Sorting (1p) (2p) a) Explain what it means that a sorting algorithm is unstable. b) Which of the sorting algorithms discussed in this course are unstable? For one of them prove that it is unstable by showing its execution on example data. Uppgift 6. Graphs Consider the directed graph. 4 2 1 6 3 (1p) (1p) (1p) 5 a) Show the order of visiting its nodes under a depth-rst search starting in node 1. Is there only one such order? b) Show the order of visiting its nodes under a breadth-rst search starting in node 2. Is there only one such order? c) Show a topological sort of the graph. Is there only one topological sort of this graph? 3 Optimering Uppgift 7 Betrakta foljande LP-problem. min z = d a 2x1 + x2 x1 + 4x2 x1 ; (3p) (2p) (2p) (1p) x2 x2 5 5 0 a) Los problemet med simplexmetoden. Ange en optimallosning samt vilka primala bivillkor som ar aktiva. Ange en dual optimallosning. (Observera att inga ytterligare berakningar behovs for att nna duallosningen.) b) Formulera LP-dualen till problemet. Kontrollera att duallosningen i uppgift a ar till aten. Kontrollera explicit att komplementaritetsvillkoren ar uppfyllda. c) Skulle en ny variabel, x5 , med m alfunktionskoeÆcient c5 = 1 och bivillkorskoeÆcienter a5 = (1; 1)T andra den optimala baslosningen? d) Hur andras det optimala m alfunktionsvardet om hogerledet okas lite, dvs. man satter b1 = 5 + ", d ar " ar ett litet positivt tal. Uppgift 8 (3p) Ange en optimal duallosning till tillordningsproblemet med foljande kostnadsmatris. losningen unik? Ange aven en till Ar aten men inte optimal duallosning. 07 7 71 @7 7 7A 7 7 8 Uppgift 9 Betrakta det s.k. frivilliga billigaste vag-problemet, dar man f ar skicka en enhet fr an startnoden till slutnoden om man skulle vilja, dvs. om man skulle tjana p a det. (En negativ kostnad ar vinst). (2p) (1p) (3p) a) Formulera problemet i variabler och bivillkor som ett LP-problem. (Tank p a mojligheten att anvanda olikhetsvillkor.) Man f ar anta att ingen negativ cykel nns. b) Formulera LP-dualen till problemet. c) Finn primal och dual optimallosning for fallet dar alla b agkostnader ar positiva. Veriera att b ada losningarna ar till atna. 4 Uppgift 10 Antag att man vill nna K b agdisjunkta vagar fr an en given startnod till en given slutnod. (Tv a vagar f ar allts a inte inneh alla n agon gemensam b age.) (1p) (2p) a) Formulera problemet som ett linjart odesproblem. b) Finn det maximala antalet b agdisjunkta vagar fr an nod 1 till nod 8 i nedanst aende graf. (Anvand kand metod.) En hjalpsam person har angivit vagen 1 - 4 - 3 - 5 - 6 - 8. Utnyttja denna som startlosning. 2 5 6 3 1 7 4 5 8