P-Class and NP-Class Problems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Introduction to Algorithms

CSE3004
Dr. Paras Jain
Associate Professor - SCAI
VIT Bhopal University
Outline

• Introduction to algorithm
• Asymptotic notation
• Time and space complexity
• P,
• NP,
• NPN,
• NPC
What are the applications of
algorithm?

• Algorithms lie at the heart of computing.


• If we observe our surroundings, we can find several algorithms
working to solve our daily life problems: Social media networks, GPS
applications, Google search, e-commerce platforms, Netflix
recommendation systems, etc.
• applications are powered by algorithms
What is the algorithms

• In mathematics and computer science, an algorithm is a finite sequence of rigorous


instructions, typically used to solve a class of specific problems or to perform a
computation.
• A finite set of steps to solve a problem is called an algorithm.
• The analysis is the process of comparing two algorithms with respect to time, space, etc.
• Primary analysis is two types
1) Priory
2) Posterior
Cont.…

1. S1: Read A 1
S2: Read B 1 2. Main()
S3: Sum = A+B 1
S4: Print (sum) 1/4 Int a, b, sum; 1
3. Main() Scanf (a,b); 1
Int a=1; Sum = a + b; 1
While(1) infinite
Printf (sum) 1
A= a+1; inf
Complexity definitions

• Big-Oh
• Big-Theta
• Big - Omega
Big Oh (O)
• f(n)= O(g(n)) iff there exist positive constants c and n0 such that f(n) ≤ cg(n)
for all n ≥ n0

• O-notation to give an upper bound on a function


Omega Notation

• Big oh provides an asymptotic upper bound on a function.


• Omega provides an asymptotic lower bound on a function.
Theta Notation

• Theta notation is used when function f can be bounded both from above
and below by the same function g
How bad is exponential complexity
• Fibonacci example – the recursive fib cannot even compute fib(50)
P, NP, NP Hard, NP
Complete
Traveling Salesperson Problem

• You have to visit n cities


• You want to make the shortest trip
• How could you do this?
• What if you had a machine that could guess?
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


P-Class

• The class P consists of those problems that are solvable in polynomial


time, i.e. these problems can be solved in time O(nk) in worst-case,
where k is constant.
• These problems are called tractable, while others are
called intractable or superpolynomial.
• Formally, an algorithm is polynomial time algorithm, if there exists a
polynomial p(n) such that the algorithm can solve any instance of
size n in a time O(p(n)).
• Problem requiring Ω(n50) time to solve are essentially intractable for large n.
Most known polynomial time algorithm run in time O(nk) for fairly low value
of k.

• The advantages in considering the class of polynomial-time algorithms is that


all reasonable deterministic single processor model of computation can
be simulated on each other with at most a polynomial.
Example
This class contains many natural problems like:
• Calculating the greatest common divisor.
• Finding a maximum matching.
• Decision versions of linear programming.
• Sorting
• Mearge sort
• Quick sort
• Insertion sort
• Others?
What is the complexity of primality testing?
public static boolean isPrime(int n){
boolean answer = (n>1)? true: false; • This loops until the square root of n so this
should be
for(int i = 2; i*i <= n; ++i)
{
• But what is the input size? How many bits does
it take to represent the number n? log(n) = k
System.out.printf ("%d\n", i);
if(n%i == 0)
• What is

{
answer = false;
break;
} Naïve primality testing is exponential!!
}
return answer;
}
Why obsess about primes?

• Crypto uses it heavily


• Primality testing actually is in P
• Proven in 2002
• Uses complicated number theory
• AKS primality test
Non-deterministic polynomial time

• Deterministic Polynomial Time: The TM takes at most O(nc) steps to accept a


string of length n
• Non-deterministic Polynomial Time: The TM takes at most O(nc) steps on
each computation path to accept a string of length n
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
• NP: the class of decision problems that are solvable in polynomial time on a
nondeterministic machine (or with a nondeterministic algorithm) – (A
deterministic computer is what we know)
• – A nondeterministic computer is one that can “guess” the right answer or
solution.
• Think of a nondeterministic computer as a parallel machine that can freely
spawn an infinite number of processes .
• Thus NP can also be thought of as the class of problems “whose solutions
can be verified in polynomial time” .
• Note that NP stands for “Nondeterministic Polynomial-time”
Sample Problems in NP

• Fractional Knapsack
• MST
• Others?
• – Traveling Salesman
• – Graph Coloring
• – Satisfiability (SAT)
• The problem of deciding whether a given Boolean formula is satisfiable
Ex.

• Graph theory has these fascinating(annoying?) pairs of problems


• Shortest path algorithms?
• Longest path is NP complete (we’ll define NP complete later)
• Eulerian tours (visit every vertex but cover every edge only once, even degree etc).
Solvable in polynomial time!
• Hamiltonian tours (visit every vertex, no vertices can be repeated). NP complete
What happened to automata?
• Problem is in NP iff it is decidable by some non deterministic Turing machine in
polynomial time.
• Remember that the model we have used so far is a deterministic Turing machine
• It is provable that a Non Deterministic Turing Machine is equivalent to a
Deterministic Turing Machine
• Remember NFA to DFA conversion?
• Given an NFA with n states how many states does the equivalent DFA have?
• Worst case …. 2n

• The deterministic version of a poly time non deterministic Turing machine will
run in exponential time (worst case)
Hamiltonian cycles
• Hamiltonian cycle, Hamiltonian circuit, vertex tour or graph cycle is a
cycle that visits each vertex exactly once. A graph that contains a
Hamiltonian cycle is called a Hamiltonian graph.
• Determining whether a directed graph has a Hamiltonian cycle does not
have a polynomial time algorithm (yet!)
• However if someone was to give you a sequence of vertices, determining
whether or not that sequence forms a Hamiltonian cycle can be done in
polynomial time.
• Therefore Hamiltonian cycles are in NP
SAT

• A boolean formula is satisfiable if there exists some assignment of the


values 0 and 1 to its variables that causes it to evaluate to 1.
• CNF – Conjunctive Normal Form. ANDing of clauses of ORs
2-CNF SAT
• Each or operation has two arguments that are either variables or negation
of variables
• The problem in 2 CNF SAT is to find true/false(0 or 1) assignments to the
variables in order to make the entire formula true.

(x  y)(y  z)(x z)(z  y)

• 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
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy