Midterm-Spring09-Solutions - University of Waterloo...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
SE240 Midterm, Spring 2004 Page 1 of 14 University of Waterloo Department of Electrical and Computer Engineering SE 240 Algorithms and Data Structures Spring 2009 Midterm Examination Instructor: Ladan Tahvildari, PhD, PEng Date: Tuesday, June 23, 2009, 5:30 p.m. Location: DWE 3518 Duration: 2 hours Type: Closed Book Instructions: There are 5 questions. Answer all 5 questions. The number in brackets denotes the relative weight of the question (out of 100). If information appears to be missing from a question, make a reasonable assumption, state it and proceed. Write your answers directly on the sheets. If the space to answer a question is not sufficient, use the last (overflow) page. When presenting programs, you may use any mixture of pseudocode/C++/Java constructs as long as the meaning is clear. Name Student ID Question Mark Max Marker 1 25 2 15 3 20 4 15 5 25 Total 100
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
SE240 Midterm, Spring 2004 Page 2 of 14 Name: Student ID: Question 1: Algorithm Analysis [25] Part A [15]. Consider the following two recursive algorithms. The input A is an array of size n . Assume that it takes constant time to divide A into sub-arrays. Each algorithm divides the incoming array into a number of sub-arrays and then recursively calls itself with one or more of the sub-arrays. Algorithm1 (A[1. .n] ) if (n <= 0) return; mid = n/2; if (mid is an even number) Algorithm1 (A[1. .mid]); else Algorithm1 (A[mid+1. .n]); end end Algorithm2 (A[1. .n] ) if ( n <= 0 ) return; q = n/4; if (q is an even number) Algorithm2 (A[1. .q]); Algorithm2 (A[q+1. .2q]); else Algorithm2 (A[2q+1. .3q]); Algorithm2 (A[3q+1. .n]); end end [i]. Write down the recurrence function for the running time of Algorithm 1 and solve it. T(n) = T(n/2) + 1 Æ a=1, b=2, f(n)=1 1 0 log log 1 2 = = = n n n a b Case 2: ) (lg ) ( n n T Θ = [ii]. Write down the recurrence function for the running time of Algorithm 2 and solve it. T(n) = 2T(n/4) + 1 a=2, b=4, f(n)=1 n n n n a b = = = 2 / 1 log log 2 4 Case 1: 5 . 0 ); 1 ( ) ( ) ( ) ( 5 . 0 log = = = = ε O n O n O n f a b ) ( ) ( n n T Θ = [iii]. Which algorithm is more efficient based on your results from parts [i] and [ii]? Algorithm 1 is more efficient since its running time is better (smaller).
Background image of page 2
SE240 Midterm, Spring 2004 Page 3 of 14 Name: Student ID: Part B. [10] Give asymptotic upper and lower bounds for the following recurrence. Assume that ) ( n T is constant for sufficiently small n . Make your bounds as tight as possible, and justify your answer.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Page1 / 14

Midterm-Spring09-Solutions - University of Waterloo...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online