Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Design and Analysis of

Algorithm

SAIRA SULTANA.
Why do we study algorithms?
To know about standard set of important
algorithms from different areas of
computing

You should be able to design new


algorithms and analyze their efficiency

Algorithm usefulness in developing


analytical skills
What is Algorithm?
An algorithm is a finite sequence of
unambiguous instructions to perform a specific
task
Components
An algorithm
◦ has a name
◦ begins with a precisely specified input
◦ and terminates with a precisely specified
output

Input and output are finite


Correct Algorithm
An algorithm is said to be correct if :
1. the algorithm terminates in a finite time;
2. on termination the algorithm returns output
as described in the output specifications.
Example
Algorithm SumOfSquares
INPUT: a; b; where a and b are integers
OUTPUT: c; where c is a sum of the squares of input numbers.
start;
c = a*a + b*b;
return c;
end;

The name of this algorithm is SumOfSquares.


Its input and output are integer sequences of
length 2 and 1, respectively.
Algorithm

At least three important questions need to be


answered for each algorithm

• Is it correct?
• How much time does it take, as a function of n?
• And can we do better?

Being problem solvers, we need to equip ourselves


with the tools and techniques to answer these
questions.
Why need algorithm analysis ?
◦ Writing a working program is not good
enough
◦ The program may be inefficient!
◦ If the program is run on a large data set, then
the running time becomes an issue
How to develop a solution of a problem in
CS?

Understand the problem

Formulate a solution / algorithm

Analyze the algorithm


• Design a program
• Implement the
program
• Execute the code
• Measure the time
See if the solution is ok
Otherwise try to come up with another solution and
again start from step 1
Example:
 A city has n stops. A bus driver wishes to follow the shortest
path from one stop to another. Between very two stops, if a
road exists, it may take a different time from other roads.
Also, roads are one-way, i.e., the road from view point 1 to 2,
is different from that from view point 2 to 1.
 How to find the shortest path between any two pairs?
 A Naïve (Brute Force) approach:
List all the paths between a given pair of view points
Compute the travel time for each.
Choose the shortest one.

 How many paths are there?


end

Start
Example
Develop an algorithm to sum numbers
from 1 to 1000.
Example
 A peasant finds himself on a riverbank with
a wolf, a goat, and a head of cabbage. He
needs to transport all three to the other side
of the river in his boat. However, the boat
has room for only the peasant himself and
one other item (either the wolf, the goat, or
the cabbage). In his absence, the wolf would
eat the goat, and the goat would eat the
cabbage. Solve this problem for the peasant
or prove it has no solution.
Example
There are four people who want to cross a rickety
bridge; they all begin on the same side. You have 17
minutes to get them all across to the other side. It is
night, and they have one flashlight. A maximum of
two people can cross the bridge at one time. Any
party that crosses, either one or two people, must have
the flashlight with them. The flashlight must be
walked back and forth; it cannot be thrown, for
example. Person 1 takes 1 minute to cross the bridge,
person 2 takes 2 minutes, person 3 takes 5 minutes,
and person 4 takes 10 minutes. A pair must walk
together at the rate of the slower person’s pace.
Formal method of algorithmic problem
solving.
1. Understand the problem.
2. Determine the goal of problem.
3. Model the problem.
4. Find the appropriate invariant.
5. Use the invariant to solve the problem.
Exercise 1.2
Q 1, 2, 4, 9

You might also like