Transcript Document
Application: Algorithms
Lecture 17 Section 3.8
Wed, Feb 9, 2005
Algorithms An algorithm is a step-by-step procedure that is guaranteed to stop after a finite number of steps for all legitimate inputs.
Example: Describe an algorithm for finding the maximum value in a list of numbers.
What if the list is empty?
What if the list is infinitely long?
An Algorithmic Language An algorithmic language is much like a computer language.
Its primary purpose is to express unambiguously the steps of an algorithm, including all decisions and special cases that may arise.
Assignment Statements Assignment statements are of the form
x
:=
e
where
x
is a variable and
e
expression.
is an Examples
count
:= 0
found
:= true
Conditional Statements One-way conditional statements: if condition then
sequence of statements
Two-way conditional statements: if condition then
sequence of statements
else
sequence of statements
Iterative Statements While loops: while condition
sequence of statements
end while
For loops: for var :=
init. expr
. to final expr .
sequence of statements
end for
A Notation for Algorithms An algorithm will be organized into three parts: Input – List all input variables and any special assumptions about them.
Algorithm body – Describe the step-by-step procedure.
Output – List the output variables.
Example – Finding the Max Input:
a
(a list of numbers),
n
(an integer equal to the size of the list, which must be at least 1) Algorithm body:
max
:=
a
[1]
i
:= 2 while
i
n
if
a
[
i
] >
max
then
max
=
a
[
i
]
i
:=
i
+ 1 end while Output:
max
Tracing an Algorithm Write a trace table for the Max algorithm and the input
a
= {7, 3, 8, 6, 4},
n
= 5.
Iteration 0 1 2 3 4
a
{7, 3, 8, 6, 4}
n
5
max
7 2
i
Tracing an Algorithm Write a trace table for the Max algorithm and the input
a
= {7, 3, 8, 6, 4},
n
= 5.
Iteration 0 1 2 3 4
a
{7, 3, 8, 6, 4}
n
5
max
7 7 2 3
i
Tracing an Algorithm Write a trace table for the Max algorithm and the input
a
= {7, 3, 8, 6, 4},
n
= 5.
Iteration 0 1 2 3 4
a
{7, 3, 8, 6, 4}
n
5
max
7 7 8 2 3 4
i
Tracing an Algorithm Write a trace table for the Max algorithm and the input
a
= {7, 3, 8, 6, 4},
n
= 5.
Iteration 0 1 2 3 4
a
{7, 3, 8, 6, 4}
n
5
max
7 7 8 8 2 3 4 5
i
Tracing an Algorithm Write a trace table for the Max algorithm and the input
a
= {7, 3, 8, 6, 4},
n
= 5.
Iteration 0 1 2 3 4
a
{7, 3, 8, 6, 4}
n
5
max
7 7 8 8 8 2 3 4 5 6
i
Greatest Common Divisors Let
a
and
b
be integers that are not both 0.
The
greatest common divisor
of
a
and
b
, denoted gcd(
a
,
b
), is the unique positive integer
d
with the following properties:
d
|
a
and
d
|
b
.
For every integer
c
, if
c c
|
d
.
|
a
and
c
|
b
, then
The High-school gcd Algorithm The high-school method is very inefficient.
Factor each number into standard form.
For each prime that appears in both factorizations, use it as a factor of the gcd along with the smaller of the two exponents.
Example Find the gcd of 81900 and 54810.
We factor them as 81900 = 2 2 3 2 5 2 54810 = 2 3 3 5 7 7 13 29 Therefore, the gcd is 2 3 2 5 7 = 630
The Euclidean Algorithm Factoring is inefficient; therefore, this algorithm is inefficient.
This algorithm is
O
( 10
d
), where
d
number of digits in the number.
is the Euclid had a better idea.
The Euclidean Algorithm is
O
(
d
), where
d
is the number of digits in the number.
The Euclidean Algorithm Input:
A
,
B
(positive integers, not both 0) Algorithm body:
a
:=
A b
:=
B
if
b
= 0 then swap
a
and
b r
:=
a
mod
b
The Euclidean Algorithm while
r
> 0
a
:=
b b
:=
r r
:=
a
mod
b
end while Output:
b
Example Apply the Euclidean Algorithm to
A
and
B
= 54810.
= 81900 Iteration 0 1 2
A
81900
B
54810
a
81900
b
54810
r
27090
Example Apply the Euclidean Algorithm to
A
and
B
= 54810.
= 81900 Iteration 0 1 2
A
81900
B
54810
a
81900 54810
b
54810 27090
r
27090 630
Example Apply the Euclidean Algorithm to
A
and
B
= 54810.
= 81900 Iteration 0 1 2
A
81900
B
54810
a
81900 54810 27090
b
54810 27090 630
r
27090 630 0
Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm terminates for all legitimate inputs
A
and
B
.
Proof: WOLOG, WMA
B
> 0.
After the first iteration of the while loop, 0
b
<
B
since
b
is the remainder of
A
divided by
B
.
Proof Each iteration produces a nonnegative remainder that is smaller than the previous remainder.
This cannot happen more than
B
before the remainder is 0.
times
Proof of the Euclidean Algorithm Lemma 1: If
b
> 0, then gcd(
b
, 0) =
b
.
Proof:
b
| 0 and
b
|
b
.
For all integers
c
, if
c c
|
b
.
| 0 and
c
|
b
, then Therefore,
b
= gcd(
b
, 0).
Proof of the Euclidean Algorithm Lemma 2: If
a
0, and
q
and
r
and
b
are integers, with are integers such that
b
a
=
qb
+
r
then gcd(
a
,
b
) = gcd(
b
,
r
).
Proof: Let
d
= gcd(
b
,
r
).
Then
d
divides |
b
and
d b
and
r
|
r
and any integer that must also divide
d
.
Proof We must show that
d
integer that divides
a
divide
d
.
|
a
and
d
and
b
|
b
and any must also We already know that
d
|
b
.
Since
a
=
qb
+
r
, it follows that
d
|
a
.
Let
c
|
d c
Since .
be an integer such that
c r
=
a
–
qb
, it follows that |
c a
|
r
and
c
|
b
and so .
Therefore,
d
= gcd(
a
,
b
).
Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm produces the gcd of
A
and
B
.
Proof:
r
After the final iteration of the while loop, = 0.
By Lemma 1, the output,
b
, is the gcd of
b
and
r
.
By Lemma 2, that is equal to the gcd of
a
and
b
in the final iteration
.
Proof But
a
and
b
on the last iteration were
b
and
r
on the previous iteration.
Therefore,
gcd(a
,
b
) on the last iteration equals gcd(
b
,
r
) on the previous iteration, which equals gcd(
a
,
b
) on the previous iteration, and so on.
Following this argument all the back to the first iteration, we see that the output is gcd(
A
,
B
).
Proof In a later chapter, we will study mathematical induction.
At that point, we will be able to make this argument more rigorous.