You are on page 1of 34

INTRODUCTION

Several decades before,any tasks that had to be accomplished had to


be completely done by humans.
Example1.if we had a task to add 2 numbers a person had to manually
perform addition and produce the result.
Example2. If we had a task to find whether 2 numbers are equal to
each other or not then, the person had to manually check .
Like wise, any task had to be performed by humans itself.
But, the problem with this approach was that their could be chances
of getting inaccurate result.
But then, their was no other available option so, people continued to
do the task manually.
Then, in the year 1943. Their was a major invention that happened
which literally blown the minds of people, their came an alternative to
do all tasks that were performed manually . And the alternative was
named MACHINE or COMPUTER.
People were very happy and started using the machine.
But then people sensed that machine was capable of understanding
only combination 0’s and 1’s .
That is when they started instructing machines using 0’s and 1’s and
were getting their tasks done.
The advantage with machines was that if the instructions was
specified in the right way, then definitely the result used to be accurate
But, as days passed by, the tasks that has to be accomplished started
to become huge and complex too
Due to this, people failed to specify right instructions to the machine.
As a result of this machines were producing incorrect result.
Once, humans sensed this problem they figured out the following
solution for getting huge and complex tasks done.

Fig1:

Analyze the task(Problem)

Define an ALGORITHM for the above analysis

Represent the ALGORITHM pictorially. I,e Design a


FLOW CHART

Express the Flow Chart in the form of INSTRUCTIONS


(in any PROGRAMMING LANGUAGE)

Feed the instructions to MACHINE

Obtain the RESULT


In this complete book we would be majorly concentrating on
designing an algorithm .
Before we exactly see how to design an algorithm we should first
understand…..

What is an ALGORITHM?
An ALGORITHM
is a “sequence of steps designed for programming a computer to solv
e a specific problem.” normally by using machine .
In simple terms
An Algorithm is just the instructions you use to accomplish a goal.

STEPS INVOLVED IN DESIGNING AN ALGORITHM

step1: Defining the goal that has to be accomplishged.


step2:Figure out the steps using which the goal has to be accomplished
and list down the steps clearly
if the above two steps are orderly performed ,there it is ….An
Algorithm is Designed.
Say supposing we have a dozen mangos and we have to find the
biggest amongst them.
Now, lets design an algorithm for achieving the same using the steps
involved in designing an algorithm .
Step1: Finding the biggest mango (goal that has to be accomplished)
Step2: step a)consider 3 baskets say, basket-A, basket-B, basket-C.
step b)Let basket-A contain all the mangos , let basket-B and
basket-C be empty.
step c)pick a mango from basket-A
step d)if it is first time that the mango is picked then place it in
basket-C
step e)if it is not the first pick, then compare the picked mango
with the one which is placed in basket-C
step f)if it weighs less than the one in basket-C, then put it into
the basket-B
step g)if it weighs high, then place it in basket-C and place the
one which you have already placed in basket-C into basket-B
step h)if there are mangos still remaining in basket-A then repeat
from step c. else check the basket-C you have a biggest one.
Go grab it and eat it, and
I call this as mango Eating Algorithm.

CRITERIAS THAT ANY ALGORITHM SHOULD SATISFY


1) Any algorithm can accept zero or more inputs.
2) Any algorithm must produce one or more outputs
3) Algorithm has to clearly specify what operation is
performed.
4) Any algorithm must specify how the operation should be
performed.
5) Any algorithm must terminate after a finite number of
operation.
ALGORITHM SPECIFICATION
We can specify the algorithm in TWO ways
1.NATURAL LANGUAGE
2.PSEUDOCODE
NATURAL LANGUAGE as in English like statements but, if we use this
representation we must make sure that the resulting instructions are
specific
PSEUDOCODE LANGUAGE is a combination of both natural language
and programming language . A pseudo code is usually more precise
than natural language.
In this book, we represent most of the algorithms using pseudo code.
Now lets us learn the above two representation considering an
example.

COMPUTING GCD OF TWO NUMBERS USING EUCLIDS


ALGORITHM

What is GCD of two numbers?


