Download A , b

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Birkhoff's representation theorem wikipedia , lookup

Eisenstein's criterion wikipedia , lookup

Factorization wikipedia , lookup

Polynomial greatest common divisor wikipedia , lookup

Fisher–Yates shuffle wikipedia , lookup

Algebraic number field wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
PRACTICE 1
SETS AND SET OPERSATIONS
DEFINITION 1 A set is an unordered collection of objects, called elements or
members of the set. A set issaid to contain its elements. We write a ∈ A to denote
that a is an element of the set A . The notation a ∉ A denotes that a is not an
element of the set A .
EXAMPLE The set V of all vowels in the English alphabet can be written as V =
{a, e, i, o, u}
EXAMPLE The set O of odd positive integers less than 10 can be expressed by O
={1,3,5,7,9}.
EXAMPLE Although sets are usually used to group together elements with
common properties, there is nothing that prevents a set from having seemingly
unrelated elements. For instance, {a, 2 , Fred, New Jersey } is the set containing
the four elements a , 2, Fred, and New Jersey.
Sometimes the roster method is used to describe a set without listing all its
members. Some members of the set are listed, and then ellipses( . . . ) are used
when the general pattern of the elements is obvious.
EXAMPLE The set of positive integers less than 100 can be denoted by { 1 , 2 , 3
, . . . , 99 }
EXAMPLE The set { N , Z , Q , R } is a set containing four elements, each of
which is a set. The four elements of this set are N, the set of natural numbers; Z,
the set of integers; Q, the set of rational numbers; and R, the set of real numbers.
DEFINITION 2Two sets are equal if and only if they have the same elements.
Therefore, if A and B are sets, then A and B are equal if and only if ∀x(x ∈ A ↔ x ∈
B) . We write A = B if A andB are equal sets.
EXAMPLE The sets { 1 , 3 , 5 } and { 3 , 5 , 1 } are equal, because they have the
same elements. Note that the order in which the elements of a set are listed does
not matter. Note also that it does not matter if an element of a set is listed more
than once, so { 1 , 3 , 3 , 3 , 5 , 5 , 5 , 5 } is the same as the set { 1 , 3 , 5 } because
they have the same elements.
THE EMPTY SETThere is a special set that has no elements. This set is called
the empty set,or null set, and is denoted by ∅ . The empty set can also be denoted
by { } (that is, we represent the empty set with a pair of braces that encloses all the
elements in this set). Often, a set of elements with certain properties turns out to be
the null set. For instance, the set of all positive integers that are greater than their
squares is the null set.
Venn Diagrams
EXAMPLE Draw a Venn diagram that represents V, the set of vowels in the
English alphabet.
Solution: We draw a rectangle to indicate the universal set U , which is the set of
the 26 lettersof the English alphabet. Inside this rectangle we draw a circle to
represent V . Inside this circle we indicate the elements of V with points (see
Figure 1).
Subsets
DEFINITION 3The set A is a subset of B if and only if every element of A is also
an element of B . We use the notation A ⊆ B to indicate that A is a subset of the set
B.
Showing that A is a Subset of B To show that A ⊆ B , show that if x belongs to A
then xalso belongs to B .
Showing that A is Not a Subset of B To show that A⊈B , find a single x ∈ A such
that x∉B .
EXAMPLE The set of all odd positive integers less than 10 is a subset of the set
of all positive integers less than 10, the set of rational numbers is a subset of the set
of real numbers, the set of all computer science majors at your school is a subset of
the set of all students at your school, and the set of all people in China is a subset
of the set of all people in China (that is, it is a subset of itself). Each of these facts
follows immediately by noting that an element that belongs to the first set in each
pair of sets also belongs to the second set in that pair.
EXAMPLE The set of integers with squares less than 100 is not a subset of the
set of nonnegative integers because − 1 is in the former set [as (− 1 ) 2 < 100], but
not the later set. The set of people who have taken discrete mathematics at your
school is not a subset of the set of all computer science majors at your school if
there is at least one student who has taken discrete mathematics who is not a
computer science major.
THEOREM 1 For every set S , (i ) ∅ ⊆ Sand (ii ) S ⊆ S .
Proof: We will prove (i ) and leave the proof of (ii ) as an exercise. Let S be a set.
To show that ∅ ⊆ S , we must show that ∀x(x ∈ ∅ → x ∈ S) is true. Because the
empty set contains no elements, it follows that x ∈ ∅ is always false. It follows that
the conditional statement x ∈ ∅ → x ∈ S is always true, because its hypothesis is
always false and a conditional statement with a false hypothesis is true. Therefore,
∀x(x ∈ ∅ → x ∈ S) is true. This completes the proof of ( i ). Note that this is an
example of a vacuous proof.
The Size of a Set
DEFINITION 4 Let S be a set. If there are exactly n distinct elements in S where n
is a nonnegative integer,we say that S is a finite set and that n is the cardinality of S
. The cardinality of S is denoted by |S| .
EXAMPLE Let A be the set of odd positive integers less than 10. Then |A| = 5.
EXAMPLE Let S be the set of letters in the English alphabet. Then |S| = 26.
EXAMPLE Because the null set has no elements, it follows that |∅| = 0.
DEFINITION 5A set is said to be infinite if it is not finite.
EXAMPLE The set of positive integers is infinite.
DEFINITION 6 Given a set S , the power set of S is the set of all subsets of the set
S . The power set of S isdenoted by P(S)
EXAMPLE What is the power set of the set { 0 , 1 , 2 } ?
Solution: The power set P({ 0 , 1 , 2 }) is the set of all subsets of { 0 , 1 , 2 } .
Hence,
P({ 0 , 1 , 2 }) = {∅, { 0 }, { 1 }, { 2 }, { 0 , 1 }, { 0 , 2 }, { 1 , 2 },
{ 0 , 1 , 2 }}. Note that the empty set and the set itself are members of this set of
subsets.
EXAMPLE What is the power set of the empty set? What is the power set of the
set {∅} ?
Solution: The empty set has exactly one subset, namely, itself. Consequently, P(∅)
= {∅}. The set {∅} has exactly two subsets, namely, ∅ and the set {∅} itself.
Therefore,
P({∅}) = {∅, {∅}}.
Cartesian Products
DEFINITION 7 The ordered n-tuple (a 1 , a 2 , . . . , an) is the ordered collection
that has a1 as its first element, a2 as its second element , . . . , and an as its nth
element.
DEFINITION 8 Let A and B be sets. The Cartesian product of A and B , denoted
by A × B , is the set of all ordered pairs (a, b) , where a ∈ A and b ∈ B . Hence,
A × B = {(a, b) | a ∈ A ∧ b ∈ B}.
EXAMPLE LetA represent the set of all students at a university, and let B
represent the set of all courses offered at the university. What is the Cartesian
product A × B and how can it be used?
Solution: The Cartesian product A × B consists of all the ordered pairs of the form
(a, b) , where a is a student at the university and b is a course offered at the
university. One way to use the set A × B is to represent all possible enrollments of
students in courses at the university.
DEFINITION 9 The Cartesian product of the sets A1 , A2 , . . . , An, denoted by A 1
× A 2 × · · · × An, is the set of ordered n -tuples( a1 , a2 , . . . , an), where aibelongs
to Ai for i = 1 , 2 , . . . , n . In other words,
A1 × A2 × · · · × An = {(a1 , a2 , . . . , an) | ai ∈ Aifor i = 1 , 2 , . . . , n}.
EXAMPLE What is the Cartesian product A × B × C , where A = { 0 , 1 } , B = {
1 , 2 } , andC = { 0 , 1 , 2 } ?
Solution:The Cartesian product A × B × C consists of all ordered triples ( a, b, c ),
where a ∈ A , b ∈ B , and c ∈ C . Hence,
A × B × C = {( 0 , 1 , 0 ), ( 0 , 1 , 1 ), ( 0 , 1 , 2 ), ( 0 , 2 , 0 ), ( 0 , 2 , 1 ), ( 0 , 2 , 2
),
( 1 , 1 , 0 ), ( 1 , 1 , 1 ), ( 1 , 1 , 2 ), ( 1 , 2 , 0 ), ( 1 , 2 , 1 ), ( 1 , 2 , 2 )}.
Exercises
1. List the members of these sets.
a) {x | x is a real number such that x2 = 1 }
b) {x | x is a positive integer less than 12 }
d) {x | x is an integer such that x2 = 2 }
2. Use set builder notation to give a
description of each of these sets.
c) {x | x is the square of an integer and x <100a) { 0 , 3 , 6 , 9 , 12 }
}
b) {− 3 , − 2 , − 1 , 0 , 1 , 2 , 3 }
c) {m, n, o, p}
3.For each of these pairs of sets, determine
whether the first is a subset of the second, the
7. Explain why A × B × C and (A ×
B) × C are not the same.
8. How many elements does each of
these sets have where a and b are
distinct elements?
second is a subset of the first, or neither is a
a) P({a, b, {a, b}})
subset of the other.
b) P({∅, a, {a}, {{a}}})
a) the set of airline flights from NewYork to
New Delhi, the set of nonstop airline flights
from New York to New Delhi
b) the set of people who speak English, the set
of people who speak Chinese
c) the set of flying squirrels, the set of living
creatures that can fly
4. Use a Venn diagram to illustrate the set of
all months of the year whose names do not
contain the letter R in the set of all months of
c) P(P(∅))
10. What is the Cartesian product A ×
B × C , where A is the set of all
airlines and B and C are both the set
of all cities in the United States? Give
an example of how this Cartesian
product can be used.
11.Let A = {a, b, c} , B = {x, y} , and
C = { 0 , 1 } . Find
a) A × B × C . b) C × B × A .
b) C × B × A .
c) C × A × B . d) B × B × B .
the year.
d) B × B × B .
5. What is the cardinality of each of these sets?
a) {a} b) {{a}}b) {{a}}
c) {a, {a}}d) {a, {a}, {a, {a}}}
6. Determine whether each of these
sets is the power set of a set, where a
and b are distinct elements.
a) ∅ b) {∅, {a}}
c) {∅, {a}, {∅, a}} d) {∅, {a}, {b},
{a, b}}
12.The defining property of an
ordered pair is that two or- dered pairs
are equal if and only if their first
elements are equal and their second
elements are equal. Surpris- ingly,
instead of taking the ordered pair as a
primitive con- cept, we can construct
ordered pairs using basic notions from
set theory. Show that if we define the
ordered pair (a, b) to be {{a}, {a, b}}
, then (a, b) = (c, d) if and only if a = c
and b = d . [Hint: First show that
{{a}, {a, b}} = {{c}, {c, d}} if and
only if a = c and b = d.]
PRACTICE 2
INJECTIVE, SURJECTIVE, BIJECTIVE FUNCTIONS
Theorem 1 Let A and B be nonempty sets. A function f from A to B is an
assignment of exactly one element of B to each element of A. We write f (a) = b if
b is the unique element of B assigned by the function f to the element a of A. If f is
a function from A to B, we write f : A → B.
Example 1 What are the domain, codomain, and range of the function that
assigns grades to students described in the first paragraph of the introduction of
this section?
Solution 1 LetGbe the function that assigns a grade to a student in our discrete
mathematics class. Note that G(Adams) = A, for instance. The domain of G is the
set {Adams, Chou, Goodfriend, Rodriguez, Stevens}, and the codomain is the set
{A,B,C,D, F}. The range of G is the set {A,B,C, F}, because each grade except D
is assigned to some student.
Example 2 Let R be the relation with ordered pairs (Abdul, 22), (Brenda, 24),
(Carla, 21), (Desire, 22), (Eddie, 24), and (Felicia, 22). Here each pair consists of a
graduate student and this student’s age. Specify a function determined by this
relation.
Solution 2 If f is a function specified by R, then f (Abdul ) = 22, f (Brenda) = 24,
f (Carla) = 21, f (Desire) = 22, f (Eddie) = 24, and f (Felicia) = 22. (Here, f (x) is the
age of x, where x is a student.) For the domain, we take the set {Abdul, Brenda,
Carla, Desire, Eddie, Felicia}. We also need to specify a codomain, which needs to
contain all possible ages of students. Because it is highly likely that all students are
less than 100 years old, we can take the set of positive integers less than 100 as the
codomain. (Note that we could choose a different codomain, such as the set of all
positive integers or the set of positive integers between 10 and 90, but that would
change the function. Using this codomain will also allow us to extend the
function by adding the names and ages of more students later.) The range of the
function we have specified is the set of different ages of these students, which is
the set {21, 22, 24}.
Theorem 2 A function f is said to be one-to-one, or an injunction, if and only if f
(a) = f (b) implies that a = b for all a and b in the domain of f.A function is said to
be injective if it is one-to-one.
Figure 1 A One-to-One Function.
Example 3 Determine whether the function f from {a, b, c, d} to {1, 2, 3, 4, 5}
with f (a) = 4, f (b) = 5, f (c) = 1, and f (d) = 3 is one-to-one.
Solution 3 The function f is one-to-one because f takes on different values at the
four elements of its domain. This is illustrated in Figure 1.
Theorem 3 A function f from A to B is called onto, or a surjection, if and only if
for every element b ∈ B there is an element a ∈ A with f (a) = b.A function f is
called surjective if it is onto.
Example 4 Let f be the function from {a, b, c, d} to {1, 2, 3} defined by f (a) = 3,
f (b) = 2, f (c) = 1, and f (d) = 3. Is f an onto function?
Solution 4 Because all three elements of the codomain are images of elements in
the domain, we see that f is onto. This is illustrated in Figure 4. Note that if the
codomain were {1, 2, 3, 4}, then f would not be onto.
Theorem 4 The function f is a one-to-one correspondence, or a bijection, if it is
both one-to-one and onto. We also say that such a function is bijective.
Example 5 Let f be the function from {a, b, c, d} to {1, 2, 3, 4} with f (a) = 4, f
(b) = 2, f (c) = 1, and f (d) = 3. Is f a bijection?
Solution 5 The function f is one-to-one and onto. It is one-to-one because no two
values in the domain are assigned the same function value. It is onto because all
four elements of the codomain are images of elements in the domain. Hence, f is a
bijection.
Exercises
1. Why is f not a function from R to R if
a) f (x) = 1/x?
b) f (x) =√x?
c) f (x) = Ѓ}_(x2 + 1)?
2. Determine whether f is a function from Z to R if
a) f (n) = Ѓ}n.
b) f (n) =√n2 + 1.
c) f (n) = 1/(n2 − 4).
3. Determine whether f is a function from the set of all bit strings to the set of
integers if
a) f (S) is the position of a 0 bit in S.
b) f (S) is the number of 1 bits in S.
c) f (S) is the smallest integer i such that the ith bit of S is 1 and f (S) = 0 when S is
the empty string, the string with no bits.
4. Find the domain and range of these functions. Note that in each case, to find the
domain, determine the set of elements assigned values by the function.
a) the function that assigns to each nonnegative integer its last digit
b) the function that assigns the next largest integer to a positive integer
c) the function that assigns to a bit string the number of one bits in the string
d) the function that assigns to a bit string the number of bits in the string
5. Find the domain and range of these functions. Note that in each case, to find the
domain, determine the set of elements assigned values by the function.
a) the function that assigns to each bit string the number of ones in the string minus
the number of zeros in the string
b) the function that assigns to each bit string twice the number of zeros in that
string
c) the function that assigns the number of bits left over when a bit string is split
into bytes (which are blocks of 8 bits)
d) the function that assigns to each positive integer the largest perfect square not
exceeding this integer
PRACTICE 3
FIBONACCI NUMBERS AND DERANGEMENT
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in
the following integer sequence:
1,1,2,3,5,8,13,21,34,55,89,144…
Or often
0,1,1,2,3,5,8,13,21,34,55,89,144…
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1,
or 0 and 1, depending on the chosen starting point of the sequence, and each
subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the
recurrence relation:
Fn=Fn-1+Fn-2
With seed values: F1=1, F2=1 or F0=0, F1=1
The Fibonacci numbers occur in the sums of "shallow" diagonals in Pascal's
triangle
These numbers also give the solution to certain enumerative problems.[20] The
most common such problem is that of counting the number of compositions of 1s
and 2s that sum to a given total n: there are Fn+1 ways to do this.
Example 1, if n = 5, then Fn+1 = F6 = 8 counts the eight compositions:
1+1+1+1+1 = 1+1+1+2 = 1+1+2+1 = 1+2+1+1 = 2+1+1+1 = 2+2+1 = 2+1+2 =
1+2+2,
all of which sum to n = 5 = 6−1.
Most identities involving Fibonacci numbers can be proved using combinatorial
arguments using the fact that Fn can be interpreted as the number of sequences of
1s and 2s that sum to n − 1. This can be taken as the definition of Fn, with the
convention that F0 = 0, meaning no sum adds up to −1, and that F1 = 1, meaning
the empty sum "adds up" to 0. Here, the order of the summand matters. For
example, 1 + 2 and 2 + 1 are considered two different sums.
Example 2, the recurrence relation
Fn=Fn-1+Fn-2
The nth Fibonacci number is the sum of the previous two Fibonacci numbers, may
be shown by dividing the Fn sums of 1s and 2s that add to n − 1 into two nonoverlapping groups. One group contains those sums whose first term is 1 and the
other those sums whose first term is 2. In the first group the remaining terms add to
n − 2, so it has F(n − 1) sums, and in the second group the remaining terms add to n −
3, so there are Fn−2 sums. So there are a total of Fn−1 + Fn−2 sums altogether,
showing this is equal to Fn.
Similarly, it may be shown that the sum of the first Fibonacci numbers up to the
nth
PRACTICE 4
CATELAN NUMBERS, SHROEDER NUMBERS
Catalan numbers form a sequence of natural numbers that occur in
various counting problems, often involving recursively-defined objects. They are
named after the Belgian mathematician Eugène Charles Catalan (1814–1894).
Using zero-based numbering, the nth Catalan number is given directly in terms
of binomial coefficients by
The first Catalan numbers for n = 0, 1, 2, 3, … are
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440,
9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420,
24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452,
… (sequence A000108in OEIS).
Schröder number describes the number of paths from the southwest corner (0, 0)
of an n × n grid to the northeast corner (n, n), using only single steps north,
northeast, or east, that do not rise above the SW–NE diagonal.
The first few Schröder numbers are
1, 2, 6, 22, 90, 394, 1806, 8558, .... (sequence A006318 in OEIS).
They were named after the German mathematician Ernst Schröder.
PRACTICE 5
BELL NUMBERS
The number of ways a set of elements can be partitioned into
nonempty subsets is called a Bell number and is denoted (not to be confused
with theBernoulli number, which is also commonly denoted ).
Example There are five ways the numbers
can be partitioned:
,
,
,
, and
, so
. (The explicit set
partitions on
can be enumerated using SetPartitions[n] in the Wolfram
Language package Combinatorica` .)
, and the first few Bell numbers for
, 2, ... are 1, 2, 5, 15, 52, 203, 877,
4140, 21147, 115975, ... (OEIS A000110). The numbers of digits in
for
,
1, ... are given by 1, 6, 116, 1928, 27665, ... (OEIS A113015).
Bell numbers are implemented in the Wolfram Language as BellB[n].
Though Bell numbers have traditionally been attributed to E. T. Bell as a result of
the general theory he developed in his 1934 paper (Bell 1934), the first systematic
study of Bell numbers was made by Ramanujan in chapter 3 of his second
notebook approximately 25-30 years prior to Bell's work (B. C. Berndt, pers.
comm., Jan. 4 and 13, 2010).
The first few prime Bell numbers occur at indices
, 3, 7, 13, 42, 55, 2841, ...
(OEIS A051130), with no others less than
(Weisstein, Apr. 23, 2006). These
correspond to the numbers 2, 5, 877, 27644437, ... (OEIS A051131).
was
proved prime by I. Larrosa Canestro in 2004 after 17 months of computation using
the elliptic curve primality proving program PRIMO.
Bell numbers are closely related to Catalan numbers. The diagram above shows the
constructions giving
and
, with line segments representing elements in
the same subset and dots representing subsets containing a single element
(Dickau). The integers can be defined by the sum
where
is a Stirling number of the second kind, i.e., as the Stirling
transform of the sequence 1, 1, 1, ....
The Bell numbers are given in terms of generalized hypergeometric functions by
(K. A. Penson, pers. comm., Jan. 14, 2007).
The Bell numbers can also be generated using the sum and recurrence relation
where
is a binomial coefficient, using the formula of Comtet (1974)
for
, where
Bell number
Example
Proof.
denotes the ceiling function. Dobiński's formula gives the th
PRACTICE 6
ALGORITHMS (SORTING, THE HALTING PROBLEM.)
DEFINITION 1
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.
EXAMPLE 1
Describe an algorithm for finding the maximum (largest) value in a finite
sequence of integers
Solution of Example 1: We perform the following steps.
1. Set the temporary maximum equal to the first integer in the sequence. (The
temporary maximum will be the largest integer examined at any stage of the
procedure.)
2. Compare the next integer in the sequence to the temporary maximum, and if it is
larger than the temporary maximum, set the temporary maximum equal to this
integer.
3. Repeat the previous step if there are more integers in the sequence.
4. Stop when there are no integers left in the sequence. The temporary maximum at
this point is the largest integer in the sequence.
ALGORITHM 1
Finding the Maximum Element in a Finite Sequence.
procedure max(a1, a2,...,an: integers)
max := a1
fori := 2 to n
if max <ai then
max := ai return max{max is the largest element}
SEARCHING ALGORITHMS
The problem of locating an element in an ordered list occurs in many
contexts. For instance, a program that checks the spelling of words searches for
them in a dictionary, which is just an ordered list of words. Problems of this kind
are called searching problems.
THE LINEAR SEARCH
The first algorithm that we will present is called the linear search, or
sequential search, algorithm. The linear search algorithm begins by comparing x
and a1. When x = a1, the solution is the location of a1, namely, 1. When x = a1,
compare x with a2. If x = a2, the solution is the location of a2, namely, 2. When x
= a2, compare x with a3. Continue this process, comparing x successively with
each term of the list until a match is found, where the solution is the location of
that term, unless no match occurs. If the entire list has been searched without
locating x, the solution is 0. The pseudocode for the linear search algorithm is
displayed as Algorithm 2.
ALGORITHM 2
The Linear Search Algorithm.
Procedure linear search(x: integer, a1, a2,...,an: distinct integers)
i := 1
while (i ≤ n and x = ai)
i := i + 1
ifi ≤ n then location := i
else location := 0
return location{location is the subscript of the term that equals x, or is 0 if x is not
found}
THE BINARY SEARCH
We will now consider another searching algorithm. This algorithm can be
used when the list has terms occurring in order of increasing size (for instance: if
the terms are numbers, they are listed from smallest to largest; if they are words,
they are listed in lexicographic, or alphabetic, order). This second searching
algorithm is called the binary search algorithm. It proceeds by comparing the
element to be located to the middle term of the list. The list is then split into two
smaller sublists of the same size, or where one of these smaller lists has one fewer
term than the other. The search continues by restricting the search to the
appropriate sublist based on the comparison of the element to be located and the
middle term. In Section 3.3, it will be shown that the binary search algorithm is
much more efficient than the linear search algorithm. Example 3 demonstrateshow
a binarysearchworks.
EXAMPLE 3
To search for 19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22, first
split this list, which has 16 terms, into two smaller lists with eight terms each,
namely, 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22. Then, compare 19 and the
largest term in the first list. Because 10 < 19, the search for 19 can be restricted to
the list containing the 9th through the 16th terms of the original list. Next, split this
list, which has eight terms, into the two smaller lists of four terms each, namely, 12
13 15 16 18 19 20 22. Because 16 < 19 (comparing 19 with the largest term of the
first list) the search is restricted to the second of these lists, which contains the 13th
through the 16th terms of the original list. The list 18 19 20 22 is split into two
lists, namely, 18 19 20 22. Because 19 is not greater than the largest term of the
first of these two lists, which is also 19, the search is restricted to the first list: 18
19, which contains the 13th and 14th terms of the original list. Next, this list of two
terms is split into two lists of one term each: 18 and 19. Because 18 < 19, the
search is restricted to the second list: the list containing the 14th term of the list,
which is 19. Now that the search has been narrowed down to one term, a
comparison is made, and 19 is located as the 14th term in the original list.
GreedyAlgorithms
Surprisingly, one of the simplest approaches often leads to a solution of an
optimization problem. This approach selects the best choice at each step, instead of
considering all sequences of steps that may lead to an optimal solution. Algorithms
that make what seems to be the “best” choice at each step are called greedy
algorithms.
EXAMPLE
Consider the problem of making n cents change with quarters, dimes,
nickels, and pennies, and using the least total number of coins. We can devise a
greedy algorithm for making change for n cents by making a locally optimal choice
at each step; that is, at each step we choose the coin of the largest denomination
possible to add to the pile of change without exceeding n cents. For example, to
make change for 67 cents, we first select a quarter (leaving 42 cents). We next
select a second quarter (leaving 17 cents), followed by a dime (leaving 7 cents),
followed by a nickel (leaving 2 cents), followed by a penny (leaving 1 cent),
followed by a penny.
ALGORITHM 6
Greedy Change-Making Algorithm.
procedure change(c1, c2,...,cr: values of denominations of coins, where
c1 > c2 > ··· >cr; n: a positive integer)
fori := 1 to r
di := 0 {di counts the coins of denomination ci used}
while n ≥ ci
di := di + 1 {add a coin of denomination ci}
n := n − ci
{di is the number of coins of denomination ci in the change for i = 1, 2,...,r}
Theorem 1
The greedy algorithm produces change using the fewest coins possible.
Proof:
We will use a proof by contradiction. Suppose that there is a positive integer
n such that there is a way to make change for n cents using quarters, dimes,
nickels, and pennies that uses fewer coins than the greedy algorithm finds. We first
note that q, the number of quarters used in this optimal way to make change for n
cents, must be the same as q, the number of quarters used by the greedy algorithm.
To show this, first note that the greedy algorithm uses the most quarters possible,
so q ≤ q. However, it is also the case that q cannot be less than q. If it were, we
would need to make up at least 25 cents from dimes, nickels, and pennies in this
optimal way to make change. But this is impossible by Lemma 1. Because there
must be the same number of quarters in the two ways to make change, the value of
the dimes, nickels, and pennies in these two ways must be the same, and these
coins are worth no more than 24 cents. There must be the same number of dimes,
because the greedy algorithm used the most dimes possible and by Lemma 1, when
change is made using the fewest coins possible, at most one nickel and at most four
pennies are used, so that the most dimes possible are also used in the optimal way
to make change. Similarly, we have the same number of nickels and, finally, the
same number of pennies.
Exercises
1. List all the steps used byAlgorithm 1 to find the maximum of the list 1, 8, 12, 9,
11, 2, 14, 5, 10, 4.
2. Determine which characteristics of an algorithm described in the text (after
Algorithm 1) the following procedures have and which they lack.
a) procedure double(n: positive integer)
while n > 0
n := 2n
b) procedure divide(n: positive integer)
while n ≥ 0
m := 1/n
n := n − 1
c) procedure sum(n: positive integer)
sum := 0
whilei< 10
sum := sum + i
d) procedure choose(a, b: integers)
x := either a or b
3. Devise an algorithm that finds the sum of all the integers in a list.
4. Describe an algorithm that takes as input a list of n integers and produces as
output the largest difference obtained by subtracting an integer in the list from the
one following it.
5. Describe an algorithm that takes as input a list of n integers in nondecreasing
order and produces the list of all values that occur more than once. (Recall that a
list of integers is nondecreasing if each integer in the list is at least as large as the
previous integer in the list.)
6. Describe an algorithm that takes as input a list of n integers and finds the
number of negative integers in the list.
7. Describe an algorithm that takes as input a list of n integers and finds the
location of the last even integer in the list or returns 0 if there are no even integers
in the list.
8. Describe an algorithm that takes as input a list of n distinct integers and finds the
location of the largest even integer in the list or returns 0 if there are no even
integers in the list
9. A palindrome is a string that reads the same forward and backward. Describe an
algorithm for determining whether a string of n characters is a palindrome.
10.. Describe an algorithm that interchanges the values of the variables x and y,
using only assignments. What is the minimum number of assignment statements
needed to do this?
11.Describe an algorithm that uses only assignment statements that replaces the
triple (x, y, z) with (y, z, x). What is the minimum number of assignment
statements needed?
12.List all the steps used to search for 9 in the sequence 1, 3, 4, 5, 6, 8, 9, 11 using
a) a linear search. b) a binary search.
13. List all the steps used to search for 7 in the sequence given in Exercise 13 for
both a linear search and a binary search.
14. Describe an algorithm that inserts an integer x in the appropriate position into
the list a1, a2,...,an of integers that are in increasing order.
15. Describe an algorithm for finding the smallest integer in a finite sequence of
natural numbers.
PRACTICE 7
SEQUENCES AND SUMMATIONS
DEFINITION 1 A sequence is a function from a subset of the set of integers
(usually either the set { 0 , 1 , 2 , . . .} or the set { 1 , 2 , 3 , . . .} ) to a set S . We
use the notation an to denote the image of the integer n .We call anaterm of the
sequence.
DEFINITION 2 A geometric progression is a sequence of the form a, ar, ar2 , . . .
, arn, . . . where the initial term a and the common ratio r are real numbers.
DEFINITION 3 An arithmetic progression is a sequence of the form
a, a + d, a + 2 d, . . . , a + nd, . . .
where the initial term a and the common difference d are real numbers
DEFINITION 4 A recurrence relation for the sequence {an} is an equation that
expresses anin terms of one or more of the previous terms of the sequence, namely,
a0 , a1 , . . . , an-1, for all integers n with n ≥ n0, where n0 is a nonnegative integer. A
sequence is called a solution of a recurrence relation if its terms satisfy the
recurrence relation. (A recurrence relation is said to recursivelydefine a sequence.
We will explain this alternative terminology in)
EXAMPLE Let {an} be a sequence that satisfies the recurrence relation an = an-1+
3 for n = 1 , 2 , 3 , . . . , and suppose that a0 = 2. What are a1, a2, and a3?
Solution: We see from the recurrence relation that a1 = a0 + 3 = 2 + 3 = 5. It then
follows that a2 = 5 + 3 = 8 and a3 = 8 + 3 = 11 .
EXAMPLE Let { an } be a sequence that satisfies the recurrence relation an = an-1an-2for n = 2 , 3 , 4 , . . . , and suppose that a0 = 3 and a1 = 5. What are a2 and a3?
Solution: We see from the recurrence relation that a = a1 – a0 = 5 − 3 = 2 and a3
= a2 −a1 = 2 − 5 = − 3. We can find a4, a5, and each successive term in a similar
way..
DEFINITION 5The Fibonacci sequence, f 0 , f 1 , f 2 , . . . , is defined by the initial
conditions f 0 = 0 , f 1 = 1, and the recurrence relation
fn = fn− 1 + fn− 2
forn = 2 , 3 , 4 , . . . .
EXAMPLE Determine whether the sequence {an} , where an = 3 n for every
nonnegative integer n , is asolution of the recurrence relation an = 2 an-1 – an-2 for n
= 2 , 3 , 4 , . . . . Answer the same question where an = 2n and where an = 5.
Solution: Suppose that an = 3nfor every nonnegative integer n . Then, for n ≥ 2, we
see that 2 an-1 – an-2 = 2 ( 3 (n − 1 )) − 3 (n − 2 ) = 3n = an. Therefore, {an} , where
an = 3n, is a solution of the recurrence relation. Suppose that an = 2nfor every
nonnegative integer n . Note thata0 = 1, a1 = 2, and a2 = 4. Because 2 a1-a0 = 2 · 2
− 1 = 3 ≠ a2, we see that {an} , where an = 2n, is not a solution of the recurrence
relation. Suppose that an = 5 for every nonnegative integern . Then for n ≥ 2, we
see that an = 2 an-1 – an-2= 2 · 5 − 5 = 5 = an. Therefore, {an} , wherean = 5, is a
solution of the recurrence relation.
Exercises
1. Find these terms of the sequence {an}, where an =
a) a0 b) a1 c) a4 d) a5
2. What is the term a8 of the sequence {an} if anequals
a) 2n-1 ? b) 7? c) 1 + (− 1 )n ?
d) −(− 2 )n?
3. What are the terms a0, a1, a2, and a3of the sequence {an} ,where an equals
a) 2n+1?b) (n + 1 )n+ 1?
c) [n/ 2] ? d) [n/ 2] +[ n/ 2] ?
4. What are the terms a 0, a 1, a 2, and a 3 of the sequence {an} ,where an equals
a) (− 2 )n? b) 3?
c) 7 + 4n?d) 2n + (− 2 )n?
5. List the first 10 terms of each of these sequences.
a) the sequence that begins with 2 and in which eachsuccessive term is 3 more than
the preceding term
b) the sequence that lists each positive integer threetimes, in increasing order
c) the sequence that lists the odd positive integers in in-creasing order, listing each
odd integer twice
d) the sequence whose nth term is n! – 2n
e) the sequence that begins with 3, where each succeed-ing term is twice the
preceding term
f ) the sequence whose first term is 2, second term is 4,and each succeeding term is
the sum of the two preceding terms
g) the sequence whose n th term is the number of bitsin the binary expansion of the
number n (defined inSection 4.2)
h) the sequence where the n th term is the number of letters in the English word for
the index n
6.Find at least three different sequences beginning with the
terms 1, 2, 4 whose terms are generated by a simple formula or rule.
7.Find the first six terms of the sequence defined by each
of these recurrence relations and initial conditions.
a) an = − 2 an− 1, a 0 = − 1
b) an = an− 1 − an− 2, a 0 = 2, a 1 = − 1
c) an = 3 a 2n− 1 , a 0 = 1
d) an = nan− 1 + a 2n− 2 , a 0 = − 1 , a 1 = 0
e) an = an− 1 − an− 2 + an− 3 , a 0 = 1 , a 1 = 1 , a 2 = 2
PRACTICE 8
INTEGER REPRESENTATIONS AND ALGORITHMS
THEOREM 1Let b be an integer greater than 1. Then if n is a positive integer, it
can be expressed uniquely in the form n = akbk + ak−1bk−1 +· · ·+a1b + a0, where k
is a nonnegative integer, a0, a1, . . . ,ak are nonnegative integers less than b, and ak=
0.
Theorem 1 is called the base b expansion of n. The base b expansion of n is
denoted by (akak−1 . . . a1a0)b. For instance, (245)8 represents 2 · 82 + 4 · 8 + 5 = 165.
Typically, the subscript 10 is omitted for base 10 expansions of integers because
base 10, or decimal expansions, are commonly used to represent integers.
BINARY EXPANSIONS Choosing 2 as the base gives binary expansions of
integers. In binary notation each digit is either a 0 or a 1. In other words, the binary
expansion of an integer is just a bit string. Binary expansions (and related
expansions that are variants of binary expansions) are used by computers to
represent and do arithmetic with integers.
EXAMPLE 1 What is the decimal expansion of the integer that has (1 0101 1111)2
as its binary expansion?
Solution: We have
(1 0101 1111)2 = 1 · 28 + 0 · 27 + 1 · 26 + 0 · 25 + 1 · 24
+ 1 · 23 + 1 · 22 + 1 · 21 + 1 · 20 = 351.
OCTAL AND HEXADECIMAL EXPANSIONS Among the most important
bases in computer science are base 2, base 8, and base 16. Base 8 expansions are
called octal expansions and base 16 expansions are hexadecimal expansions.
EXAMPLE 2 What is the decimal expansion of the number with octal expansion
(7016)8?
Solution: Using the definition of a base b expansion with b = 8 tells us that
(7016)8 = 7 · 83 + 0 · 82 + 1 · 8 + 6 = 3598.
Sixteen different digits are required for hexadecimal expansions. Usually, the
hexadecimal digits used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where
the letters A through F. represent the digits corresponding to the numbers 10
through 15 (in decimal notation).
EXAMPLE 3 What is the decimal expansion of the number with hexadecimal
expansion (2AE0B)16?
Solution: Using the definition of a base b expansion with b = 16 tells us that
(2AE0B)16 = 2 · 164 + 10 · 163 + 14 · 162 + 0 · 16 + 11 = 175627.
Each hexadecimal digit can be represented using four bits. For instance, we see
that
(1110 0101)2 = (E5)16 because (1110)2 = (E)16 and (0101)2 = (5)16. Bytes, which are
bit strings of length eight, can be represented by two hexadecimal digits.
EXAMPLE 4 Find the octal expansion of (12345)10
Solution: First, divide 12345 by 8 to obtain
12345 = 8 · 1543 + 1.
Successively dividing quotients by 8 gives
1543 = 8 · 192 + 7,
192 = 8 · 24 + 0,
24 = 8 · 3 + 0,
3 = 8 · 0 + 3.
The successive remainders that we have found, 1, 7, 0, 0, and 3, are the digits from
the right to the left of 12345 in base 8. Hence,
(12345)10 = (30071)8.
EXAMPLE 5 Find the hexadecimal expansion of (177130)10.
Solution: First divide 177130 by 16 to obtain
177130 = 16 · 11070 + 10.
Successively dividing quotients by 16 gives
11070 = 16 · 691 + 14,
691 = 16 · 43 + 3,
43 = 16 · 2 + 11,
2 = 16 · 0 + 2.
The successive remainders that we have found, 10, 14, 3, 11, 2, give us the digits
from the rightto the left of 177130 in the hexadecimal (base 16) expansion of
(177130)10. It follows that
(177130)10 = (2B3EA)16.
(Recall that the integers 10, 11, and 14 correspond to the hexadecimal digits A, B,
and E, respectively.)
EXAMPLE 6 Find the binary expansion of (241)10.
Solution: First divide 241 by 2 to obtain
241 = 2 · 120 + 1.
Successively dividing quotients by 2 gives
120 = 2 · 60 + 0,
60 = 2 · 30 + 0,
30 = 2 · 15 + 0,
15 = 2 · 7 + 1,
7 = 2 · 3 + 1,
3 = 2 · 1 + 1,
1 = 2 · 0 + 1.
The successive remainders that we have found, 1, 0, 0, 0, 1, 1, 1, 1, are the digits
from the right
to the left in the binary (base 2) expansion of (241)10. Hence,
(241)10 = (1111 0001)2.
ALGORITHM 1 Constructing Base b Expansions.
procedure base b expansion(n, b: positive integers with b > 1)
q := n
k := 0
while q = 0
ak := q mod b
q := q div b
k := k + 1
return (ak−1, . . . , a1, a0) {(ak−1 . . . a1a0)b is the base b expansion of n}
Remark: Note that Algorithm 1 can be thought of as a greedy algorithm, as the
base b digits are taken as large as possible in each step.
PRACTICE 9
Algorithms for Integer Operations
The algorithms for performing operations with integers using their binary
expansions are extremely important in computer arithmetic.We will describe
algorithms for the addition and the multiplication of two integers expressed in
binary notation. We will also analyze the computationalcomplexity of these
algorithms, in terms of the actual number of bit operations used.
Throughout this discussion, suppose that the binary expansions of a and b are
a = (an−1an−2 . . . a1a0)2, b = (bn−1bn−2 . . . b1b0)2,
so that a and b each have n bits (putting bits equal to 0 at the beginning of one of
these expansions if necessary).
ADDITION ALGORITHM
To add a and b, first add their rightmost bits. This gives
a0 + b0 = c0 · 2 + s0,
where s0 is the rightmost bit in the binary expansion of a + b and c0 is the carry,
which is either 0 or 1. Then add the next pair of bits and the carry,
a1 + b1 + c0 = c1 · 2 + s1,
where s1 is the next bit (from the right) in the binary expansion of a + b, and c1 is
the carry. Continue this process, adding the corresponding bits in the two binary
expansions and the carry, to determine the next bit from the right in the binary
expansion of a + b. At the last stage, add an−1, bn−1, and cn−2 to obtain cn−1 · 2 +
sn−1. The leading bit of the sum is sn = cn−1. This procedure produces the binary
expansion of the sum, namely, a + b = (snsn−1sn−2 . . . s1s0)2
EXAMPLE 8 Add a = (1110)2 and b = (1011)2.
Solution: Following the procedure specified in the algorithm, first note that
a0 + b0 = 0 + 1 = 0 · 2 + 1,
so that c0 = 0 and s0 = 1. Then, because
a1 + b1 + c0 = 1 + 1 + 0 = 1 · 2 + 0,
it follows that c1 = 1 and s1 = 0. Continuing,
a2 + b2 + c1 = 1 + 0 + 1 = 1 · 2 + 0,
so that c2 = 1 and s2 = 0. Finally, because
a3 + b3 + c2 = 1 + 1 + 1 = 1 · 2 + 1
follows that c3 = 1 and s3 = 1. This means that s4 = c3 = 1. Therefore, s = a + b = (1
1001)2.
ALGORITHM 2 Addition of Integers.
procedure add(a, b: positive integers)
{the binary expansions of a and b are (an−1an−2 . . . a1a0)2
and (bn−1bn−2 . . . b1b0)2, respectively}
c := 0
for j := 0 to n − 1
d := (aj + bj + c)/2
sj := aj + bj + c − 2d
c := d
sn := c
return (s0, s1, . . . , sn) {the binary expansion of the sum is (snsn−1 . . . s0)2}
MULTIPLICATION ALGORITHM
Using the distributive law, we see that
ab = a(b020 + b121 +· · ·+bn−12n−1)= a(b020) + a(b121)+· · ·+a(bn−12n−1).
ALGORITHM 3 Multiplication of Integers.
procedure multiply(a, b: positive integers)
{the binary expansions of a and b are (an−1an−2 . . . a1a0)2
and (bn−1bn−2 . . . b1b0)2, respectively}
for j := 0 to n − 1
ifbj = 1 then cj := a shifted j places
elsecj := 0
{c0, c1, . . . , cn−1 are the partial products}
p := 0
for j := 0 to n − 1
p := p + cj
return p {p is the value of ab}
EXAMPLE 10 Find the product of a = (110)2 and b = (101)2.
Solution: First note that
ab0 · 20 = (110)2 · 1 · 20 = (110)2,
ab1 · 21 = (110)2 · 0 · 21 = (0000)2,
andab2 · 22 = (110)2 · 1 · 22 = (11000)2.
ALGORITHM FOR div AND mod
Given integers a and d, d > 0, we can find q =a div d and r = a mod d using
Algorithm 4.
ALGORITHM 4 Computing div and mod.
procedure division algorithm(a: integer, d: positive integer)
q := 0
r := |a|
while r ≥ d
r := r − d
q := q + 1
if a < 0 and r > 0 then
r := d − r
q := −(q + 1)
return (q, r) {q = a div d is the quotient, r = a mod d is the remainder}
PRACTICE 10
THE EUCLIDEAN ALGORITHM
Computing the greatest common divisor of two integers directly from the prime
factorizations of these integers is inefficient. The reason is that it is timeconsuming to find prime factorizations. We will give a more efficient method of
finding the greatest common divisor, called the Euclidean algorithm. This
algorithm has been known since ancient times. It is named after the ancient Greek
mathematician Euclid, who included a description of this algorithm in his book The
Elements. Before describing the Euclidean algorithm, we will show how it is used
to find gcd (91, 287).
First, divide 287, the larger of the two integers, by 91, the smaller, to obtain
287 = 91 ・ 3 + 14.
Any divisor of 91 and 287 must also be a divisor of 287 − 91 ・ 3 = 14. Also, any
divisor of 91 and 14 must also be a divisor of 287 = 91 ・ 3 + 14. Hence, the
greatest common divisor of 91 and 287 is the same as the greatest common divisor
of 91 and 14. This means that the problem of finding gcd(91, 287) has been
reduced to the problem of finding gcd(91, 14).
Next, divide 91 by 14 to obtain
91 = 14 ・ 6 + 7.
Because any common divisor of 91 and 14 also divides 91 − 14 ・ 6 = 7 and any
common divisor of 14 and 7 divides 91, it follows that gcd(91, 14) = gcd(14, 7).
Continue by dividing 14 by 7, to obtain
14 = 7 ・ 2.
Because 7 divides 14, it follows that gcd(14, 7) = 7. Furthermore, because gcd(287,
91) =gcd(91, 14) = gcd(14, 7) = 7, the original problem has been solved.
We now describe how the Euclidean algorithm works in generality.We will use
successive divisions to reduce the problem of finding the greatest common divisor
of two positive integers to the same problem with smaller integers, until one of the
integers is zero. The Euclidean algorithm is based on the following result about
greatest common divisors and the division algorithm.
LEMMA 1 Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) =
gcd(b, r).
Proof: If we can show that the common divisors of a and b are the same as
the common divisors of b and r, we will have shown that gcd(a, b) = gcd(b, r),
because both pairs must have the same greatest common divisor.
So suppose that d divides both a and b. Then it follows that d also divides a −
bq = r. Hence, any common divisor of a and b is also a common divisor of b and r.
Likewise, suppose that d divides both b and r. Then d also divides bq + r =
a. Hence, any common divisor of b and r is also a common divisor of a and b.
Consequently, gcd(a, b) = gcd(b, r).
Example 1 Find the greatest common divisor of 414 and 662 using the Euclidean
algorithm.
Solution 1: Successive uses of the division algorithm give:
662 = 414 ・ 1 + 248
414 = 248 ・ 1 + 166
248 = 166 ・ 1 + 82
166 = 82 ・ 2 + 2
82 = 2 ・ 41.
Hence, gcd(414, 662) = 2, because 2 is the last nonzero remainder.
In Algorithm 1, the initial values of x and y are a and b, respectively. At
each stage of the procedure, x is replaced by y, and y is replaced by x mod y, which
is the remainder when x is divided by y. This process is repeated as long as y _= 0.
The algorithm terminates when y = 0, and the value of x at that point, the last
nonzero remainder in the procedure, is the greatest common divisor of a and b.
We will study the time complexity of the Euclidean algorithm in Section 5.3,
where we will show that the number of divisions required to find the greatest
common divisor of a and b, where a ≥ b, is O(log b).
Exercises
1. Use the Euclidean algorithm to find
a) gcd(1, 5).
b) gcd(100, 101).
c) gcd(123, 277).
d) gcd(1529, 14039).
e) gcd(1529, 14038).
f ) gcd(11111, 111111).
2. Use the Euclidean algorithm to find
a) gcd(12, 18).
b) gcd(111, 201).
c) gcd(1001, 1331).
d) gcd(12345, 54321).
e) gcd(1000, 5040).
f ) gcd(9888, 6060).
3. How many divisions are required to find gcd(21, 34) using the Euclidean
algorithm?
4. How many divisions are required to find gcd(34, 55) using the Euclidean
algorithm?
5. Use the extended Euclidean algorithm to express gcd(1001, 100001) as a linear
combination of 1001 and 100001.
PRACTICE 11
DIVISIBILITY AND MODULAR ARITHMETIC
Theorem 1Let a, b and c be integers, where a  0. Then
(I)
If a / b and a / c, then a /(b  c) ;
(II) If a / b ,then a / bc, for all integers c;
(III) If a / b and b / c, then a / c ;
Example 1 Let n and d be positive integers. How many positive integers not
exceeding n are divisible by d?
Solution: The positive integers divisible by d are all the integers of the form dk,
where k is a positive integer. Hence, the number of positive integers divisible by d
that do not exceed n equals the number of integers k with 0< k  n/d. Therefore,
there are [n/d] positive integers not exceeding n that are divisible by d.
The Division Algorithm
Theorem 2 The Division Algorithm Let a be an integer and d a positive integer,
Then there are unique integers q and r, with 0  r <d, such that a= dq+r.
Example 2 What are the quotient and remainder when 101 is divided by 11?
Solution:We have
101=11*9+2.
Hence, the quotient and remainder when 101 is divided by 11 is 9=101 div11, and
the remainder is 2=101 mod 11.
Modular Arihmetic
Theorem 3Let a and b be integers, and let m be a positive integer. Then
a  b(mod m) if and only if a mod m  b mod m.
Example 3 Determinewhether17 is cogruent
PRACTICE 12
COMPLEXITY OF MATRIX MULTIPLICATION
The definition of the product of two matrices can be expressed as an algorithm for
computing the product of two matrices. Suppose that C = [cij ] is the m × n matrix
that is the product of the m × k matrix A = [aij ] and the k × n matrix B = [bij] .
The algorithm based on the definition of the matrix product is expressed in
pseudocode in Algorithm 1.
ALGORITHM 1Matrix Multiplication.
procedure matrix multiplication(A, B: matrices)
fori := 1 to m
for j := 1 to n
cij := 0
for q := 1 to k
cij:= cij + aiqbqj
return C { C = [cij] is the product of A and B }
EXAMPLE How many additions of integers and multiplications of integers are
used by Algorithm 1 to multiply two n × n matrices with integer entries?
Solution: There are n 2entries in the product of A and B. To find each entry requires
a total of n multiplications and n − 1 additions. Hence, a total of n 3multiplications
and n 2 (n − 1 )additions are used.
EXAMPLE How many bit operations are used to find A ʘ B, where A and B are n
× n zero–one matrices?
Solution: There are n 2 entries in AʘB. Using Algorithm 2, a total of n ORs and n
ANDs are used to find an entry of A ʘB. Hence, 2 n bit operations are used to find
each entry. Therefore, 2 n 3bit operations are required to compute AʘBusing
Algorithm 2.
ALGORITHM 2 The Boolean Product of Zero-One Matrices.
procedure Boolean product of Zero-One Matrices (A, B: zero–one
matrices)
fori := 1 tom
forj := 1 ton
cij:= 0
forq := 1 tok
cij:= cij ∨ (aiq ∧ bqj)
return C { C = [cij] is the Boolean product of A and B }
Exercises
1. Give a big- O estimate for the number of operations (where an operation is an
addition or a multiplication) used in this segment of an algorithm.
t := 0
fori := 1 to 3
for j := 1 to 4
t := t + ij
2. Give a big- O estimate for the number additions used in this segment of an
algorithm.
t := 0
fori := 1 to n
for j := 1 to n
t := t + i + j
3. Give a big- O estimate for the number of operations, where an operation is a
comparison or a multiplication, used in this segment of an algorithm (ignoring
comparisons used to test the conditions in the for loops, where a 1 , a 2 , ..., an are
positive real numbers).
m := 0
fori := 1 to n
for j := i + 1 to n
m := max (aiaj , m)
4. Give a big- O estimate for the number of operations, where an operation is an
addition or a multiplication, used in this segment of an algorithm (ignoring
comparisons used to test the conditions in the while loop).
i := 1
t := 0
whilei ≤ n
t := t + i
i := 2 i
5. How many comparisons are used by the algorithm givenin Exercise 16 of
Section 3.1 to find the smallest naturalnumber in a sequence of n natural numbers?
6.
a) Use pseudocode to describe the algorithm that puts the first four terms of a list
of real numbers of arbitrary length in increasing order using the insertion sort.
b) Show that this algorithm has time complexity O( 1 ) in terms of the number of
comparisons used.
7. Suppose that an element is known to be among the first four elements in a list of
32 elements. Would a lin- ear search or a binary search locate this element more
rapidly?
8. Given a real number x and a positive integer k , determine the number of
multiplications used to find x 2k starting with x and successively squaring (to find x
2
, x 4 , and soon). Is this a more efficient way to find x 2k than by mul- tiplying x by
itself the appropriate number of times?
9. Give a big- O estimate for the number of comparisons used by the algorithm that
determines the number of 1s in a bit string by examining each bit of the string to
deter- mine whether it is a 1 bit (see Exercise 25 of Section 3.1).
10. a) Show that this algorithm determines the number of 1 bits in the bit string S :
procedure bit count( S : bit string)
count := 0
while S = 0
count : = count + 1
S : = S ∧ ( S − 1)
return count {count is the number of 1s in S }
Here S − 1 is the bit string obtained by changing the rightmost 1 bit of S to a 0 and
all the 0 bits to the right of this to 1s. [Recall that S ∧ (S − 1 ) is the bitwise AND
of S and S − 1.]
b) How many bitwise AND operations are needed to find the number of 1 bits in a
string S using the algorithm in part (a)?
11.
a) Suppose we have n subsets S1 , S 2 , . . . , Sn of the set { 1 , 2 , . . . , n} . Express
a brute-force algorithm that de- termines whether there is a disjoint pair of these
sub- sets. [Hint: The algorithm should loop through the subsets; for each subset Si,
it should then loop through all other subsets; and for each of these other subsets Sj ,
it should loop through all elements k in Si to determine whether k also belongs to
Sj .]
b) Give a big- O estimate for the number of times thealgorithm needs to determine
whether an integer is inone of the subsets.
PRACTICE 13
MODULAR EXPONENTIATION
In cryptography it is important to be able to find bn mod m efficiently, where
b, n, and m are large integers. It is impractical to first compute bn and then find its
remainder when divided by m because bn will be a huge number. Instead, we can
use an algorithm that employs the binary expansion of the exponent n.
Before we present this algorithm, we illustrate its basic idea. We will
explain how to use the binary expansion of n, say n = (ak−1 . . . a1a0)2, to
compute bn. First, note that bn = b ak−1・2 k−1+・・・+a 1・2+a0 = b ak−1・2 k−1 ・
・ ・ b a1・2 ・ b a0 .
This shows that to compute bn, we need only compute the values of b, b2,
(b2)2 = b4, (b4)2 =b8, . . . , b2k . Once we have these values, we multiply the terms b2j
in this list, where aj = 1. (For efficiency, after multiplying by each term, we reduce
the result modulo m.) This gives us bn. For example, to compute 311 we first note
that 11 = (1011)2, so that 311 = 383231. By successively squaring, we find that 32 =
9, 34 = 92 = 81, and 38 = (81)2 = 6561. Consequently, 311 =
383231 = 6561 ・ 9 ・ 3 = 177,147.
Example 1 Use Algorithm 5 to find 3644 mod 645.
Solution 1 Given algorithm initially sets x = 1 and power = 3 mod 645 = 3. In the
computation of 3644 mod 645, this algorithm determines 32j mod 645 for j = 1, 2, . .
. , 9 by successively squaring and reducing modulo 645. If aj = 1 (where aj is the
bit in the j th position in the binary expansion of 644, which is (1010000100)2), it
multiplies the current value of x by 32j mod 645 and reduces the result modulo 645.
Here are the steps used:
Example 2 Find 7222 mod 11.
Solution 2 We can use Fermat’s little theorem to evaluate 7222 mod 11 rather than
using the fast modular exponentiation algorithm. By Fermat’s little theorem we
know that 710 ≡ 1 (mod 11), so (710)k ≡ 1 (mod 11) for every positive integer k. To
take advantage of this last congruence, we divide the exponent 222 by 10, finding
that 222 = 22 ・ 10 + 2.We now see that 7222 = 722・10+2 = (710)2272 ≡ (1)22 ・ 49 ≡ 5
(mod 11).
It follows that 7222 mod 11 = 5.
Examples:
1)
2)
3)
4)
5)
Calculate the value of: 2320 mod 29.
Calculate the value of: 23391 mod 55.
Calculate the value of: 31397 mod 55.
Find the sequence of pseudorandom numbers generated by the power
generator with p = 7, d = 3, and seed x0 = 2.
Find the sequence of pseudorandom numbers generated by the power
generator with p = 11, d = 2, and seed x0 = 3.
PRACTICE 14
PRIMES AND GREATEST COMMON DIVISORS
Introduction
In Section 4.1 we studied the concept of divisibility of integers. One
important concept based on divisibility is that of a prime number. A prime is
an integer greater than 1 that is divisible by no positive integers other than 1
and itself. The study of prime numbers goes back to ancient times. Thousands
of years ago it was known that there are infinitely many primes; the proof of
this fact, found in the works of Euclid, is famous for its elegance and beauty.
We will discuss the distribution of primes among the integers. We will
describe some of the results about primes found by mathematicians in the last
400 years. In particular, we will introduce an important theorem, the
fundamental theorem of arithmetic. This theorem, which asserts that every
positive integer can be written uniquely as the product of primes in
nondecreasing order, has many interesting consequences. We will also discuss
some of the many old conjectures about primes that remain unsettled today.
Primes have become essential in modern cryptographic systems, and we
will develop some of their properties important in cryptography. For example,
finding large primes is essential in modern cryptography. The length of time
required to factor large integers into their prime factors is the basis for the
strength of some important modern cryptographic systems.
In this section we will also study the greatest common divisor of two
integers, as well as the least common multiple of two integers. We will
develop an important algorithm for computing greatest common divisors,
called the Euclidean algorithm.
Primes
Every integer greater than 1 is divisible by at least two integers, because
a positive integer is divisible by 1 and by itself. Positive integers that have
exactly two different positive integer factors are called primes.
DEFINITION 1:
An integer p greater than 1 is called prime if the only positive factors of p are 1 and
p. A positive integer that is greater than 1 and is not prime is called composite.
Remark:
The integer n is composite if and only if there exists an integer a such
that a \ n and 1 < a < n.
EXAMPLE 1:
The integer 7 is prime because its only positive factors are 1 and 7,
whereas the integer 9 is composite because it is divisible by 3.
THEOREM 1:
THE FUNDAMENTAL THEOREM OF ARITHMETIC Every integer greater
than 1
can be written uniquely as a prime or as the product of two or more primes where
the prime factors are written in order of nondecreasing size.
Example 2 gives some prime factorizations of integers.
EXAMPLE 2:
The prime factorizations of 100, 641, 999, and 1024 are given by
100 = 2 · 2 · 5 · 5 = 22 52 ,
641 = 641,
999 = 3 · 3 · 3 · 37 = 33 · 37,
1024 = 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 = 210 .
Trial Division
It is often important to show that a given integer is prime. For instance,
in cryptology, large primes are used in some methods for making messages
secret. One procedure for showing that an integer is prime is based on the
following observation.
THEOREM 2:
If n is a composite integer, then n has a prime divisor less than or
equal to √n.
Proof: If n is composite, by the definition of a composite integer, we know that it
has a factor a with 1 < a < n. Hence, by the definition of a factor of a positive
integer, we have n = ab, where b is a positive integer greater than 1.We will
showthat a ≤ √n or b ≤√n. Ifa >√n andb >√n, then ab >√n ·√n = n, which is a
contradiction. Consequently, a ≤√n or b ≤√n. Because both a and b are divisors of
n, we see that n has a positive divisor not exceeding √n. This divisor is either prime
or, by the fundamental theorem of arithmetic, has a prime divisor less than itself. In
either case, n has a prime divisor less than or equal to √n. From Theorem 2, it
follows that an integer is prime if it is not divisible by any prime less than or equal
to its square root. This leads to the brute-force algorithm known as trial division.
To use trial division we divide n by all primes not exceeding √n and conclude that
n is prime if it is not divisible by any of these primes. In Example 3 we use trial
division to show that 101 is prime.
EXAMPLE 3:
Show that 101 is prime.
Solution: The only primes not exceeding √101 are 2, 3, 5, and 7. Because
101 is not divisible by 2, 3, 5, or 7 (the quotient of 101 and each of these
integers is not an integer), it follows that 101 is prime.
Because every integer has a prime factorization, it would be useful to
have a procedure for finding this prime factorization. Consider the problem
of finding the prime factorization of n. Begin by dividing n by successive
primes, starting with the smallest prime, 2. If n has a prime factor, then by
Theorem 3 a prime factor p not exceeding √n will be found. So, if no prime
factor not exceeding √n is found, then n is prime. Otherwise, if a prime
factor p is found, continue by factoring n/p. Note that n/p has no prime
factors less than p. Again, if n/p has no prime factor greater than or equal to
p and not exceeding its square root, then it is prime. Otherwise, if it has a
prime factor q, continue by factoring n/(pq). This procedure is continued
until the factorization has been reduced to a prime. This procedure is
illustrated in Example 4.
Exercises:
1. Determine whether each of these integers is prime.
a) 21 b) 29
c) 71 d) 97
e) 111 f) 143
2. Determine whether each of these integers is prime.
a) 19 b) 27
c) 93 d) 101
e) 107 f) 113
3. Find the prime factorization of each of these integers.
a) 88 b) 126 c) 729
d) 1001 e) 1111 f) 909,090
4. Find the prime factorization of each of these integers.
a) 39 b) 81 c) 101
d) 143 e) 289 f) 899
5. Find the prime factorization of 10!.
PRACTICE 15
Representing Graphs and Graph Isomorphism
Introduction
There are many useful ways to represent graphs. As we will see
throughout this chapter, in working with a graph it is helpful to be able to
choose its most convenient representation. In this section we will show how
to represent graphs in several different ways.
Sometimes, two graphs have exactly the same form, in the sense that
there is a one-to-one correspondence between their vertex sets that preserves
edges. In such a case, we say that the two graphs are isomorphic. Determining
whether two graphs are isomorphic is an important problem of graph theory
that we will study in this section.
Representing Graphs
One way to represent a graph without multiple edges is to list all the
edges of this graph. Another way to represent a graph with no multiple edges
is to use adjacency lists, which specify the vertices that are adjacent to each
vertex of the graph.
EXAMPLE 1:
Use adjacency lists to describe the simple graph given in Figure 1.
Solution: Table 1 lists those vertices adjacent to each of the vertices of
the graph.
TABLE 1 An Adjacency List
for a Simple Graph.
Vertex
a
b
c
d
e
Adjacent Vertices
b, c, e
a
a, e, d
c, e
a, c, d
FIGURE 1 A Simple Graph.
TABLE 2 An Adjacency List for a
Directed Graph
Vertex
Adjacent Vertices
a
b
c
d
e
b, c, d, e
b, d
a, c, e
b, c, d
FIGURE 2 A Directed Graph.
EXAMPLE 2:
Represent the directed graph shown in Figure 2 by listing all the vertices that
are the terminal vertices of edges starting at each vertex of the graph.
Solution: Table 2 represents the directed graph shown in Figure 2
Adjacency Matrices
Carrying out graph algorithms using the representation of graphs by lists of
edges, or by adjacency lists, can be cumbersome if there are many edges in the
graph. To simplify computation, graphs can be represented using matrices. Two
types of matrices commonly used to represent graphs will be presented here. One
is based on the adjacency of vertices, and the other is based on incidence of
vertices and edges.
Suppose that G = (V ,E) is a simple graph where |V| = n. Suppose that the
vertices of G are listed arbitrarily as v1, v2, . . . , vn. The adjacency matrix A (or
AG) of G, with respect to this listing of the vertices, is the n x n zero–one matrix
with 1 as its (i, j )th entry when vi and vj are adjacent, and 0 as its (i, j )th entry
when they are not adjacent. In other words, if its
adjacency matrix is A = [aij ], then
EXAMPLE 3:
Use an adjacency matrix to represent the graph shown in Figure 3.
Solution: We order the vertices as a, b, c, d. The matrix representing this
graph is
0111
1010
1100
1000
FIGURE 3 Simple Graph.
Exercises