Constraints x > 2
Y >1
ALGORITHM SPECIFICATIONS
1. PSUEDOCODE
2. RECURSIVE ALGORITHMS.
Algorithm Specification
Comments begin with //
Blocks are indicated with matching braces {}
An identifier begins with a letter, data type is not defined
Assignment of values to variables is by “:=”
Two Boolean values
Multidimensional arrays use [ ], A[I,j]
Loops used
while (condition ) do { }
for variable := value1 to value2 step stepsize do { }
repeat{} until (condition)
Conditional statement
If (condition) then statement
If (condition) then statement1 else statement2
Input and output is done by read and write
Only one type of procedure - Heading
Algorithm Name of the algorithm
( parameter list)
Example of an algorithm
1. Algorithm Max(A,n)
2. //A is an array of size n
3. {
4. result := A[1];
5. for I := 2 to n do
6. if (A[I] > result) then result := A[I];
7. return result;
8. }
RECURSIVE ALGORITHMS
TOWER OF HANOI
Towers of Hanoi
1. Algorithm TowersOfHanoi(n,x,y,z)
2. // Move the top n disks from x to tower y
3. {
4. if(n>=1) then
5. {
6. TowersOfHanoi(n-1,x,z,y);
Eg3. Factorial
1.Algorithm Factorial(n)
2 {
3. //n is the number of which the factorial is to be found
4. fact :=1;
5. k :=n;
6. if (n>1) then
7 . fact=k*factorial(k-1);
8. else fact=1;
9. return(fact);
10. }
Time and space complexity
Performance Analysis
Performance measurement.
Space complexity
The space complexity of an algorithm is the amount of memory
it needs to run to
Completion.
1.Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}
Explanation:
Assuming a,b ,c takes fixed place , it’s S(P) is fixed and Sp=0
(i.e no instance characterstics ).
2.Algorithm sum(a,n)
{
s=0.0;
for i:= 1 to n do
s:=s+a[i];
return s;
}
Explanation:
n-requires 1 word
s- requires 1 word
i-requires 1 word
a[]-requires n words.
So
S sum(n)>=(n+3)
3.Algorithm Rsum(a,n)
{
if(n<=0) then return 0.0;
else return Rsum(a,n-1)+a[n];
}
n- 1word
return - 1 word
pointer to a[] - 1word
Depth of recursion n+1
So SRsum>=3(n+1)
Instance charecteristics
Program Steps
For eg:
Algorithm Sum(a,n)
{
S:=0;
For i:=1 to n do
S:=S+a[i];
Return s;
Algorithm Sum(a,n)
{
S:=0;
Count :=Count +1;//for initialization of S
For i:=1 to n do
{
Count:=Count+1;//for each For stmt
S:=S+a[i];
Count:=Count+1;//for each S
}
Count:=Count+1 ; //for last execution of for i.e (n+1) th time
Count:=Count+1;// for return
Return S;
}
t(p)= n+n+1+1+1=2n+3
EG2:
Algorithm Rsum(a,n)
{
If (n<=0) then returm 0;
Else return Rsum (a, n-1)+a[n];
Algorith Rsum(a,n)
{
Count:=Count+1;//for if
If (n<=0 then)
{
count=count+1;//for return 0
return 0;
}
else
{
Count:=Count+1 //for return
Return Rsum(a,n-1)+a[n];
}
}
tRsum ={ 2 if n=0
2+tRsum(n-1) if n > 0
Time compexity of an algorithm can be seen in 3 perspectives.
Best Case
Worst Case
Average case
Best Case
The minimum number of steps can be executed for a
particular algorithm.
Worst Case
Maximum number of steps that can be executed for given
parameters.
Average Case
It is the average number of steps that can be executed for a
given parameters for a particular algorithm