GCD is the acronym of GREATEST COMMON DEVISOR.
GCD of 2 numbers is the largest positive integer that divides the
number without a remainder
For example, GCD of 8 and 12 is 4.
How?
Devisors of 8 are 1,2,4,8.
Devisors of 12 are 1,2,3,4,12.
The common devisors of both 8 and 12 are “ 1,2,3,4”
And the greatest amongst them is 4. Hence ,
The GCD is 4.

Natural language representation


STEP1:find the remainder of two numbers say, A and B
STEP2: a] Find the remainder of 2 numbers A and B

REM A%B
b] Assign the value of B to A

AB
c] Assign the value of remainder to B

BREM
Repeat above 3 steps (a,b,c) as long as the value of B
becomes zero.
There it is you have the GCD in A.
A 8 12(step b) 8
4
B 12 8(step c) 4
0
REM 8(step a) 4 0

PSEUDOCODE REPRESENTATION
ALGORITHM: GCD
Step1:[Accept 2 numbers]
Read A,B
Step2:[compute GCD]
While(b!=0)
{
REM=A%B;
A=B;
B=REM;
}
GCD=A
Step3:[Display result]
Print GCD
Step4:[Finished]
stop

PSEUDO CODE CONVENSIONS


The following are the conventions used to write algorithm using
pseudo code
1] An Algorithm can consist of single statement or a set of statements
If it has a set of statements then we call it as compound
statement.
We always represent a set of statements in a block as shown below.
{
s1
S2
S3
S4
-
-
-
Sn
}
blocks are indicated with a set of braces { and }.
2] Every statement should be separated with a semicolon as shown
below.
{
s1;
S2;
S3;
S4;
-
-
-
Sn;
}

3] the variables can be declared but datatypes need not be explicitly


specified.
{
Datatype1 variable1;
Datatype2 variable2;
Datatype3 variable3;
}
4] the variable can be defined a value in the following manner.
Variable name:=value/expression ;
5]in order to handle yes/no type of data we can use the bo0lean value
true of false .
6]Arrays are indicated using a set of square brackets
In order to access an array a’s , ith element then we can specify
as a[i]
If array -a is 2-D array then in order to access its ith and jth
element we can use a[i][j].
7]In order to repeat a set of statements we can use looping statements
such as for, while, repeat-until .
Representation of for.
for variable:=value to condition step inc/dec
do
{
S1
-
-
-
-
Sn
}
Representation of while.
While condition
do
{
S1
-
-
-
Sn
}
Representation of repeat-until
repeat
{
S1
-
-
-
-
Sn
} until (condition)

In java and few other programming languages repeat-until


Is represented using do-while

8]the control constructs should be represented as shown below

 if condition
then
{
S1
-
-
-
Sn
}

 if condition
then
{
S1
-
-
-
Sn
}
else
{
s1
-
-
-
sn
}

Always remember that the condition here must be a


Boolean expression
 case
{
Cond1 : s1
-
-
sn
Cond2 : s1
-
-
sn
Cond3 : s1
-
-
sn
else : s1
-
-
sn
}

In case control construct always if cond1 is true then statements inside


cond1 i,e statements 1 to n will get executed and case statement is
ended.
If cond1 is false then cond2 is tested . if cond2 is true then statements
inside cond2 i,e statements from 1 to n is executed and case statement
is ended .
If condition 1 and cond2 is false then cond3 is tested. If cond3 is true
then statements inside cond3 i,e statements from 1 to n is executed and
case statement is ended
If none of cond1, cond2, cond3 is true then by default statements inside
else gets executed.
Note: in java and a few other programming languages case is
represented as switch.
9] input and output statements are represented as read and write
respectively.
10]Procedures or methods can be represented as shown below
Methodname(parameter list)
{
s1
-
-
-
-
sn
}
Example 1: Algorithm for a method which finds the maximum number
in an array.
Algorithm maximum(Arr,n)
//Arr is an array of size n
{
Big=Arr[0];
For i:=1 to i<=n-1 step i++
{
if Big<Arr[i]
then
{
Big:=Arr[i];
}
}
}
write Big;

In the above algorithm we are considering an array (Arr) in which n


elements are stored .
The task here is to find the biggest element amongst those n
elements.
So, we first place the element which is present at the 0th index of the
array (Arr[0]) in a variable Big
And then we are repeatedly comparing each element of the array
until the last element with the variable Big to check whether the
element in Big is lesser than the element present in array or not
if the value of big is lesser than the array element, then we place the
array element into variable Big.
By the end of the process we will have the greatest element in the
variable Big.
Finally we are displaying the value present in variable Big.

Example 2: Algorithm for a method which sorts the elements in


ascending order in an array using BUBBLE SORT .

What is Sorting?
Sorting is a process of arranging the random numbers either in
Ascending or Descending order
Algorithm bubble_SortS (Arr,n)
//Arr is an array of size n
{
for i:=0 to i<=n-1 step i++
{
for j:=0 to j<-n-2-i step j++
{
if Arr[j]>Arr[j+1]
then
{
temp=Arr[j];
Arr[j]=Arr[j+1];
Arr[j+]=temp;
}
}
}
Tricky is it? Ok lets learn the logic used in the above algorithm
Logic of BUBBLE SORT: compare the adjacent numbers . if the number
to the left is greater than the number to the right, then exchange them.
If number to the left is not greater than the number to the right, then
don’t exchange them. This process of comparing has to be repeated
many times
Let us consider an example of an array which has 5 elements (n=5)

you can notice that the largest element 86 has reached the end of the
array(which is the place where it has to be in ascending order).so we
will have to sort the remaining portion of the array.

you can notice that the second largest element 42 has reached the last
but one position of the array(which is the place where it has to be in
ascending order).so we will have to sort the remaining portion of the
array.
Thus, you can notice that the array is sorted in ascending order . you
can also notice that the comparison operation has to be performed
many times . hence the comparison and exchange must be placed
inside the loop.
so let us see the loop again .
this is it. This is the logic of bubble sort.
Both Example1(finding maximum element ) and Example2(bubble sort)
are such methods which produces the expected result when they are
called only once. But, there are few methods which produces the
expected result when they are called RECURSIVELY. For such methods
which should be called recursively we write RECURSIVE ALGORITHMS.

RECURSIVE ALGORITHMS

Example1:Algorithm for computing the factorial of a


given number n using Recursion.

Algorithm Factorial
//to find the factorial of n
Step1:[Accept n]
Read n;
Step2:[call the function fact()]
result=fact(n);
Step3:[display result]
Write result;
Step4:[finished]
Stop;

Algorithm fact(n)
{
if n==0
then
return 1;
else
return n*fact(n-1);
}

this is the complete algorithm to find the factorial . butt, how would it
work???
Continue reading…
We all know that factorial of 0 is 1 (ie. 0!=1). For all numbers(n) greater
than 0,factorial of n would be n* factorial(n-1). The same logic ,if we
have to depict it in the form of a recurrence relation then, it would go
as follows
lets now consider a number n to be 3 and lets apply the above
algorithm fact(n] to get the factorial (3!)
As , noticed in the above example inorder to find 3! We had to call
fact(n) 3 times. This is one of the few methods which produces the
expected result when they are called RECURSIVELY.And the algorithm
associated with it is a recursive algorithm.
Example 2:Recurssive algorithm for obtaining a solution for TOWER OF
HANOI problem .
Lets now see the recursive algorithm for tower of Hanoi
Algorithm TowerOfHanoi
Step1:[Accept the number of discs]
Read n;
Step2:[call the function tower()]
tower(n,peg1,peg2,peg3);
Step3:[finish]
Stop;
Algorithm tower(n,src,inter,dest)

//nno. of discs , srcsource peg, interintermediate peg ,


//destdestination peg
{
if n==1
then
{
Write “move disc1 from src to dest”;
}
else
{
tower(n-1,src,dest,inter);
Write “move disc n from src to dest”;
tower(n-1,inter,src,dest);
}
}
this is the complete algorithm to obtain the solution for tower of
Hanoi.
butt, how would it work???
Continue reading…
If there is only one disk to be moved then it can be directly moved
from source peg to destination peg as given below

If there are more than one disc say 3 discs


1)move n-1 discs from source peg to intermediate peg using destination
peg
Thus it can be noticed that out of 3 discs at source peg 2 discs have
been moved to intermediet peg.during this movement , the destination
peg was used.

