Docsity

Proving Polynomial Time Solutions for Bin-Packing and Hamiltonian Cycle - Prof. Zhaojun Ba, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A: Algorithm Design and Analysis
Homework Assignment #7 Solutions
Problem 1
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 ∈Pand consider the following algorithm:
Bin-Packing-Optimization(S)
1for k←1to |S|
2do if Bin-Packing-Decision(S, k) == true
3then return k
Our algorithm for determining the minimum number of bins required to pack all of the objects in Scalls the
decision problem Bin-Packing-Decision as a subroutine. The algorithm takes as input a set S, and returns an
integer kthat 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 kbins. 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
Problem 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 ∈Pand consider the following algorithm:
Ham-Cycle-Order(G)
1G′←G
2for all edges e∈E
3do remove efrom G′
4if Ham-Cycle-Decision(G′) == true
5then permanently remove efrom G′
6return G′
Assume that our input Gcontains a hamiltonian cycle, and we must now find the order of that existing
cycle. Our algorithm works as follows. We set G′=Gand 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
Problem 3
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 Wbe 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.
1
pf3

Partial preview of the text

Download Proving Polynomial Time Solutions for Bin-Packing and Hamiltonian Cycle - Prof. Zhaojun Ba and more Assignments Computer Science in PDF only on Docsity!

ECS122A: Algorithm Design and Analysis Homework Assignment #7 Solutions

Problem 1

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

Problem 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

Problem 3

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.

Problem 4

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.

  1. First we show that 4-Color ∈ NP. Given a graph G, and a coloring assignment of the vertices, simply walk the graph and make certain that all adjacent vertices have a different color, and make certain that only 4 colors are used. This is clearly O(V + E).
  2. Now show that 4-Color ∈ NP-Hard. To do this, we reduce from 3-Color to 4-Color, or show that 3-Color ≤p 4-Color. Let G^3 be an instance of 3-Color. Construct a new graph G^4 as follows: Add a single extra vertex v and connect it to every other vertex in the graph. This is clearly polynomial in the size of the graph. Now we must show that G^3 is a yes-instance of 3-Color if and only if G^4 is a yes-instance of 4-Color. Consider the following proof.

“=⇒” 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.