Practice Exam for Second Midterm

Directions:

• Answer the problems on the exam pages.
• There are six problems for 100 total points. Probable scale is A=93, C=69.
• No books, notes, calculators, or collaboration.
• The actual exam will have a time limit of 120 minutes, though it is not intended that you will need all the time.
• Questions 1 and 2 are "true/false with explanation" -- you get five points for a correct boolean answer, and up to five additional points for a convincing justification.

```  Q1: 10 points
Q2: 10 points
Q3: 20 points
Q4: 20 points
Q5: 25 points
Q6: 15 points
Total: 100 points
```

• Question 1 (10):

True or false with justification: Let A be an algorithm that operates on a binary tree, without changing the tree, as follows: A first spends O(1) time processing the root node of the tree, then recursively calls A on the left and the right subtrees. Then A can be substantially sped up on general binary trees using dynamic programming.

• Question 2 (10):

True or false with justification: Let k be any positive integer constant greater than 1 . Then the recurrence T(n) = kT(n/k) + O(n), with T(1) = O(1), has the same big-O solution for any such k. (You may assume that T(n) is evaluated only when n is a power of k.)

• Question 3 (20):

Consider the following Java method (assume that it will only be called with 0 ≤ k and k ≤ n):

``````
int choose (int n, int k) {
if ((k == 0) || (k == n)) return 1;
return choose (n-1, k) + choose (n-1, k-1);}
``````

Explain why the running time will not be polynomial in n in general. Describe (in code or in English) how to revise the algorithm to be polynomial time in n. Carefully determine and justify a big-O bound on the running time of your improved version.

• Question 4 (20):

Let G be a directed graph and let s1,...,sk and t1,...,tk be any 2k distinct vertices of G. We want to know whether there exist k paths P1,...,Pk such that:

1. Each Pi starts at si and ends at ti, and
2. No edge of G appears in more than one of the paths Pi.

Describe an algorithm to solve this problem in a time that is polynomial in m, the number of edges in G. Determine the big-O running time of your algorithm in terms of m, n (the number of vertices in G) and k (the number of paths to be found).

• Question 5 (25):

Consider the following algorithm strangeSort, which sorts n `Comparable` items in a list A:

1. If n ≤ 1, return A unchanged
2. For each item x in A, scan A and count how many other items in A are less than x
3. Put the items with counts less than n/2 in a list B
4. Put the other items in a list C
5. Recursively sort B and C using strangeSort
6. Append the sorted C to the sorted B and return the result

• (a,10) Prove by induction on n that strangeSort correctly sorts all lists of length n, with smaller items first.
• (b,15) Formulate a recurrence for the running time T(n) of strangeSort on an input list of size n. Solve this recurrence to get the best possible big-O bound on T(n) -- you may assume if you like that n is a power of 2.

• Question 6 (15):

Let G be a directed graph with exactly one source s, exactly one sink t, and a positive integer capacity on each edge. Explain carefully why we know that there is a maximum-size flow in G from s to t that sends an integer flow over each edge of G.