You are on page 1of 57

Design and Analysis of Algorithm

Tanveer Ahmed Siddiqui

Department of Computer Science


COMSATS University, Islamabad
Recap

Lecture No 1

Department of Computer Science 2


Recap

Algorithm It is sequence of unambiguous instructions for solving a


(Definition) particular problem in a finite amount of time

Properties Input, Output, Precision, Determinism, Finiteness,


Definiteness, Correctness, Generality
Building Blocks Sequence, Decision Structure, Repetition Structure

Representation Structured Description, Flow Chart, Pseudo Code

Important Point There may exist several algorithms for solving the same
problem.

Department of Computer Science


YOUR TURN

Department of Computer Science


TASK 1

 Design an algorithm to calculate which known


locations are closest to current known
location.

Department of Computer Science


Lecture No 2

Framework for designing and


analyzing algorithms for new problems

(Fundamentals of Algorithmic Problem Solving)

Department of Computer Science 6


Today Covered

After completing this lecture, you will be able to


understand:
 Framework for designing and analyzing
algorithms for new problems
 A sequence of steps one typically goes through in
designing an algorithm
 Problem-solving strategy with a computer
 Fundamental design concepts and principles
 Design algorithms for solving simple
problems.

Department of Computer Science


Two main issues related to algorithms

 Solving problem by a computer is challenging and


exciting with possibilities for creative satisfaction(why)
 when we are solving a problem with a computer, we are
essentially teaching a dumb servant how to do a job?
 Solving a problem (or getting job done) by a computer
can be quite a demanding task(why)
 Because it requires:
 Considerable thoughts
 Careful planning, and
 Attention to details

Department of Computer Science


TASK 2

 Outline a sequence of steps one typically goes


through in designing an algorithm(You may give your
description in English)

Department of Computer Science


Steps of development of an algorithm

 Understand the problem /Writing


the statement of the problem
 Develop a model/choose
appropriate data structure
 Select appropriate designing
technique.
 Specify the algorithm
 Prove correctness of the algorithm
 Analyze the complexity of the
algorithm
 Implement the algorithm/code

Department of Computer Science


Steps of development of an algorithm

Designing an algorithm is easy if you

Understand
the
problem

Department of Computer Science


Understanding the problem

“problem definition” phase.


 Decide what must be done(understand clearly the requirements
in a particular problem).
 From the problem statement which may be vague or imprecise, we have to
find out a set of precisely define tasks(how).

PROBLEM:
Some friends of yours are working on techniques for coordinating
groups of mobile robots. Each robot has a radio transmitter that it
uses to communicate with a base station, and your friends find that
if the robots get too close to one another, then there are problems
with interference among the transmitters. So, a natural problem
arises how to plan the motion of the robots in such a way that each
robot gets to its intended destination, but in the process the robots
don’t come close enough together to cause interference problems.

Department of Computer Science


Understanding the problem

 From the problem statement which may be vague or


imprecise, we have to find out a set of precisely
define tasks(how).
 Ask originator of the problem various questions and answer
them, until the problem is clear.
 Do a few small examples by hand, think about special cases,
etc.
 Some relevant questions that can be asked are:
 Do we understand the vocabulary used in
the raw statement of the problem?
 The problem may be in a particular field having its own
vocabulary or technical words. Do we understand them clearly,
at least clear enough to develop the algorithm?
 E.g., what does interference- free schedule means in above
Department ofproblem?
Computer Science
Understanding the problem

 What information is given?


 Precisely what are the given data? In other words, what is the
input to algorithm?
 What are the given conditions?
 What is to be found out?
 What should be the output from the algorithm and the
corresponding program?
 Trivial as it may sound answering this question is not that easy
when it comes to quite a few real-life problems.
 How to recognize a valid solution?
 We should understand how precisely a correct solution is
defined; so that we can include in the algorithm means to
detect them and reject invalid or unacceptable solutions.

Department of Computer Science


Understanding the problem

 What information required to define the


algorithm, is missing?
 The raw problem statement may not have given all the
necessary information, or some information may be vague or
conflicting.

Department of Computer Science


