Professional Documents
Culture Documents
Unit No:1
Unit Name: Fundamental of Algorithms
Faculty Name : Dr.Vanita Mane
Ms Harsha Saxena
Ms Siddhi Kadu
.
Course Objectives
2
What we learn in this course?
3
What we learn in this course?
4
What we learn in this course?
5
What we learn in this course?
6
Course Outcome
7
Outline-Module 1
S.No Topic
4 Master Theorem
6 Amortized analysis
Lecture No: 1
Introduction to Analysis of
Algorithms
Algorithm
Set of Rules to
obtain the
Input expected Output
output from
the given input
Algorithm
Algorithm??m
•How does Google Hangouts transmit live video across
the Internet so quickly?
Algorithm??
13 Lecture No: 1 Introduction to Analysis of Algorithms
Google Map
1Truck
25 location
=24! Routes
=620,448,401,
733,239,439,
360,000 routes
1Truck
25 location
..solved using
“Nearest
insertion”
algorithm
Analysis of Algorithms
To analyze an algorithm means determining the amount of
resources (such as time and storage) needed to execute it
•Efficiency of an algorithm can be measured in terms of:
Execution time (time complexity)
Time complexity: A measure of the amount of time required to
execute an algorithm
The amount of memory required (space complexity)
Calculated by considering data and their size
S(P)=C+SP(I)
Algorithm ADD(x,n)
{
//Problem Description: The algorithm performs addition of all the elements in an array.
Array is of floating type
//Input: An array x and n is total number of element in array
// output: return sum which is of data type float.
Sum=0.0 ----------1 unit of space by Sum
for i=1 to n do ----------1 unit of space by i ----------1 unit of space by n
Sum=Sum + x[i] ----------n unit of space by x[]
return Sum
}
S(p)≥(n+3)
Let Suppose
1.Python
Intel Dual core
6GB RAM
TIME=20MIN
2.Python
Intel Quad Core
16GB RAM
TIME=7MIN
Nearest
Number of Brute force
Insertion
nodes
1 1 1
2 4 1
3 9 2
4 16 6
…
620,448,401,
25 625 733,239,439,
360,000
n n^2 n!
TP(n)=caADD(n)+csSUB(n)+cmMUL(n)+cdDIV(n)…..
Where,
n is the instance characteristics
Ca, Cm,Cs,Cd are the time for addition, multiplication,substraction, division so on..
1.Sum of 2 numbers :
Sum(a,b){
return a+b //Takes 2 unit of time(constant) one for arithmetic operation and one for
return. cost=2 no of times=1
}
Tsum= 2 = C =O(1)
2.Sum of all elements of a list :
list_Sum(A,n)
{ //A->array and n->number of elements in the array
total =0 // cost=1 no of times=1
for i=0 to n-1 // cost=2 no of times=n+1 (+1 for the end false condition)
sum = sum + A[i] // cost=2 no of times=n
return sum // cost=1 no of times=1
}
Tsum=1 + 2 * (n+1) + 2 * n + 1 = 4n + 1 =C1 * n + C2 = O(n)
1. 3n+2=O(n)
2. 5n5 +100n+6=O(n5)
3. 10n2+4n+2=O(n2)
5. log(log(n))+log(n) =O(log(n))
Let the algorithm is for sequential search and P be a probability of getting successful
search n is the total number of elements in the list.
• The first match of the element will occur at ith location. Hence probability of
occurring first match is P\n for every ith element.
• The probability of getting unsuccessful search is(1-P)
Now we can find average case
Cavg(n)=Probability of successful search + Probability of unsuccessful search
Cavg=[1.P/n+2. P/n+3. P/n……+i. P/n]+n(1-P)
=P/n [1+2+3……i+….n]+n(1-P)
=P(n+1)/2 + n(1-P)
If P=0 means no successful search
Cavg=n --> worst case
If P=1 we get a successful search
Cavg=(n+1)/2
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with the element in the second
position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the amount of order in the file
8 4 6 9 2 3 1 1 2 3 4 9 6 8
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
Algorithm SELECTION-SORT(A[0,1….n-1])
{//problem description:This algorithmsort the element using selection sort
//Input: an array of element a[0,1…n-1] that is to be stored
//output: the sorted array A[0,1,….n-1]
for i ← 0 to n – 2
do smallest ← i 8 4 6 9 2 3 1
for j ← i + 1 to n-1
do if A[j] < A[smallest]
then smallest ← j
//swap A [i] and A[smallest]
temp=A[i]
A[i]=A[smallest]
A[smallest]=temp
}
(n i 1)
n2
n2
i 0
(n i 1)
then smallest ← j c5
//swap A [i] and A[smallest]
temp=A[i] c6 n-1
A[i]=A[smallest] c7 n-1
A[smallest]=temp c8 n-1
i 0 ( n i )
n2 n2
i 0
(n i 1)
T(n)= c1n+ c2 (n-1) + c3 + (c4 +c5 ) +(c6 +c7 +c8 )n-1 =O(n²)
51 51
Lecture No: 1 Introduction to Analysis of Algorithms
Analysis of Selection Sort
(n 1)i 0 1 i 0 i
n2 n2
=
= (n-1)(n-2-0+1)-((n-2)(n-2+1)/2)
= n(n-1)/2
=O(n²)
Lecture No:2
Complexity of recursive
algorithms
Recurrence relation
What is Recurrence?
T(n) = T(n-1) + 1
A recurrence relation is an equation that defines a sequence based on a rule that gives
the next term as a function of the previous term(s). It is also defined as a function by
means of an expression that includes one or more smaller instances of itself.
Example for the recursive definition is factorial function , Fibonacci series.
https://www.coursera.org/learn/simulation-algorithm-analysis-pointers/lecture/oyhDs/recursion
n!=
{ 1
n x (n-1)!
if n=0 or n=1
n>1
T(1) = 1
T(2) = T(1) + 1 = 2
Factorial(int n)
T(3) = T(2) + 1 = 3
{ if(n==0 || n==1) .
.
return 1;
.
else T(n) = T(n-1) + 1
return n* Factorial(n-1)
}
T(n)=
{ 1
T(n-1)+1
if n=0 or n=1
n>1
Best Case, Average Case and Worst case are same in Merge sort:
T(1)=1 for n=1
T(n)= T(n/2) +T(n/2)+ Cn for n>1
T(n)=
{ 1 if n=1
2T(n/2)+cn n>1
2 1
1 x x for |x| < 1
1 x
For n=n/2
For n=n/4
Lecture No:3
Complexity of recursive
algorithms
Activity
T(n)=2T(√n/2)+log₂ n
Let, m=log₂ n n= 2m
T(2m )=2T(2m/2 )+m ------- equation 1
Let, T(2m)=S(m)
S(m)=2 S(m/2) -------putting this in equation 1 we get ------- equation 2
S(m/2)=2 S(m/4)+m/2
S(m)=2[2 S(m/4)+m/2 ] + m -------putting this in equation 2 we get
S(m) = 22 S(m/ 22 ) + 2m ------- equation 3
S(m/4)=2S(m/8)+m/4 -------putting this in equation 2 we get
S(m) = 22 [2 S(m/8)+m/4 ] +2 m
S(m)= 23 S(m/ 23 ) + 3m ------- equation 3
S(m)= 2k S(m/2k )+km ------- equation k
S(1)=1 is compare the term S(m/2k)=S(1)
m/2k=1 => m=2k => then k= log₂ m
Put the value of k in general equation
S(m)= mS(1)+mlog₂ m = m+mlog₂ m now replace S(m) with T(2m)
T(2m)= m+mlog₂ m now replace m=log₂ n and 2m = n
T(n)= log₂n+ log₂n * log₂log₂n = O(log₂n * log₂log₂n )
= n5/5
= O(n5)
Constant C
= 4k T(n/ 3k )+ Cn2
T(1)=1 is compare the term T(n/3k)=S(1)
n/3k=1 => n=3k => then k= log3 n
= 4log n T(1)+ Cn2 = nlog 4 + Cn2 = n1.26 + Cn2 =O(n2 )
3 3