CS2223
HW#7

Due: Tuesday , December 16

1. (8 points) Consider Problem 6.21 (b) on page 217 of our text. In HW#4 we saw that the suggested greedy algorithm does not always yield an optimal solution. Find a dynamic programming algorithm to solve this problem in worst case time in . (Hint: As always, consider using tools we’ve already developed.)

2. (5 points) Prove or give a counterexample to the following.

Conjecture: Let  G be any graph such that every edge has a distinct positive length (no two edges have the same length). For every vertex vÎN, if l is the length of a shortest edge incident with v, then the array D returned by Floyd’s algorithm has an entry l.

3. (5 points) Prove or give a counterexample to the following.

Conjecture: Let  G be any graph such that there is an edge between every pair of vertices and every edge has a distinct positive length (no two edges have the same length). For every pair of vertices v,wÎN, the array D returned by Floyd’s algorithm satisfies D[v,w]=L[v,w].

4. (7 points) Describe an algorithm (precise pseudocode suffices) to check if a graph is a tree. Your algorithm should work in time in O(n). 

CS2223
HW#7 Solutions

1. Reduce the instance of the Programming_Packing problem to an instance of 
(0/1)-Knapsack such that
     Knapsack      Program_Packing
	n		n
	W 	            D
	v[i]		si
	w[i]		si
The “weight” and “value” of each program is its size, and the capacity D of the disk corresponds to the capacity W of the knapsack. The optimal packing produced by the solution to the instance of knapsack problem yields an optimal packing of programs into the disk.

2. The conjecture is true. For any v, let edge (v,w) be a minimal length edge (of length l) incident with v. Since all paths from v to w not including the edge (v,w) start with a length greater than l, and all lengths are positive, the rest of the path makes it even longer,  the length of a shortest path from v to w is l. Floyd’s algorithm returns D[v,w]=l. 

3. The conjecture is false. The triangle (complete graph on three vertices) with edge lengths 5, 6 and 40 is a counterexample.

4. Do a depth first search to check if the graph is connected, but quit as soon as a cycle is found. If the graph is connected and does not have a cycle, then it’s a tree, otherwise it’s not a tree. Since a cycle, if it exists, will be found after examining at most n edges, the algorithm works in time in O(n).