Sunteți pe pagina 1din 6

Introduction to algorithms

An algorithm is defined as finite sequence of unambiguous instructions followed to accomplish a given task.
It is also defined as unambiguous, step by step procedure (instructions) to solve a given problem in finite number
of steps by accepting a set of inputs and producing the desired output. After producing the result, the algorithm
should terminate.
The following figure (notion of algorithm) shows how an algorithm is used to get the desired output:
Input
Problem Algorithm Program Computer Output

The solution to a given problem is expressed in the form of an algorithm. The algorithm is converted into a
program. The program when it is executed, accept the input and produces the desired output.
An algorithm must satisfy the following criteria:
➢ Input: Each algorithm should have zero or more 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 unambiguous.
➢ Effectiveness: The instruction should be simple and should transform the given input to the desired
output.
➢ Finiteness: The algorithm must terminate after a finite sequence of instructions.
➢ By looking at the algorithm, the programmer can write the program in C or C ++ or any of the
programming language. Before writing any program the solution has to be expressed in the form of
algorithms.
Computing GCD: The notion of an algorithm can be explained by computing the GCD of two numbers. The GCD
(short form for Greatest Common Divisor) of two number m and n denoted by GCD (m,n) is defined as the largest
integer that divides both m and n such that the remainder is zero. GCD of two numbers is defined only for positive
integers but, not defined for negative integers and floating point numbers. For example, GCD (10, 30) can be
obtained as shown below:
Step 1: The numbers 1, 2,5,10 can divide 10
Step 2: The numbers 1, 2, 5,6,10 and 30 can divide 30
Step 3: Observe from step 1 and step 2 that the numbers 1, 2, 5 and 10 are common and hence the numbers
1, 2, 5 and 10 are called common divisors of 10 and 30. So, GCD (10, 30) =10.
The GCD of two numbers can be computed using various methods as shown below:
Using modules (Euclid’s algorithm)
Different way Repetitive subtraction (Euclid’s algorithm)
Of computing Consecutive inter checking algorithm
GCD Middle school procedure using prime factors

The recursive definition to compute GCD of two numbers M and N using EUCLID’s algorithm is shown below:

M if N=0
GCD (M, N)
GCD (N, M % N) otherwise
The recursive algorithm to compute GCD using Euclid’s algorithm is shown below:

ALGORITHM GCD (m, n)


//Input: M and N should be +ve integers
//Output: Greatest common divisor of M and N //RECURSIVE C FUNCITON
int gcd (int m, int n)
Step 1: [Terminal condition] {
If (N=0) return M if (n==0) return m;

Step 2: [General case] return gcd (n, m % n);


return GCD(N, M % N) }

To compute GCD using repetitive subtraction: The GCD of two numbers can also be obtained using Euclid’s
algorithm by repetitive subtraction. When M and N are same, GCD is either the final value of M or final value of N.

Using the above steps, the algorithm can be written as shown below:

Algorithm (represented in natural language) to compute GCD of two numbers using


Euclid’s algorithm

Repeat as long as M and N are different


Step 1: If M>N, subtract N from M, store result in M
Step 2: If M<N, subtract N from M and store the result in N
End of repeat

Output either M or N
Note: If one of two numbers is a Zero, return non-negative number as the GCD.

To compute GCD using consecutive integer checking: The alternate method to find the GCD of two
numbers is using consecutive integer checking.
Design By definition, the GCD of two numbers m and n is the largest integer that divides both m and n such that
the remainder is zero. So, GCD of m and n cannot be more than the smaller of these two numbers. So, to start
with we take smaller of two numbers m and n as shown below:
Small=min (m, n);
And divide m and n by small. If remainder in both the cases is zero, small is the GCD. Otherwise, decrement small
by 1 and repeat the process till both m and n are divisible by small. For example, if m is 10 and n is 6, the gcd of
10 and 6 can be computed using consecutive integer checking as shown below:

Small M % small N % small Small is GCD?


6 10%6=4 6%6=0 6 is not GCD
5 10%5=0 6%5=1 5 is not GCD
4 10%4=2 6%4=2 4 is not GCD
3 10%3=1 6%3=0 3 is not GCD
2 10 % 2 = 0 6%2=0 2 is GCD
(remainder is 0)
The algorithm to find GCD of m and n using consecutive integer checking is shown below:

Algorithm (Using natural language) to compute GCD of 2 numbers (consecutive inter


checking)

ALGORITHM GCD (m, n)


//Input: Two non-negative and non-zero integer values m and n
//Output: GCD of m and n

Step 1: Find the minimum (m, n) and assign to small

Step 2: Divide m and n by small


If remainder is 0 in both cases
Return small as the answer
else
go to step 3
end if
Step 3: Decrement small by 1 and go to step 2

Fundamentals of Problem solving using Algorithms: The algorithm design and analysis process is
explained by considering the following flow-chart:

Understanding the problem

Model development

Ascertain the capabilities of computational device

Select exact/approximate algorithms

Select the data structure

Design an algorithm

Prove algorithm’s correctness

Analyze the algorithm

Implement the algorithm

