DM Set07 Algorithms
DM Set07 Algorithms
Tassos Dimitriou
CpE-203
Discrete Structures
Set 7
Tassos Dimitriou
Outline
Algorithms
Why study them?
Definitions
Problems and instances
Analysis of Algorithms
Examples
1
1/17/2019
Tassos Dimitriou
Algorithms
There are many general classes of problems that arise in
discrete mathematics.
Given a sequence of integers, find the largest one; given a set, list
all its subsets; given a set of integers, put them in increasing
order; given a network, find the shortest path between two
vertices, and so on.
First thing to do is to construct a model. Discrete structures used
in such models include sets, sequences as well as permutations,
relations, graphs, trees, networks, etc..
Tassos Dimitriou
2
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
What is an Algorithm?
An algorithm is a procedure for solving a usually
complicated problem by carrying out a precisely
determined sequence of simpler, unambiguous steps.
This is a rather
Step 1: Do something vague definition.
You will get to know
Step 2: Do something a more precise
Step 3: Do something else definition when you
attend CpE300
…
3
1/17/2019
Tassos Dimitriou
A tribute to Al-Khawarizmi
The word algorithm comes from the world algorism which means
working with decimal numerals.
Abu Abdullah Mohammad Ibn Musa al-Khawarizmi was born at
Khawarizm (Kheva), south of Aral sea. Very little is known about his
early life, except for the fact that his parents had migrated to a place
south of Baghdad.
His work on algebra was outstanding, which established him as the
founder of Algebra. The very name Algebra has been derived from his
famous book Al-Kitāb al-mukhtaṣar fī hīsāb al-ğabr wa’l-muqābala. It
was this book which introduced this new science to the West
“completely unknown till then”.
The influence of Khawarizmi on the growth of science, in general, and
mathematics, astronomy and geography in particular, is well
established in history. Several of his books were readily translated into
a number of other languages, and, in fact, constituted the university
textbooks till the 16th century.
Tassos Dimitriou
Properties of Algorithms
• Input from a specified set,
• Output from a specified set (solution to the problem),
• Definiteness of every step in the computation,
• Correctness of output for every possible input,
• Finiteness of the number of calculation steps,
• Effectiveness of each calculation step and
• Generality for a class of problems.
4
1/17/2019
Tassos Dimitriou
Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
Instances
Input: 5 -1 2 1 9 3 4 8
Output: -1 1 2 3 4 5 8 9
CpE-203: Discrete Structures 9
Tassos Dimitriou
Algorithm Examples
Can be described using pseudocode, which slightly reminds
us of JAVA or C, etc.
5
1/17/2019
Tassos Dimitriou
Linear search
An algorithm that linearly searches a sequence for a particular
element.
if i n then
OUTPUT “Found at location i"
else
OUTPUT “Not Found“
Tassos Dimitriou
Binary search
But what if the terms in a sequence are ordered? Can we do
better than just scanning a list from left to right?
6
1/17/2019
Tassos Dimitriou
search interval
a c d f g h j l m o p r s u v x z
center element
Tassos Dimitriou
search interval
a c d f g h j l m o p r s u v x z
center element
7
1/17/2019
Tassos Dimitriou
search interval
a c d f g h j l m o p r s u v x z
center element
Tassos Dimitriou
search interval
a c d f g h j l m o p r s u v x z
center element
8
1/17/2019
Tassos Dimitriou
search interval
a c d f g h j l m o p r s u v x z
center element
Found !
CpE-203: Discrete Structures 17
Tassos Dimitriou
Binary search
// Find x in ordered list a1, a2, …, an
while (i < j) do
m := (i + j)/2 // middle point
if x > am then i := m + 1
else j := m
end-while
9
1/17/2019
Tassos Dimitriou
Complexity of algorithms
Big questions
When does an algorithm provide a satisfactory solution to a
problem?
Given an algorithm, how can we compare it with other
algorithms that solve the same problem?
How can the efficiency of an algorithm be analyzed?
What are the criteria that help us judge the quality of an
algorithm?
Tassos Dimitriou
Worst-case:
10
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
11
1/17/2019
Tassos Dimitriou
Time complexity
In general, we are not so much interested in the time and
space complexity for small inputs.
Tassos Dimitriou
12
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
n 5,000n 1.1n
10 50,000 3
13
1/17/2019
Tassos Dimitriou
Oh-notation
Generally, the complexity is expressed using “big O”
notation.
Captures the order of magnitude of the computational complexity
and hides the underlying constants.
Thus an algorithm taking 2n2 + 3n +100 steps to sort a sequence
of numbers is O(n2). [It is read “order n squared”]
Why do that?
Makes complexity analysis system independent. You don’t have
to know the exact timings of various instructions or the speed of
the processor.
These differences “disappear” when the size of the input
increases…
Tassos Dimitriou
Oh-notation
Definition
The running time T(n) of an algorithm is O(f(n)) if there is some
constant c such that for large enough inputs, T(n) < cf(n)
In pictures cf(n)
T(n)
n0 n
14
1/17/2019
Tassos Dimitriou
Growth of Functions
This notation can be used for any functions f and g.
Tassos Dimitriou
Oh-notation
5000000 40000
T1(n) 35000
T3(n)
4000000 0.01n2 = O(n2) 30000
10-6 2n =O(2n)
3000000 25000
2000000
T2(n) 20000
15000
This notation allows us to see how the input size affects the time
and space requirements
15
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
x2 + 2x + 1 x2 + 2x2 + x2 4x2
16
1/17/2019
Tassos Dimitriou
Yes. x3 grows faster than x2, so x3 grows also faster than f(x).
Tassos Dimitriou
17
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
Classification of algorithms
Algorithms are classified according to their time or space
complexities:
An algorithm is constant if its complexity is independent of
n; in symbols O(1)
An algorithm is linear if its time complexity is O(n)
Algorithms can also be quadratic, cubic, and so on. All
these algorithms are called polynomial time algorithms.
Their complexity is O(nk), for some constant k.
Algorithms whose complexity is O(cf(n)), where c is some
constant greater than 1 and f(n) some polynomial function
of n, are called exponential.
18
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
19
1/17/2019
Tassos Dimitriou
Tassos Dimitriou
20