Algorithmic Problem Solving
Algorithmic Problem Solving
What is Problem?
Examples
Sorting - Sort a list S of n numbers in non-decreasing order
Program
Programming
Problem Language
Statement Statements
C/C++
you can’t start writing a computer program directly, Because Statements
The next step towards solving the problem is developing the algorithm.
As there can be more than one algorithm to solve the same problem.
problem
and
Algorithms for the same problem can be based on very different ideas and can solve the
problem with dramatically different speeds.
So,
We will select the best among them.
1
Algorithmic Problem Solving Algorithms: Introduction
There can be more than one algorithm to solve the same problem.
problem
EXAMPLE
Problem: Sorting
Algorithms
Insertion Sort
Bubble Sort Brute force
Selection Sort
Merge Sort Divide & Conquer
Quick Sort etc.
1
Algorithmic Problem Solving Algorithms: Introduction
Brute Force
es
Divide and Conquer e c hni q u
t
o r th es e
Dynamic Programming
ble to f
aila
e av
Greedy Algorithms
hms ar
alg orit
Back Tracking
i c ie nt
Eff
Branch and Bounds
The knowledge of these problem solving strategies prevent us from investigating newer
strategy for our problem.
We can find the resemblance of our problem with the problems that have been solved
using these techniques, so we can apply the same problem solving strategy to solve our
problem.
Algorithmic Problem Solving Algorithms: Introduction
Once the algorithm has been specified, you have to prove its correctness.
and
An incorrect algorithm
EXAMPLE
Greatest Common Divisor : (Consecutive Integer Checking Algorithm)
GCD ( m , n )
t = min(m, n)
while (t<=0) {
if (m % t = 0){
This algorithm does not work correctly when one
if (n % t = 0) of its input numbers is zero.
return t;
}
t = t – 1;
}
Observation:
An incorrect algorithm may work correctly for majority of inputs but crash on some
boundary value.
Algorithmic Problem Solving Algorithms: Introduction
Incorrect Algorithm
if we want to find GCD of (6,0),
Example: Find the greatest common divisor of 60, 24
It fails to produce correct result.
GCD(6,0):
6 mod 0 (Crash)
Solution:
Obviously, such a common divisor can not be greater than the smaller of these numbers
Why it is necessary ?
Imagine what will happen if the computer inside missile has an program implemented by
using incorrect algorithm due to which missile strike your own city rather than the enemy
site.
In applications like
atomic power plants
Industrial safety systems
1
Algorithmic Problem Solving Algorithms: Introduction
by simply taking each of the possible input data, feeding it to the program, and
showing that the result is correct.
Tracing the algorithm’s correctness for a few specific inputs is not sufficient
you have to prove it for every input instance.
One of the most difficult requirement to satisfy is to show that a program works correctly,
outputting results in complete range of data.
How can computer programmer be sure that program is correct, without exhaustive
testing.
r ithm
Solving a Problem With a Computer lg o
i ngA
al ys
n
is A
t step
Algorithm Design and Analysis nex
Try Another Design Model The Program
Analysis 1
Analysis 2
Programming
Correctness
Problem Algorithm Analysing Language
of
Statement Design Algorithm Statements
Algorithm
Suppose a program runs for 4 hours, has not generated any result,
We do not know, either
To avoid this situation, we have to analyse the algorithm before constructing the program.
1
Algorithmic Problem Solving Algorithms: Introduction
If solution to the problem obtained in reasonable time and there are more than one
solution exist then decide which one is the best.
1
Algorithmic Problem Solving Algorithms: Introduction
Case 1:
If solution to the problem obtained in reasonable time and there are more than one solution
exist
EXAMPLE:
Describe the algorithm for finding the maximum value in a finite sequence of integer.
Algorithm 1 Algorithm 2
Algorithm maximum (a1, a2, a3, . . . , an) Algorithm maximum (A)
max = a1 for j = 2 to n // Using Insertion Sort
for i = 2 to n key = A[ j ]
if max < i then i=j–1
max = ai while i > 0 and A[ i ] > key
return max A[ i+1 ] = A[ i ]
i=i–1
A[ i+1 ] = key
Time Complexity: n comparisons
return A[size-1]
Case 2:
If solution is not obtained in reasonable time, change your strategy (try for another solution)
Case 2:
If solution is not obtained in reasonable time, change your strategy (try for another solution)
} 1
Algorithmic Problem Solving Algorithms: Introduction
Program
Programming
Correctness
Problem Algorithm Analysing Language
of
Statement Design Algorithm Statements
Algorithm
C/C++
Statements
Program
Exact
Programming
Vs Algorithm Correctness
Problem Analysing Language
Approximate Design of
Statement Algorithm Statements
Design Algorithm
Technique
C/C++
Statements
Thank You