Sunday, February 16, 2014

Algorithm Introduction

Definition

An algorithm is a finite set of discrete statements (or Steps) in some particular sequence (or Order) to accomplish a predefined particular task.

Properties:  

An algorithm must have the following properties:

Input(s)      : An algorithm must have one(1) or more pre-specified input(s).
Output(s)     : An algorithm must have one(1) or more output(s).
Definiteness  : Each steps of an algorithm must define clearly (i.e. without any confusion or Contradiction or Ambiguity).
Finiteness    : Each steps of an algorithm must be terminated in finite(tolerable) amount of time.
Effectiveness : Any calculations performed / Decision taken by a computer or any electronic machine with respect to an algorithm should be computable by the human beings with pencil and paper although time may be longer.
Correctness   : An algorithm should always produce correct result with respect to it's domain ( of the inputs ).



Classification of Algorithms in terms of Flow of Control
Algorithm can be classified two category as follows:

  • Iterative Algorithm: In this category of algorithm mainly Loop are used to solve a difficult problem in simple manner.
  • Recursive Algorithm: In this category of algorithm the concept of Recursion are used to solve a difficult problem in simple manner.    
What is Loop?
Loop is a concept where a set of statement(s) written in a block with in the algorithm will be computed a finite number of times, limited by some condition(s).

Example of an iterative algorithm:

Sample-algorithm(N, s)
1. i = 0, s = 0.
2. WHILE ( i <= N)        [ Start of Loop ]
      s = s + i.
      i = i + 1 
   EndWhile               [ End of Loop ]
3. Return



What is Recursion?
In recursion, in contrast with loop, has the following properties:
  • An algorithm should be capable to Call Itself.
  • There should be some Base Criteria, for which the algorithm should not call itself.
  • Each time the algorithm will call itself, result should converge toward the solution of the problem.

Example of an recursive algorithm:

Sample-algorithm(N, s)
1. i = 0, s = 0.
2. Call Rec-Add(N, s, i)
3. Return

Rec-Add(N, s, i)
1. IF (i > N ) Return.           [ Base Criteria, No More Call ]
2. s = s + i.
3. Call Rec-Add(N, s, (i+1))     [ Calling itself ]
4. Return.

Here Rec-Add is defined recursively not iteratively. 

Reference - here

0 comments:

Post a Comment