Asymptotic Notations
Asymptotic Notations
UCS301
Asymptotic Notations
Department of CSE
Thapar Institute of Engineering and Technology, Patiala
Algorithm and its complexity
• Amount of computational
Time Complexity time required by an algorithm
Complexity of to perform complete task.
Algorithms
• Amount of memory required
Space Complexity
by an algorithm to complete
its execution.
3 cases to analyze an algorithm
1) Worst Case Analysis (Usually Done) –
– we calculate upper bound on running time of an algorithm.
– We must know the case that causes maximum number of
operations to be executed.
3
Asymptotic Notation: Omega Notation (Ω)
• Asymptotic lower bound used to describe
best-case running times
• Let f(n) and g(n) are functions over non-
negative integers
• if there exists constants c and n0, such
that
c g(n) ≤ f(n) for n ≥ n0
• Then we can write f(n) = Ω (g(n)),
• Example f(n) = 18n+9, g(n) =18n
i.e f(n) ≥ 18n
so we can say f(n) = Ω (n) when c = 18
Example: Big-Omega
Show that: n2/2 – 3n = Ω(n2)
• Determine positive constants c1 and n0 such that
c1n2 ≤ n2/2 – 3n for all n ≥ n0
• Diving by n2
c1 ≤ 1/2 – 3/n
• For: n = 1, c1 ≤ 1/2 – 3/1 (Not Holds)
n = 2, c1 ≤ 1/2 – 3/2 (Not Holds)
n = 3, c1 ≤ 1/2 – 3/3 (Not Holds)
n = 4, c1 ≤ 1/2 – 3/4 (Not Holds)
n = 5, c1 ≤ 1/2 – 3/5 (Not Holds)
n = 6, c1 ≤ 1/2 – 3/6 (Not Holds and Equals ZERO)
n = 7, c1 ≤ 1/2 – 3/7 or c1 ≤ (7-6)/14 or c1 ≤ 1/14 (Holds for c1 ≤ 1/14)
• The inequality holds for any n ≥ 7 and c1 ≤ 1/14.
• Thus by choosing the constant c1 = 1/14 and n0 = 7, one can verify that
n2/2 – 3n = Ω(n2) holds. 34
Asymptotic Notation: Omega Notation (Ω)
• Consider f(n) = 2n2 +5, g(n) = 7n. Find some constant c such
that f(n) ≥ g(n)
• For n=0, f(n) = 0+5=5, g(n) = 0 -> f(n)>g(n)
37
ω-Notation
• ω-notation denotes a lower bound that is not
asymptotically tight.
• One way to define it is as f(n) ∈ ω(g(n)) if and only if
g(n) ∈ o(f(n)).
• Formally, ω(g(n)) (“little-omega of g of n”) is defined as the set
i=1; n+1
While (i<=n)
{ }
n+1
Do { }
While(i<=n)
Frequency Count
Example -1
Code FC Reason
for(i=1; i<=n; i++) n+1 execute 1 to n times for true and 1 more
time for false
{ - ignore
x =x +1; n execute whenever you will enter into the
loop that means only for true condition.
So loop is true from 1 to n times
} ignore
Total F(n) = =n+1+n TC = O(n)
=2n+1
i = 1 -> x = x+1, i=i+1 -> i = 2
for(i=1;i<=5;i++) i = 2 -> x = x+1, i=i+1 -> i = 3
{ i = 3 -> x = x+1, i=i+1 -> i = 4
i = 4 -> x = x+1, i=i+1 -> i = 5
x =x+1
i = 5 -> x = x+1, i=i+1 -> i = 6
} i = 6 checks condition, exits for loop
Frequency Count
Example -2
Code FC Reason
for(i=1; i<=n; i++) n+1 execute 1 to n times for true and 1 more time for
false
{ - ignore
for(i=1; i<=m; i++) n( m+1) execute 1 to m times for true and 1 more time for
false i.e m+1 for every true condition of outer loop.
So outer loop gives n times true i.e n(m+1)
{ ignore
x =x +1; nm Inner loop is true m times per true iteration of outer
loop. So outer loop is true n times so nm times this
statement will execute
}} ignore
Total F(n) = = n+1+n(m+1)+mn TC = O(mn)
= n+1+nm+n+mn
= 2nm+2n+1
Example -3
A(n)
{
for(i=1;i<=n,i++) n
for(j=1;j<=n;j++) n
printf(“hi”);
}
Assume n>=2
n= 2 4 8 …………. n
Loop will run 1 2 3 ……….. 2k
1
1. for i = 1 to N – 1 do
2. sum = 0
Option 2 is better
3. for j = 0 to i do
4. sum = sum + A[j] 2
5. A[i] = sum 1. for i = 1 to N – 1 do
2. A[i] = A[i] + A[i – 1]
Contd…
Cost Frequency
1. for i = 1 to N – 1 do c1 N
2. sum = 0 c2 N–1
3. for j = 0 to i do c3
4. sum = sum + A[j] c4
5. A[i] = sum c5 N–1
Contd… Cost Frequency
1. for i = 1 to N – 1 do c1 N
3 6 9 1 8
temp = 1
3 6 9 1 8
3 6 9 1 8
3 36 6
9 9 8
1 3 6 9 8
temp = 8
1 3 6 8 9 1 3 6 9 8
1 3 6 9 9
Algorithm insertionSort(A, N)
Input: An array A containing N elements.
Output: The elements of A get sorted in increasing order.
1. for i = 1 to N – 1 c1 N
2. temp = A[i] c2 N–1
3. j=i c3 N–1
4. while j > 0 and a[j-1] > temp c4
5. a[j] = a[j-1] c5
6. j=j–1 c6
Best case:
Worst case: