CORE EXAM: ALGORITHMS PART Department of Computer Science New York University Jan 21, 2003 Question 1 PART (i) (2 points) Given the array [1 8] = 6 10 13 5 8 3 2 11 Partition pseudo-code A ; :::; < ; ; ; ; ; ; ; > , the pivot element [1] = 6, and the A PARTITION( ): 1. let [ ] (pivot) 2. let 3. for + 1 to 4. do if [ ] , 5. then +1 6. exchange [ ] $ [ ] 7. print 8. exchange [ ] $ [ ] 9. print 10. return A; p; q x A p i p j p q A j x i i A i A j A A i A j A i Show the arrays that are printed eachtime lines 7 and 9 are executed (for a total of 4 arrays). PART (ii) (3 points) Assume all array elements are distinct. Given the Pseudo-Code of the quicksort program, provide the worst-case running time, ( ). First, set up the recurrence equation (2 points) and then solve it (1 point) T n Hint: The worst-case happens when the array input is provided sorted or reverse sorted and the Partition program splits the array with one side having zero elements. QUICKSORT( ): 1. if 2. then 3. QUICKSORT( 4. QUICKSORT( A; p; r p < r q ( P ART I T I ON A; p; r 1) +1 ) ) A; p; q A; q ;r QUICKSORT( 1 ) (3 points) What is the best-case running time, T(n)? First, set up the recurrence equation (2 points) and then solve it (1 point) Initial call: PART (iii) A; ;n Hint: The best-case happens when the Partition program splits the array evenly at every step. (2 points) Suppose the input array is such that the Partition program alternates, at one step splitting the array evenly (lucky) and at the next step splitting the array with one side having zero elements (unlucky). What will be the running time of quicksort for such arrays of size ? PART (iv) n 1 Solution to Question 1 PART (i) 1. 2. 3. 4. A A A A = = = = < < < < 6 5 10 13 8 3 2 11 6 5 3 10 13 8 2 11 6 5 3 2 10 13 8 11 5 3 2 6 10 13 8 11 ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; > ; ; ; > ; ; ; > ; ; ; > ; PART (ii) ( ) = (0) + ( 1) + ( ) = (1) + ( 1) + ( ) = ( 1) + ( ) = ( 2 ) T n T T n n T n n T n n n since X ( ) = ( 2) n k k=1 n : PART (iii) ( ) = 2 ( 2 ) + ( ) = ( log ) since it gives a recursion Tree with height log and the sum of the costs of the nodes at each depth is ( ). T n T n n n n n n PART (iv) We alternate lucky, unlucky, lucky, unlucky, lucky, ... ( ) = 2 ( 2 ) + ( ) lucky ( ) = ( 1) + ( ) unlucky n L n U U n L n n n Solving it ( ) = 2( ( 2 1) + ( 2 )) + ( ) = 2 ( 2 1) + ( ) = ( log ) L n n L L n n n n n n 2 (1) Questions 2 Let be a directed graph with edges that have lengths. Present, in high-level pseudo-code, an eÆcient algorithm that computes, for all pairs of vertices , the length of the shortest path from to , and the number of paths from to that have this shortest length. For this problem, the length of a path is the sum of its edge lengths. Assume that all cycles have positive total length. The edge lengths are stored in the array [1 1 ], so that if ( ) is an edge in , then [ ] is the length of the edge. You can also assume that ( ) is in nite ( ) is not in . Two paths are di erent if the sequence of edges that de ne the path are di erent. For example, in the gure below, there are seven di erent shortest paths from to . G i; j i j i j E ::n; ::n i; j E i; j G i; j i G j 6 2 2 1 1 3 i 3 2 1 j Solution to Question 2 procedure Count(C[1..n,1..n],Pcount[1..n,1..n]); forall pairs do if [ ] 6= 1 then [ ] 1 else [ ] 0 endif endfor; for 1 to do for 1 to do for 1 to do if [ ] [ ] + [ ] then [ ] [ ] + [ ]; [ ] [ ] [ ] elseif [ ] = [ ] + [ ] then [ ] [ ]+ [ ] i; j C i; j P count i; j P count i; j k n i n j n C i; j > C i; k C i; j C i; k P count i; j C i; j P count i; j C k; j C k; j P count i; k C i; k P count k; j C k; j P count i; j P count i; k endif endfor endfor endfor end Count . 3 [ ] timesP count k; j E i; j Question 3 1) The Towers of Hanoi problem is the following. You are given three posts, , , and . There are rings n , n 1 , 2 1 sitting on post , with i directly on top of i+1 , for 1 , so that ring 1 is the top ring. The other posts are empty. The problem is to move the rings so that they all wind up on post subject to the following rules: A ring i cannot be placed on a pole that holds a (smaller indexed) ring h where . Only the top ring on a post can be removed at any step. Only one ring can be moved at each step, and it must be removed from one post and placed on another. Recursion gives an easy solution to the problem. procedure TH( , , , ); if = 1 then move top ring on to else TH( 1, , , ); f Move the top 1 rings form to g move the top ring on to ; TH( 1, , , ); f Move the top 1 rings from to g endif end-TH. a) Present the recurrence equation for the exact number of ring moves for TH( , , , ). Be sure to include the initial condition as well as the recurrence equation. b) Now suppose that in addition to posts , and , there is a short post , which can hold a single ring of any type. All other aspects of the problem are unchanged. Present, in high level code, as eÆcient a solution to this new problem as you can, c) Present the recurrence equation for the exact number of ring moves for the solution given in part b. Be sure to include the initial condition as well as the recurrence equation. A A r B C r n i < n r r :::; r ; r r B r r h < i n A B C n A n A C B A n B N A C N C B B C B A n A B C A Solution to Question 3 a) (1) = 1; ( ) = 2 ( 1) + 1. b) procedure THD( , , , ); if = 1 then move top ring on to elseif = 2 then move top ring on to ; move top ring on to ; move top ring on to else TH( 2, , , ); THD(2, , , , ); TH( 2, , , ) endif end-THD. c) (1) = 1; (2) = 3); ( ) = 2 ( 2) + 3, 2. B C T T n T n n A B C n A B n n A D A B D B A C B A B C D n C B A T T T n T n n > 4 D