Cs1410 Design and Analysis of Algorithms: P, NP, NP P NP
Cs1410 Design and Analysis of Algorithms: P, NP, NP P NP
Ex. Prove that 5 (or 7) comparisons are necessary and sufficient for sorting 4 keys (or 5 keys,
respectively).
Adversary: Puts the number in a larger of the two subsets generated by last question
Adversary: Keep the ordering b1<a1<b2<a2< … <bn<an in mind and answer comparisons
consistently
Claim: Any algorithm requires at least 2n-1 comparisons to output the above ordering
(because it has to compare each pair of adjacent elements in the ordering)
Ex: Design an adversary to prove that finding the smallest element in a set of n
elements requires at least n-1 comparisons.
Reduction from Q to P: Given a set X = {x1, …, xn} of numbers (i.e. an instance of the
uniqueness problem), we form an instance of MST in the Cartesian plane: Y = {(0,x1), …,
(0,xn)}. Then, from an MST for Y we can easily (i.e. in linear time) determine if the elements
in X are unique.
P = NP ?Dilemma Revisited
P = NP would imply that every problem in NP, including all NP-complete
problems,could be solved in polynomial time
If a polynomial-time algorithm for just one NP-complete problem is discovered, then
every problem in NP can be solved in polynomial time, i.e. P = NP
Most but not all researchers believe that P NP , i.e.P is a proper subset of NP. If P
NP, then the NP-completeproblems are not in P, although many of them are very
useful in practice.
5.4 BACKTRACKING
Backtracking constructs its state-space tree in the depth-first search fashion in the
majority of its applications. If the sequence of choices represented by a current node of the
state-space tree can be developed further without violating the problem’s constraints, it is
done by considering the first remaining legitimate option for the next component. Otherwise,
the method backtracks by undoing the last component of the partially built solution and
replaces it by the next alternative.
A node in a state-space tree is said to be promising if it corresponds to a partially
Q Q Q
Q
Illegal Illegal
Q Q
Q Q
Illegal Illegal
There is no solution when the value of n = 1 , 2 and 3
4 - Queens Problem
The solution for the 4 – Queens’s problem is easily obtained using the backtracking
algorithm.
The aim of the problem is to place 4 – Queens on the chessboard in such a way that
none of the queens hit each other.
Solving Procedure
Step 1:
Let us assume the queens are placed in the row wise. The 1 st Q is placed I 1st row at
(1,1)
Q1
Step 2:
The second Queen has to be placed in 2 nd row. It is not possible to place the Q 2 at the
following places.
(2,1) – placing the queens in the same column
(2,2) – placing the queens in the same diagonal.
So the queen can be placed in (2,3)
Q1
Q2
Step 3:
The third Queen has to be placed in 3 rd row. It is not possible to place the Q 3 at the
following places.
(3,1) – placing the queens in the same column
(3,2) – placing the queens in the same diagonal.
(3,3) – placing the queens in the same column
(3,4) – placing the queens in the same diagonal.
Q1
Q2
X X X X
Step 4:
The third Queen has to be placed in 3 rd row. It is not possible to place the Q 3 at the
following places.
(3,1) – placing the queens in the same column
(3,3) – placing the queens in the same diagonal.
(3,4) – placing the queens in the same column
So the queen has to be placed in (3,2)
Q1
Q2
Q3
Step 5:
The fourth Queen has to be placed in 4th row. It is not possible to place the Q 4 at the
following places.
(4,1) – placing the queens in the same column
(4,2) – placing the queens in the same column
(4,3) – placing the queens in the same diagonal.
(4,4) – placing the queens in the same column
Q1
Q2
Q3
X X X X
Backtracking to the previous solution
Q3
Step 6:
Q1
Q2
Q3
Q4
Basic Terminologies
Solution Space
Tuples that satisfy the constraints
The solution space can be organized into a tree
State Space
State Space is the set of all paths from root node to other nodes.
State Space Tree
State Space Tree is the tree organization of the solution space.
In backtracking technique while solving a given problem a tree is constructed based on
the choices made.
Such a tree with all possible solutions is called State Space Tree.
Promising and Non-Promising Node
A node in State Space Tree is said to be promising
The node which are not promising for solution in a state space tree is called non –
promising node.
Q1= 1 Q1= 3
1
Q1= 2
2 6 Q2 = 4Q2 = 1 1
Q2 = 3
3 4 7 1
Not a Q3 = 2 Q3 = 1Q4 = 2
Solution
5Not a 8 Q4 = 3Q4 = 2 1
Solution
9 1
Solution Solution
Q1
Q1
1 Q1= 1
Q1= 2 Q1= 3
2
Q2 = 3 Q 29= 4 1 Q2 = 6
3 1
1 Q3 = 2
Q3 = 6 Q3 = 1
Q4 = 7 1 Q4 = 3
4 1
Q4 = 3
Q5 = 1 5 Q5 = 7 Q5 = 5
1 1
Q6 = 2
Not a Q6 = 2 Solution
6 1 1
Q7 = 4 Q7 = 8
7 1
Not a
Q8 = 5 Solution
8 2
Solution
Analysis
Time complexity of 8 Queen’s problem is O ( k – 1 )
2
5.6 SUM OF SUBSETS
The sum of subsets is solved using the backtracking method.
Problem Definition
Given n distinct positive numbers (weights) W i, 1 ≤ i ≤ n, and m, find all subsets of the
Wi whose sum is m.
Consider the problem of finding a Hamiltonian circuit in the graph in Figure a. Without
loss of generality, we can assume that if a Hamiltonian circuit exists, it starts at vertex a.
accordingly, we make vertex a the root of the state-space tree (Figure b). The first component
of our future solution, if it exists, is a first intermediate vertex of a Hamiltonian circuit to be
constructed. Using the alphabet order to break the three-way tie among the vertices adjacent
to a, we select vertex b. From b, the algorithm proceeds to c, then to d, then to e, and finally
to f, which proves to be a dead end. So the algorithm backtracks from f to e, then to d, and
then to c, which provides the first alternative for the algorithm to pursue. Going from c to e
eventually proves useless, and the algorithm has to backtrack from e to c and then to b. From
there, it goes to the vertices f , e, c, and d, from which it can legitimately return to a, yielding
the Hamiltonian circuit a, b, f , e, c, d, a. If we wanted to find another Hamiltonian circuit, we
could continue this process by backtracking from the leaf of the solution found.
5.8 BRANCH-AND-BOUND
Example
Job 1 Job 2 Job 3 Job 4
Person a 9 2 7 8
Person b 6 4 3 7
Person c 5 8 1 8
Person d 7 6 9 4
Complete state-space tree for the instance of the assignment problem solved with the best-
first branch-and-bound algorithm (Figure).
Nearest-neighbor algorithm
Example 1 For the instance represented by the graph in Figure, with a as the starting vertex,
the nearest-neighbor algorithm yields the tour (Hamiltonian circuit) sa: a − b − c − d − a of
length 10.
Multifragment-heuristic algorithm
Step 1 Sort the edges in increasing order of their weights. (Ties can be broken
arbitrarily.) Initialize the set of tour edges to be constructed to the empty set.
Step 2 Repeat this step n times, where n is the number of cities in the instance being
solved: add the next edge on the sorted edge list to the set of tour edges, provided this
addition does not create a vertex of degree 3 or a cycle of length less than n;
otherwise, skip the edge.
Step 3 Return the set of tour edges.
Example 2 As an example, applying the algorithm to the graph in Figure yields {(a, b), (c, d),
(b, c), (a, d)}. This set of edges forms the same tour as the one produced by the nearest-
neighbor algorithm.
Twice-around-the-tree algorithm
Stage 1: Construct a minimum spanning tree of the graph (e.g., by Prim’s or Kruskal’s
algorithm)
Stage 2: Starting at an arbitrary vertex, create a path that goes twice
around the tree and returns to the same vertex
Stage 3: Create a tour from the circuit constructed in Stage 2 by making shortcuts to
avoid visiting intermediate vertices more than once
Example 3:
Example 4 Let us trace the Christofides algorithm in Figure a. The graph’s minimum
spanning tree is shown in Figure b. It has four odd-degree vertices: a, b, c, and e. The
minimum-weight matching of these four vertices consists of edges (a, b) and (c, e). (For this
tiny instance, it can be found easily by comparing the total weights of just three alternatives:
(a, b) and (c, e), (a, c) and (b, e), (a, e) and (b, c).) The traversal of the multigraph, starting at
vertex a, produces the Eulerian circuit a − b − c − e − d − b − a, which, after one shortcut,
yields the tour a − b − c − e − d − a of length 37.
The knapsack problem, another well-known NP-hard problem: given n items of known
weights w1. . . wn and values v1, . . . , vn and a knapsack of weight capacity W, find the most
valuable subset of the items that fits into the knapsack.
Example: consider the instance of the knapsack problem with the knapsack capacity 10 and
the item information as follows:
Item weight value
1 7 $42
2 3 $12
3 4 $40
4 5 $25
Computing the value-to-weight ratios and sorting the items in nonincreasing order of these
efficiency ratios yields
Item weight value value/weight
1 4 $40 10
2 7 $42 6
3 5 $25 5
4 3 $12 4
The greedy algorithm will select the first item of weight 4, skip the next item of weight
7, select the next item of weight 5, and skip the last item of weight 3. The solution obtained
happens to be optimal for this instance
Example: consider the instance of the knapsack problem with the knapsack capacity 10 and
the item information as follows:
Item weight value
1 7 $42
2 3 $12
3 4 $40
4 5 $25
Computing the value-to-weight ratios and sorting the items in nonincreasing order of these
efficiency ratios yields
Item weight value value/weight
1 4 $40 10
2 7 $42 6
3 5 $25 5
4 3 $12 4
The algorithm will take the first item of weight 4 and then 6/7 of the next item on the
sorted list to fill the knapsack to its full capacity. This algorithm always yields an optimal
solution to the continuous knapsack problem. The items are ordered according to their
efficiency in using the knapsack’s capacity. If the first item on the sorted list has weight w1
and value v1, no solution can use w1 units of capacity with a higher payoff than v1. If we
cannot fill the knapsack with the first item or its fraction, we should continue by taking as
much as we can of the second most efficient item, and so on.
[ ]
∝ 20 30 10 11
15 ∝ 16 4 2
3 5 ∝ 2 4
19 6 18 ∝ 3
16 4 7 16 ∝
2. Discuss the approximation algorithm for NP-hard problems. (Dec 2018)
3. Explain the 4-Queens problem using backtracking. Write the algorithms. Give the estimated
cost for all possible solutions of 4-queens problem. Specify the implicit and explicit
constraints. (Dec 2018) (Dec 2016)
4. What is class NP? Discuss about any five problems for which no polynomial time algorithm
has been found. (May 2018)
5. Elaborate on the nearest-neighbor algorithm and multiragment-heiristic algorithm for TSP
problem. (May 2018)
6. Apply branch and bound algorithm to solve the travelling salesman problem for the
following: (May 2017)
9. Find a Hamiltonian circuit or disprove its existence in the graph given below. (Dec 2017)
10. Describe the backtracking solution to solve 8-queens problem. (May 2017)
11. Describe the approximation algorithm for NP-hard problems. (May 2017)
12. Solve the following instance of knapsack problem by branch and bound algorithm. (Dec
2016)
Item weight value
1 5 $40
2 7 $35
3 2 $18
4 4 $4
5 5 $10
6 1 $2
Capacity W = 15.
13. State the subset sum problem and complete the state space tree of the backtracking
algorithm applied to the instance A = {3, 5, 6, 7} and d =15 of the subset sum problem. (May
2016)
14. State the relationships among the complexity class algorithms with the help of neat
diagram. (Dec 2015)
15. What is approximation algorithm? Give example. (Dec 2015)
16. Suggest an approximation algorithm for travelling salesman problem. Assume that the
cost function satisfies the triangle inequality. (May 2015)
17. Explain how job assignment problem could be solved, given n tasks and n agents where
each agent has a cost to complete each task, using branch and bound technique. (May 2015)