This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: York University COSC 3101 Fall 2004 Midterm (Nov 4) Instructor: Jeff Edmonds 1. 2 9 = 18 marks: Compute the solution and tell which rules that you use. (a) n i =0 3 i i 8 = ( Type of sum: Answer: (3 n n 8 ), Geometric. (b) n i =0 300 i 2 log 9 i + 7 i 3 log 2 i + 16 = ( Type of sum: Answer: ( n 4 log 2 n ), Arithmetic. (c) n i =0 n j =0 i 2 j 3 = ( Answer: n i =1 n j =0 i 2 j 3 = n i =1 i 2 [ n j =0 j 3 ] = n i =1 i 2 ( n 4 ) = ( n 4 )[ n i =1 i 2 ] = ( n 4 )( n 3 ) = ( n 7 ) (d) 7 2 3 n 5 = 3 ( n 5 ) True False Answer: True (e) What is the formal definition of f ( n ) = n (1) ? Answer: c 1 > , c 2 > , n , n n , n c 1 f ( n ) n c 2 . (f) n 2 100 n o ( n 2 ) True False Answer: False (g) If T ( n ) = 8 T ( n 4 ) + ( n ). Then T ( n ) = ( Answer: Because log a log b = log 8 log 4 = log 2 8 log 2 4 = 3 2 = 1 . 5 > 1 = c , the technique concludes that time is dominated by the base cases and T ( n ) = ( n log a log b ) = ( n 1 . 5 ) (h) The solution of the recurrence T ( n ) = 5 T ( n 5 ) + n 2 log( n ) is T ( n ) = ( Answer: Because log a log b = log 5 5 log 5 5 = 2 1 = 2 = 2 = c , the technique concludes that time is dominated by the base cases and T ( n ) = ( f ( n )log n ) = ( n 2 log 2 n ). (i) If T ( n ) = 8 T ( n 5). Then T ( n ) = ( Answer: T ( n ) = 8 T ( n 5) = 8 2 T ( n 2 5) = 8 3 T ( n 3 5) = 8 i T ( n i 5) = (8 i/ 5 ). 2. 4 marks: Consider the following priority queue implemented as a heap. Consider a reasonable algorithm that changes the prioity of a node. (Its input includes a pointer to the node). Give the resulting heap after changing the priority 7 node to have priority 48 and and the priority 45 node to 3. 18 45 22 24 10 40 35 32 23 20 7 2 16 15 12 8 51 4 1 1 Answer: Question cut from midterm: In three or four sentences, describe the algorithm for changing the priority of a node. Ans: If the priority is increased, bubble it up by repeatedly comparing it to its parent and swapping if bigger. If the priority is decreased, bubble it down by repeatedly comparing it to both its children and swapping if smaller with the bigger of the two. Answer: 18 10 35 32 23 2 16 15 12 8 51 4 1 48 40 20 24 22 3 3. 6 marks: Considering Programs. algorithm Eg 1 ( n ) ( pre cond ) : n is an integer. ( post cond ) : Prints His. begin i = 0 put Hi; put Hi; put Hi; put Hi loop i = 1 . . . n put Hi; put Hi; put Hi loop j = 1 . . . n put Hi; put Hi end loop end loop end algorithm algorithm Eg 2 ( n ) ( pre cond ) : n is an integer....
View Full
Document
 Winter '10
 A

Click to edit the document details