Introduction
Introduction
Introduction
Instructor
Dr. Arabi Keshk
E-mail: [email protected]
As you start this course, you may
have some questions:
primes
class tag length 0 1 2 3 4 5
int[] 6 2 3 5 7 11 13
Example: Java object array (1)
Suppose that a Date object has fields y,
m, d.
Code to create an array of Date objects:
Date[] hols = new Date[3];
Animation:
1. Draw intersecting circles
Animation:
To compute the GCD of positive integers m and n:
1. Set p to m, and set q to n.
2. Until q exactly divides p, repeat:
2.1. Set p to q, and set q to (p modulo q).
3. Terminate with answer q.
m 77 n 21
p 14
77
21 q 14
21
7
Example: GCDs (3)
Implementation in Java:
static int gcd (int m, int n) {
// Return the greatest common divisor of
positive integers m and n.
int p = m, q = n;
while (p % q != 0) {
int r = p % q;
p = q; q = r;
}
return q;
}
Introduction
Algorithms
– History
– Examples
– Algorithms vs. programs
– Performance measure
Data structures
Abstract data types
Algorithms vs. programs (2)
Here we express our algorithms in (precise)
English.
Steps may be numbered consecutively:
1. Do this. Do this and then do that.
2. Do that.
The extent of a conditional is indicated by
indentation:
7. If …: Do this and then do that, but
7.1. Do this. only if condition … is true.
7.2. Do that.
The extent of a loop is indicated by indentation:
8. While …, repeat: Do this and then do that, as
8.1. Do this.
long as condition … is true.
8.2. Do that.
Algorithms vs. programs (3)
If we wish to use the algorithm on a computer,
we must first code it in a programming
language.
There may be many ways of coding the
algorithm, and there is a wide choice of
programming languages. But all the resulting
programs are implementations of the same
underlying algorithm.
Here we express our implementations in Java.
(Alternatives would be C, Pascal, Ada, etc.)
Introduction
Algorithms
– History
– Examples
– Algorithms vs. programs
– Performance measure
Data structures
Abstract data types
Efficiency
Given several algorithms to solve the
same problem, which algorithm is “best”?
Given an algorithm, is it feasible to use it at
all? In other words, is it efficient enough
to be usable in practice?
How much time does the algorithm
require?
How much space (memory) does the
algorithm require?
In general, both time and space
requirements depend on the algorithm’s
input (typically the “size” of the input).
Example: efficiency
Hypothetical profile of two sorting algorithms:
4
Key:
3 Algorithm A
Algorithm B
time
2
(ms)
0
10 20 30 40 items to be sorted, n
• Algorithm B’s time grows more slowly than A’s.
Efficiency: measuring time
Measure time in seconds?
+ is useful in practice
– depends on language, compiler, and
processor.
Count algorithm steps?
+ does not depend on compiler or processor
– depends on granularity of steps.
Count characteristic operations? (e.g.,
arithmetic ops in math algorithms,
comparisons in searching algorithms)
+ depends only on the algorithm itself
+ measures the algorithm’s intrinsic efficiency.
Remedial Mathematics: Powers
log2 2 = 1
since 2 = 42
log2 4 = 2
log2 8 = 3 since 2 = 83
log2 (2n) = n
log2 (xy) = log2 x + log2 y
log2 (x/y)= log2 x – log2 y
40
30
multiplications
smart power
20 algorithm
10
0
0 10 20 30 40 50
n
Complexity
For many interesting algorithms, the exact
number of operations is too difficult to
analyse mathematically.
To simplify the analysis:
– identify the fastest-growing term
– neglect slower-growing terms
– neglect the constant factor in the fastest-
growing term.
The resulting formula is the algorithm’s
time complexity. It focuses on the growth
rate of the algorithm’s time requirement.
Similarly for space complexity.
Example : analysis of power algorithms (1)
Comparison: 50
n
40
30
20
10
log n
0
0 10 20 30 40 50
n
O-notation (1)
We have seen that an O(log n) algorithm is
inherently better than an O(n) algorithm
for large values of n.
O(log n) signifies a slower growth rate
than O(n).
Complexity O(X) means “of order X”,
i.e., growing proportionally to X.
Here X signifies the growth rate,
neglecting slower-growing terms and
constant factors.
O-notation (2)
Common time complexities:
O(1) constant time (feasible)
O(log n) logarithmic time (feasible)
O(n) linear time (feasible)
O(n log n) log linear time (feasible)
O(n2) quadratic time (sometimes
feasible)
O(n3) cubic time (sometimes
feasible)
O(2n) exponential time (rarely
feasible)
Growth rates (1)
Comparison of growth rates:
1 1 1 1 1
log n 3.3 4.3 4.9 5.3
n 10 20 30 40
n log 33 86 147 213
n
n2 100 400 900 1,600
n3 1,000 8,000 27,000 64,000
2n 1,024 1.0 1.1 1.1
million billion trillion
Growth rates (2)
Graphically: 100
2n n2 n log n
80
60
n
40
20
log n
0
0 10 20 30 40 50
n
Example: growth rates (1)
Consider a problem that requires n data items to
be processed.
Consider several competing algorithms to solve
this problem. Suppose that their time
requirements on a particular processor are as
follows:
Algorithm Log: 0.3 log2 nseconds
Algorithm Lin: 0.1 n seconds
Algorithm LogLin: 0.03 n log2 nseconds
Algorithm Quad: 0.01 n2 seconds
Algorithm Cub: 0.001 n3seconds
Algorithm Exp: 0.0001 2nseconds
Example: growth rates (2)
Compare how many data items (n)
each algorithm can process in 1, 2,
Log …, 10 seconds:
Lin
LogLi
n
Quad
Cub
0:06
0:09
0:08
0:07
0:05
0:04
0:03
0:02
0:10
0:00
0:01
Exp
0 10 20 30 40 50 60 70 80 90 100
n
Introduction
Algorithms
– History
– Examples
– Algorithms vs. programs
– Performance measure
Data structures
Abstract data types
Data structures
A data structure is a systematic way of
organizing a collection of data.
A static data structure is one whose capacity
is fixed at creation.
E.g.: array.
A dynamic data structure is one whose
capacity is variable, so it can expand or
contract at any time.
E.g.: linked list, binary tree.
For each data structure we need algorithms
for insertion, deletion, searching, etc.
Example: representing strings
mat sat
Introduction
Algorithms
– History
– Examples
– Algorithms vs. programs
– Performance measure
Data structures
Abstract data types
Abstract Data Types
An abstract data type (ADT) is an organized
collection of information and a set of
operations used to manage that information
The set of operations defines the interface to
the ADT
In one sense, as long as the ADT fulfills the
promises of the interface, it doesn't matter
how the ADT is implemented
Objects are a perfect programming
mechanism to create ADTs because their
internal details are encapsulated
Information Hiding
ADT hides the implementation details of the
operations and the data from the users of the ADT
Examples
Associated
Operations ADT behavior