0% found this document useful (0 votes)
0 views21 pages

Introduction to Algorithms M Tech

The document provides an overview of Advanced Algorithms, focusing on algorithm fundamentals, design, and analysis. It outlines key characteristics of algorithms, such as input, output, definiteness, effectiveness, and finiteness, and discusses time complexity with examples. The analysis differentiates between prior and posterior methods, using sorting algorithms as a comparison to illustrate efficiency.

Uploaded by

kumarrahul572000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views21 pages

Introduction to Algorithms M Tech

The document provides an overview of Advanced Algorithms, focusing on algorithm fundamentals, design, and analysis. It outlines key characteristics of algorithms, such as input, output, definiteness, effectiveness, and finiteness, and discusses time complexity with examples. The analysis differentiates between prior and posterior methods, using sorting algorithms as a comparison to illustrate efficiency.

Uploaded by

kumarrahul572000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Advanced Algorithms (MTCS 102)

UNIT-1
Algorithm Fundamentals

From:
Asif Khan
Department of CSE
GNIOT, Greater Noida

Reference:
Thomas H. Corman et al.
The MIT Press
Algorithms

What is an algorithm:
“ An algorithm is a set of steps or instructions to
solve any problem”

Our subject Advanced Algorithms will cover the


following :
1. Design of algorithms.
2. Analysis of Algorithms.
3. Design of Parallel Algorithms
Design of an Algorithm

When we design an algorithm, we should put in


our mind following characteristics of
algorithms:
1. Input: An algorithm should have zero or more
well defined inputs.
2. Output: An algorithm should have one or more
well defined outputs.
3. Definiteness: Every step must be definite ie
clear and unambiguous.
Design of an Algorithm

4. Effectiveness: All the steps must be feasible


with the available resources.
5. Finiteness: Algorithm must terminate after a
finite number of steps.
Analysis of an Algorithm

We may design more then one algorithms to


solve any problem. To compare these
algorithms, we nee to analyze these algorithm
on the basis of their efficiency.
Analysis may be of two types:
1. Priory : Analysis before executing the code. It
is independent of machine or hardware.
2. Posteriori : Analysis after executing the code. It
is dependent of machine or hardware.
Analysis of an Algorithm

Let we compare two algorithms of sorting –


Insertion Sort and Merge Sort. Let the time
taken by these algorithms is c1n2 and c2nlgn
respectively. Here c1 and c2 are constants.
Insertion Sort typically has smaller constant
factor so c1<c2.
Analysis of an Algorithm

Insertion Sort Merge Sort


Hardware Machine A (Fast) Machine B(Slow)
Speed 10 B inst/sec 10 M inst/sec
constant c1=2 c2=50

Time 2. (107)2 / 1010 50. 107 .lg107 / 107


=20000 sec (more =1163 sec (less than
than 5.5Hrs) 20 min)
Time Complexity of an Algorithm
Time complexity of an algorithm is represented by a
function in terms of n i.e. f(n). Where n is the number
of inputs.
Time Complexity actually represent the rate of growth
of time needed to execute the algorithm, when we
increase the number of inputs n.
For different algorithms this rate of growth of time is
different.
The slower the rate of growth of time, better the
algorithm.
To compare different algorithms we will calculate time
complexity of the algorithms.
Time Complexity of an Algorithm

30

25

20
Y-Values
15
Column1
10 Column2
5

0
0 1 2 3 4
Time Complexity of an Algorithm
Example 1: Algorithm to find out the sum of an array of
length n.

Sum(A, n)
1. s= 0;
2. for ( i=1; i<=n; i++)
3. s= s+ A[i];
4. return (s);
Time Complexity of an Algorithm
Tabulation Method
s/e frequency steps
Sum(A, n)
s= 0; 1
for ( i=1;i<=n;i++) 1

s= s+ A[i] 1

return (s); 1
Time Complexity of an Algorithm
Tabulation Method
s/e frequency steps
Sum(A, n)
s= 0; 1 1
for ( i=1;i<=n;i++) 1 n+1

s= s+ A[i] 1 n

return (s); 1 1
Time Complexity of an Algorithm
Tabulation Method
s/e frequency steps
Sum(A, n)
s= 0; 1 1 1
for ( i=1;i<=n;i++) 1 n+1 n+1

s= s+ A[i] 1 n n

return (s); 1 1 1
Time Complexity of an Algorithm
Tabulation Method
s/e frequency steps
Sum(A, n)
s= 0; 1 1 1
for ( i=1;i<=n;i++) 1 n+1 n+1

s= s+ A[i] 1 n n

return (s); 1 1 1
Total Steps 2n+3
Time Complexity of an Algorithm
Time needed to execute the code t = k * (2n + 3)
where k is the time required to execute one statement
So t α (2n + 3)
as 3 is negligible for higher values of n
So t α 2n
So t α n
So T(n) = θ(n)
Time Complexity of an Algorithm
Example 2: Algorithm to find out factorial of n.

Fact( n )
1. if n= 0
2. return (1)
3. else
4. return n * Fact(n-1);
Time Complexity of an Algorithm
s/e frequency steps

Fact( n )
if n= 0 1
return (0); 1

else 0

return n * T(n-1) +1
Fact(n-1);
Time Complexity of an Algorithm
s/e frequency steps
n=0 n>0
Fact( n )
if n= 0 1 1 1
return (0); 1 1 0

else 0 - -

return n * T(n-1) +1 0 1
Fact(n-1);
Total
Steps
Time Complexity of an Algorithm
s/e frequency steps
n=0 n>0 n=0 n>0
Fact( n )
if n= 0 1 1 1 1 1
return (0); 1 1 0 1 0

else 0 - - 0 0

return n * T(n-1) +1 0 1 0 T(n-1) +1


Fact(n-1);
Time Complexity of an Algorithm
s/e frequency steps
n=0 n>0 n=0 n>0
Fact( n )
if n= 0 1 1 1 1 1
return (0); 1 1 0 1 0

else 0 - - 0 0

return n * T(n-1) +1 0 1 0 T(n-1) +1


Fact(n-1);
Total 2 T(n-1) +2
Steps
THANK YOU

You might also like