CMPSCI 311: Theory of Algorithms

Actual (in-class) exam is Monday 10 November 2003

Directions: Each of the first three questions is a statement that may be true or false. Please state whether it is true or false -- you will get five points for a correct boolean answer and there is no penalty for a wrong guess. Then justify your answer -- the remaining points will depend on the quality and validity of your justification.

• Question 1 (10): (true/false with justification) If G is an undirected graph, any depth-first search of G starting from vertex v will contain the longest simple path starting at v. (A simple path is defined to be a path that never revisits a vertex.)

• Question 2 (10): (true/false with justification) Let u be a string of length &Theta(1) and v a string of length n. Then the Boyer-Moore method to determine whether u is a substring of v is asymptotically faster than the brute force method.

• Question 3 (10): (true/false with justification) If A is a 3-by-3 matrix of integers, the matrix An can be computed in O(log n) time, assuming that individual integers can be added or multiplied in O(1) time.

• Question 4 (20): Consider a max-heap implemented using pointers rather than an array, so that the root has pointers to two smaller heaps, all of whose elements are smaller than the root's element. Give an algorithm to find the smallest element in the heap, and argue that your algorithm always runs in O(n) time on a heap with n elements.

• Question 5 (25): The triangle problem is to input an undirected graph and determine whether it contains three distinct nodes a, b, and c such that all three edges (a,b), (a,c), and (b,c) are in the graph. Let n be the number of nodes in G and e the number of edges.

• (a,5) Describe a brute-force algorithm to solve the triangle problem in O(n3) time.

• (b,10) Explain how the triangle problem can be solved in o(n3 time using the Solovay-Strassen algorithm for matrix multiplication. (You may quote the performance of Solovay-Strassen.)

• (c,10) Explain how the triangle problem can be solved in O(n+e) time. (Hint: Use depth-first search.)

• Question 6 (25): Let A be an array containing n elements, all distinct. The successor of an element x in A is the element y such that x is smaller than y but no element z is strictly between x and y. Every element except the maximum has a successor.

• (a,5) Explain how to find the successor of x in O(n) time whether A is a sorted array or not.

• (b,10) Explain how to find the successor of x in O(log n) time if A is sorted. Would you recommend presorting A to make finding the successor easier?

• (c,10) Suppose you have a table allowing you to find the successor of any element in O(1) time. Explain how to sort A in O(n) time.