2)move the nth disc from src peg to destination peg.

Thus ,the third disc has been moved from src to dest peg.
3)move the n-1 disc from inter to dest peg using the src peg.
Thus 2 discs (n-1 discs) have been moved from inter to dest peg. During
this movement , the src peg was used.
The above logic in the form of recurrence relation would be-

Analysis Framework
Once the algorithm is built, the next step is to analyze
How efficient the algorithm is ?
In order to analyze the algorithm, we actually consider
two parameters.
*)TIME
*)SPACE
We measure efficiency of an algorithm in terms of how
fast it runs (executes) and we refer to it as time
efficiency.
And we also measure efficiency of an algorithm in
terms of how much extra (more ) space the algorithm
requires to run(executes).

In this book, we would be primarily consider with time efficiency by


analysis that we do further is also applicable to analyze space
efficiency also
Time efficiency can be analyzed on following 2 factors
1). Based on number of inputs the algorithm accepts.
It is known fact that as number of inputs given to the
algorithm increases , the consumed to execute the same would also
increase .
For Example, it always takes a conger time to solve a tower
of Hanoi problem with 5 discs when compared it with 3 discs
2)Measuring Unit of Time
An Algorithm running time can be measured in several units
of time
Eg: we may use a few standard units such as milliseconds,
microseconds etc.
But we have drawbacks with these units such as speed of
computer , the programming language used to implement the
algorithm etc.,makes it difficult to measure algorithms efficiency . we
would like to have a parameter (unit) which does not depend on
above factors.
# ONE WAY IS TO COUNT THE NUMBER OF TIMESTHE BASIC
OPERATION IS EXECUTED.

Hence , the time efficiency is analyses by determining the number of


times the basic operation is repeated as a function of input size
(number of inputs accepted)

I,e T(n)≈ Cop C(n)

Where Trunning time CopExecution time of basic operation


Cnumber of times basic operation is executed nsize of input.
#. In the analysis of algorithm efficiency knowing order of growth is
also an important factor.

ORDEROF GROWTH
We know that the algorithm works at different speeds
based on the number of inputs provided(n)
But we always expect that the algorithm should work
efficiently for all values of n.
As we have noticed that behavior of the algorithm changes as and
when the value of n increases this change is usually analyzed using
higher value of n
That is the order of growth would usually be determined for higher
number of inputs(n)
The order of growth for few functions with different number of inputs
(n)

n log n n n log n n2 n3 2n n!
21 0.3 21 0.6 4 8 4 2
22 0.6 22 2.4 16 64 16 24
23 0.9 23 7.2 64 512 256 40320
24 1.2 24 19.2 256 4096 65536 20922789888000
25 1.5 25 48.1 1024 32768 4294967296 2.63e35

the above table indicates the running time of the algorithm based on the function
of the algorithm uses
As we can observe in the above table as the value of n increases the value of the
functions are increasing . we refer to this as ORDER OF GROWTH
But, if you still carefully analyze the data with respect to the function 2n and n! as
the input size ( n) increases in a small amount the value obtained is increasing to a
great extent, we refer this to as exponential growth.

The Algorithm running time ofcourse can be measured for a few algorithms by using the functions based
on the number of inputs I,e the value of n but,

For few algorithms the running time is not only dependent on input . because , for the same amount of
inputs the running time(efficiency) of the algorithm can very such algorithms efficiency is measured
using

1)Worst Case Efficiency

2)Best Case Efficiency

3)Average Case Efficiency

Consider an example

Algorithm LinearSearch(Arr[0,n-1],key)

//Arr is an array containing n elements


// Key is an element that has to be searched

For i:=0 to i<=n-1 step i++

do

If key==Arr[i]

then

{
write “elements found @ index I”

write “Element not found”

the linear search algorithm is a classic example for an algorithm whose efficiency dosenot depend on
number of inputs

What does it mean?

On what else does the algorithm efficiency depends on ?

Lets first see the working of linear search algorithm


Working of linear search algorithm
The set of elements would be stored in an array. The elements which has to be searched is considered
as the KEY.

The key is compared repeatedly with the array elements till we find the key or till we reach the end of
the array .

Now ,lets consider the set of elements

You might also like