

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


ECS122A: Algorithm Design and Analysis Homework Assignment #7 Solutions
We are asked to show that if Bin-Packing-Decision can be solved in polynomial time, then Bin-Packing- Optimization can also be solved in polynomial time. Assume Bin-Packing-Decision ∈ P and consider the following algorithm:
Bin-Packing-Optimization(S) 1 for k ← 1 to |S| 2 do if Bin-Packing-Decision(S, k) == true 3 then return k
Our algorithm for determining the minimum number of bins required to pack all of the objects in S calls the decision problem Bin-Packing-Decision as a subroutine. The algorithm takes as input a set S, and returns an integer k that is the smallest number of bins required to hold all of the objects in S. The largest number of bins is clearly |S|, where every object is in a different bin. Therefore we simply walk from k = 1 to k = |S| calling our decision problem each time to determine whether or not we can pack the objects into k bins. The running time of Bin-Packing-Optimization is O(n · O(Bin-Packing-Decision)). Since Bin-Packing-Decision ∈ P , then Bin-Packing-Optimization can be solved in polynomial time. 2
We are asked to show that if Ham-Cycle-Decision can be solved in polynomial time, then we can also find the order of the hamiltonian cycle in polynomial time. Notice that we only need to find which edges are specifically necessary for a hamiltonian cycle. Once we find that minimum set of necessary edges, we can pick any vertex as a starting point, and the order of the vertices will be defined by the set of edges. Assume Ham-Cycle-Decision ∈ P and consider the following algorithm:
Ham-Cycle-Order(G) 1 G′^ ← G 2 for all edges e ∈ E 3 do remove e from G′ 4 if Ham-Cycle-Decision(G′) == true 5 then permanently remove e from G′ 6 return G′
Assume that our input G contains a hamiltonian cycle, and we must now find the order of that existing cycle. Our algorithm works as follows. We set G′^ = G and remove an edge from G′^ and see if that edge is necessary for the hamiltonian cycle. If we still have a hamiltonian cycle present without that edge, then that edge is not essential and we can permanently remove it. We walk all of the edges, checking that condition each time, and at the end of the algorithm, we are left with a graph that contains only the hamiltonian cycle. Therefore, we have the order. The running time of Ham-Cycle-Order is O(|E| · O(Ham-Cycle-Decision)). Since Ham-Cycle-Decision ∈ P , then Ham-Cycle-Order can be solved in polynomial time. 2
Let TSP(G,k) be a Boolean function that solves the traveling salesperson decision problem for a weighted graph G = (V, E) and cycle bound k.
(a) Let W be the sum of the weights of all edges. The weight of an optimal cycle is at most W. The idea of the algorithm is to use the Binary Search on the integers 1, 2 ,... , W to find the optimal cycle weight.
TSP-Optimization(G) 1 low = 1 2 high = W 3 while low < high 4 do mid = (low+high)/ 5 if T SP (G, mid) 6 then high = mid − 1, else low = mid + 1
The number of iterations of the loop is Θ(log W ). Since W ≤ |E| × (max{largest edge weight}), log W is polynomially bounded in the size of the input. If TSP runs in polynomial time, then so the whole algorithm does.
(b) Let T be the weight of an optimal cycle (founding in Polynomial time by the algorithm in part (a). The next algorithm constructs an optimal cycle by testing each edge in the graph and discarding those that are not required. (We consider an edge discarded if its weight is not restored to its original value in the for loop. Clearly, an edge of weight T + 1 can not be in a cycle of total weight T ).
TSP-Cycle-Order(G) 1 for each edge e in G 2 do change the weight of e to T + 1 3 if not TSP(G,T) 4 then change e’s weight back to its original value 5 mark e as being in the cycle
TSP is called |E| times, so if TSP runs in polynomial time, then entire algorithm does.
A graph G is said to be k-colorable if there is a way to paint its vertices using k different colors such that no adjacent vertices are painted in the same color.
a. Here is a O(V + E) algorithm for testing if a graph is 2-colorable. Modify BFS to alternate coloring of the nodes. Basically, we are checking for a cycle with an odd length of edges. If a node is colored red, all of its adjacent neighbors must be colored blue. If a node ever has an adjacent neighbor that has already been visited, its color must be opposite the color of the current node. If BFS reports an odd length cycle, the graph is not 2-colorable.
b. Assume that the 3-Color problem is NP-Complete, show that 4-Color problem is also NP-Complete.
“=⇒” Assume G^3 is 3-colorable. Therefore, G^4 is 4-colorable because the added vertex v, which is connected to all the other vertices in the graph, can be colored with a 4th color, and it will always be connected to vertices that are 1 of 3 other colors. “⇐=” Assume G^4 is 4-colorable. Because v is connected to every vertex in the graph, v must be the only vertex in G^4 that has a certain color. Therefore, all other vertices in the graph are colored 1 of 3 colors. Therefore, G^3 is 3-colorable.