You are on page 1of 57

COM207 Data Structures and

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.

COM 207 DATA STRUCTURES AND


06/03/20 36
ALGORITHMS
Fundamentals of Algorithm cont..
 The space needed by algorithm is the sum of the
following components:-
• A fixed part that is a space required to store certain data and
variables, that are independent of the size of the problem. For
example, simple variables and constants used, program size,
etc.
• A variable part is a space required by variables, whose size
depends on the size of the problem. For example, dynamic
memory allocation, recursion stack space, etc.
• Space complexity S(P) of any algorithm P is S(P) = C + SP(I),
where C is the fixed part and S(I) is the variable part of the
algorithm, which depends on instance characteristic I.

COM 207 DATA STRUCTURES AND


06/03/20 37
ALGORITHMS
Example

•There are three variables A, B, and C and one constant.


Hence S(P) = 1+3. Now, space depends on data types of
given variables and constant types and it will be
multiplied accordingly.

COM 207 DATA STRUCTURES AND


06/03/20 38
ALGORITHMS
Fundamentals of Algorithm cont..
• Time Complexity:-how much time does it take
to run and respond to user? i.e. it represents
the amount of time required by the
algorithm to run to completion.
 It is a function that describes the time of
execution of an algorithm based on its input
parameters. In complex algorithms it is
difficult to find the exact time taken.

COM 207 DATA STRUCTURES AND


42
ALGORITHMS
Fundamentals of Algorithm cont..
• Time requirements can be defined as a numerical
function T(n), where T(n) can be measured as the
number of steps, provided each step consumes
constant time.
• For example, addition of two n-bit integers takes
n steps. Consequently, the total computational
time is T(n) = c*n, where c is the time taken for
the addition of two bits. Here, we observe that
T(n) grows linearly as the input size increases.
 Cost Complexity.
 Effectiveness:-Is the algorithm solving the
COM 207 DATA STRUCTURES AND
06/03/20 43
problem? ALGORITHMS
Algorithm classification
• Algorithms that use a similar problem-solving
approach can be grouped together.
• This classification scheme is neither exhaustive
nor disjoint.
• The purpose is not to be able to classify an
algorithm as one type or another, but to
highlight the various ways in which a problem
can be attacked.

COM 207 DATA STRUCTURES AND


06/03/20 44
ALGORITHMS
A short list of categories
• Algorithm types
– Simple recursive algorithms
– Backtracking algorithms
– Divide and conquer algorithms
– Dynamic programming algorithms
– Greedy algorithms
– Branch and bound algorithms
– Brute force algorithms
– Randomized algorithms
COM 207 DATA STRUCTURES AND
06/03/20 45
ALGORITHMS
Simple recursive algorithms
• A simple recursive algorithm:
– Solves the basic problems using recursion.
– It converts the solution to a simpler sub problem
to arrive at a correct solution.
• It is called simple because several of the other
algorithm types are inherently recursive.

COM 207 DATA STRUCTURES AND


06/03/20 46
ALGORITHMS
Example recursive algorithms
• To count the number of elements in a list:
1.If the list is empty, return zero; otherwise go to
step 2,
2. Leave the first element and go to step 3
3. Count the element remaining and add one to the
result.
4. Start again from step 1

COM 207 DATA STRUCTURES AND


06/03/20 47
ALGORITHMS
Backtracking algorithms
• Backtracking algorithms are based on a depth-first
recursive search. It can be applied only for problems
which have the concept of a partial candidate solution
and a quick test of whether it can possibly be completed
to a valid solution.
• A backtracking algorithm:
– Tests to see if a solution has been found, and if so,
returns it; otherwise
– For each choice that can be made at this point,
• Make that choice
• Recur
• If the recursion returns a solution, return it
– If no choices remain,
06/03/20
return
COM 207 DATA failure
STRUCTURES AND
48
ALGORITHMS
Divide and Conquer
• A divide and conquer algorithm consists of
two parts:
– Divide the problem into smaller subproblems of
the same type, and solve these subproblems
recursively
– Combine the solutions to the subproblems into a
solution to the original problem
• Traditionally, an algorithm is only called
divide and conquer if it contains two or more
recursive calls. Example includes Merge Sort
and Quick Sort
COM 207 DATA STRUCTURES AND
06/03/20 49
ALGORITHMS
Dynamic Programming
• A dynamic programming algorithm remembers past results
and uses them to find new results.
• Dynamic programming is generally used for optimization
problems
– Multiple solutions exist, need to find the “best” one
– Requires “optimal substructure” and “overlapping
subproblems”
• Optimal substructure: Optimal solution contains
optimal solutions to subproblems
• Overlapping subproblems: Solutions to subproblems
can be stored and reused in a bottom-up fashion
• This differs from Divide and Conquer, where subproblems
generally need not overlap.
COM 207 DATA STRUCTURES AND
06/03/20 50
ALGORITHMS
Greedy algorithms
• An optimization problem is one in which you
want to find, not just a solution, but the best
solution.
• A “greedy algorithm” sometimes works well for
optimization problems
• A greedy algorithm works in phases: At each
phase:
– You take the best you can get right now, without regard for
future consequences
– You hope that by choosing a local optimum at each step, you will
end up at a global optimum

COM 207 DATA STRUCTURES AND


06/03/20 51
ALGORITHMS
Example: Counting money
• Suppose you want to count out a certain amount of money,
using the fewest possible bills and coins
• A greedy algorithm to do this would be:
At each step, take the largest possible bill or coin that does
not overshoot
– Example: To make $6.39, you can choose:
• a $5 bill
• a $1 bill, to make $6
• a 25¢ coin, to make $6.25
• A 10¢ coin, to make $6.35
• four 1¢ coins, to make $6.39
• For US money, the greedy algorithm always gives the
optimum solution COM 207 DATA STRUCTURES AND
06/03/20 52
ALGORITHMS
Branch and bound algorithms
• Branch and bound algorithms are generally used for
optimization problems
– As the algorithm progresses, a tree of subproblems is formed
– The original problem is considered the “root problem”
– A method is used to construct an upper and lower bound for a given
problem
– At each node, apply the bounding methods
• If the bounds match, it is deemed a feasible solution to that
particular subproblem
• If bounds do not match, partition the problem represented by that
node, and make the two subproblems into children nodes
– Continue, using the best known feasible solution to trim sections of
the tree, until all nodes have been solved or trimmed.
Example Travelling sales man Problem

COM 207 DATA STRUCTURES AND


06/03/20 53
ALGORITHMS
Brute force algorithm
• A brute force algorithm simply tries all
possibilities until a satisfactory solution is found
– Such an algorithm can be:
• Optimizing: Find the best solution. This may require
finding all solutions, or if a value for the best solution is
known, it may stop when any best solution is found
– Example: Finding the best path for a travelling salesman
• Satisfying: Stop as soon as a solution is found that is
good enough
– Example: Finding a travelling salesman path that is within 10%
of optimal

COM 207 DATA STRUCTURES AND


06/03/20 54
ALGORITHMS
Randomized algorithms
• A randomized algorithm uses a random
number at least once during the computation
to make a decision. It employs a degree of
randomness as part of its logic.
– Example: In Quicksort, using a random number to
choose a pivot.
– Example: Trying to factor a large number by
choosing random numbers as possible divisors.

COM 207 DATA STRUCTURES AND


06/03/20 55
ALGORITHMS
Class
• What are the differences between algorithm,
program, and data

COM207 56
Exercise
• What are the properties of an algorithm?

• Why do we study algorithms and


performance?
• Explain the gcd as a notion of algorithm

• What are the basic issues related to


algorithms?
COM207 57

You might also like