Understanding the problem

 What assumptions have been made?


 Most of the times, assumptions are unstated, even the originator
of problem may not be aware of them.
 They are to identified and taken care of while developing the
algorithm.
 For example, “write a function which, given an integer
number input, outputs its factorial”.
 Here, the fact that the integer is non-negative is unspecified, but
assumed.
 Also, the assumption that function is supposed to work only up to
some relatively small input (like 20 or so) is unstated.
 Many more questions are possible depending upon
the field and nature of the problem.

Department of Computer Science


Steps of development of an algorithm

Designing an algorithm is easy if you

Develop a
Model

Department of Computer Science


Developing a model
 A model is an abstraction of reality.
 It will help people to represent a reality in an
understandable form.
 We have to develop a model where calculations can
be done.
 A model may be in the form of a
 Graph
 Matrix
 Equation(s)
 sequence of logic
 Mathematical problem with an objective function and
a set of constraints etc.
There are no specific rules for this step, mainly experience
countsDepartment
and ofthis is where the art of programming comes in.
Computer Science
Develop a Model
PROBLEM:
Some friends of yours are working on techniques for
coordinating groups of mobile robots. Each robot has a
radio transmitter that it uses to communicate with a base
station, and your friends find that if the robots get too
close to one another, then there are problems with
interference among the transmitters. So a natural
problem arises: how to plan the motion of the robots in
such a way that each robot gets to its intended
destination, but in the process the robots don’t come
close enough together to cause interference problems.
How can we model this problem?

We can model this problem with an undirected graph.

Department of Computer Science


Steps of development of an algorithm

Designing an algorithm is easy if you


know

Design
Techniques

Department of Computer Science


Design Techniques

 An algorithm design techniques (or “strategy” or


“paradigm”) is a general approach for solving
problems algorithmically that is applicable to a
variety of problems from different area of
computing.
 The design approach depends mainly on the
model chosen.

Department of Computer Science


Algorithm design techniques/strategies

Department of Computer Science


steps of development of an algorithm

Designing an algorithm is easy if you


know

Methods of
Specifying an
Algorithm

Department of Computer Science


Methods of Specifying an Algorithm

 Commonly used methods as discussed in first


lecture are:
 Flow chart
 Natural language
 Pseudo code
 A mixture of a natural language and programming
language-like structures
 Precise and succinct.
 Pseudo-code in this course
 omits declarations of variables
 use indentation to show the scope of such statements as
for, if, and while.
 use  for assignment

Department of Computer Science


Pseudo code Details

 Control flow
if … then … [else …]

 while … do …  Expressions
 repeat … until …
 Assignment
 for … do … (like  in Java)
 Indentation replaces braces  Equality testing
 Method declaration (like  in Java)
Algorithm method (arg [, arg…]) n 2
Superscripts and other
mathematical
Input …
formatting allowed
Output …

Department of Computer Science 25


Steps of development of an algorithm

Designing an algorithm is easy if you

Prove the
Correctness of
the Algorithm

Department of Computer Science


Checking the correctness of the algorithm
 Once an algorithm has been specified, you
have to prove its correctness which is one of
the difficult, but essential task in algorithm
development.
 The correctness of algorithm means the
correctness of the logic/steps of the
algorithm.
 How can be sure that algorithm always
produces the correct output?

Department of Computer Science


Checking the correctness of the algorithm

 One possible method: tracing the algorithm’s


performance for few specific inputs can be very
worthwhile activity.(Dry run)
 Make a Trace Table: usually takes the form of a multi-
column, multi-row table; With each column showing a
variable, and each row showing each number input into
the algorithm and the subsequent values of the
variables.
 This approach cannot prove the algorithm’s
correctness conclusively(why)
 It is impossible to test the algorithm for all set of inputs
values.
 It is not sufficient for programs for applications where
reliability
Department is of paramount importance.
of Computer Science
Checking the correctness of the algorithm

 Other way: ?
 rely on some mathematical proof to ensure that the
algorithm will always yield the correct output.
 Correctness of algorithm can be confirmed by the
uses of some:
 Inference Rules
 Proving Techniques.

