Vous êtes sur la page 1sur 28

aqadir@jinnah.edu.

pk 1
Why do we study algorithms?

aqadir@jinnah.edu.pk
Algorithm?

• an algorithm is a finite sequence of


unambiguous instructions to perform a
specific task

aqadir@jinnah.edu.pk
To develop a computer based
solution of a problem

aqadir@jinnah.edu.pk
Why do we study algorithms?
• you have to know a standard set of important
algorithms from different areas of computing

• in addition, you should be able to design new


algorithms and analyze their efficiency

• Another reason for studying algorithms is


their usefulness in developing analytical skills
aqadir@jinnah.edu.pk 5
Algorithm

aqadir@jinnah.edu.pk 6
Algorithmics: the Spirit of Computing
• As per David Harel (A writer on Algorithms)
• Algorithmics is more than a branch of
computer science. It is the core of computer
science, and, in all fairness, can be said to be
relevant to most of science, business, and
technology. [Har92, p. 6]

aqadir@jinnah.edu.pk 7
Donald Knuth, one of the most prominent computer scientists in
the history of algorithmic, put it as follows:

A person well-trained in computer science knows how to deal with algorithms:


how to construct them, manipulate them, understand them, analyze them.
This knowledge is preparation for much more than writing good computer
programs; it is a general-purpose mental tool that will be a definite
aid to the understanding of other subjects, whether they be
chemistry, linguistics, or music, etc.
The reason for this may be understood in the following way:
It has often been said that a person does not really understand something
until after teaching it to someone else. Actually, a person does not really
understand something until after teaching it to a computer, i.e.,
expressing it as an algorithm . . . An attempt to formalize things as
algorithms leads to a much deeper understanding than if we simply try to
comprehend things in the traditional way. [Knu96, p. 9]

aqadir@jinnah.edu.pk 8
Algorithm?
• An algorithm has a name, begins with a
precisely specified input, and terminates with
a precisely specified output
• Input and output are finite sequences of
mathematical objects

aqadir@jinnah.edu.pk
Correct Algorithm
• An algorithm is said to be correct if given input
as described in the input specifications:
1. the algorithm terminates in a finite time;
2. on termination the algorithm returns output as
described in the output specifications.

aqadir@jinnah.edu.pk
Algorithm?
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.
• In this course all algorithms are functions, whereby the
output follows from the input through a finite sequence of
deterministic steps; that is, the outcome of each step
depends only on the outcome of the previous steps.
aqadir@jinnah.edu.pk
Algorithm?
• Algorithms do not necessarily represent
functions.
• The instruction: `Toss a coin; if the outcome is
head, add 1 to x, otherwise, do nothing' is a
legitimate and unambiguous, but not
deterministic
• The output of an algorithm containing such
instruction is not a function of the input alone
• Algorithms of this kind are called probabilistic

aqadir@jinnah.edu.pk
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.

aqadir@jinnah.edu.pk 13
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
aqadir@jinnah.edu.pk 14
Algorithmic Problem Solving.
1. Understand the problem.
2. Ascertaining the Capabilities of the Computational
Device
3. Choosing between Exact and Approximate Problem
Solving
4. Algorithm Design Techniques
5. Designing an Algorithm and Data Structures
6. Methods of Specifying an Algorithm
7. Proving an Algorithm’s Correctness
8. Analyzing an Algorithm
9. Coding an Algorithm

aqadir@jinnah.edu.pk
Exercise 1.2

aqadir@jinnah.edu.pk 16
Important Problem Types

aqadir@jinnah.edu.pk
Important Problem Types
• Sorting
• Searching
• String processing
• Graph problems
• Combinatorial problems
• Geometric problems
• Numerical problems

aqadir@jinnah.edu.pk 18
Sorting
• What is sorting?
• Why do we need sorting?
• Two Important properties of a Sorting Algo
– A sorting algorithm is called stable if it preserves
the relative order of any two equal elements in
its input
– Amount of extra memory the algorithm requires
• An algorithm is said to be in-place if it does
not require extra memory, except, possibly, for
a few memory units
aqadir@jinnah.edu.pk 19
Searching
• What is Searching?
• Why do we need searching algo?
• Issues
• underlying data may change frequently
relative to the number of searches
• organizing very large data sets for efficient
searching poses special challenges with
important implications for real-world
applications
aqadir@jinnah.edu.pk 20
String processing
• What is string processing
• string matching

aqadir@jinnah.edu.pk 21
Graph Problems
• Graphs
• Graphs can be used for modeling a wide variety
of applications
• Basic graph algorithms include graph-traversal
algorithms
• shortest-path algorithms
• topological sorting for graphs with directed edges
• Some graph problems are computationally very
hard

aqadir@jinnah.edu.pk 22
Combinatorial Problems
• These are problems that ask, explicitly or
implicitly, to find a combinatorial object—such
as a permutation, a combination, or a
subset—that satisfies certain constraints
• A desired combinatorial object may also be
required to have some additional property
such as a maximum value or a minimum cost.

aqadir@jinnah.edu.pk 23
Geometric Problems
• Geometric algorithms deal with geometric
objects such as points, lines, and polygons.
• The closest-pair problem
– given n points in the plane, find the closest pair
among them
• The convex-hull problem
– to find the smallest convex polygon that would
include all the points of a given set

aqadir@jinnah.edu.pk 24
Numerical Problems
• that involve mathematical objects of
continuous nature:
• solving equations and systems of equations,
computing definite integrals, evaluating
functions, and so on.

aqadir@jinnah.edu.pk 25
Exercise 1.3

aqadir@jinnah.edu.pk 26
Fundamental Data Structures
• Linear Data Structures
• Graphs
• Trees
• Sets and Dictionaries

aqadir@jinnah.edu.pk 27
Exercise 1.4

aqadir@jinnah.edu.pk 28

Vous aimerez peut-être aussi