Unit 5
Unit 5
Time complexity refers to the computational complexity that describes the amount of time an
algorithm takes to complete as a function of the input size (denoted as n). It is expressed using
Big O notation (e.g., O(n), O(n²)) to classify algorithms by their growth rates.
Measuring Complexity
Measuring complexity involves analyzing the number of basic operations an algorithm performs.
The key types are:
The Class P
Class P consists of decision problems (yes/no questions) that can be solved by a deterministic
Turing machine in polynomial time, i.e., time bounded by a polynomial function of the input
size (like O(n), O(n²), etc.). These are considered efficiently solvable problems.
A decision problem belongs to the class P if there exists a deterministic algorithm (like one that
runs on a typical computer) that solves the problem in polynomial time, i.e., time T(n) = O(n^k)
for some constant k, where n is the size of the input.
It contains all decision problems (i.e., problems with yes/no answers) that can be solved
by a deterministic Turing machine in polynomial time.
In simple terms, these are the problems for which an efficient algorithm exists.
The Class NP
Class NP includes decision problems for which a solution can be verified in polynomial time by
a deterministic Turing machine. It also includes problems solvable in polynomial time by a non-
deterministic Turing machine.
Class NP stands for Nondeterministic Polynomial time. It refers to a class of decision problems
(problems with "yes" or "no" answers) for which:
If the answer is “yes”, there exists a certificate (or solution) that can be verified in
polynomial time using a deterministic Turing machine.
In other words, NP problems may not be easy to solve, but if you are given a solution, you can
verify its correctness quickly (in polynomial time).
NP-Completeness
1. It is in NP.
2. Every problem in NP can be reduced to it in polynomial time.
These are the hardest problems in NP; solving one NP-complete problem in polynomial time
implies P = NP.
Unsolvable Problems
Unsolvable problems are those that no algorithm can solve for all possible inputs. These
problems are undecidable. Example: The Halting Problem, where it's impossible to determine
whether an arbitrary program halts or runs forever.
The Subset Sum Problem is a classic NP-complete decision problem. It can be stated as:
Given a set of positive integers S={s1,s2,...,sn}S = {s_1, s_2, ..., s_n}S={s1,s2,...,sn} and a
target integer T,
Does there exist a subset of S such that the sum of the elements in the subset equals T?
Why Is It in NP?
Why Is It NP-Complete?
The Hamiltonian Path Problem is a famous NP-complete problem in graph theory. It can be
defined as:
Why Is It NP-Complete?
The Hamiltonian Path problem was proven to be NP-complete through reduction from the
Hamiltonian Cycle problem (which is also NP-complete).
Why Is It NP-Complete?
A problem is said to be decidable if there exists an algorithm (or Turing machine) that always
halts and gives a correct YES/NO answer for every possible input instance of the problem.
A problem is undecidable if no such algorithm exists that can always give a correct answer and
halt in a finite amount of time.
Intractable problems are theoretically solvable (if decidable), but too slow to be used directly
for large inputs.
Cook's Theorem:
Cook’s Theorem is a fundamental result in computational complexity theory that established the
concept of NP-completeness.
In computational complexity theory, the Cook–Levin theorem, also known as Cook's theorem,
states that the Boolean satisfiability problem is NP-complete. That is, it is in NP, and any
problem in NP can be reduced in polynomial time by a deterministic Turing machine to the
Boolean satisfiability problem.