Midterm-Spring06-Solutions

# Midterm-Spring06-Solutions - University of Waterloo...

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

SE240 Midterm, Spring 2004 Page 1 of 15 University of Waterloo Department of Electrical and Computer Engineering SE 240 Algorithms and Data Structures Spring 2006 Midterm Examination Instructor: Ladan Tahvildari Date: Monday, June 12, 2006, 4:30 p.m. Duration: 1.5 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 15 4 20 5 25 Total 100

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

View Full Document
SE240 Midterm, Spring 2004 Page 2 of 15 Name: Student ID: Question 1: Algorithm Analysis [25] Part A [10]. A computer program reads the input symbol into an array in memory, adding symbols to the end of the array until the end-of-file symbol is met. The number of input symbols is not known in advance, and no approximation assumptions can be made about it. To save memory, the program first allocates an array of length 1 = L . Then, whenever the array gets full, a new, larger array of length L L f is allocated, and the data from the old array is copied to the new array ( L copying operations), and the old array is de-allocated. Consider, we use kL L = (where k is a constant) for incrementing the size of an array when it overflows. Write a recurrence and solve it to compute how many copying operations are required to get to the array of length n . Give asymptotic upper and lower bounds for this recurrence. If we have an array of length n, then during the last reallocation, we copied n/d elements and increased the size of the array from n/d to n. If the size of the array is 1, then we have not yet done any reallocations (zero copying operations). Thus, the recurrence is: T(1) = 0 n=1 T(n) = T(n/k) + n/k n > 1 Solving using the repeated substitution (and geometric series formula): T(n) = T(n/k) + n/k = (T(n/ 2 k ) + n/ 2 k ) + n/k = T(n/ i k ) + ( = i j j k n 1 / ) = 0 + ( = n j j k k n log 1 / ) = ( = 1 log 0 n j j k k ) = ( 1 log n k k ) / (k – 1) = (n – 1) / (k – 1) = ) ( n Θ
SE240 Midterm, Spring 2004 Page 3 of 15 Name: Student ID: Part B. [8] Let ) ( n f and ) ( n g be asymptotically non-negative functions. Using the basic definition of Θ notation, prove that max )) ( ) ( ( )) ( ), ( ( n g n f n g n f + Θ = .

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

View Full Document
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 / 15

Midterm-Spring06-Solutions - University of Waterloo...

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

View Full Document
Ask a homework question - tutors are online