Department of Computer Science


Checking the correctness of the algorithm
 A common technique for proving correctness of
an algorithm is to use the
 Mathematical Induction(Why)
 because an algorithm’s iteration provides a natural
sequence of steps needed for such proofs.
 Try to justify each step. This may involve some condition
which existed before and after the step is executed.
 Try to provide some proof that algorithm will terminate
and while doing so, it would have read all appropriate
input data and would have generated all the expected
output.

Department of Computer Science


Steps of development of an algorithm

Analyzing the
Complexity of
the algorithm

Department of Computer Science


Analyzing the complexity of the algorithm

 After correctness, by far the most important is


efficiency.
 There are two kinds of efficiency:
 Time efficiency: indicates how fast the algorithm runs
 Space efficiency. indicates how much extra memory
the algorithm needs.

Department of Computer Science


Examples

Department of Computer Science


Example-1
 Problem: Design an algorithm that check whether all
the elements in a given array of n elements are
distinct.
 Solution:
 What is input
 An array A of n numbers e.g. 4 1 8 9 3 7 10 2 3 1

 What should be the output


 Returns “true” if all the elements in A are distinct
 Which designing technique?
 Brute-Force

 Logic/Idea
Take the first element and check whether it is in the rest of the array,
take the second element and check if it is in the rest of the array, and
so on…
Department of Computer Science
Example-1
 Problem: Design an algorithm that check whether all
the elements in a given array of n elements are
distinct.
 Logic/Idea
Take the first element and check whether it is in the rest of the array,
take the second element and check if it is in the rest of the array, and
so on…

Department of Computer Science


Example-1
 Problem: Design an algorithm that check
whether all the elements in a given array of n
elements are distinct.
Representation

Department of Computer Science


Example-2

 Problem: Design an algorithm that find the minimum


difference between any pair in the given array.
 Solution:
 Understand the problem
 What information is given?
 An array of numbers.

 What assumptions have been made?


 Given a list of distinct unsorted integers.

 What is to be found out?


 The smallest absolute difference between any pair in

the given array

Department of Computer Science


Example-2

 Problem: Design an algorithm that find the minimum


difference between any pair in the given array.
 Solution:
 Understand the problem
 How to recognize a valid solution?
 Input: {1, 5, 3, 19, 18, 25}
 Output: 1
 Explanation: Minimum difference is between 18 and 19
 Input: {30, 5, 20, 9}
 Output: 4
 Explanation: Minimum difference is between 5 and 9
 Input: {1, 19, -4, 31, 38, 25, 100}
 Output: 5
 Explanation: Minimum difference is between 1 and -4
Department of Computer Science
Example-2

 Problem: Design an algorithm that find the minimum


difference between any pair in the given array.
 Solution:
 Understand the problem
 Which designing technique?
 Brute-force
 Logic/Idea?
Take first element and find its difference from rest of the
numbers in array, take the second element and find its
difference from rest of the numbers in array, and so on…

Department of Computer Science


Example-2
 Problem: Design an algorithm that find the
minimum difference between any pair in the
given array.
Representation

Department of Computer Science


Example-2
 Can you make as many improvements as you
can in this algorithmic solution to the
problem?
 You can: decrease the number of times the
innermost loop is executed, make that loop run
faster (at least for some inputs), or, more
significantly, design a faster algorithm from scratch.

Department of Computer Science


Example-3: Your Turn

 Problem: Design an algorithm that find the two closest


elements in an array of numbers.
 Solution:
 Understand the problem
 What information is given?
 An array of numbers.

 What does two closest elements in an array of


numbers means?
 The pair of elements that have the smallest absolute

difference between them.


 What is to be found out?
 Find the pair of elements that have the smallest

absolute difference between them? If there are multiple


pairs, find them all.
Department of Computer Science
Example-3

 Problem: Design an algorithm that find the two closest


elements in an array of numbers.
 Solution:
 Understand the problem
 What assumptions have been made?
 Given a list of distinct unsorted integers.

 How to recognize a valid solution?


 Input: arr[] = {10, 50, 12, 100}
 Output: (10, 12)
 The closest elements are 10 and 12
 Input : arr[] = {5, 3, 4, 2}
 Output : (2, 3), (3, 4), (4, 5)

Department of Computer Science


Example-3

 Problem: Design an algorithm that find the two closest


elements in an array of numbers.
 Solution:
 Logic/Idea?
Take first element and find its difference from rest of the
numbers in array, take the second element and find its
difference from rest of the numbers in array, and so on.
Return the pair whose minimum distance is minimum.

Department of Computer Science


Example-3

Problem: Design an algorithm that find the two


closest elements in an array of numbers.
Representation

Department of Computer Science


TASK 1

 Problem: Design an algorithm to calculate which known


locations are closest to current known location.
 Solution:
 Understand the problem

Department of Computer Science


TASK 1

 Problem: Design an algorithm to calculate which known


locations are closest to current known location.
 Solution:
 Understand the problem
 What information is given?
 A list P of n (n ≥ 2) points p1(x1, y1), . . . , pn(xn, yn) and current
known location p0(x0, y0),
 What assumptions have been made?
 For simplicity, we consider the two-dimensional case of the closest-
pair problem. We assume that the points in question are specified in
a standard fashion by their (x, y) Cartesian coordinates

Department of Computer Science


TASK 1

 Problem: Design an algorithm to calculate which known


locations are closest to current known location.
 Solution:
 Understand the problem
 What is to be found out?
 Compute the distance between each pair of distinct points and
find a pair with the smallest distance. Of course, we do not
want to compute the distance between the same pair of points
twice. To avoid doing so, we consider only the pairs of
points (pi, pj ) for which i < j.
 How to compute the distance between pair of points?
 This distance is typically measured using the Haversine
formula, which calculates the great-circle distance between
two points on the Earth’s surface.

Department of Computer Science


Develop the formula

 The Haversine Formula:


 Calculate geographic distance on earth
 The haversine formula is a very accurate way of
computing distances between two points on the
surface of a sphere using the latitude and longitude
of the two points.

Department of Computer Science


Develop the Model

 The Haversine Formula:

Department of Computer Science


TASK 1

 Problem: Design an algorithm to calculate which known


locations are closest to current known location.
 Solution:
 What is input
 GPS coordinate of current known location say P(x, y)
 A list L of n GPS coordinate of known locations.
P1 P2 P3 P4 P5 P6
 What should be the output
 Return the index of known location whose distance
from P is minimum
 Which designing technique?
 Brute-force

 Logic/Idea?
Take first element and find its distance from P, take the second
element and find its distance from P, and so on… . Store these
Department of Computer Science
distances in an array A and find minimum from A.
Design Technique

 Brute Force
 Algorithm Steps: Now, let’s outline the
algorithm to calculate the nearest location
based on longitude and latitude:
1. Define the reference point with its longitude and
latitude coordinates.
2. Iterate over the set of locations and calculate the
distance between each location and the reference
point using the Haversine formula.
3. Keep track of the minimum distance encountered so
far and the corresponding location.
4. After iterating over all the locations, the location
with the minimum distance is the nearest location.
Department of Computer Science
Design Technique

Department of Computer Science


Haversine formula

 For your convenience, I have written the


function for Haversine formula in Python
syntax.

Department of Computer Science


What we learn?

CONCLUSION

Department of Computer Science


Conclusion
 Understanding the problem
 Asking questions, do a few examples by hand, think about
special cases, etc.
 Deciding on
 Exact vs. approximate problem solving
 Appropriate data structure
 Choose appropriate Designing technique
 Specify using e.g., Pseudo code.
 Prove correctness
 Analyzing an algorithm
 Time efficiency : how fast the algorithm runs
 Space efficiency: how much extra memory the algorithm needs.
 Simplicity and generality
 Implement/Coding an algorithm.

Department of Computer Science


Conclusion

 As a rule, a good algorithm is


a result of repeated effort and
rework.
 The Joy of Algorithms
Even if you have been fortunate
enough to get an algorithmic idea
that seems perfect, you should still
try to see whether can be
improved.

Department of Computer Science

You might also like