Design & Analysis of Algorithm
Unit II
Analysis of Algorithms and Complexity Theory
By Prof. Bhavana A. Khivsara
U2: Analysis of Algorithms and Complexity Theory
Syllabus:
Counting Dominant operators, Growth rate, upper bounds
Analysis
Input Size:
Input size (number of elements in the input)
How do we determine the input size?
The compile time does not depend on the instance characteristics. But run time is depending on the instance characteristics.
f(n) :Where n denotes the instance characteristics
Types of Analysis
Efficiency of algorithms, is known as analysis of algorithms.
The function f(n), gives the running time of an algorithm, depends not only on the size ‘n’ of the input data but also on the particular data.
The complexity function f(n) for certain cases are:
Cont…
2. Worst Case : The maximum value of f(n) for any key possible input.
3. Average Case : The expected value of f(n).
Example- Linear Search
Example- Linear Search
Example- Sorting
Example- Sorting
�Rate of Growth �
Asymptotic Growth
(i.e., for large values of n)
Asymptotic Growth
Big-O Notation
Examples
Big Oh Notation
More Examples
f(n)<=c*g(n)
Let f(n)=2n+3
If g(n)=log n then , f(n)=O(log n); not true
So closest func for Big O is f(n)=O(n)
Ω - notation
Examples
f(n)>=c*g(n)
Let f(n)=2n+3
If g(n)= n^2 then , f(n)= Ω( n^2); not true
So closest func for Big Ω is f(n)= Ω(n)
Θ-notation
Θ(g(n)) is the set of functions with the same order of growth as g(n)
Examples
C1*g(n)<=f(n)<=c2*g(n)
Let f(n)=2n+3
1*n<= 2n+3<=5n ; True ,n>=1 : f(n)= Θ(n)
If g(n)= n^2 then , f(n)= Θ( n^2); not true
If g(n)= log n then , f(n)= Θ( log n); not true
So func is exact equal to f(n)= Θ(n)
Little o
Complexity Theory
Types of Problem
Problems
Polynomial
Non- Polynomial
Types of Problem
Types of Problem
Types of Algorithm
Algorithm
Deterministic
Non- Deterministic
Types of Algorithm
Non Deterministic Algorithm
NDC Algorithm:
some of the terms related to the non-deterministic algorithm are defined below:
1. choice(X) : chooses any value randomly from the set X.
2. failure() : denotes the unsuccessful solution.
3. success() : Solution is successful and current thread terminates.
Consider the problem of searching for an element x in a given set of elements A[1:n]
Non Deterministic search
j= choice(a, n)
if(A[j]==x) then
{ write(j); success(); }
write(0); failure();
Decision Problem Vs Optimization Problems
Decision Problem | Optimization Problem |
Any problem for which the answer is either Zero or One (Yes or No) is called a decision problem | Any problem that involves the identification of an optimal (either maximum or minimum) value of a given function is known as an optimization problem |
An decision algorithm is used to solve decision problem | An optimization algorithm is used to solve an optimization problem |
Deterministic problems are easier as compared to Optimization problem | Optimization problems are difficult as compared to Deterministic problem |
P, NP, NP Hard, NP Complete
Computational Complexity Problems
P, NP
NP
P
P Vs NP
P | NP |
P Problems are set of problems which can be solved in polynomial time by deterministic algorithm | NP Problems are set of problems which can be solved in polynomial time by Non-deterministic algorithm |
P problems can be solved & Verified in Polynomial Time | Solution to NP problems cannot be Solved/Obtained in Polynomial time, but if the solution is given, it can be verified in Polynomial time. |
P Problems are subset of NP Problems | NP Problems are Superset of P Problems |
All P Problems are deterministic in Nature | All NP Problems are non-deterministic in nature |
Eg. Selection Sort, Linear Search | Eg. TSP, Knapsack Problem |
NP Complete
Relation Between P, NP, NP Hard, NP Complete
NP
P
NP
Hard
NP Complete
NP-Hard Vs NP-Complete
NP Hard | NP Complete |
NP Hard problem(Say A) can be solved if & only if there is NP Complete problem (Say B) can be reducible into A in Polynomial time. | NP Complete problems can be solved by deterministic algorithm in Polynomial time |
To solve NP Hard problem, it must be in NP class | To solve NP Complete problems, it must be in both NP and NP hard problems |
It is not a decision problems | It is a decision Problems |
Eg. Halting Problems, Hamiltonian Cycle Problems | Eg. Determine whether a graph has a hamiltonian cycle or not. |
Polynomial Problem Reduction
Fig- Reduction in NP- Completeness
Every Problem in NP
Circuit - SAT
CNF- SAT
3- SAT
Vertex Cover/Node Cover
Clique
Set-Cover
Sum of Subset
Hamiltonian Cycle
Knapsack
TSP
Steps to Prove Problem L2 is NP- Complete
Steps to Prove Problem L2 is NP- Hard
SAT/Satisfiability Means?
Circuit SAT
Theorem: Circuit SAT is in NP
What is 3-SAT Problem (Restricted Satisfiability)?
(X1 V X2 V X3) (X2 V X3V X4)
Prove that 3-SAT is NP Complete
Proof:
Prove that 3-SAT is NP Complete
Prove that 3-SAT is NP Complete
Prove that 3-SAT is NP Complete
Prove that 3-SAT is NP Complete
Node Cover/Vertex Cover
Node Cover/Vertex Cover
1
2
3
5
4
Theorem- Node Cover is NP Complete
Proof: To Prove this we require to follow steps given below
Theorem- Node Cover is NP Complete
Proof: To Prove this we require to follow steps given below
Theorem- Node Cover is NP Complete
1
2
3
4
5
Theorem- Node Cover is NP Complete
1
2
3
4
5
1
2
3
4
5
Theorem- Node Cover is NP Complete
1
2
3
4
5
1
2
3
4
5
Theorem- Node Cover is NP Complete
1
2
3
4
5
1
2
3
4
5
Theorem- Node Cover is NP Complete
1
2
3
4
5
1
2
3
4
5
Theorem- Node Cover is NP Complete
1
2
3
4
5
1
2
3
4
5
Graph- G
Clique of Size 3 with Vertex {1,2,3}
Graph- G’
Node Cover of Size 2 with Vertex {4,5}