Complexity in Design and Analysis of Algorithms
Complexity in Design and Analysis of Algorithms
f(n)= O(g(n)) iff there exist positive constants c and n0 such that
f(n) ≤ cg(n) for all n ≥ n0
Most Searching & Sorting algorithms are polynomial None of the problems in this group has been solved by any
time algorithms polynomial time algorithm
Ex: Ordered Search (O (log n)), Ex: Traveling Sales Person O(n 2 2n)
Polynomial evaluation O(n) Knapsack O(2n/2)
Sorting O(n.log n)
The class P
• The class P consists of those problems that are
solvable in polynomial time.
• More specifically, they are problems that can
be solved in time O(nk) for some constant k,
where n is the size of the input to the problem
• The key is that n is the size of input
NP
• NP is not the same as non-polynomial
complexity/running time. NP does not stand
for not polynomial.
• NP = Non-Deterministic polynomial time
• NP means verifiable in polynomial time
• Verifiable?
– If we are somehow given a ‘certificate’ of a
solution we can verify the legitimacy in polynomial
time
There are two classes of non-polynomial time problems
1. NP-Hard
2. NP-Complete
(xy)(yz)(xz)(zy)
(xy)(yz)(xz)(zy)
• Any of the OR clauses can be converted to
implication clauses
2-SAT is in P
• Create the implication graph
x
y
x
y
z
z
Satisfiability via path finding
• If there is a path from
• And if there is a path from
• Then FAIL!
• How to find paths in graphs?
– DFS/BFS and modifications thereof
3 CNF SAT (3 SAT)
• Not so easy anymore.
• Implication graph cannot be constructed
• No known polytime algorithm
• Is it NP?
– If someone gives you a solution how long does it
take to verify it?
– Make one pass through the formula and check
• This is an NP problem
P is a subset of NP
• Since it takes polynomial time to run the
program, just run the program and get a
solution
• But is NP a subset of P?
• No one knows if P = NP or not
• Solve for a million dollars!
– http://www.claymath.org/millennium-problems
– The Poincare conjecture is solved today
What is not in NP?
• Undecidable problems
– Given a polynomial with integer coefficients, does it have
integer roots
– Hilbert’s nth problem
– Impossible to check for all the integers
– Even a non-deterministic TM has to have a finite number of
states!
– More on decidability later
• Tautology
– A boolean formula that is true for all possible assignments
– Here just one ‘verifier’ will not work. You have to try all possible
values
Amusing analogy
If we know a single problem in NP-Complete that helps when we are asked to prove some
other problem is NP-Complete
┐x3 x3 x3
Vertex cover problem
• A vertex cover of an undirected graph G=(V,E) is a
subset of vertices such that every edge is incident
to at least one of the vertices
• We’re typically interested in finding the minimum
sized vertex cover
• To show vertex cover is NP-complete
• What problem should we try to reduce to it
• It sounds like the ‘reverse’ of CLIQUE
• Reduction is done from CLIQUE to vertex cover
G’ =
G Complement
Of
G
C←∅
while E = ∅
pick any {u, v} ∈ E
C ← C ∪ {u, v}
delete all edges incident to either u or v
return C
How bad is that approximation?
• Look at the edges returned in that algorithm