Program testing
Understanding the problem (Statement of the problem): Given a problem, we have to understand the
problem completely and clearly. In the description of the problem, if we have any doubts we should ask the
questions and clarify the doubts. The designer should understand what information is missing. If the information is
provided, we should know in what way the information is useful to solve the problem. We should know what input
should be given and what output is obtained. It is very essential to specify the exact range of inputs given to
algorithm. If exact range is not specified, the algorithm may work correctly for a majority of inputs but may crash
on some other input. The correct algorithm is not the one that works most of the time but one that works correctly
for all legitimate values (input).
Model development: After stating the problem clearly, mathematical model should be constructed. Use pen and
pencil to represent the problem definition. The problem can be represented using a graph or a network or any
other suitable mathematical model. Constructing a mathematical model for a given problem is purely based on
the experience or studying the already existing models for a given problem. A mathematical model can be built for
any specified problem.
Ascertain the capabilities of a Computational Device: Once we understand the problem clearly, we need to
ascertain the capabilities of the computational device based on the following factors:
1. Architecture of the device: Based on the architecture of computational device, we may have to write two
types of algorithms.
➢ For the devices based on Von Neumann architectures in which instructions are executed sequentially, we
have to design sequential algorithms.
➢ If a device is capable of executing the instructions in parallel, we may have to design parallel algorithms.
2. Speed of the device: For many problems we need not worry about the speed of the device. But, in
problems involving military applications, in communication and real time products (such as mobiles) etc.,
we have to worry about the speed of the device.
3. Memory space: There are problems that involve huge transaction of data. In such situations, we have to
choose a machine with more memory space to store huge amount of data.
Select exact/approximate algorithms
There are two types of algorithms based on the output (result) obtained:

Exact algorithm
Approximate algorithm
Exact algorithm: The algorithms that solve the problem exactly are called exact algorithms. Algorithms for
sorting, searching, string matching, traveling salesman problem, knapsack problem etc., give the exact result and
hence are called exact algorithms.
Approximate algorithm: An algorithm that solves the problem approximately is called approximate algorithm.
For example, finding square root, solving non-liner equations etc. The exact algorithms such as traveling sales
person problem are extremely slow and we may require fast approximate result. In such case, we have to choose
between the exact and approximate results.
Selection of appropriate data structures: The next step is to choose the proper data structures. Using well
defined algorithms and data structures, we can write efficient programs
Algorithms + Data structures = Programs
As shown above, we can use various data structures and write variety or programs. So, to write efficient

programs, we have to choose efficient algorithm and efficient data structures.


Design an algorithm: The next step is to design an algorithm. An algorithm design technique is a general
method of solving a problem in the form of algorithms. The various techniques are shown below:
➢ Brute force
➢ Divide and conquer
➢ Decrease and conquer
➢ Transform and conquer
➢ Dynamic programming
➢ Greedy technique
An algorithm can be specified in various way. The algorithm can be specified using following three methods:

Natural language
Pseudo code
Flowchart
Natural language: Algorithms can be written in English like statements with very little mathematical expressions.
Normally, the algorithms using natural language are not preferred, because of its inherent ambiguity. The
instructions specified in natural language should be clear and unambiguous. Each one of us can interpret a given
statement written in English in different manner. Hence, natural language is not preferred to represent the
solution. So, it is rarely used.
Flowchart: A flow chart is a pictorial representation of an algorithm. That is, flowchart consists of sequence of
instructions that are carried out in an algorithm. All the steps are drawn in the form of different shapes of boxes,
circles and connecting arrows. These shapes represent various operations that are carried out and arrows
represent the sequence in which these operations are carried out. Flowcharts are mainly used to help the
programmer to understand the logic of the program.
The flowcharts work well if and only if the algorithm is small and simple, flowcharts are not used for larger
algorithms.
Pseudo code: The pseudo code is a method of representing an algorithm using natural language and programming
language constructs. Thus, pseudo code is a mixture of natural language and programming language constructs.
The pseudo code may be similar any of these languages such as C, Pascal or Java and hence, if we know any of
these languages, we should have little trouble reading the algorithms. The pseudo code to find the area of a
rectangle can be written as

Read (length, breadth)


Area <- length*breadth
Write “Area of rectangle is”, area
Exit
Proof of algorithm’s correctness:
➢ After designing the algorithm for a specific problem, we have to prove its correctness.
➢ It is our responsibility to prove that the algorithm produces the required output for every legitimate input.
➢ The proof of correctness of an algorithm is normally done using mathematical inductions.
Analysis of algorithms: The solution to the problem can be obtained using different algorithms. For example,
elements can be sorted using bubble sort, selection sort, insertion, quick sort etc. Out of the many sorting
techniques we have to choose the most efficient algorithm. The efficiency of an algorithm can be measured using:
➢ Time efficiency
➢ Space efficiency
Time efficiency: indicates how fast the algorithm can be executed
Space efficiency indicates how much (minimum) extra memory is used by the algorithm during execution
➢ After designing the algorithm, the estimate of time and space for a given problem should be obtained.
Then, select an algorithm which is more efficient in terms of time and space.
➢ Analysis also helps to find the bottlenecks in a given program. For example, which portion of the program
consumes more time? By knowing this, one can re-design the algorithm to solve the same problem much
efficiently and easily. The weaker algorithm can be improved.
➢ It is very important to write the programs so as to maintain the simplicity. The simple algorithms are
easier to understand, easier to debug and easy to program.
➢ Another characteristic feature of an algorithm is the generality. It is required to write general programs
always. If we are not satisfied with the efficiency, simplicity and generality of an algorithm, it is necessary

to re-design the algorithm.


Implementation (Coding an algorithm): The algorithm should be converted into a program. Usage of
different languages (such as C/C++/C#) for solving a problem results in different memory requirements and
affects the speed of the program. The syntax may vary from language to language. The selection of the
programming language is also important. The language selected should support the features mentioned in the
design phase. For an object oriented design one can select C++ where as C language cannot be selected even
though C is subset of C++.
Program testing: Testing is the process of identifying errors in a program and finding how well the program
works. For this to happen, we have to consider various data inputs and check whether the desired output is
obtained for the legitimate input. If the desired results are not obtained, the program has to be corrected or
changed to get the desired result.

S-ar putea să vă placă și