University of Toronto Department of Electrical and Computer Engineering Midterm Examination ECE 345 Algorithms and Data Structures Fall 2010 Print your name and ID number neatly in the space provided below; print your name at the upper right corner of every page. The exam is eight (8) pages including the cover page; if not, report it to the instructor or TA. Name: ID Number: This is an open book exam. You are permitted to use the textbook for the course but nothing else is permissible. You may reference/use textbook page/section/algorithms if you wish so. Non-native English speakers may use a dictionary. Do all three problems in this booklet. Try not to spend too much time on one problem. Use terminology from the textbook. You must define any different terms before you use them. Write clearly and only in the space provided. Ask the proctor if you need more paper. Nothing on the back of the sheets will be graded. You have 110 minutes for this exam. Raise your hand if you have a question. Turn off your mobile phone. Do not give C code! Write pseudocode and analyze time/memory requirements of your algorithms when asked to receive full credit. All logarithms are base 2 unless otherwise noted. Question Points Score Grader 1 50 2 25 3 25 Total 100
ECE 345 Midterm Fall 2010 1 Name: 1. [Asymptotics, Induction, Sorting, RBTs, Recurrences, BSTs, Heaps] Short Answers, 8+8+8+8+9+9 points. Answer only in the space provided. (a) Does there exist (positive) functions f(n) and g(n) satisfying both f(n) O(g(n)) and g(n) O(f(n))? If yes, give an example of such functions. If no, briefly explain why not. yes no (b) Give the best asymptotic (i.e., in Big-Oh) characterization of the worst case and the best case time complexities of the following algorithm. This algorithm takes as input arrays of integers A and B each of size n. You must explain your reasoning. Algorithm i = 0 sum = 0 while i < n if A[i] < 0 i = n else if A[i] < n for j = 0 to A[i] sum = sum + B[j] endif i = i + 1 Best case: Worst case:
ECE 345 Midterm Fall 2010 2 Name: (c) Use induction to prove that: 1 2 + 2 3 +... + n(n + 1) = n(n + 1)(n + 2)/3 whenever n is a positive integer. Clearly show the induction base/hypothesis/step. (d) Use the Master Theorem to solve the following recurrence. Show your work. T (n) = 2T ( n 2 ) + n lg lg (5) n
ECE 345 Midterm Fall 2010 3 Name: (e) Let T be a binary search tree storing 512 entries. What is the smallest and largest possible heights for T? Assume that the leaves do not store any entries. Explain your reasoning for each case. (f) Let H be a minheap storing more than 3 entries. Assume it stores unique keys, i.e. no repeated keys allowed. Mark the following statements as true or false. You do not need to explain your reasoning for this question. Preorder traversal always lists keys in non-decreasing order Preorder traversal never lists keys in non-decreasing order Inorder traversal sometimes lists keys in non-decreasing order Postorder traversal sometimes lists keys in non-decreasing order
ECE 345 Midterm Fall 2010 4 Name: 2. Search, 25 points. You are given an array A of n integers in the range 0 to k. Describe an algorithm that answers any query about how many of the n integers fall into a range [a..b]. This algorithm should process the information in A into a new array C within Θ(n + k) time. Then it should be able to answer each query in O(1) time. Hint: During preprocessing, remember how many elements are i.
ECE 345 Midterm Fall 2010 5 Name: 3. Greedy Algorithms, 13+12 points. Let us consider a long, quiet country road with houses scattered very sparsely along it. Picture the road as a long line segment, with an eastern endpoint and a western endpoint. Further, let s suppose that the residents of all these houses are avid cell phone users. You want to place cell phone base stations at certain points along the road, so that every house is within four miles of one of the base stations. (a) Give a greedy algorithm that achieves this goal, using the minimum number of base stations. Hint: Start moving from west to east, appropriately placing stations along the way. (b) Prove the optimality of your algorithm.