Complexity
Complexity
2
C ONTENTS
Introduction
Complexity Classes
Complexity of Algorithms
Time Complexity
Algorithm Analysis
Asymptotic Notations
Complexity of Problems
COMPLEXITY 23-NOV-24
3
I NTRODUCTION
Complexity
The state of being formed of many parts; the state of being difficult to
understand (Oxford Advanced Learner's Dictionary)
COMPLEXITY 23-NOV-24
4
C OMPLEXITY C LASSES
COMPLEXITY 23-NOV-24
5
T YPES OF C OMPLEXITY C LASSES
P Class
NP Class
CoNP Class
NP-hard
NP-complete
COMPLEXITY 23-NOV-24
6
P C LASS
The P stands for Polynomial Time. It is the collection of decision problems
(problems with a “yes” or “no” answer) that can be solved by a deterministic
machine in polynomial time.
Features:
The solution to P problems is easy to find.
P is often a class of computational problems that are solvable and tractable.
Tractable:- the problems can be solved in theory as well as in practice.
Intractable:- the problems that can be solved in theory but not in practice.
Examples:
Calculating the greatest common divisor, Finding a maximum matching,
Linear programming and Merge Sort
COMPLEXITY 23-NOV-24
7
NP C LASS
COMPLEXITY 23-NOV-24
8
C O NP C LASS
Complement of NP Class.
If the answer to a problem in Co-NP is No, then there is proof that can be
checked in polynomial time.
Features:
If a problem X is in NP, then its complement X’ is in CoNP.
For an NP and CoNP problem, there is no need to verify all the answers at
once in polynomial time, there is a need to verify only one particular answer
“yes” or “no” in polynomial time.
Examples:
To check prime number and Integer Factorization.
COMPLEXITY 23-NOV-24
9
NP- HARD
An NP-hard problem is at least as hard as the hardest problem in NP and it is a
class of problems such that every problem in NP reduces to NP-hard.
Features:
All NP-hard problems are not in NP.
It takes a long time to check them.
If a solution for an NP-hard problem is given then it takes a long time to check
whether it is right or not.
A problem A is in NP-hard if, for every problem L in NP, there exists a
polynomial-time reduction from L to A.
Examples:
Halting problem and No Hamiltonian cycle.
COMPLEXITY 23-NOV-24
10
NP- COMPLETE
COMPLEXITY 23-NOV-24
11
I NTRODUCTION
Complexity of Algorithms
Complexity of Problems
COMPLEXITY 23-NOV-24
C OMPLEXITY OF A LGORITHMS
COMPLEXITY 12 23-NOV-24
13
C OMPLEXITY OF A LGORITHMS
COMPLEXITY 23-NOV-24
14
C OMPLEXITY OF A LGORITHMS
Complexity
Time Complexity – The amount of time required to execute an
algorithm
Space Complexity – The amount of memory required to execute an
algorithm.
Often there is a space/time trade-off.
We are considering Time Complexity
COMPLEXITY 23-NOV-24
15
T IME C OMPLEXITY
COMPLEXITY 23-NOV-24
16
T IME C OMPLEXITY
COMPLEXITY 23-NOV-24
17
T IME C OMPLEXITY
Many algorithms consist of a basic loop which is executed for each item of
the input.
As the input size grows, the number of instructions carried out in the loop
far exceeds those before and after the loop.
To simplify the calculations, do not count instructions outside the basic
loop.
Do we need to count all instructions inside the loop?
COMPLEXITY 23-NOV-24
18
T IME C OMPLEXITY
The number of instructions inside the loop for any two algorithms to solve
a specific problem does not vary widely !
What does differ is the number of times the loop is executed.
So, count the number of times the loop is executed.
COMPLEXITY 23-NOV-24
19
T IME C OMPLEXITY
In a sorting algorithm, the loop compares the current element of the list
with the item to be placed.
In this case it is the number of comparisons that we count
The number of comparisons depends on the size of the list.
Express the number of comparisons in terms of the list length.
COMPLEXITY 23-NOV-24
20
T IME C OMPLEXITY
Consider two algorithms A and B that solve the same class of problems.
The time complexity of A is 5,000n, the one for B is 1.1n for an input with
n elements.
For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior
to A.
For n = 1000, however, A requires 5,000,000 steps, while B requires
2.51041 steps.
COMPLEXITY 23-NOV-24
21
T IME C OMPLEXITY
COMPLEXITY 23-NOV-24
22
I NSERTION S ORT A LGORITHM
8 5 2 6 9 4 6
5 8 2 6 9 4 6
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
23
I NSERTION S ORT A LGORITHM
5 8 2 6 9 4 6
2 5 8 6 9 4 6
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
24
I NSERTION S ORT A LGORITHM
2 5 8 6 9 4 6
2 5 6 8 9 4 6
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
25
I NSERTION S ORT A LGORITHM
2 5 6 8 9 4 6
2 5 6 8 9 4 6
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
26
I NSERTION S ORT A LGORITHM
2 5 6 8 9 4 6
2 4 5 6 8 9 6
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
27
I NSERTION S ORT A LGORITHM
2 4 5 6 8 9 6
2 4 5 6 6 8 9
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
28
I NSERTION S ORT A LGORITHM
2 4 5 6 6 8 9
We’re done!
Sorted portion of the list First element in unsorted portion of the list
COMPLEXITY 13-FEB-14
29
A LGORITHM A NALYSIS T IME !
INSERTION-SORT(A) cost times
1 for j ← 2 to length[A] c1 n
2 do key ← A[ j ] c2 n−1
3 Insert A[ j ] into the sorted
sequence A[1 . . j − 1]. 0 n−1
4 i←j−1 c4 n−1
n
5 while i > 0 and A[i ] > key c5 t j =2
j
n
6 do A[i + 1] ← A[i ] c6 (t
j =2
j − 1)
n
7 i ←i − 1 c7 (t
j =2
j − 1)
COMPLEXITY
8 A[i + 1]← key c8 n−1 23-NOV-24
30
R UNNING T IME
+ c6 (t j − 1) + c7 (t j − 1) + c8(n − 1)
j =2 j =2
.
COMPLEXITY 23-NOV-24
31
R UNNING T IME
COMPLEXITY 23-NOV-24
33
A QUICK LOOK AT S PACE C OMPLEXITY
COMPLEXITY 23-NOV-24
34
W HY IS TIME COMPLEXITY IMPORTANT ?
COMPLEXITY 23-NOV-24
35
A SYMPTOTIC E FFICIENCY
COMPLEXITY 23-NOV-24
36
A SYMPTOTIC E FFICIENCY
COMPLEXITY 23-NOV-24
37
A SYMPTOTIC N OTATIONS
COMPLEXITY 23-NOV-24
38
A SYMPTOTIC N OTATIONS
COMPLEXITY 23-NOV-24
39
B IG Θ N OTATION
Let f and g be functions from the integers or the real numbers to the real
numbers.
Θ(g(n)) = {f(n) : there exist positive constants c1, c2, and n0 such that 0 ≤
c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0}.
COMPLEXITY 23-NOV-24
40
B IG Θ N OTATION
COMPLEXITY 23-NOV-24
41
B IG O N OTATION
COMPLEXITY 23-NOV-24
42
N OTATIONAL I SSUES
COMPLEXITY 23-NOV-24
43
B IG O N OTATION
Example:
Show that f(x) = x2 + 2x + 1 is O(x2).
f(x) is O(x2).
COMPLEXITY 23-NOV-24
44
B IG O N OTATION
COMPLEXITY 23-NOV-24
45
I NTUITIVE N OTION OF B IG -O
domain – [0,2]
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
COMPLEXITY 23-NOV-24
46
I NTUITIVE N OTION OF B IG -O
domain – [0,5]
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
COMPLEXITY 23-NOV-24
47
I NTUITIVE N OTION OF B IG -O
y=x3
y=x2
y=x
COMPLEXITY 23-NOV-24
48
I NTUITIVE N OTION OF B IG -O
domain – [0,100]
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
COMPLEXITY 23-NOV-24
49
I NTUITIVE N OTION OF B IG -O
y = 5x 3
y = 3x 3+5x 2 –9
y=x2
y=x
COMPLEXITY 23-NOV-24
50
B IG O N OTATION
Yes. x3 grows faster than x2, so x3 grows also faster than f(x).
Therefore, we always have to find the smallest simple function g(x) for which
f(x) is O(g(x)).
COMPLEXITY 23-NOV-24
51
U SEFUL R ULES FOR B IG -O
For any polynomial f(x) = anxn + an-1xn-1 + … + a0, where a0, a1, …, an are real
numbers, f(x) is O(xn).
If f1(x) is O(g1(x)) and f2(x) is O(g2(x)), then
(f1 + f2)(x) is O(max(g1(x), g2(x)))
If f1(x) is O(g(x)) and f2(x) is O(g(x)), then
(f1 + f2)(x) is O(g(x)).
If f1(x) is O(g1(x)) and f2(x) is O(g2(x)), then
(f1f2)(x) is O(g1(x) g2(x)).
COMPLEXITY 23-NOV-24
52
B IG Ω N OTATION
COMPLEXITY 23-NOV-24
53
B IG Ω N OTATION
COMPLEXITY 23-NOV-24
54
B IG Ω AND B IG Θ
COMPLEXITY 23-NOV-24
55
L ITTLE O N OTATION
COMPLEXITY 23-NOV-24
56
L ITTLE ω N OTATION
COMPLEXITY 23-NOV-24
57
T IME C OMPLEXITY, THE BIGGER PICTURE .
One of the big questions in Computer Science right now is the finding a
way to determine if an NP-Complete problem can be computed in
polynomial time.
NP-Complete problems are problems that cannot, to our knowledge, be
solved in polynomial time, but whose answer can be verified in polynomial
time.
COMPLEXITY 23-NOV-24
58
B IG -O: A G RAIN OF S ALT
Big-O notation gives a good first guess for deciding which algorithms are
faster.
But, the guess isn’t always correct.
Consider n 6 vs. 1000n 5.9.
Asymptotically, the second is better. But…
COMPLEXITY 23-NOV-24
59
B IG -O: A G RAIN OF S ALT
Running-time
In days
Assuming each operation
T(n) = takes a nano-second, so
1000n 5.9 computer runs at 1 GHz
T(n) = n 6
Input size n
COMPLEXITY 23-NOV-24
60
B IG -O: A G RAIN OF S ALT
COMPLEXITY 23-NOV-24
C OMPLEXITY OF P ROBLEMS
COMPLEXITY 61 23-NOV-24
62
A LGORITHM VS . P ROBLEM C OMPLEXITY
COMPLEXITY 23-NOV-24
63
T HE U PPER B OUND
Defined by an algorithm
Defines that we know we can do at least this good
Perhaps we can do better
Lowered by a better algorithm
“For problem X, the best algorithm was O(N3), but my new algorithm is
O(N2).”
COMPLEXITY 23-NOV-24
64
T HE L OWER B OUND
Defined by a proof
Defines that we know we can do no better than this
It may be worse
Raised by a better proof
“For problem X, the strongest proof showed that it required O(N), but
my new, stronger proof shows that it requires at least O(N2).”
COMPLEXITY 23-NOV-24
65
U PPER AND L OWER B OUNDS
The Upper bound is the best algorithmic solution that has been found for
a problem.
“What’s the best that we know we can do?”
COMPLEXITY 23-NOV-24
66
C HANGING THE B OUNDS
Lowered by better
Upper bound algorithm
Raised by better
Lower bound proof
COMPLEXITY 23-NOV-24
67
O PEN P ROBLEMS
Unknown
Raised by better
Lower bound proof
COMPLEXITY 23-NOV-24
68
C LOSED P ROBLEMS
Lower bound
COMPLEXITY 23-NOV-24
69
C LOSED P ROBLEMS
COMPLEXITY 23-NOV-24
70
T RACTABLE VS . I NTRACTABLE
Problems are tractable if the upper and lower bounds have only
polynomial factors.
O (log N)
O (N)
O (NK) where K is a constant
Problems are intractable if the upper and lower bounds have an
exponential factor.
O (N!)
O (NN)
O (2N)
COMPLEXITY 23-NOV-24
71
R EFERENCES
COMPLEXITY 23-NOV-24
72
COMPLEXITY 23-NOV-24