You are on page 1of 69

PRESIDENCY COLLEGE

Presidency College
(Autonomous)
(Autonomous)

UNIT 1: Analysis Techniques


Chapter 1: Introduction to Analysis of
Algorithms
Reaccredited by
NAAC with A+ By
VEENA S BADIGER, Asst Professor
B.E, M.Tech
Department of Computer Applications

Presidency
Group
Presidency College
(Autonomous)
Definition

• An algorithm is defined as finite sequence of


unambiguous instructions followed to
accomplish a given task.
Reaccredited by • Step by step procedure to solve a given
problem in finite number of steps by accepting
NAAC with A+

a set of inputs and producing the desired


output.
Presidency
Group
Characteristics of algorithm
• Input : each algorithm should have zero or more
Presidency College
(Autonomous)

inputs. The range of inputs for which algorithm


works should be satisfied.
• Output : the algorithm should produce correct
results. At least one output has to be produced.
• Definiteness : each instruction should be clear and
Reaccredited by
NAAC with A+ unambiguous.
• Effectiveness : the instruction should be simple and
should transform the given input to the desired
Presidency output.
Group

• Finiteness : the algorithm must terminate a finite


sequence of instruction .
Notion of algorithm
Presidency College

• Algorithm should not be ambiguous.


(Autonomous)

• The range of inputs for which an algorithm


works has to be specified carefully.
• The same algorithm can be represented in
several different ways.
Reaccredited by
NAAC with A+
• Several algorithms for solving the same
problem on very different ideas .
• Algorithms for same problem can be based on
Presidency
very different ideas and can solve the problem
Group
with different speeds.
Algorithm control structures
Presidency College
(Autonomous)
• Conditional statement
1. If -then
2. If – else - if
• Looping statement
Reaccredited by
1. For loop
NAAC with A+
2. While loop
3. Repeat until loop
Presidency
Group
Analysis of algorithms
Presidency College
(Autonomous) • The algorithm and design process is explained by
considering the following flow chart
Understanding the problem
Data structures
algorithm design techniques
Reaccredited by
NAAC with A+
Design an algorithm

Prove algorithms correctness


Presidency

Analyze the algorithm


Group

Implement the algorithm


6
Method of specifying an algorithm
Presidency College
(Autonomous)
• Natural language
• Flow chart
• pseudo code

Reaccredited by
NAAC with A+

Presidency
Group

7
Design an algorithm
Presidency College
(Autonomous)

• Brute force
• Divide and conquer
• Decrease and conquer
• Transform and conquer
Reaccredited by
NAAC with A+ • Space and time tradeoffs
• Dynamic programming
• Backtracking
• Branch and bound
Presidency
Group

• Greedy method
8
Analysis of algorithm efficiency
Presidency College
/performance analysis
(Autonomous)

• Main purpose is to design most


efficient algorithm.
• Analysis helps in
1. select an algorithm which is more
Reaccredited by
NAAC with A+ efficient in terms of time and space.
2. Find the bottlenecks in a given
Presidency
program.
Group

3. To maintain simplicity.
4. To maintain generality.
9
Presidency College
(Autonomous)
Algorithms are analyzed in two ways

1.By checking the correctness of an


algorithm.
2.By measuring time and space
Reaccredited by
NAAC with A+
complexity of an algorithm.

Presidency
Group

10
Presidency College
Two phases of analysis of algorithm
(Autonomous)

1.Priori analysis: functions are obtained


which bounds the algorithms computing
time.
Reaccredited by 2.Posteriori analysis : actual statistics are
collected in conjunction with time and
NAAC with A+

space while executing.


Presidency
Group

11
Presidency College
Contd..
(Autonomous)

• The efficiency of an algorithm


depends on two factors
• Space efficiency
Reaccredited by
NAAC with A+
• Time efficiency

Presidency
Group

12
Presidency College
(Autonomous)
Difference between analysis and
profiling or difference between priori
analysis and posteriori analysis.

Reaccredited by
NAAC with A+

Presidency
Group

13
PRESIDENCY COLLEGE
Presidency College
(Autonomous)
(Autonomous)

Space complexity

