Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Data Structures

Radhamadhab Dalai
Asst. Professor
CSE
BIT Mesra, Ranchi

01-09-2020 1
Algorithm: time complexity
• Algorithm is a step by step procedure to execute
a program which produces after a finite amount
of time.
• Run time analysis: It is a measure to know how
much does an algorithm or procedure take time
to finish execution. This time is dependent upon
factors like memory fetching time or cpu
execution time.
• This also shows how running time of program
depends upon input size too.
01-09-2020 2
Algorithm: time complexity
• A program is nothing but a valid set of instructions which is syntactically and semantically correct.
• void display1()
{
int a = 0;
printf(“value of a is =%d”, a );
}

• void display2()
{
int a = 0;
int I =0;
while( i < 1000)
{
printf(“value of a is =%d”, a );
i++;
}
}

01-09-2020 3
Algorithm: time complexity
• void display3()
{
int a = 0;
int I =0;
while( i)
{
printf(“value of a is =%d”, a );
i++;
}

01-09-2020 4
Algorithm: time complexity
• Efficiency of a program : algorithm analysis
• It is mainly running time but it may include
compilation time, memory time and other
linking time too.
• It is mainly cpu time or execution time.

01-09-2020 5
Algorithm: time complexity
• This processing time is directly proportional to
size of problem.
• Size of program : input size
• Size of data
• Array size, matrix size
• Vector size
• No of binary inputs or bits
• Degrees of polynomials

01-09-2020 6
Algorithm: time complexity
• While preparing for school :
• Most of time taken = bath time + breakfast time where
dressing time is negligible.
• O(1), O(n), O(n^2),O(logn)
• O(1) < O(logn) < O(n) < O(n^2) for all n > 0
• Worst case: Running time takes long amount of time of
execution.
• Best case: According to the algorithm or data structure
the execution happens in shortest possible time.
• Average Case: The average runtime of each approach.

01-09-2020 7
Best case
• Example: taking a flight to destination, train or
bus
Source
Destination

Example : Searching an item in a single linked list. Best case - O(1),


Worst case -O(n), Average case - O(n)

01-09-2020 8
Questions
• Questions ?

01-09-2020 9
Big-O Notation
• f(n) =O(g(n)) -> implies f(n) runs at a speed not
greater than degree of g(n) .Upper bound g(n)
g(n)

f(n)
F(n)

n (input size)
k0

01-09-2020 10
Omega-Ω- Notation
• f(n) = Ω(g(n))
• It implies f(n) runs at a speed not less than
f(n)
degree of g(n)
g(n)
F(n)

n (input size)
k0

01-09-2020 11
Theta-θ- Notation
• g1(n)<=f(n) <=g2(n). It implies f(n) runs at a running time of
order not less than degree of g2(n) nor greater than g1(n).
g1(n)
f(n)

g2(n)
F(n)

N -> input size


k0

01-09-2020 12
O(n)
• Run time analysis
2
f (n) = n + 4n + 7
• f(n) = O(n^2)
• Upper bound : F(n) = O(n^2)
for (i=0; i<m; i++){ // O(m)
for (j=0;j<n;j++){ // O(n)
printf(“%d, %d”, i, j); // O(1)
}
}
• f(n) = O(mn)

01-09-2020 13
O(n) : Example
• Find upper bound for sum of natural numbers
for a given input n.
• int n=1000, i =1, sum = 0;
while (i <= n)
{
sum = sum + i;
i++;
}

01-09-2020 14
O(n) Part-2
• For (i = 0; j= m; i < m; i++,j--){ // O(?)
temp =a; // O(1)
a= b; // O(1)
b= temp; // O(1)

}
}
• F(n) = O(m ^2 ) Correct?

01-09-2020 15
Recursive expression : O(n)
• Recursive expression (Fibonacci, gcd, factorial or random function)
• T(n) =T(n-1) +1
• Where T(n) is execution time for module or program block of input
size n.
• Question - Find upper bound.
• T(n-1) = T(n-2) +1
• T(n-2) = T(n-3) +1
• Similarly continuing T(n) = 1 + T(n-1)= 1+1+T(n-2)
• T(n) = 1 + 1 + 1 + 1 + ………….+ T(0)
• Where initial condition is either given or it is assumed.
• Let us assume T(0) = k (some constant)
• T(n)= O(n)

01-09-2020 16
More Examples
• Solve for T(n) = T(n/2) + 1?
• Space complexity
• Whether any extra memory needed except
memory taken as input.
• Single byte or address allocation – O(1)
• n such allocation for n number of inputs –
O(n)

01-09-2020 17
Questions
• Questions?

01-09-2020 18

You might also like