# Second Midterm Exam

### Directions:

• Answer the problems on the exam pages.
• There are six problems for 100 total points. Actual scale was A = 84, C = 48.
• No books, notes, calculators, or collaboration.
• The exam had a time limit of 120 minutes, though it was not intended that you would 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: 20 points
Q6: 20 points
Total: 100 points
```

• Question 1 (10): True or false with justification: Let L be a list of n items, all distinct, and let k be an integer with k < n. Our goal is to find the item in L that has exactly k items in L less than it. But the only method we have available is called `Split`: Given a list of m items it returns two lists, one with the smallest floor(m/2) (Java "m/2") items in the input (in some unknown order) and the other list with the rest.

If `Split` takes O(n) time on a list of size m, then we can create a list containing only our desired item in O(n) time, no matter what k is.

• Question 2 (10): True or false with justification: Let G be a network flow diagram, let f be a valid flow in G, and let Δ be a positive constant. Define Gf(Δ) to be the graph consisting of those edges in the residual graph Gf that have labels of at least Δ. If there is no path in Gf(Δ) from s to t, then the size of f is within Δ of the maximum possible flow in G.

• Question 3 (20): Let L and ε be two real numbers such that L = 2kε, where k is a non-negative integer. We have an L by L square region of desert, enclosed by a fence, in which we know that there is a lion. The following recursive algorithm R(L) will allow us to capture the lion:
• If L = ε, capture the lion by placing a cage over the entire fenced area. Otherwise,
• Build a fence of length L bisecting the region from north to south.
• Determine which half of the region contains the lion.
• Build a fence of length L/2 bisecting that half-region from east to west.
• Apply the algorithm R(L/2) to the enclosed quarter-region containing the lion.

Write and solve a recurrence to determine the exact (not big-O) length of fence required to capture the lion with algorithm R(L). Do not count the fence enclosing the entire region at the beginning. (Hint: It may help to first solve the problem for L = ε, L = 2ε, and L = 4ε.)

• Question 4 (20): Let w1, ..., wk be a set of words and let vi for each i be the positive point value of work wi. Given an input string z of length n, we want to find a set of non-overlapping occurrences of some of the wi's within z such that the total point value of the words in the set is as large as possible. (If {wi} were the set of all English words and z were "cattledog", for example, we would need to determine whether the value of "cat" plus "led" was greater than that of "cattle" plus "dog" or "cat" plus "dog".) (Note also that there is no restriction on using a word wi more than once as long as there is no overlap.)

Describe an algorithm that will determine the optimal set of words. Determine the big-O running time of your algorithm as a function of n and k, and verify that it is polynomial in them.

• Question 5 (20): The roads in the region including Point A and Point B are represented by an undirected graph with n nodes (including A and B) and m edges, where an edge means that a road may be traversed in either direction. An accident prevents drivers from using the road in either direction. Transportation planners want to know the smallest number of simultaneuous accidents which, in the worst case, could prevent all travel from Point A to Point B.

Describe an algorithm t determine this number, and determine its running time as a function of m and n. (I intended to insist that this running time be polynomial in m and n, but I didn't.) If you make use of a standard algorithm from the book, you need not describe it in detail but you must indicate how you are transforming your given input into an input suitable for that algorithm, and what you are doing with its output.

• Question 6 (20): We are given a weighted directed graph G with n nodes and m edges, where the weights may be positive, negative, or zero. Describe (in some detail) an algorithm to determine whether a negative cycle exists in G. (Recall that a cycle in a directed graph is a directed simple path of one or more edges from some vertex to itself. A negative cycle is one where the sum of the weights of the edges in the path is negative.)

Determine the big-O running time of your algorithm in terms of m and n -- it should be polynomial in them.