Reaccredited by
NAAC with A+
By
Veena S Badiger
B.E, M.Tech
Presidency
Asst Professor
Group
It is the amount of a memory that may be
Presidency College
required to run a program.
(Autonomous)
Following components are important in
calculating space requirements.
1. Instruction space: space requried to store the
machine code generated by the compiler.
2. Data space: space needed for constants , static
variables , intermediate variables , dynamic
Reaccredited by
NAAC with A+
variables etc.
3. Stack space: space to store return addresses ,
return values etc .
space compelxity given as
S(p)=cp+sp [sp-> space for code segments
Presidency
Group

cp->space for activation


record]
Examples on iterative algorithms
Presidency College

Example 1:
(Autonomous)

#include <stdio.h>
Void main()
{
int x,y,z,sum;
Printf(“enter the three numbers”);
Reaccredited by
NAAC with A+ Scanf(“%d%d%d”,&x,&y,&z);

Sum=x+y+z;
Printf(“the sum =%d”,sum);
}
Presidency
Group

ANS : s(p)=4n
Presidency College
(Autonomous)

Example 2:
algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0
Reaccredited by
NAAC with A+
}

Presidency
Group ANS : s(p)= 0
Example 3:
Algorithm sum(a,n)
Presidency College
(Autonomous)

{
s=0.0;
For i=1 to n do
s=s+a[i]
Reaccredited by
NAAC with A+
Return s;
}

Presidency
Group
ANS: s(p)=n+3
Example 4:
Find min(int [] x)
Presidency College
(Autonomous)

{
int k=0; int n=x.length;
For(int i=1 ; i<n; i++)
{
Reaccredited by
NAAC with A+
if (x[i] < x[k])
K=i;
}
Presidency
Group
Return k;
}
ANS: s(p)=n+3
Example 5:
Void multvect (int [] x , int [] [] a)
Presidency College
(Autonomous)
{
int k=0;int n=x.length;
For(int i=1;i<n;i++)
{
for (int j=1 ; j<n;j++)
Reaccredited by
{
NAAC with A+
a[i][j]=x[i]*x[j]
}
}
Presidency
Group
}

ANS:s(p)= n x n+n+4
Examples for recursive algorithm
Presidency College
(Autonomous)

Example 1:
Algorithm Rsum(a,n)
{
Reaccredited by if(n<=0) then return 0.0
NAAC with A+

else return (Rsum(a,n-1)+a[n]);


}
Presidency
Group

ANS: s(p)≥3(n+1)
Presidency College
(Autonomous)

Reaccredited by
NAAC with A+

Presidency
Group
Example 2:
Algorithm fact(n)
Presidency College
(Autonomous)

{
if (n==0) then
Return 1
Reaccredited by
NAAC with A+
else
Return (n* fact(n-1))
}
Presidency
Group

ANS: s(p)≥2(n+1)
PRESIDENCY COLLEGE
Presidency College
(Autonomous)
(Autonomous)

Time complexity

Reaccredited by
NAAC with A+

Presidency
Group
Presidency College

Definition
(Autonomous)

• It is the amount of computer time


algorithm needs to run to completion.
• The T(p) taken by a program p is the sum
of the compile time and the execution
Reaccredited by time. T(p)=cp+tp
NAAC with A+
• The compile time does not depends on
the instance characteristics. Also we may
assume that a complied program will run
Presidency
Group
several times without recompilation.
• Therefore the runtime is denoted by tp.
Presidency College
(Autonomous)
Following are some standard methods of
computing the time efficiency of
algorithms:

Reaccredited by
NAAC with A+
• Operation counts
• Step counts
Presidency
• Asymptotic notations
Group
Operation counts
Presidency College
(Autonomous)

It is the simplest way to analyze .In this


method count the number of basic
operations in an algorithm .

Reaccredited by
What is basic operation?
NAAC with A+
• The operation that contributes most towards
the running time of the algorithm is called
basic operation.
Presidency
Group
• A statement that executes maximum number
of times in a function is called basic
operation.
Importance of basic operation
Presidency College
(Autonomous)
• The number of times basic operation is
executed depends on the size of the input.
• Basic operation is the time consuming
operation in the algorithm.
• The running time is given by:
Reaccredited by
NAAC with A+
T(n) ~ b*c(n)
T -> is the running time of the algorithm.
n-> the size of the input.
Presidency
Group b-> execution time for basic operation.
c-> represent number of times the basic operation is
executed.
Examples
Presidency College
(Autonomous)
 A statement present in the innermost loop.
For (i=0;i<n;i++)
if(a[i]<a[j]) --------basic operation
 Addition operation while adding two matrices.
