Anda di halaman 1dari 16

UNIT 1

Definition: An algorithm is a set of instructions that if followed


Accomplishes a particular task. In addition algorithm must specify the
following criteria.

1. Input : Zero or more quantities are externally supplied.


2. Output :Alleast one quantity is produced.
3. Definiteness : Each instruction is clear and unambiguous
(should not use directions as ‘add 6 or 7 to x’ or ‘compute 5/2’)
4 Finiteness : If we trace out the instructions of an algorithm
then for all cases the algorithm terminates after a finite
a number of steps.
5.Effectiveness : Every instruction must be basic so that it can be
carried out{Each instruction must be feasible}Effectiveness in
principle ,each instruction can be carried out using a pencil and paper.

A program is the expression of an algorithm in a programming language.

4 AREAS OF RESEARCH REGARDING ALGORITHMS

1. HOW TO DEVISE ( INVENT(create) ) ALGORITHMS

 Creating an algorithm is an art which may never be fully


automated ( mechanized ).
 To invent algorithms many algorithm design techniques are
used.
 Some of the useful design techniques are given below

1. Divide and Conquer


2. Greedy Method
3 Dynamic programming
4 .Search & Traversal methods
5. Backtracking
6. Branch & Bound
7. Algebraic Methods
8. Linear programming
9. Non Linear programming
10 Integer Programming

Algebraic Methods: Solving the values of X and Y using mathematical


methods.
Eg: 10x+2y=30
x+y=7
replace y=7-x
Linear programming: Maximize/Minimize problems
With constraints .
Like
maximize 10x+ 9y(with linear
equations)

Constraints x > 2
Y >1

Nonlinear programming: Maximize/Minimize problems


With constraints
Like maximize 10x2+ 9y2
Degree is 2 (equation is not linear)
Constraints x > 2
Y >1

Integer Programming :In linear and nonlinear programming the


result values(values of x &y) can be float.But in the case of integer
programming results can be integers only.

2. HOW TO VALIDATE ALGORITHMS


 Validation assures that algorithm work correctly independently of the
issues concerning the programming language.
 A proof of correctness is the outcome of validation process.

A proof of correctness involves


1. Set of assertions about Input &Output variables in a program &
Expressed in predicate calculus.

2. A Specification (I/P & O/P Specification) which is also expressed in


predicate calculus

3. A complete proof shows each statement is precisely defined


and all basic operations are correct( Which may be longer
than the program)

3. HOW TO ANALYZE ALGORITHMS

 When algorithm is executed it uses CPU to perform operations


and memory.
 Performance Analysis is the task of determining how much
computing
Time and storage an algorithm uses.

4. HOW TO TEST ALGORITHMS

Testing involves debugging & profiling.

 Debugging: It is the process of executing programs on a sample


data set to determine whether faulty results occur & if so to correct
them.

 Profiling( Performance measurement ) It is the process of


executing a correct program on data sets and measuring time and
space it takes to compute results.
 By profiling results of the previous analysis can be
confirmed & logical places can be pointed out to perform
Useful optimizations in time & space.

ALGORITHM SPECIFICATIONS

1. PSUEDOCODE

2. RECURSIVE ALGORITHMS.

( ‘PSUEDO’ MEANS NOT REAL) PSUEDOCODE :STEPS IN NATURAL


LANGUAGE TO DO A TASK.

HERE PSUEDOCODE THAT RESEMBLES C & PASCAL ARE FOLLOWED.

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

 AN ALGORITHM IS RECURSIVE IF SAME ALGORITHM IS INVOKED


IN THE BODY.
 RECURSIVE ALGORITHMS ARE OF TWO TYPES.

 DIRECT RECURSIVE: AN ALGORITHM CALLS IT SELF.

 INDIRECT RECURSIVE: ALGORITHM ‘A’ IS INDIRECT


RECURSIVE IF IT CALLS ANOTHER ALGORITHM WHICH IN
TURN CALLS ALGORITHM ‘A’.

EXAMPLE FOR RECURSIVE ALGORIHMS


