Course Code 18CSC204J_Unit1
Course Code 18CSC204J_Unit1
Dr.S.PRASANNA DEVI
Professor & Head, DCSE
SRMIST, VDP
SYLLABUS
Properties of Algorithm
• An algorithm takes zero or more inputs.
• An algorithm results in one or more outputs.
• All operations can be carried out in a finite time.
• An algorithm should be efficient and flexible.
• It should use less memory space as much as possible.
• An algorithm must terminate after a finite number of steps.
• Each step in the algorithm must be easily understood for someone
reading it.
• An algorithm should be concise and compact to facilitate verification
of their correctness.
• Worst Case:
– Neglecting the constants, the worst-base running time of 7
insertion sort is proportional to
1 2 …n 6
1 2 3 4 5 6
o – notation
This notation is used to describe the worst case analysis of algorithms and concerned with small values of n
t(n) = o(g(n)) iff
ω - notation
This notation is used to describe the best case analysis of algorithm and concerned with small values of n.
t(n) = ω(g(n)) iff
18CSC204J - UNIT 1 - Dr.S.PRASANNA
16
DEVI - SRM-VDP
Mathematical analysis
1. Let f(n) = 7n + 8 and g(n) = n. Is f(n) ∈ O(g(n))?
For 7n + 8 ∈ O(n), we have to find c and n0 such that 7n + 8 ≤ c · n, ∀n ≥ n0.
By inspection, it’s clear that c must be larger than 7. Let c = 8.
Now we need a suitable n0. In this case, f(8) = 8 · g(8). Because the definition of O() requires
that f(n) ≤ c · g(n), we can select n0 = 8, or any integer above 8 – they will all work.
We have identified values for the constants c and n0 such that 7n + 8 is ≤ c · n for every n ≥
n0, so we can say that 7n + 8 is O(n).
3. Is 7n + 8 ∈ o(n2 )?
2. Let f(n) = 7n + 8 and g(n) = n. Is f(n) ∈ o(g(n))?
In order for that to be true, for any c, we have to be able to find an n0 that makes f(n) < c · g(n)
asymptotically true.
However, this doesn’t seem likely to be true. Both 7n + 8 and n are linear, and o() defines
loose upper bounds.
To show that it’s not true, all we need is a counter–example. Because any c > 0 must work
for the claim to be true, let’s try to find a c that won’t work.
Let c = 100. Can we find a positive n0 such that 7n + 8 < 100n? Sure; let n0 = 10. Try again!
Let’s try c = 1 100 . Can we find a positive n0 such that 7n + 8 < n 100 ? No; only negative
values will work.
Therefore, 7n + 8 ∈/ o(n), meaning g(n) = n is not a loose upper-bound on 7n + 8.
Now let us prove correctness of this formula using mathematical induction as follows:
Prove M(n) = n by using mathematical induction
Basis: let n = 0 then M(n) = 0
i.e; M(0) = 0 = n
Induction : if we assume M(n-1) = n-1 then M(n) = n
M(n) = M(n-1) + 1
= n-1 + 1
=n
Thus the time complexity of factorial function is Ө(n).
Mathematical Analysis
Step 1 : The input size is ‘n’.