module 1
module 1
INTRODUCTION TO ALGORITHM
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to
get the desired output. Algorithms are generally created independent of underlying languages, i.e. an
algorithm can be implemented in more than one programming language.
Characteristics of an Algorithm
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.
When we design an algorithm to solve a problem, it needs some computer memory to complete its execution.
For any algorithm, memory is required for the following purposes.
1. Instruction Space: It is the amount of memory used to store compiled version of instructions.
2. Environmental Stack: It is the amount of memory used to store information of partially executed
functions at the time of function call.
3. Data Space: It is the amount of memory used to store all the variables and constants.
The space needed by an algorithm is the sum of fixed components and variable component
Fixed component: this consists of space needed by variables, constants, instruction space.
Variable component: this consist of the space needed by referenced variables and recursion stack space.
Example: 1
Algorithm abc (a,b,c)
{
Return ((a+)/c+5));
}
The space needed for variable ‘a’ is 1 unit.
for(i=1;i<=n;i++)
s=s+a[i];
return s
}
The space needed for a[ ] is n units.
Time complexity
It is the amount of computing time needed to run to completion. The time taken by a program is the sum of
compile time and execution time. The following factors effect the time complexity of an algorithm:
1. Characteristics of compiler used to compile the program.
2. Computer machine on which the program is executed and physically clocked.
3. Multiuser execution system.
4. Number of program steps.
{
Int I;
For(i=0;i<n;i++)
{
If(a[i]==x)
Return I;
}
Return -1;
}
Worst case time complexity
It is the function defined by maximum number of steps taken on any input of size n.
Example:
For linear search the worst case happens when the element to be searched is not present in the array in the
last position of the array.
If the element to be searched is in the middle of the array then the average number of comparison is n/2.
Best case time complexity
It is function defined by the minimum number of steps taken on any input of size n;
In the linear search problem, the best case occurs when the element to be searched is present at the first
position.
In the divide and conquer approach, the problem is divided into several small sub-problems. Then the sub-
problems are solved recursively and combined to get the solution of the original problem.
The divide and conquer approach involve the following steps at each level −
The divide and conquer approach are applied in the following algorithms –
Greedy Method
In greedy algorithm of optimizing solution, the best solution is chosen at any moment. A greedy algorithm is
very easy to apply to complex problems. It decides which step will provide the most accurate solution in the
next step.
This algorithm is a called greedy because when the optimal solution to the smaller instance is
provided, the algorithm does not consider the total program as a whole. Once a solution is considered, the
greedy algorithm never considers the same solution again.
A greedy algorithm works recursively creating a group of objects from the smallest possible
component parts. Recursion is a procedure to solve a problem in which the solution to a specific problem is
dependent on the solution of the smaller instance of that problem.
Dynamic Programming
Dynamic programming is an optimization technique, which divides the problem into smaller sub-problems
and after solving each sub-problem, dynamic programming combines all the solutions to get ultimate
solution. Unlike divide and conquer method, dynamic programming reuses the solution to the sub-problems
many times.
Backtracking Algorithm
In backtracking, we start with a possible solution, which satisfies all the required conditions. Then we move
to the next level and if that level does not produce a satisfactory solution, we return one level back and start
with a new option.
A branch and bound algorithm is an optimization technique to get an optimal solution to the problem. It looks
for the best solution for a given problem in the entire space of the solution. The bounds in the function to be
optimized are merged with the value of the latest best solution. It allows the algorithm to find parts of the
solution space completely.
The purpose of a branch and bound search is to maintain the lowest-cost path to a target. Once a solution is
found, it can keep improving the solution. Branch and bound search are implemented in depth-bounded
search and depth–first search.
Department of computer science
5 SAC, Peruvanthanam