Ada_module 5_np 1 (1)
Ada_module 5_np 1 (1)
Module 5 - Chapter 1
• Each leaf represents a possible outcome of the algorithm’s run on some input of
size n.
2
3
• An important point is that the number of leaves must be at least as
large as the number of possible outcomes.
• The algorithm’s work on a particular input of size n can be traced
by a path from the root to a leaf in its decision tree, and the number
of comparisons made by the algorithm on such a run is equal to the
length of this path.
• Hence, the number of comparisons in the worst case is equal to the
height of the algorithm’s decision tree.
4
Decision Trees for Sorting
• Most sorting algorithms are comparison based, i.e., they work by comparing
elements in a list to be sorted. By studying properties of decision trees for such
• algorithms, we can derive important lower bounds on their time efficiencies.
P-Class NP-Class
NP-Hard NP-Complete
P, NP, and NP-Complete Problems
We say that an algorithm solves a problem in polynomial time.
if its worst-case time efficiency belongs to O(p(n)) where p(n) is a polynomial of
the problem’s input size n.
Problems that can be solved in polynomial time are called tractable, and
problems that cannot be solved in polynomial time are called intractable.
Deterministic algorithm Given a particular input, will always produce the same
output, with the underlying machine always passing through the same sequence of
states.
State machine: a state describes what a machine is doing at a particular instant in
time. State machines pass in a discrete manner from one state to another.
Non-deterministic Algorithms- If it uses external state other than the input, such as
user input, a global variable, a hardware timer value, a random value, or stored disk
data. If it is timing-sensitive, e.g. if it has multiple processors writing to the same
data at the same time.
• The theory of the NP-Completeness does not provide any method of obtaining
polynomial time algorithms for the problems of the second group. “Many
of the problems for which there is no polynomial time algorithm
available are computationally related”.
Two Classes
1. NP-Complete- have the property that it can be solved in polynomial time if all
other NP-Complete problems can be solved in polynomial time.
2.NP-Hard- if it can be solved in polynomial time then all NP-Complete can be solved
in polynomial time.
Chapter 9: NP H&C Problems
NP-Hard
NP-Complete
8
Chapter 9: NP H&C Problems
P vs NP problems
–The problems in class P can be solved in O(Nk) time, for
some constant k (polynomial time)
–The problems in class NP can be verified in polynomial time. If we are given
a certificate of a solution, we can verify that the certificate is correct in
polynomial time in the size of input to the problem.
–Some polynomial-time solvable problems look very similar to NP-
complete problems.
• An algorithm on such a non-deterministic machine
consists of two phases:
ATME College of
Engineer ing
Vertical Attack:
(i, x[i]) means the position of ith queen in row i and column x[i]
(k, x[k]) means the position of kth queen in row k and column x[k]
If ith & kth queen are in same column then
X[i] == x[k] --------------- (1)
Hence indicate that queens attack vertically
Depar t ment
of CSE
Sum of subsets
Depar t ment
of CSE
Depar t ment
of CSE
Sum of Subsets
Find a subset of a given set S= {S1, S2, S3, S4, ------ Sn}
Of n +ve integers whose sum is equal to given +ve integer d subject
to the constrains
1. Implicit: All Xi values should be distinct and should belong to the set S
2. Explicit: optimal solution be ∑𝑘 𝑆𝑖 = d
𝑖=1
Depar t ment
of CSE
For a node at level i, the left child corresponds to Xi = 1
and the right child to Xi = 0
The bounding function X[X1, X2, X3, -----Xn] = true iff
𝑊𝑖 𝑋𝑖 + ∑𝑛 𝑊𝑖 ≥ 𝑑
∑𝑖
𝑘= 𝑖=𝑘+1
1
X1=1 X1=0
8, 3, 13 3, 3, 13
14, 4, 7 8, 4, 7 9, 4, 7 3, 4, 7
X4=1
15, 5, 0
Solution
Sum of subsets
Depar t ment
of CSE
Depar t ment ATME Colleg e of Eng ineer ing
of CSE
Branch and Bound
The term Branch means the way in which we search the state space tree and
Bound means assigning bounding function at each node. This bounding
function is used to prevent the expansion of nodes that cannot possibly
lead to an answer node.
Knapsack Problem
Knapsack Problem: Given n items of known weights wi and values vi, i=1, 2, . . . ,
n,and a knapsack of capacity W, find the most valuable subset of the items that fit in
the knapsack. It is convenient to order the items of a given instance in descending
order by their value-to-weight ratios. Then the first item gives the best payoff per
weight unit and the last one gives the worst payoff per weight unit
•First arrange in V/W in decreasing order
•Since it is a maximization problem. The upper bound is calculated
using the function
Ub = 100
Ub = 100
Ub = 100
= 76 = 60
= Not Feasible = 70
= 69 = 64
Not Feasible = 65