Document

Introduction
Fundamentals
Euclid’s algorithm
What is an algorithm?
A sequence of deterministic instructions for solving a
problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time. I.e. a recipe,
process, method, technique, procedure, routine,… with
following requirements:
1. Finiteness terminates after a finite number of steps
2. Definiteness rigorously and unambiguously specified
3. Input
valid inputs are clearly specified
4. Output
provably correct given a valid input
5. Effectiveness steps are sufficiently simple and basic
Notion of algorithm
problem
algorithm
input
“computer”
output
Algorithmic solution
The Study of Algorithms
input
data
output
data
algorithm
An algorithm is a process which transforms data.
A:IO
A (in) = out
An algorithm A is said to solve a problem P if for
every instance (in) of P, the relation below holds:
(in) P A(in)
1.1
Good algorithms
Algorithms will be specified in pseudo-code, and the key issues will be
worst/average-case analysis of:
1. their accuracy
(correctness)
a mathematical proof
2. their efficiency
(complexity)
can be theoretical or empirical
Better:
improved use of time and/or space
Best:
optimality; lower-bounds
design strategies
• Brute force
•
• Divide and conquer
•
• Decrease and conquer
•
Backtracking and
Branch and bound
•
Space and time
tradeoffs
Greedy approach
Dynamic programming
• Transform and conquer
Analysis of Algorithms
The rate or order of growth of the running time of the
algorithm, as a function of n.
Usually, input size measures the number of items in
the input (not necessarily the number of bits)
The running time is the number of “steps” or primitive
operations on a particular machine model.
1.2
Sizes and Models
How to measure input size can have very
different interpretations. For example the set:
S = {1, …, n} can be encoded as n in binary.
n items
log2 n bits
Machine models can have different notions of a singe step.
For example clearing an array:
A[1,…, n]  0
is one step on a parallel machine
A[1]  0
…
A[n]  0
is n steps on a sequential machine
1.2
Euclid’s algorithm for finding the
greatest common divisor (GCD)
q
if q | p
(p, q) =
(q, p mod q)
otherwise
(p, q) stands for the maximal divisor of p and q.
q | p means that q divides p exactly
p mod q is the remainder of p ÷ q
9
Pseudo-code
function GCD(p, q)
r := p mod q
if r = 0 then return (q) else return GCD(q, r)
This is the adaptation to whole numbers of the
original geometric procedure for determining the
greatest common measure of two quantities by
repeatedly subtracting the smaller from the larger
to obtain a remainder. It terminates (with no final
remainder) if and only if their ratio was rational.
10
Diagrammatic execution
•mod
↑
30
•=
→
↑
21
↓
•mod
•mod
↓
9
↓
•= →
•= →
↑
3
↑
•
•
↓
0
• This method works for any pair of numbers.
11
Correctness Proof
Statement: The greatest common divisor of
p and q is the largest integer which divides
both p and q exactly. Let us show that the
GCD(p, q) is correct by induction on q.
Basis: If r = 0, then GCD(p, q) = q is clearly
the correct answer since q | q and q | p and
no larger integer would divide q.
12
Induction Step
If r > 0, our algorithm sets GCD(p, q) =
GCD(q, r) which is correct by induction
hypothesis (since r < q). Now since p = nq +
r for some n and since GCD(q, r) | (q & r) we
get that GCD(q, r) | p. So we know that
GCD(p, q) divides both p and q. It must be
the largest divisor of p and q because any
divisor d of p and q must also divide r = p −
nq and hence would divide both q and r.
Since GCD(q, r) is assumed to be correct, d
≤ GCD(q, r) = GCD(p, q).
13
Complexity Analysis
Time of O(log n) is based on the fact that
every other number in the sequence goes
down by at least ½. I.e.
p
>
q
>
(p mod q) < p⁄2
Pf: Analyze cases by comparing q & p⁄2.
N.b. # of steps is proportional to # of digits!
14