# CMPSCI 311: Theory of Algorithms

### Posted Thursday 18 December 2003

Directions: There are 150 total points. Each of the first four 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) Let G be a flow diagram (a directed graph with edge capacities) and let k be a positive number. Then there is a flow through G of size k if and only if the edges out of the start node have capacities adding to at least k and the edges into the finish node have capacities adding to at least k.

• Question 2 (10): (true/false with justification) Let T(n) be a function obeying the recurrence T(n) = 5T(n/5) + a with initial condition T(1) = b, where a and b are positive numbers. Then T(n) = Θ(n log n).

• Question 3 (10): (true/false with justification) Let N be an NP-procedure with the property that on inputs of size n, the number of possible different guesses that N can make in its guess phase is O(n3). Then the decision problem of N is in the class P. (Recall that this decision problem is take x as input and decide whether there exists a guess of N on input x that will cause its evaluate phase to return ``true''.)

• Question 4 (10): (true/false with justification) Let A be an array of n items, each of which is a character in the set {a,b,c,...,z}. Then A can be sorted in O(n) time.

• Question 5 (45): The problem MST-DECISION is to take as input a weighted undirected graph G (with n vertices) and a number k, and output ``true'' if and only if G has a spanning tree of weight at most k.

• (a,10) Describe an NP-procedure for this problem, proving that it is in the class NP.

• (b,20) Prove that MST-DECISION is in the class P. If you use a standard algorithm from the course, give enough detail to convince me that you know how and why it works.

• (c,15) Suppose that every edge of G has weight 1, and that the number of edges in G is e. Describe an algorithm that runs in O(e) time and produces a minimum spanning tree of G if any spanning tree exists. (If there is no spanning tree or if any edge has a weight other than 1, the algorithm should discover this. Also remember that there can be more than one minimum spanning tree if they are tied for the smallest total cost, and that in this case your algorithm may return any of them.)

• Question 6 (40): As in an exercise on Homework 9, King Arthur has a large number n of knights and some the knights are quarrelling with one another. Arthur has an undirected graph where the nodes represent the knights and there is an edge between nodes x and y if and only if knights x and y are quarreling with each other.

• (a,10) Arthur must select five of his knights for a quest, and does not want to have two knights on the quest quarrelling with each other. Indicate how, given his undirected graph as input and using time polynomial in n, he can decide whether this is possible.

• (b,20) Suppose that Arthur wants to decide, given a number k in the range from 0 to n, whether he can select k knights for a quest without including a quarrelling pair. Prove that this problem is NP-complete, where the input is considered to be both his graph and k. This means you must prove that his problem is in the class NP and reduce a known NP-complete problem to it.

• (c,10) Now assume that each knight is quarreling with at most one other knight. Show that Arthur can find at least n/2 knights to go on the quest. Show that in the worst case, if n is even, he cannot do better than this.

• Question 7 (25): Given a non-negative integer n, we want to determine how many strings of 1's, 2's, and 3's add up to n. For example, with n=3 we have the strings 111, 12, 21, and 3 -- four of them. With n=5 we have thirteen: 11111, 1112, 1121, 113, 1211, 122, 131, 2111, 212, 221, 23, 311, and 32.

• (a,10) Let T(n) be the number of such strings adding to n. Give a recurrence for T(n) when n is positive, based on the fact that the first digit in the string may be 1, 2, or 3. What is T(0)?

• (b,15) Describe an efficient algorithm to compute T(n). What is its big-O running time as a function of n? (Hint: you may want to describe a simple but inefficient recursive algorithm and describe how it may be improved.)