0% found this document useful (0 votes)
7 views

Introduction

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Algorithms

Dr. R. Kanchana
To understand …..
• What is an Algorithm
• Types of problems and algorithms
• Steps to problem solving
• Tools for problem solving
• Algorithmic Notation
Algorithm
• An algorithm is an explicit, precise, unambiguous, mechanically-executable sequence of
elementary instructions
• A well-defined computational procedure to solve the problem in a finite number of
steps.
problem

algorithm

input “computer” output


Algorithm - Examples

Algorithm SumOf1toN ( n ) Algorithm InsertionSort(A, n)


Input: A[ ] - Array of n integers
Input: n Output: Array of integers with elements in
Output: sum of integers 1 to n ascending order
var: integer i, sum var: integer I, j, key
1. sum = 0 1. for i = 2 to n
key = A[i]
2. for i = 1 to n j = i - 1;
sum = sum + I while (j > 0) and (A[j] > key)
A[j+1] = A[j]
end for j=j-1
end while
3. return sum
A[j+1] = key
end for
2. return.
Types of problems
• Computational
– Can be solved by a computer
– Can formalize all legal inputs and expected outputs
– Can characterize relationship between output and input
– Eg. sorting, searching, …
• Non computational
– Can not be solved by a computer
– involves intellectual complexity
– Eg. recognizing an object, opinions, emotions
• Algorithm design
– Both art and science
Stages of Problem solving
1. Understanding the problem
– Is the given problem solvable
2. Planning an Algorithm
– computational model, data representation & structure
3. Designing an algorithm
– technique/strategy, specification
4. Validating and verifying an algorithm
– compare actual output with expected output
– mathematical proof - induction
5. Analysing an algorithm
– complexity analysis
6. Implementing an algorithm
– syntax and logical errors, debugging
7. Empirical analysis
– testing with a benchmark dataset
Types of Algorithms
1. Based on Implementation
– Recursive and Non-recursive
– Sequential and parallel
– Exact and approximation
– Deterministic and non-deterministic
2. Based on design
– Brute force, divide & conquer, dynamic programming, greedy,
backtracking, branch & bound
3. Based on area of specialization
– General algorithms, string algorithms, graph algorithms, combinatorial
algorithms, geometric algorithms
4. Based on Tractability
– Easily solvable, unsolvable, intractable
Tools for Problem solving
1. Top-down design / Stepwise refinement
– Starts with outline of the problem and then dividing it into
sub problems
2. Bottom-up design
– Sub problems are first solved and then integrated to
solve the given problem
Structured Programming
– Sequence, selection, repetition
– Subroutines, block structure
– Avoid ‘goto’
Algorithm Specification - Notation
Pseudocode Constructs Constructs
Algorithm <algoname> (<param>) • Conditional repeat
Input: <Description> if (<condn>) ------
Output: <Description> then --------- ------
Var: <list of variables / DS else --------- until (<condn>)
with their types> endif I/O statements
1. …. case (C ) read num
… condn 1 : ------ read num1, num2
n. return / end condn 2: -------
end case print num
Guidelines • Iteration print “Number = “ + num
• Block structure – use tab (No while (<condn>)
space) ------ DONTs
• Step numbers – not line numbers ------ 1. Long sentences
• Assignment - use ← end while e.g. Instead of “initialize I to be
• Comparison operator = for (i = 1 to n) 1, simply write i ← 1
• To exit from loop – use break ----- 2. Random variable names – it
• To jump to a sub algorithm – ------ must be meaningful and must
Call <sub-algor name> (<param>) end for follow consistent rules
3. Prog. Lang. constructs e.g.
++
Review Questions
? Name a problem which cannot be solved by
the computer
? Name the algorithm design techniques you
already know

You might also like