Running Time
Analysis of Algorithms
An algorithm is a step-by-step procedure for solving a problem in a
finite amount of time.
Input Algorithm Output
What is algorithm analysis ? Two aspects:
Running time – How much time is taken to complete the algorithm
execution?
Storage requirement – How much memory is required to execute the
program?
Running Time
• Different Algorithms for the same problem can have different
“Running Times”
• Different inputs of the same size may result in different running times
• Running Time Analysis is based on count of the number of the
primitive operations to be executed
• Algorithm analysis does not give us the exact running time in seconds.
As the execution time duration is dependent on the machine to be
used. We only model Time in terms of input size.
Running Time Representation
• We represent running time as a function of the input size ‘N’ as T(N)
O(3N-7), O(2 N2 ), O(N3 + N2)
• ‘N’ represents input data size
• We compare “running time” of different algorithms in terms of these
Running Time Representations.
How Is An Algorithm Analysed?
• Experimental Analysis
• Theoretical analysis
Theoretical Analysis
• Uses description of the algorithm instead of an implementation
• Characterizes running time as a mathematical function of the input
size n.
• Takes into account all possible inputs
• Best and Worst cases
• Allows us to evaluate an algorithm independent of the
hardware/software environment (uses RAM model)
• Uses Mathematics
Running Time T(N)
• Running time expression is the number of primitive operations (steps)
executed.
• To simplify the process, the running time is calculated on the basis of
every statement instead of primitive operations involved in each
expression.
Calculate Running time: Example 1
Sigle Loop:
for (int i = 1; i <= n; i++) { Time complexity: O ( n )
// code
}
Nested Loop
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) { Time complexity: O( n² )
// code
}
}
Calculate Running time: Example 1
Algo Sum(A,n){
S=0;
for (i = 0; i <n; i++) {
S=S+A[i];
}
Return S;
}
Calculate Running time: Example 1
Algo Add(A,B,n)
{
S=0;
for (i = 0; i < n; i++)
{
for(j=0;j<n;j++){
c[i][j]= A[i][j]+ B[i][j];
}
}
}
Calculate Running time: Example 1
Algo Mul(A,B,n)
{
S=0;
for (i = 0; i < n; i++)
{
for(j=0;j<n;j++){
c[i][j]=0;
for(k=0;k<n;k++){
c[i][j]= c[i][j]+ A[i][j]* B[i][j];
}
}
}
}
Calculate Running time: Example 1
Calculate Running time: Example 2
Calculate Running time: Example 3
Calculate Running time: Example 4
• For i=1 to n do -------- n+1
{
for j= 1 to i do {
x=x+1; --------- n(1+2+3+----+n)
Since; 1+2+3+……+n= n(n+1)/2
1+n(n+1)/2
O(n²)
}
}
Calculate Running time: Example 4
i j code
• for (int i = 1; i <= n; i=i+2) { F(n)=n/2
0 × 0
// code
} 1 0 1
1×
• for (int i = 0; i <= n; i++) {
1+2+3+……+n= n(n+1)/2 2 0 2
for (int j = 0; j <= n; j++) { 1
O(n²) 2×
// code 3 0 3
1
} 2
3×
}
Calculate Running time: Example 5
• P=0; F(n)= √’n i p
1+2+3+……+k= k(k+1)/2
for (int i = 1; p <= n; i++) { Assume: p>n 1 0+1
Since, p=k(k+1)/2 > n
p=p+i k² > n 2 1+2
}
3 1+2+3+4
Calculate Running time: Example 6
F(n)= logn i
• for (int i = 1; i< n; i=i*2) { 1
Assume: i>=n
Since, i= 2k 1*2 =2
//code
2k>=n
} K=logn n
2*2 =22
2
22*2=23
• for (int i = n; i>=1 ; i=i/2) {
//code ?
}
Calculate Running time: Example 7
• for ( i = 0; i <= n; i++) { n
for ( j = 1; j <= n; j=j*2) { n*logn
// code n*logn
}
T(n) =?
} O(?)
Order of Growth:
• A time complexity does not tell us the exact number of times the
code inside a loop is executed, but it only shows the order of
magnitude.
• It is an abstraction to ease the comparison of algorithms. In growth
rate we measure an algorithm’s time requirement as a function of the
problem size ‘n’.
• The most important factor to know is how quickly the algorithm’s
time requirement grows as a function of the problem size.
Order of Growth:
• An algorithm’s proportional time requirement is known as Growth
Rate. We can compare the efficiency of two algorithms by comparing
their Growth Rates only.
• Some Common growth rates:
A Comparison of Growth-Rate Functions
A Comparison of Growth-Rate Functions
Next Home Task
• Insertion Sort
• Selection Sort
• Bubble Sort
• Linear Search
• Binary Search