For (i=0;i<n;i++)
for(j=0;j<n;j++)
Reaccredited by c[i][j]=a[i][j]+b[i][j]--------basic operation
NAAC with A+
 Search for a key in an array.(key comparisons)
 Sorting using bubble sort.(comparison of successive
elements) .
Presidency
 Multiplication of two matrices ,in this both
Group
multiplication and addition is performed since
multiplication takes longer this is considered to be
the basic operation.
orders of growth
Presidency College
(Autonomous)
• The behavior of some algorithm changes with
increase in value of n. the change in behavior of
the algorithm and algorithm efficiency can be
analyzed by considering the highest order of n.
• The order of growth is normally determined for
larger values of n.
Reaccredited by
NAAC with A+

• The running time of an algorithm is measured as


logarithmic , linear, quadratic , cubic ,
Presidency
Group exponential , factorial .
The order of growth is shown below by
considering the common computing time
Presidency College
(Autonomous)
N logN N NlogN N2 N3 2N N!
1 0 1 0 1 1 2 1

2 1 2 2 4 8 4 2

Reaccredited by 4 2 4 8 16 64 16 24
NAAC with A+

8 3 8 24 64 512 256 4032


0
Presidency
Group 16 4 16 64 256 4096 65536 High

32 5 32 160 1024 32768 4294967296 Very


high
• Exponential function grows very fast even for
Presidency College
small variations of N
(Autonomous)
• LogN: indicates the running time of a program
is logarithmic.
• N : indicates the running time is linear.
• NlogN : indicates running time is NlogN
• N2 : indicates running time is quadratic.
Reaccredited by
NAAC with A+ • N3 : indicates running time is cubic.
• 2N : indicates running time is exponential.
• N!: indicates running time is factorial.
Presidency
Group
• Order of growth is given as
1 < logN < N < NlogN < N2 < N3 < 2N <N!
Efficiency of an algorithm
Presidency College
(Autonomous) • Worst case : the efficiency of an algorithm for
the input of size n for which the algorithm takes
longest time to execute among all possible
inputs.
• Best case : the efficiency of an algorithm for
Reaccredited by the input of size n for which the algorithm takes
NAAC with A+
least time during execution among all possible
inputs of that size is called best case.
Presidency
• Average case: The running time for any given
Group
size input will be the average number of
operations over all problem instances for a
given size.
Liner search
Presidency College
(Autonomous)
Algorithm Lin_srch(A,N,key)
// Input: A is an array of size N & key is the
//element to be searched
// Output: return position if key present or -1 if key not
found
{
Reaccredited by
NAAC with A+
for i=1 to N do
{
if a[i] = key then
return i;
Presidency
Group }
return -1;
}
Important equations
u
1. ∑ 1+1+1---------+1 = u-l+1
Presidency College
(Autonomous)

i=1
n
2. ∑ 1 = n
i=1
n -1
3. ∑ 1 = n-1
Reaccredited by
NAAC with A+
1
n
4. ∑ n = n + n + ------ + n = n.n = n2
Presidency
Group
i=1
n
5. ∑ i = 1+2+3 ------+n = n(n+1)/2 ≈ ½ n2
i=1
n-1
6. ∑ i = 1+2+3 ----- --------- +(n-1) = n(n-1)/2 ≈ ½ n2
i=1
Presidency College

n
(Autonomous)

7. ∑ i2 = 12 + 22 + ----------n2 = n(n+1)(2n+1)/6 ≈
(1/3)n3
i=1
n
8. ∑
i=1ik = 1k + 2k + ----------- nk ≈ nk+1/k+1
Reaccredited by
n
NAAC with A+

9. ∑
i=0a = 1+a+a +-----------------+a =a -1 / a-1 (a≠1)
i 2 n n+1

n
Presidency
Group

10. ∑ 2
i=0
i
= =2n+1
-1
n
11. ∑ 1/i = 1+ 1/2+ 1/3+ ------------- +1/n ≈ log(n)+ y
i=1
n
12. ∑ log i ≈ n log n
i=1
Presidency College
(Autonomous)
n
13. ∑ 2i -1 = 2 + 22 + 23 -------- 2k-1 + 2k = 2k – 2
i=1
n
14. ∑ 2i = 2i – 2
i=1
Reaccredited by
NAAC with A+

Presidency
Group
Logarithmic equations
Presidency College
(Autonomous)

