Chapter2 Notes
Chapter2 Notes
1
Chapter
ALGORITHMSand INTEGERS
2.1 Algorithms
2.2 Growth of Functions
2.3 Complexity of Algorithms
2.4 The Integers and Division
2.5 Integers and Algorithms
2.6 Applications of Number Theory
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.1 Algorithms 2.1.1
2.1 ALGORITHMS
def: An algorithm is a nite set of precise
instructions for performing a computation or for
solving a problem.
Example 2.1.1: A computer program is an
algorithm.
Remark: From a mathematical perspective, an
algorithm represents a function. The British
mathematician Alan Turing proved that some
functions cannot be represented by an algorithm.
CLASSROOM PERSPECTIVE
Every computable function can be repre-
sented by many dierent algorithms. Naive
algorithms are almost never optimal.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.1.2
terminology: A good pseudocoding of an
algorithm provides a clear prose representation of
the algorithm and also is transformable into one
or more target programming languages.
Algorithm 2.1.1: Find Maximum
Input: unsorted array of integers a , a , . . . , a
n
Output: largest integer in array
{Initialize} max := a
For i := 2 to n
If max < a
i
then max := a
i
Continue with next iteration of for-loop.
Return (max)
Remark: For a sorted array, there would be a
much faster algorithm to nd the maximum.
In general, the representation of the data pro-
foundly aects the choice of an algorithm and of
the execution time.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.1 Algorithms 2.1.3
Algorithm 2.1.2: Unsorted Sequential Search
Input: unsorted array of integers a , a , . . . , a
n
target value x
Output: subscript of entry equal to target value,
or 0 if not found
{Initialize} i := 1
While i n and x = a
i
i := i + 1
Continue with next iteration of while-loop.
If i n then loc := i else loc := 0
Return (loc)
Remark: If the array were presorted into
ascending (or descending) order, then faster
algorithms could be used.
(1) linear search could stop sooner
(2) 2-level search could avoid many comparisons
(3) binary search could divide-and-conquer
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.1.4
Algorithm 2.1.3: Sorted Sequential Search
Input: sorted array of integers a , a , . . . , a
n
target value x
Output: subscript of entry equal to target value,
or 0 if not found
{Initialize} i := 1
While i n and x < a
i
i := i + 1
Continue with next iteration of while-loop.
If (i n cand x = a
i
) then loc := i else loc := 0
Return (loc)
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.1 Algorithms 2.1.5
Algorithm 2.1.4: Two-level Search
Input: sorted array of integers a , a , . . . , a
n
target value x
Output: subscript of entry equal to target value,
or 0 if not found
{Initialize} i := 10
{Find target sublist of 10 entries}
While i n and x < a
i
i := i + 10
Continue with next iteration of while-loop.
{Linear search target sublist of 10 entries}
{Initialize} j := i 9
While j < i and x < a
j
j := j + 1
Continue with next iteration of while-loop.
If (j n cand x = a
j
) then loc := j else loc := 0
Return (loc)
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.1.6
Algorithm 2.1.5: Binary Search
Input: unsorted array of integers a , a , . . . , a
n
target value x
Output: subscript of entry equal to target value,
or 0 if not found
{Initialize} left := 1; right := n
While left < right
mid := (left + right)/2
If x > a
mid
then left := mid else right := mid
Continue with next iteration of while-loop.
If x = a
left
then loc := left else loc := 0
Return (loc)
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.2 Growth of Functions 2.2.1
2.2 GROWTH OF FUNCTIONS
def: Let f and g be functions R R. Then
f is asymptotically dominated by g if
(K R)(x > K)[f(x) g(x)]
notation: f g.
Remark: This means that eventually, there is
an location x = K, after which the graph of the
function g lies above the graph of the function f.
BIG OH CLASSES
def: Let f and g be functions R R. Then
f is in the class O(g) (big-oh of g) if
(C R)[f Cg]
notation: f O(g).
disambiguation: Properly understood, O(g) is
the class of all functions that are asymptotically
dominated by any multiple of g.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.2.2
terminology note: The phrase f is big-oh
of g makes sense if one imagines either that the
word in preceded the word big-oh, or that
big-oh of g is an adjective.
Example 2.2.1: 4n + 21n + 100 O(n )
Proof: First suppose that n 0. Then
4n + 21n + 100 4n + 24n + 100
4(n + 6n + 25)
8n which holds whenever
n 6n + 25, which holds whenever
n 6n + 9 34, which holds whenever
n 3
and K = 0
Theorem 2.2.2. Let p(x) be a polynomial of
degree n. Then p(x) O(x
n
).
Proof: Informally, just generalize Example
2.2.1. Formally, just apply Lemma 2.2.1.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.2 Growth of Functions 2.2.5
Example 2.2.3: 100n O(e
n
). Observing
that n = e
n
inspires what follows.
Proof: Taking the upper Riemann sum with
unit-sized intervals for ln x =
n
dx
x
implies for
n > 1 that
ln(n) <
1
1
+
1
2
+ +
1
n
1
1
+ +
1
5
+
1
6
+ +
1
n
1
1
+ +
1
5
+
1
6
+ +
1
6
5 +
n 5
6
Therefore, 6 ln n n + 25, and accordingly,
100n = 100 e
n
< 100 e
n
< e e
n
n comparisons.
Big-Oh: Time complexity is in O(
n).
Increasing to k levels further decreases the
execution time to O(
k
n),
provided that k is not too large.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.3 Complexity 2.3.7
Example 2.3.5:
Algorithm 2.1.5: Binary Search
Input: unsorted array of integers a , a , . . . , a
n
target value x
Output: subscript of entry equal to target value,
or 0 if not found
{Initialize} left := 1; right := n
While left < right
mid := (left + right)/2
If x > a
mid
then left := mid else right := mid
Continue with next iteration of while-loop.
If x = a
left
then loc := left else loc := 0
Return (loc)
Target in or not in Array:
Every case takes lg n comparisons.
Big-Oh: Time complexity is in O(lg n).
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.3.8
COMPLEXITY JARGON
def: A problem is solvable if it can be solved
by an algorithm.
Example 2.3.6: Alan Turing dened the
halting problem to be that of deciding whether
a computational procedure (e.g., a program)
halts for all possible input. He proved that the
halting problem is unsolvable.
def: A problem is in class P if it is solvable by
an algorithm that runs in polynomial time.
def: A problem is tractable if it is in class P.
def: A problem is in class NP if an algorithm
can decide in polynomial time whether a puta-
tive solution is really a solution.
Example 2.3.7: The problem of deciding
whether a graph is 3-colorable is in class NP.
It is believed not to be in class P.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.4 The Integers and Division 2.4.1
2.4 THE INTEGERS AND DIVISION
In mathematics, specifying an axiomatic model
for a system precedes all discussion of its proper-
ties. The number system serves as a foundation
for many other mathematical systems.
Elementary school students learn algorithms for
the arithmetic operations without ever seeing
a denition of a number or of the operations
that these algorithms are modeling.
These coursenotes precede discussion of division
by the construction of the number system and of
the usual arithmetic operations.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.4.2
AXIOMS for the NATURAL NUMBERS
def: The natural numbers are a mathematical
system
^ = N, 0 N, s : N N
in which the number 0 is called zero, and the
operation s : N N is called successor, such
that
(1) (,n)[0 = s(n)]. Zero is not the successor of
any number.
(2) (m, n N)[m ,= n s(m) ,= s(n)]. Two dif-
ferent numbers cannot have the same successor.
(3) (S N)
(0 S) (n S)[s(n) S] S = N
.
Given a subset S of the natural numbers, sup-
pose that it contains the number 0, and suppose
that whenever it contains a number, it also con-
tains the successor of that number. Then S = N.
Remark: Axiom (1) N has at least one other
number, namely, the successor of zero. Lets call
it one. Using Axioms (1) and (2) together, we
conclude that s(1) , 0, 1. Etc.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.4 The Integers and Division 2.4.3
ARITHMETIC OPERATIONS
def: The predecessor of a natural number n is
a number m such that s(m) = n.
notation: p(n).
def: Addition of natural numbers.
n + m =
n if m = 0
s(n) + p(m) otherwise
def: Ordering of natural numbers.
n m means
m = 0 or
p(n) p(m)
def: Multiplication of natural numbers.
n m =
0 if m = 0
n + n p(m) otherwise
OPTIONAL: (1) Dene exponentiation. (2)
Dene positional representation of numbers.
(3) Verify that the usual base-ten methods for
addition, subtraction, etc. produce correct an-
swers.
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Chapter 2 ALGORITHMS and INTEGERS 2.4.4
DIVISION
def: Let n and d be integers with d ,= 0. Then
d divides n if there exists a number q such that
n = dq. notation: dn.
def: The integer d is a factor of n or a divisor
of n if dn.
def: A divisor d of n is proper if d ,= n.
def: The number one is called a trivial divisor.
def: An integer p 2 is prime if p has no non-
trivial proper divisors, and composite other-
wise.
Algorithm 2.4.1: Naive Primality Algorithm
Input: positive integer n
Output: smallest nontrivial divisor of n
For d := 2 to n
If dn then exit
Continue with next iteration of for-loop.
Return (d)
Time-Complexity: O(n).
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.
Section 2.4 The Integers and Division 2.4.5
Theorem 2.4.1. Let n be a composite number.
Then n has a divisor d such that 1 < d
n.
Proof: Straightforward.
Algorithm 2.4.2: Less Naive Primality Algorithm
Input: positive integer n
Output: smallest nontrivial divisor of n
For d := 2 to
n
If dn then exit
Continue with next iteration of for-loop.
Return (d)
Time-Complexity: O(
n).
Example 2.4.1: Primality Test 731.
Upper Limit: