Professional Documents
Culture Documents
Algorithms
L01
–Introduction/Fundamental of
Algorithms
The aim of this module is to:
• Introduce the fundamental algorithms used in
Computer Science
• Provide an appreciation of the concepts
involved in the design and analysis of
algorithms
COM207 2
What is an Algorithm?
• Algorithms are used in solving problems in a
systematic way. It is difficult to give a single specific
definition for an algorithm. Different fields of
development and study use algorithms to solve
their own problems.
• Levitin describes An algorithm as a sequence of
unambiguous (not open to more than one
interpretation or clearly expressed/understood)
instructions for solving a problem, i.e., for
obtaining a required output for any legitimate
input in a finite amount of time.
COM207 3
What is an algorithm cont..
4
What is an Algorithm cont.…
• The reference to “instructions” in the definition
implies that there is something or someone capable
of understanding and following the instructions
given.
• We call this a “computer,” keeping in mind that
before the electronic computer was invented, the
word “computer” meant a human being involved in
performing numeric calculations. Nowadays, of
course, “computers” are those ubiquitous electronic
devices that have become indispensable in almost
everything we do.
COM207 5
DEFINITIONS OF ALGORITHM
• An algorithm is defined as a set of well defined
instructions used to accomplish a particular
task. It is considered as the cornerstone of
good programming. The efficiency of
algorithms depends on speed, size and
resource consumption.
• Different algorithms may finish the same
process with a different set of instructions in
more or less time, space or effort than others.
There is no fixed definition for an algorithm. It
varies according to the area of use.
COM207 6
VARIOUS DEFINITIONS OF
ALGORITHM
• It is the exact set of instructions describing the
order of actions to achieve the result of the
decision problem in the finite time.
• Algorithm is a finite set of rules that defines
the sequence of operations to solve a specific
set of goals and has five important features
finiteness, definiteness ,input, output,
efficiency.
COM207 7
VARIOUS DEFINITIONS CONT..
• Algorithms are all systems of calculations
performed strictly defined rules which after a
number of steps obviously leads to a the
solution of the problem.
• Algorithm is the exact prescription, defining
the computing process, going from the
variable input data to the desired result.
• Algorithm is the exact requirement of the
performance in a specific order of certain
operations, leading to the solution of all
problems of this type.
COM207 8
IMPORTANCE OF ALGORITHM IN
COMPUTING
• Humans believe that a computer can do
anything and everything that we imagine. The
truth about computer is that they work on
algorithms written by humans themselves and
there is a need to write more software
algorithms due to increasing complexity of
computers.
• Algorithms are available for many different
applications and highly advanced systems such
as artificial intelligence.
COM207 9
Properties/Characteristics of
an Algorithm
• An algorithm may have zero or more inputs
externally and it should produce one or more
output. Also, an algorithm must terminate after a
finite number of steps. Properties of algorithm
include:
Correctness:-it should provide correct and
accurate output for all legitimate input.
Finiteness:-when we trace out the instructions of
an algorithm, it has to terminate after a finite
number of steps for all cases.
COM207 10
Properties of Algorithm Cont..
• Generality:-Algorithms need to have general
instructions which can be applied in any case.
• Clearly specified Input: An algorithm should
have 0 or more well-defined inputs.
• Feasibility − Should be feasible with the
available resources.
• Independent − An algorithm should have step-
by-step directions, which should be
independent of any programming code.
COM207 11
Properties of Algorithm Cont..
• Clearly specified/expected Output: An
algorithm should have 1 or more well-defined
outputs, and should match the desired output.
• Effectiveness: steps are sufficiently simple and
basic
• Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases),
and their inputs/outputs should be clear and
must lead to only one meaning.
COM207 12
Why study algorithms and
performance?
• Algorithmic mathematics provides a language
for talking about program behaviour
• Theoretical importance – core of computer
science
• Practical importance – for designing and
analysing algorithms for new problems
COM207 13
Practical Applications of Algorithm
• Do you use your GPS to find a route to travel? It runs
what we call a “shortest-path” algorithm to find the
route.
• Do you buy products on the Internet? Then you use
(or should be using) a secure website that runs an
encryption algorithm. When you buy products on the
Internet, are they delivered by a private delivery
service? It uses algorithms to assign packages to
individual trucks and then to determine the order in
which each driver should deliver packages.
COM207 14
Practical Applications….
• Algorithms run on computers all over the place
—on your laptop, on servers, on your
smartphone, on embedded systems (such as in
your car, your microwave oven, or climate-
control systems)—everywhere!
• Communication
• Pattern Recognition
• Robotics
• Computer graphics
COM207 15
ALGORITHM,PROGRAM AND
PSEUDOCODE
• An algorithm is a finite set of unambiguous
statement to solve a problem in finite amount
of time. It can be natural language expressions
designed for a common man.
• A program can be called as an algorithm
implemented using the required data
structure. It is the expression of an algorithm
in a programming language with all the
language specific codes. Procedure,function
and subroutine are synonyms for program.
COM207 16
Algorithm, Program and Pseudocode
Cont..
• A pseudocode is a compact, informal
environment-independent description of a
computer programming language. It uses the
structural conventions of a programming
language but only humans can read it and not
machines. Pseudocode omits details that are
not essential for human understanding of the
algorithm such as variable declaration,
system-specific code and subroutines. There is
no specific standard for pseudocode syntax.
COM207 17
Problems in which Algorithms Can Be
Applied
• SORTING:- The sorting problem is to rearrange the
items of a given list in non decreasing order. As a
practical matter, we usually need to sort lists of
numbers, characters from an alphabet, character
strings, and, most important, records similar to those
maintained by schools about their students, libraries
about their holdings, and companies about their
employees.
COM207 18
The problem of sorting
• Input: sequence 〈a1, a2, …, an〉 of numbers
• Output: permutation 〈a'1, a'2, …, a'n〉 such
that a'1≤a'2≤…≤a'n
Example:
Input:8 2 4 9 3 6
Output:2 3 4 6 8 9
COM207 19
Problems…
• SEARCHING:-The searching problem deals with finding
a given value, called a search key, in a given set (or a
multiset, which permits several elements to have the
same value). There are plenty of searching algorithms
to choose from. They range from the straightforward
sequential search to a spectacularly efficient but
limited binary search and algorithms based on
representing the underlying set in a different form
more conducive to searching.
COM207 20
Problems…
• STRING PROCESSING:-A string is a sequence of
characters from an alphabet.Strings of particular
interest are text strings, which comprise letters,
numbers, and special characters; bit strings, which
comprise zeros and ones; and gene sequences,which
can be modeled by strings of characters from the
four-character alphabet {A,C, G, T}.
COM207 21
Problems…
• GRAPH PROBLEMS:- Graphs can be used for modeling
a wide variety of applications, including
transportation, communication, social and economic
networks, project scheduling, and games. Studying
different technical and social aspects of the Internet in
particular is one of the active areas of current
research involving computer scientists, economists,
and social scientists. Basic graph algorithms include
graph-traversal algorithms (how can one reach all the
points in a network?), shortest-path algorithms (what
is the best route between two cities?), and topological
sorting for graphs with directed edges
COM207 22
Problems…
• 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.
COM207 23
Problems…
• GEOMETRIC PROBLEMS:- Geometric algorithms deal
with geometric objects such as points, lines, and
polygons. The ancient Greeks were very much
interested in developing procedures (they did not call
them algorithms, of course) for solving a variety of
geometric problems, including problems of
constructing simple geometric shapes—triangles,
circles, and so on—with an unmarked ruler and a
compass.
COM207 24
Problems…
• NUMERICAL PROBLEMS:- Numerical problems,
another large special area of applications, are
problems that involve mathematical objects of
continuous nature: solving equations and systems of
equations, computing definite integrals, evaluating
functions, and so on.
COM207 25
Writing an Algorithm
• Design an algorithm to add two numbers and
display the result.
COM207 26
Alternatively, it can also be
written as follows
COM207 27
Explaining the Notion of
Algorithm(Greatest Common
Divisor)
The first algorithm “invented” in history was Euclid’s
algorithm for finding the greatest common divisor
(GCD) of two natural numbers. Three(3) methods for
solving the same problem i.e computing the greatest
common divisor of two integers. These examples will
help us to illustrate several important points:-
•The nonambiguity requirement for each step of an
algorithm cannot be compromised.
COM207 28
Euclid’s Algorithm
• The range of inputs for which an algorithm works has
to be specified carefully.
• The same algorithm can be represented in several
different ways.
• There may exist several algorithms for solving the
same problem.
• Algorithms for the same problem can be based on
very different ideas and can solve the problem with
dramatically different speeds.
COM207 29
Euclid’s Algorithm…
• DEFINITION:- The greatest common divisor of two
nonnegative, not-both-zero integers m and n,
denoted gcd (m, n), is defined as the largest integer
that divides both m and n evenly, i.e., with a
remainder of zero.
• Euclid of Alexandria (third century b.c.) outlined an
algorithm for solving this problem in one of the
volumes of his Elements most famous for its
systematic exposition of geometry. In modern terms,
Euclid’s algorithm is based on applying repeatedly
the equality.
COM207 30
Euclid’s Algorithm…
gcd(m, n) = gcd(n, m mod n),
where m mod n is the remainder of the division of m by
n, until m mod n is equal to 0. Since gcd (m, 0) = m
because the last value of m is also the greatest common
divisor of the initial m and n.
Note:-it is termed common factor or divisor.
COM207 31
Euclid’s Algorithm….
• Problem: Find gcd(m,n), the greatest common divisor
of two nonnegative, non zero integers m and n
• Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0)
=?
Euclid’s algorithm is based on repeated application of
equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the
problem trivial
• Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
COM207 32
Two descriptions of Euclid’s
algorithm
COM207 33
Euclid’s Algorithm..
• The algorithm can also be expressed in a simpler and
elementary way as:-
COM207 34
Basic Issues Related to
Algorithms
• How to design algorithms
• How to express algorithms
• Proving correctness
• Efficiency (or complexity) analysis
– Theoretical analysis
– Empirical analysis
• Optimality
COM207 35
Fundamentals of Algorithm
• How do we select the best algorithm?
An efficient algorithm is one which solve the
problem within space and time resource
constraints.
Space Complexity:- how much storage space
does it take ? i.e Memory space
Space Complexity is defined as the amount of
memory that an algorithm needs.
COM207 56
Exercise
• What are the properties of an algorithm?