1. Log a1 = 0
2. Log aa = 1
3. Log ax < 0
4. Log ax >1
Reaccredited by
NAAC with A+
5. Log a(xy )= (Log ax) + (Log ay)
6. Log a(x/y) =(Log ax) - (Log ay)
Presidency
Group
7. Log axz = zLog ax
8. Log aa2 = 2
9. aLogax = x
Exponential equations
Presidency College
(Autonomous)
1. a0 = 1
2. a1 = a
3. a-1 = 1/a
4. (am)n= amn
5. am.an = am+n
Reaccredited by
NAAC with A+

Presidency
Group
Examples on step count
Algorithm sum(a,n)
Presidency College
(Autonomous) {
s=0.0
For (i=1 to n do)
{
s=s+a[i]
Reaccredited by
NAAC with A+
}
Return s; T(p)=2n+3
Presidency
}
Group
Presidency College
Example 2
(Autonomous)
Algorithm add(a,b,c,m,n)
{
for i=1 to m do
{
Reaccredited by
NAAC with A+
for j=1 to n do
{
c[i,j]=a[i,j]+b[i,j]
T(p)=2mn+2m+1
Presidency
Group }
}
}
Example 3
Algorithm fibo(n)
Presidency College
{
(Autonomous)
if(n<=1) then
write n
else
{
f2=0;f1=1;
{
Tfibo = n<=1  2
for i=2 to n do If n>1  4n+1
Reaccredited by
NAAC with A+
{
f3=f1+f2
f1=f2
f2=f3
Presidency
Group
}
write (f3)
}
}
Recursive algorithm example
Presidency College
(Autonomous)

Example 4
Algorithm Rsum(a,n)
{
Reaccredited by if(n<=0) then {
Trsum= 2 if n =0
NAAC with A+
2 + Trsum(n-1) if n>
return 0.0 = 2(n+1)
else
Presidency
Group
return( Rsum( a,n-1 )+a[n]);
}
Example 5:
Presidency College

Algorithm fact(n)
(Autonomous)

{
if (n==0) then {
Tfact= 2 if n =0
return 1 2 + Tfact(n-1) if n>0
Reaccredited by = 2(n+1)
NAAC with A+
else
return (n* fact(n-1))
Presidency
Group
}
Example 6:
Algorithm fibon(n)
Presidency College
(Autonomous)
// input the number of elements
// output Fibonacci series
{
if n==0 or n==1
return 0;
Reaccredited by
NAAC with A+
Else
If n==2
return 1
Presidency
Group
else
return fibon(n-1) + fibon(n-2)
}
Asymptotic notations
Presidency College
(Autonomous) • The word asymptotic means the study of
functions of parameters ‘n’, as n grows larger and
larger without bound . i.e the runtime of
algorithm increases with the size of the input.
• In this we are concerned with how the running
time increases when the size of i/p increases.
• The value of function may increase or decrease
Reaccredited by as the value of n increases. Based on the order of
NAAC with A+
growth of n the behavior of the function varies.
• Asymptotic notations are the notations using which
two algorithms can be compared with respect to
efficiency based on the order of growth of an
algorithm.
Presidency
Group
Different asymptotic notations
Presidency College
(Autonomous)

• O ( Big Oh)
•  ( Big Omega)
• Θ ( Big Theta)

Reaccredited by
NAAC with A+

Presidency
Group
Bigh Oh(O)
Presidency College

• It is also called Landau notation , Bachmann-


(Autonomous)

Landau notation.
• This was introduced by number theorist Paul
bachman.
• It is often used to describe how the size of the
Reaccredited by input data affects an algorithm running time.
NAAC with A+
• It is a measure of the longest amount of time that
it could possibly take for the algorithm to
complete execution.
Presidency
Group • Bigoh notation gives upper bound on a function
f(n).the upper bound on f(n) indicates that the
function f(n) will be the worst case that it does
not consume more than this computing time.
Contd ..
Presidency College
• Let n indicates the size of input and g
(Autonomous)
(n) is a function Big Oh is defined as a
set of functions with a small or same
order of growth as g (n) as n goes to
infinity.
• Let f (n ) be the time efficiency of an algorithm . the
Reaccredited by
NAAC with A+ function f (n) is said to be O (g (n)) denoted as
F (n) € O (g (n))
• If c is positive constant n0 is positive integer then
Presidency F (n) ≤ c*g (n) for all n≥ n0
Group
Presidency College
(Autonomous)

Reaccredited by
NAAC with A+

Presidency
Group
Problem’s on BigOh notation
Presidency College

1. Let g(n)=n3 ,p.T n2 and n belongs to O(n3).


(Autonomous)

2. Let f(n)=3n+2 .p.t f(n)=O(n)


3. Let f(n)=50n+6 p.t f(n)=O(n).
4. Let f(n)=100n+5 express f(n) using bigoh notation.
5. Let f(n)=10n2+4n+3 .p.t f(n)=O(n2).
6. Let f(n)=3n2+4n-2 p.t f(n)=O(n2).
7. Let f(n)=17n3-5 . P.t f(n)=O(n3).
Reaccredited by
NAAC8.
with A+Let f(n)=10n3+8 express f(n) using bigoh notation.
9. Let f(n)=5n3+2n2-5 . P.t f(n)=O(n3).
10. Let f(n)=6*2n+n2. express f(n) using bigoh notation.
11. P.t running T(n)=n3+20n+1 is O(n3).
12. P.t running T(n)=n3+20n+1 is not O(n2).
Presidency
Group

13. P.t 2n+10 is O(n).


14. P.t 7n-1 is O(n).
15. 3n3+20n2+5 is O(n3).
Big Omega notation ( )
• This gives the lower bound of f(n). The lower
bound implies that below this time the
Presidency College
(Autonomous)

algorithm can not perform better. The


algorithm will take at least this much time.
Definition:
• Let f (n ) be the time efficiency of an
Reaccredited by
algorithm . the function f (n) is said to be Ω(g
NAAC with A+ (n)) denoted as
F (n) € Ω (g (n))
Iff there exists c a positive constant and n0 is
Presidency
Group positive integer then
F (n) ≥ c*g (n) for all n ≥ n0
Presidency College
(Autonomous)

Reaccredited by
NAAC with A+

Presidency
Group
• Always big Oh gives upper bound i.e worst
Presidency College
case.
• Always big omega gives lower bound i.e best
(Autonomous)

case.
Example:
• find max min element worst case is O(n) and
best case is (n).
Reaccredited by • In linear search
NAAC with A+
a. for key found case best case  (1) and
worst
Presidency
case is O(n).
Group
b. For key not found case best case  (n) and
worst case is O(n).
Problem‘s on big omega( ) notation
Presidency College
(Autonomous)

1. Let g(n)=n3,p.t n3,n4,0.001n4,n5+n+3 belongs to


(n3).
2. Let f(n)=3n+2 . P.t f(n)=  (n)
3. F(n)=10n+5 . P.t f(n)= (n).
4. F(n)=100n+5 . P.t f(n)=  (n)
5. F(n)=100n2+10n-6 . P.t f(n)=  (n2).
Reaccredited by
NAAC with A+

6. F(n)=6.2n+n2 . P.t f(n)=  (2n).


7. F(n)=5n2 . P.t f(n)=  (n).
8. F(n)=10n3+5 . P.t f(n)=  (n3)
Presidency
Group

9. F(n)=n3+20n . P.t f(n)=  (n2)


Big Theta notation (Θ)
• This notation is used in some problems
Presidency College
(Autonomous)

where the lower and upper bound may


yield the same order of growth i.e. the
 and O will have the same g(n) , in
such type of problems we use Θ
Reaccredited by
NAAC with A+
notation.
• Example :
finding maximum and minimum
Presidency
Group
element in an array time complexity is ,
worst case is O(n) and best case is
(n).
Definition
Presidency College
(Autonomous)
Let (n ) be the time efficiency of an
algorithm . the function f (n) is said to be
Θ (n) denoted as
F (n) € Θ (g (n))
Reaccredited by
NAAC with A+
if c1 ,c2 are positive constants , n0 is
non negative integer then
C1 * g (n) ≤ f (n) ≤ c2 * g (n) for all
Presidency
Group n ≥ n0
Presidency College
(Autonomous)

Reaccredited by
NAAC with A+

Presidency
Group
Problem’s on big theta (Θ)notation
Presidency College
(Autonomous)

1. given f(n)=3n+2 , p.t f(n)= Θ(n)


2. Let f(n)=100n+2 , p.t f(n)= Θ(n)
3. Let f(n)=10n2+4n+2 , p.t f(n)= Θ(n2)
4. Let f(n)=10n3+5 , p.t f(n)= Θ(n3)
Reaccredited by

5.
NAAC with A+
Let f(n)=6*2n+n2, p.t f(n)= Θ(2n)

Presidency
Group
Mathematical analysis of Non-
Presidency College
recursive Algorithms
Steps:
(Autonomous)

1. What is the input size?


2. Find out the basic operation from the main body
of the loop.
Reaccredited by
NAAC with A+
3. What is the number of times the basic operation
is executed?

Presidency
4. Using the formula, establish f(n). Once we know
Group
f(n), finding its order of growth or the complexity
is straight forward.
Mathematical analysis of non-
Presidency College
recursive algorithms
(Autonomous)
The following steps specify the general plan for
analyzing time efficiency of non recursive
algorithms
1. Decide on the parameters indicating input size.
2. Identify the basic operations in the algorithm.
3. Check whether the number of times the basic
Reaccredited by
operation is executed depends only on the size
NAAC with A+ of an input. If it also depends on some additional
property , the worst case , average case and best
case efficiencies has to be determined.
4. Obtain the total number of times a basic
Presidency
Group operation is executed.
5. Using standard formulae and rules , find the
order of growth.
Examples on non recursive
algorithms
Presidency College
(Autonomous)
Example 1:
Algorithm : MaxElement(A[0:n-1])
// to find the largest element in an array
// input: An array A[0: n-1] of real numbers
// output: value of largest element of array A.
Reaccredited by
{
NAAC with A+
max=A[0]
for(i=1 to n-1 do)
if (A[i]>max)
Presidency
Group
max=A[i]
return (max)
}
Example 2
Presidency College
(Autonomous)
Algorithm : Uniqueness (A[0 : n-1])
// to check whether there are any duplicates .
// Input: An array A[0 : n-1]
// output: Return ‘1’ if all the elements are distinct and ‘0’ if not.
{
for (i=1 to n-1 do)
{
Reaccredited by for(j=i+1 to n do)
NAAC with A+
{
if ( A[i]==A[j])
return 0
}
Presidency
Group }
return 1
}
Example 3:
Presidency College
(Autonomous)
Algorithm: Matrix Multiplication (A[0:n-1],B[0:n-
1],C[0:n-1])
// Multiplication of two n x n matrices A and B
// Input : Two n X n matrices A and B.
// output : Another n X n matrix c=AB
{
Reaccredited by
NAAC with A+
for i=0 to n-1 do
for j= 0 to n-1 do
c[i,j]=0
for k=1 to n do
Presidency
Group
c[i,j]= c[i,j]+A[i,k]*B[k,j]
}
Mathematical analysis of
Presidency College
recursive Algorithms
(Autonomous)

Basically there are 3 methods


• Substitution method(backward substitution
method).
Reaccredited by
NAAC with A+
• Iteration method.--- sigma
• Master method. Divide and conquer

Presidency
Group
Steps to be followed for analyzing
Presidency College
time efficiency of recursive algorithm
(Autonomous)

1. Based on the input size decide the various


parameters to be considered.
2. Identify the basic operation in the
algorithm.
3. Obtain the number of times the basic
Reaccredited by
NAAC with A+ operation is executed.
4. Obtain the recurrence relation.
5. Solve the recurrence relation using
Presidency
Group
formulae or backward substitution
method ,obtain the order of growth and
express using asymptotic notation.
Mathematical analysis of recursive Algorithms
Presidency College
Example 1:
(Autonomous)

Algorithm Fact(n)
//compute n factorial for non-negative number
{
Reaccredited by
NAAC with A+
if n==0
return 1;

Presidency
else
Group

return n*Fact(n-1);
}
EXAMPLE 2: Fibonacci
Presidency College
(Autonomous)
Algorithm: Fibonacci(n)
// input : A non negative number
// output: nth element of the Fibonacci series
{
if n=0 || n=1
Reaccredited by
NAAC with A+ return n;
else
Presidency
return (Fibonacci(n-1) + Fibonacci(n-
Group
2));
}
EXAMPLE 3: Tower of Hanoi
Presidency College Algorithm: ToweroHanoi(n)
(Autonomous)
// input : Number of discs n
// output: sequence of moves
{

if n==1
Reaccredited by
NAAC with A+ move disk from source to destination(s to d)
else
ToweroHanoi(n-1,s,d,t)
Presidency
Group
move disk from source to destination(s to d)
ToweroHanoi(n-1,t,s,d)
}

You might also like