Algorithem Chapter One
Algorithem Chapter One
Input size (x = n) 16
Running Times for Large Inputs
Running time
Input size (x = n) 17
Asymptotic notation/ Growth of function
- It is a way to describe the characteristics of a function in the limit.
- It describes the rate of growth of functions.
- Focus on what’s important by abstracting away low-order terms and
constant factors.
- It is a way to compare “sizes” of functions
There are five notations used to describe a running time function. These are:
✓ Big-Oh Notation (O) >= asymptotic upper bound for f(n)
✓ Big-Omega Notation (Ω )<= asymptotic lower bound for f(n)
✓ Theta Notation (𝚹) = can be big-O or Big- Ω -> asymptotic tight
bound for f(n)
✓ Little-o Notation (o) >
✓ Little-Omega Notation () <
Cont…
Big Oh notation
A function f(n) is said to be O(g(n)) if there exist some constant
c0>0 and n0 ≥ 0 such that f(n) ≤ c0.g(n) for all n ≥ n0
Big Omega notation
A function f(n) is said to be Ω(g(n)) if there exist some constant
c0>0 and n0 ≥ 0 such that f(n)≥c0.g(n) for all n ≥ n0
Big Theta Notation
A function f(n) is said to be Ω(g(n)) if there exist some constant
C0 ,C1 >0 and n0 ≥ 0 such that c0.g(n) ≤ f(n)≤c1.g(n) for all n ≥ n0
Analysis Rule:
• 1. Assume an arbitrary time unit
• 2. Execution of one of the following operations takes time unit 1
– Assignment statement
– Single I/O statement;.
– Single Boolean statement.
– Single arithmetic.
– Function return.
• 3. selection statement
– Time for condition evaluation + the maximum time of from all cases
• 4. Loop statement
– Sum of(initialization time + time of condition checking+ time of action performed)+no of loop
iteration *(body of loop statement execution times)
• 5. For function call
– 1+ time(parameters) + body time
Eg1.
• int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum +(i * i * i);
return partial_sum;
}
Time Units to Compute
-------------------------------------------------
1 for the assignment.
1 assignment, n+1 tests, and n increments.
n loops of 4 units for an assignment, an addition, and two multiplications.
1 for the return statement.
-------------------------------------------------------------------
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)
Eg2.
for (int i = 1; i <=n; i++) 1 + (n+1) + n
{
for (int j = 1; j <=n; j++) n*(1 + (n+1) +1)
{
cout<<“hello world”; 1
}}