Time and Space Complexity
Time and Space Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 1 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 2 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 3 / 50
Time and Space Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 4 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 5 / 50
Time Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 6 / 50
Time Complexity
1: x ←x +1 ▷ Frequency count is 1
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 7 / 50
Time Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 8 / 50
Time Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 10 / 50
Space Complexity
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 11 / 50
Space Complexity
We can solve the problem without consuming any extra space, hence the
space complexity is constant.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 12 / 50
Space Complexity
n, sum and i take constant sum of 3 units, but the variable A is an array,
it’s space consumption increases with the increase of input size n.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 13 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 14 / 50
Basics
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 15 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 16 / 50
Asymptote
A ’Line’ that continually approaches a given curve but does not meet
it at any finite distance.
The term asymptotic means approaching a value or curve arbitrarily
closely (i.e., as some sort of limit is taken).
A line or a curve A that is asymptotic to given curve C is called the
asymptote of C.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 17 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 18 / 50
O (Big-Oh) notation
Big-Oh is used as a tight upper-bound on the growth of an
algorithm’s effort (this effort is described by the function f(n)).
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is O(g(n)) or f(n) ∈ O(g(n)), if
there exists a real constant c > 0 and there exists an integer constant
n0 ≥ 1 such that f(n) ≤ cg(n) for every integer n ≥ n0 .
In other words O(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 20 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 21 / 50
Ω (Big-Omega) notation
Big-Omega (Ω) is the tight lower bound notation.
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is Ω(g(n)) or f(n) ∈ Ω(g(n)) if
there exists a real constant c > 0 and there exists an integer constant
n0 ≥ 1 such that f(n) ≥ cg(n) for every integer n ≥ n0 .
In other words Ω(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 22 / 50
Ω (Big-Omega) notation
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 23 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 24 / 50
o (Little-Oh) notation
Little-oh (o) is used as a loose upper-bound on the growth of an
algorithm’s effort (this effort is described by the function f(n)).
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is o(g(n)) or f(n) ∈ o(g(n)) if for
any real constant c > 0, there exists an integer constant n0 ≥ 1 such
that f(n) < cg(n) for every integer n ≥ n0 .
In other words o(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤ f(n) < cg(n) for all n ≥ n0 }.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 26 / 50
ω (Little-Omega) notation
Little Omega (ω) is used as a loose lower-bound on the growth of
an algorithm’s effort (this effort is described by the function f(n)).
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is ω(g(n)) or f(n) ∈ ω(g(n)) if for
any real constant c > 0, there exists an integer constant n0 ≥ 1 such
that f(n) > cg(n) for every integer n ≥ n0 .
In other words ω(g(n)) = {f(n): there exist positive constants c and
n0 such that 0 ≤ cg(n) < f(n) for all n ≥ n0 }.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 28 / 50
θ (Theta) notation
Let f(n) and g(n) be functions that map positive integers to positive
real numbers. We say that f(n) is θ(g(n)) or f(n) ∈ θ(g(n)) if and
only if f(n) ∈ O(g(n)) and f(n) ∈ Ω(g(n))
θ(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 }
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 29 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 30 / 50
Limit Definition
f (n)
1 if f (n) ∈ O(g (n)) then limn→∞ g (n) ∈ [0, ∞)
2 if f (n) ∈ o(g (n)) then limn→∞ gf (n)
(n) = 0
3 if f (n) ∈ Ω(g (n)) then limn→∞ gf (n)
(n) ∈ (0, ∞]
f (n)
4 if f (n) ∈ ω(g (n)) then limn→∞ g (n) =∞
5 if f (n) ∈ θ(g (n)) then limn→∞ gf (n)
(n) ∈ (0, ∞)
Examples
2
1. n2 − 2n + 5 ∈ O(n3 ) ⇔ limn→∞ n −2n+5
n3
= limn→∞ n1 − n22 + n53 = 0
2
2. n2 + 1 ∈ Ω(n) ⇔ limn→∞ n n+1 = ∞
2
3. n2 + 3n + 4 ∈ θ(n2 ) ⇔ limn→∞ n +3n+4
n2
= limn→∞ (1 + n3 + n42 ) = 1
4. 7n + 8 ∈ o(n2 ) ⇔ limn→∞ 7n+8n2
= limn→∞ ( n7 + n82 ) = 0
4n+6
5. 4n + 6 ∈ ω(1) ⇔ limn→∞ 1 = ∞
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 31 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 32 / 50
Growth of Functions
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 33 / 50
Growth of Functions
n 10n n2 /2
1 10 0.5
5 50 12.5
10 100 50
15 150 112.5
20 200 200
25 250 312.5
30 300 450
For n≤20, algorithm two had a smaller computing time, but once past
that point, algorithm one became better. This shows why we chose the
algorithm with the smaller order of magnitude.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 34 / 50
Growth of Functions
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 35 / 50
Growth of Functions
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 36 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 37 / 50
Types of Time Complexities
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 38 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 39 / 50
Rules for Complexity Analysis
Rule 1: Sequence
The worst case running time of a sequence of C statements such as
statement 1;
statement 2;
statement 3;
.
.
.
statement m;
is O(max(T1 (n), T2 (n), ...Tm (n))), where running time of Si , the ith
statement in the sequence, is O(Ti (n))
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 40 / 50
Rules for Complexity Analysis
Rule 2: Iteration
The worst case running time of a C for loop such as
for(statement 1; statement 2; statement 3)
statement 4
is O(max(T1 (n), T2 (n)(I(n)+1), T3 (n)I(n), T4 (n)I(n))), where the
running time of statement Si is O(Ti (n)), for i=1,2,3 and 4, and I(n) is
the number of iterations executed in the worst case.
Rule 2: Selection
The worst care running time of a C if- else such as
if (statement 1) statement 2;
else statement 3;
is O(max(T1 (n), T2 (n), T3 (n))), where the running time of statement Si ,
is O(Ti (n)), for i= 1,2 and 3.
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 41 / 50
Rules for Complexity Analysis
Algorithm 4 Prefix-sum
1: procedure prefix-sum(A, n)
2: for i ← n − 1 to 0 do
3: sum ← 0
4: for j ← 0 to i do
5: sum ← sum + A[j]
6: end for
7: A[i] ← sum
8: end for
9: end procedure
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 42 / 50
Rules for Complexity Analysis
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 43 / 50
Outline
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 44 / 50
Space Complexity Analysis
Example 1
In Algorithm 2, the variable n occupies a constant 4 Bytes of
memory. The function call and return statement come under the
auxiliary space and let’s assume 4 Bytes all together.
The total space complexity is 8 Bytes. Algorithm 2 has a space
complexity of O(1).
Example 2
In Algorithm 3, the variables n, sum, and i occupy a constant 12
Bytes of memory. The function call, initialisation of the for loop
and write function all come under the auxiliary space and let’s
assume 4 Bytes all together.
The total space complexity is 4n + 16 Bytes. Algorithm 3 has a
space complexity of O(n).
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 45 / 50
Space Complexity Analysis
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 48 / 50
Summary
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 49 / 50
For Further Reading I
Dr. Ashutosh Satapathy Time and Space Complexity September 25, 2022 50 / 50