FACTORIAL , TOWER OF HANOI,
PERMUTATION

TOWER OF HANOI

Three Towers are there and disks are arranged in the


increasing order of it’s size in the first tower.
They have to be transferred to any of the other two
Under the following restrictions.
a) Only one disk can be moved at a time.
b) No larger disk can be placed on a smaller one.

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);

7. write(“move top of x to y”);


8. TowersOfHanoi(n-1,z,y,x);
9. }
10.}
Permutation Generator
1. Algorithm Perm(a,k,n)
2. {
3. if (k=n) then write (a[1:n]);//Output permutation
4. else //a[k:n] has more than one permutation.
5. for I = k to n do
6. {
7. t:=a[k];a[k]:=a[I];a[I]=t;
8. Perm(a,k+1,n);
9. // All permutations of a[k+1:n]
10. t:=a[k];a[k]:=a[I];a[I]=t;
11. }
12. }

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

Time and space complexity in general means the time


needed for algorithm execution and memory (space) needed
for the algorithm.

The measurement of complexities is mainly divided into


two

Performance Analysis
Performance measurement.

Performance analysis is a priori because it is done before the


execution.
Performance Measurement a posteriori is performed after the
execution.
It is done using gettime() function at the beginning and and at
the end of program execution.

Space complexity
The space complexity of an algorithm is the amount of memory
it needs to run to
Completion.

Space complexity S(P)=C+Sp


C =fixed part that is independent of characteristics of I/P and
O/P.
i.e the number and size of of inputs and outputs.It also
includes space for code,space for variables and constants.

Sp=Variable part ,This depends each instance of problem .It


depends on recursive Stack space and size of referenced
variable.

Referenced variable :variable where dynamic allocation comes.


Eg: Array size known only at run time.

Examples for calculating space complexity

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)

Time Complexity T(P)


Amount of computer time needed to run to the completion of
the program.
T(P)=Compile time + t(p)

Compile Time doesn’t depend on instance characteristics


T(P) -- Time taken by Program P
t(p)----Run time

t(p)=Ca Add(n)+Cs Sub(n)+ Cm Mul(n)+Cd Div(n)


n-Instance charecteristics.

Ca,Cs,Cm,Cd Time needed for one addition, Subtraction


,Multiplication, division respectively.

Instance charecteristics

Reference Variable’s number


Recursive stack space.
Input/Output, i/o size
Number of arithmetic operations,

Input Size:Number of Words needed to describe the instance.


Eg:Add n elements in array
n  Listing ‘n’ elements
1 ->For n itself
Input size is n+1
Output Size
n  Listing ‘n’ elements
1 ->For n itself
1- For the Sum
So output size=n+2
Practically it is difficult to calculate based on the above method,
because the arithmetic operations involves numbers, and
involves a lot of arithmetic operations. So to calculate
t(p) this method is not used.

So method of program steps are used for calculating t(p) time


complexity.

Program Steps

It is syntactically or semantically meaningful


segment of a program that has execution time that is
independent of instance characterstics. Instance means a single
time program running.

Number of steps depends on kind of statement

Comments-- Zero step


Statement--- One step
Assignment  One step(without call to other statements)
Iterative Statements: For each control part execution one step
each.

For eg:
Algorithm Sum(a,n)
{
S:=0;
For i:=1 to n do
S:=S+a[i];
Return s;

Introduce variable count


Intially count=0

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];
}
}

When n=0 then pgm count=2


When n=1 then pgm count=4
When n=2 pgm count=6

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

Due to the incorrectness of definition in program steps


It is enough to represent tp < C1 n
tn <C2n

So we use asymptotic notations,


#include <stdio.h>
#include <conio.h>
main() {
clrscr();
int *array[3];
int x = 10, y = 20, z = 30;
int i;
array[0] = &x;
array[1] = &y;
array[2] = &z;
for (i=0; i< 3; i++) {
printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]),
array[i]);
}
getch();
return 0;

 Here the if we want the number of elements a can be one ,or


two, or maximum three.
 If one then one address can be stored. If two two addresses can
be stored.

Anda mungkin juga menyukai