Transcript Document

Chapter 1
Mathematical
Tools and
Techniques
1
Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Logic and Proofs
• Logic involves propositions which have truth values,
either true or false
– “0 = 1” is a proposition whose value is false
– “peanut butter is a source of protein” is true
• A proposition containing a free variable can be true or
false, depending on the value of the variable
– “x - 1 is prime” is true for x = 8, false for x = 10
Introduction to Computation
2
Logic and Proofs (cont’d.)
• Compound propositions are constructed using the
logical connectives ∧, ⋁, ¬, , and ↔
Introduction to Computation
3
Logic and Proofs (cont’d.)
• The truth value of ¬p is the opposite of the truth
value of p
• Truth tables show possible combinations of p and q
p
q
p⋀q
p∨q
p→q
p↔q
T
T
T
T
T
T
T
F
F
T
F
F
F
T
F
T
T
F
F
F
F
F
T
T
Introduction to Computation
4
Logic and Proofs (cont’d.)
• For a proposition like (p ∨ q) ∧ ¬(p → q), fill in the
table in the order shown. Column 3 is the negation of
column 2, column 4 is the conjunction of columns 1
and 3
1
4
3
2
p
q
(p ∨ q)
∧
¬
(p → q)
T
T
T
F
F
T
T
F
T
T
T
F
F
T
T
F
F
T
F
F
F
F
F
T
Introduction to Computation
5
Logic and Proofs (cont’d.)
• A tautology is a proposition that is always true
– Example: p ∨ ¬ p
• A contradiction is a proposition that is always false
– Example: p ∧ ¬ p
• Two propositions P and Q are said to be logically
equivalent if they always have the same truth value
– This is written as P ⇔ Q
• A proposition P is said to logically imply a proposition
Q if, whenever P is true, Q is also true
– This is written as P ⇒ Q
Introduction to Computation
6
Logic and Proofs (cont’d.)
• P → Q and P ⇒ Q look similar; however,
– P → Q is a proposition; it has a truth value
– P ⇒ Q is a “meta-statement”, an assertion about the
relationship between propositions P and Q
– P ⇒ Q means that P → Q is a tautology
– Similarly, P ⇔ Q means that P ↔ Q is a tautology
Introduction to Computation
7
Logic and Proofs (cont’d.)
• Logical identities can be used to simplify compound
propositions:
– The commutative laws
p∨qq∨p
p∧qq∧p
– The associative laws
p ∨ (q ∨ r)  (p ∨ q) ∨ r
p ∧ (q ∧ r)  (p ∧ q) ∧ r
– The distributive laws
p ∨ (q ∧ r)  (p ∨ q) ∧ (p ∨ r)
p ∧ (q ∨ r)  (p ∧ q) ∨ (p ∧ r)
Introduction to Computation
8
Logic and Proofs (cont’d.)
• Logical identities (cont’d.)
– The De Morgan laws
(p ∨ q)  p ∧ q
(p ∧ q)  p ∨ q
– An equivalent formulation of the conditional
(p  q)  (p ∨ q)
– The contrapositive of a conditional
(p  q)  (q  p)
– An equivalent formulation of the biconditional
(p  q)  (p  q) ∧ (q  p)
Introduction to Computation
9
Logic and Proofs (cont’d.)
• The truth value of a proposition like “x - 1 is prime”
depends on the value of x
– We can use logical quantifiers,  (for every) or  (for
some) to obtain statements that are no longer
statements about specific elements in the domain, but
statements about the domain itself
• x (x - 1 is prime)
• x (x - 1 is prime)
– The first proposition is true if x – 1 is prime for every
value of x in the domain; the second is true if x – 1 is
prime for some (at least one) value of x.
Introduction to Computation
10
Logic and Proofs (cont’d.)
• In statements with more than one quantifier, order
matters:
– x (y (x < y)) and y (x (x < y)) are not logically
equivalent
– The first says that for every x there is some larger y
(perhaps depending on x); the second says that there
is a single value y that is larger than every x
• Here are two identities involving the negation of a
quantified statement:
(x (P(x))  x (P(x))
(x (P(x))  x (P(x))
Introduction to Computation
11
Logic and Proofs (cont’d.)
• A proof is a series of statements, each of which is
derived from:
– Initial assumptions
– Statements that have been derived previously
– Generally accepted facts
• The derivations use principles of logical reasoning
Introduction to Computation
12
Logic and Proofs (cont’d.)
• A direct, constructive proof
– Prove that for every two integers a and b, if a and b are
odd, then ab is odd
– Use the definition of odd to restate this as follows:
If there exist integers i and j so that a = 2i +1 and
b = 2j +1, then there exists k such that ab = 2k +1
Proof: ab = (2i + 1)(2j + 1)
= 4ij + 2i + 2j + 1
= 2(2ij + i + j) + 1
If we let k be (2ij + i + j), we have the result we want
Introduction to Computation
13
Logic and Proofs (cont’d.)
• An indirect proof
– Prove that for every three positive integers i, j, and n, if
ij = n, then i ≤  n or j ≤  n
– Prove the contrapositive: assume there exist i, j, and n
such that (i ≤ n  j ≤ n)
– By De Morgan, this implies (i ≤ n)  (j ≤ n), or
(i > n)  (j > n)
– Therefore i j > n n = n, which implies that i j  n
– This is a direct proof of the contrapositive statement,
and thus an indirect proof of the original statement
Introduction to Computation
14
Logic and Proofs (cont’d.)
• A proof by contradiction that 2 is irrational
– Assume that there exist positive integers m and n such
that m/n = 2
– Then, by dividing m and n by all common factors, we
get p and q with no common factors such that
p/q = 2
– Then all these statements are true: p = q2; p2 = 2q2;
p2 is even; p is even; p = 2r; p2 = 4r2; q2 = 2r2; q is even.
– Therefore, p and q have the common factor 2. This is a
contradiction, which shows that the assumption is
false
Introduction to Computation
15
Logic and Proofs (cont’d.)
• A proof by cases
– If we can enumerate all of the possible cases, and
prove that the statement is true in each case, then we
have proven the statement
– For example, if we want to prove that one proposition
P implies another proposition Q, then looking at the
truth tables for P and Q gives us one way of
enumerating all the possible cases
Introduction to Computation
16
Sets
• A finite set can be described by listing its elements
A = {1, 2, 4, 8}
• Sometimes we use ellipses
B = {0, 3, 6, 9, … }
C = {13, 14, 15, … , 71}
E = {0, 3, 5, 6, 8, 9, … } (what set is this? Look 2 slides
ahead)
• More generally, we can use a defining property
B = {x | x is a nonnegative integer multiple of 3}
C = {x | x is an integer and 13 ≤ x ≤ 71}
B = {3y | y is a nonnegative integer}
Introduction to Computation
17
Sets (cont’d.)
• x  A means that x is an element of A
– Similarly, x  A means that x is not an element of A
• A  B means that A is a subset of B
– i.e., every element of A is also an element of B
• The empty set is denoted by 
• The order of elements when we write a set is not
significant: for example, {0, 1} = {1, 0}
• Repetition has no effect: {0, 0, 1, 1, 1, 2} = {0, 1, 2}
• To show that A = B, we need to show that A  B and
that B  A
Introduction to Computation
18
Sets (cont’d.)
• A few sets will come up frequently:
– ℕ is the set of natural numbers, or nonnegative
integers
– ℤ is the set of all integers
– ℝ is the set of all real numbers
– ℝ+ is the set of nonnegative real numbers
• Now we can write B and E from above more
concisely:
B = {3y | y  ℕ}
E = {3i + 5j | i, j  ℕ}
Introduction to Computation
19
Sets (cont’d.)
• The union, intersection and difference of two sets are
defined as follows:
A ∪ B = {x | x  A  x  B}
A ∩ B = {x | x  A  x  B}
A - B = {x | x  A  x  B}
• Examples:
{1, 2, 3, 5} ∪ {2, 4, 6} = {1, 2, 3, 4, 5, 6}
{1, 2, 3, 5} ∩ {2, 4, 6} = {2}
{1, 2, 3, 5} - {2, 4, 6} = {1, 3, 5}
Introduction to Computation
20
Sets (cont’d.)
• The complement of a set
–
–
–
–
We assume that A is a subset of some universal set U
Then the complement of A, written A’, is U - A
We think of A’ as the set of “everything that is not in A”
What this means, however, can be very different
depending on the choice of U ; for example, what
{0, 1}’ means depends on whether {0,1} is thought of as
a subset of ℕ, ℤ, or ℝ.
Introduction to Computation
21
Sets (cont’d.)
• Many useful set identities are analogous to the logical
identities
– Union and intersection are commutative and
associative, and distribute across each other
• Two sets are disjoint if their intersection is empty
• A collection of sets is pairwise disjoint if every two
distinct sets in the collection are disjoint
• A partition of a set S is a collection of pairwise
disjoint sets whose union is S
Introduction to Computation
22
Sets (cont’d.)
• We can describe the union of a set of sets
– ∪ {Ai | 0 ≤ i ≤ n} = {x | x  Ai for some i with 0 ≤ i ≤ n}
• Similarly for intersection:
– ∩ {Ai | 0 ≤ i ≤ n} = {x | x  Ai for every i with 0 ≤ i ≤ n}
• The set of all subsets of a set A is called the power set
of A and is written 2A
– 2A = { X | X  A }
– Example:
2{a,b,c} = {, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c}}
– Note that the empty set and the set A itself are in A’s
power set
Introduction to Computation
23
Sets (cont’d.)
• The Cartesian product of two sets A and B, denoted
A  B, is the set of all ordered pairs with first element
from A and second element from B
– A  B = {(a, b) | a  A and b  B}
– We can generalize this to ordered k-tuples:
A1  A2  …  Ak = {(a1, a2, … , ak | ai  Ai for each i}
Introduction to Computation
24
Functions and Equivalence Relations
• f : A →B means that f is a function from A to B
– To each element of A, one element of B is assigned
• Examples:
f : ℕ →R defined by the formula f(x) = x
g : 2 ℕ → 2 ℕ defined by g(A) = A ∪ {0}
• A is the domain of the function and B the codomain
• f and g are equal if and only if they have the same
domain and codomain and f (x) = g(x) for every x in
the domain
• Partial functions from A to B may assign values to
only some elements of A
Introduction to Computation
25
Functions and Equivalence Relations
(cont’d.)
• The range of a function is the set of elements of the
codomain that are actually values of the function
– { f (x) | x  A} (a subset of the codomain B)
Introduction to Computation
26
Functions and Equivalence Relations
(cont’d.)
• If f : A →B is a bijection then we can define the
inverse function f -1 from B to A by these two
formulas: for every x  A and y  B,
– f -1( f(x) ) = x
– f (f -1(y)) = y
• It is easy to check that f -1 is also a bijection
Introduction to Computation
27
Functions and Equivalence Relations
(cont’d.)
• An n-ary operation on a set A is a function that
assigns to every ordered n-tuple of elements of A an
element of A
• Unary and binary operations are of most interest
– Binary operations on the integers include addition
– For every set S, binary operations on 2S include union
and intersection
– Unary operations include negation (on the set of
integers, for example) and complementation (on the
set 2A)
Introduction to Computation
28
Functions and Equivalence Relations
(cont’d.)
• For a unary or binary operation on a set A
– We say that a subset A1 of A is closed under the
operation if the result of applying the operation to
elements of A1 is an element of A1
• If A = 2ℕ and A1 is the set of nonempty subsets of A,
then A1 is closed under union but not under
intersection
• The set of even natural numbers is closed under
addition and multiplication
• The set of odd natural numbers is closed under
multiplication but not addition
Introduction to Computation
29
Functions and Equivalence Relations
(cont’d.)
• Relations
• We can express relationships several ways: If R is a
relation on a set, we can write “a is related to b” as
a R b or as (a, b)  R
Introduction to Computation
30
Functions and Equivalence Relations
(cont’d.)
• Examples:
– The equality relation (a relation on any set)
– The relation on A containing all ordered pairs
– The relation of congruence mod n on the set ℕ
Introduction to Computation
31
Functions and Equivalence Relations
(cont’d.)
• We may drop the subscript and just say [x] if there is
no opportunity for confusion
• Theorem: If R is an equivalence relation on A, the
equivalence classes with respect to R form a partition
of A, and two elements of A are equivalent if and only
if they are elements of the same equivalence class
Introduction to Computation
32
Languages
• An alphabet is a finite set of symbols usually denoted
by 
– Examples: {a,b}, {0,1}, {A, B, C, … , Z}
•
•
•
•
•
A string over  is a finite sequence of symbols
|x| stands for the length of the string x
na(x) is the number of occurrences of a in the string x
The null string  is a string over any alphabet 
|| = 0
Introduction to Computation
33
Languages (cont’d.)
•
•
•
•
The set of all strings over  is *
Example: {a,b}* = {, a, b, aa, ab, ba, bb, aaa, aab,…}
A language over  is a subset of *
Examples:
– The empty language 
– {, a, aab}, a finite language
– The palindromes over {a,b} (strings like , a, and
baabaab that read the same backwards as forwards)
– {x  {a, b}* | na(x) > nb(x)}
– {x  {a, b}* | |x| ≥ 2 and x begins and ends with b}
Introduction to Computation
34
Languages (cont’d.)
• xy is the concatenation of the two strings x and y; this
is the basic operation on strings
– If x = ab and y = bab then xy = abbab and yx = babab
– For every string x, x = x = x
– |xy| = |x| + |y|
• Concatenation is associative, i.e., (xy)z = x(yz), so we
can write xyz without worrying about how terms are
grouped
• If s = tuv then t is a prefix of s, v is a suffix, and u is a
substring. Every string is a prefix (and suffix, and
substring) of itself
Introduction to Computation
35
Languages (cont’d.)
• For languages L1, and L2 over 
– L1 ∪ L2, L1 ∩ L2, and L1 − L2 are also languages over 
• If L  *, the complement of L is a language, * - L
• For languages L1, L2 over 
– L1L2 is the language {xy | x  L1 and y  L2}
• We use exponential notation ak = aaa…a, where there
are k occurrences of a
• This also applies to strings (xk = xxx…x) and languages
(Lk = LLL…L)
• a0 = x0 = , L0 = {} (for every a  , x  *, L  *)
Introduction to Computation
36
Languages (cont’d.)
• If L is a language over , then L* denotes the language
of all strings that can be obtained by concatenating
zero or more strings in L
– This operation is known as the Kleene star
• L* = ∪ {Lk | k  ℕ}
•   L* for every language L, since L0 = {}
• Strings are finite, and languages may not be, but to
use a language we need a finite description
– L1 = {ab,bab}* ∪ {b} {ba}*{ab}*
– L2 = {x  {a,b}* | na(x) > nb(x)}
Introduction to Computation
37
Recursive Definitions
• A recursive definition of a set has a basis statement
that specifies at least one member of the set, and a
recursive part that specifies how additional members
of the set can be generated in terms of given
members.
• The prototypical example is ℕ, the set of natural
numbers. It can be defined as follows:
– Basis statement: 0  ℕ
– Recursive part: if n  ℕ then n+1  ℕ
– Every element of ℕ can be obtained from the first two
statements
Introduction to Computation
38
Recursive Definitions (cont’d.)
• Summary:
– The third statement in the definition of ℕ is what says
that ℕ is the smallest set that contains 0 and is closed
under the successor operation (addition by 1)
– The statement that the set being defined is the smallest
is frequently omitted but always understood
• Example: the subset B = { 2i 5j | i, j  ℕ }
– 1B
– For every n  B, 2n  B
– For every n  B, 5n  B
Introduction to Computation
39
Recursive Definitions (cont’d.)
• We denote by F the subset of 2{a,b}* defined by:
– , {}, {a}, {b}  F
– if L1, L2  F then L1 ∪ L2  F
– if L1, L2  F then L1L2  F
• F is the smallest set of languages that contains the
languages , {}, {a}, and {b} and is closed under
union and concatenation
• It is easy to see that F is the set of all finite languages
over {a,b}
Introduction to Computation
40
Recursive Definitions (cont’d.)
• The set of cities reachable from city s
– Suppose that C is a finite set of cities, and the relation R
is defined on C so that c R d means there is a nonstop
commercial flight from c to d
– For a city s  C, we would like to describe r(s), the set
of cities reachable from s by taking zero or more
nonstop flights. We can define r(s) this way:
• s  r(s)
• if c  r(s) and c R d then d  r(s)
Introduction to Computation
41
Structural Induction
• Consider again the language Expr:
– a  Expr.
– For every x and every y in Expr, x ◦ y and x • y are in Expr
(where x ◦ y and x • y are the strings x+y, x*y)
– For every x  Expr, ◊(x)  Expr (where ◊(x) is (x) )
• Note: this seems like unnecessarily confusing notation
(why do we need the operator symbols ◦, •, and ◊,
when we already have the “operators” + and *?)
• The reason is that + and * are operations on numbers,
but we’re talking about strings, not numbers. In this
discussion +, *, (, and ) are just symbols in the alphabet
Introduction to Computation
42
Structural Induction (cont’d.)
• To prove that every string x  Expr satisfies a condition
P(x), use structural induction: show that
– P(a) is true
– For every x and every y in Expr, if P(x) and P(y) are
true, then P(x ◦ y) and P(x • y) are true
– For every x  Expr, if P(x) is true, then P(◊(x)) is true
• In other words, show that the set of elements x satisfying
the property P contains a and is closed under ◦, •, and ◊.
• This set must then contain every element in the smallest
set that contains a and is closed under the operations; i.e.,
every element in Expr
Introduction to Computation
43
Structural Induction (cont’d.)
• The recursive definition of Expr consists of
– A basis part ( a  Expr )
– Three recursive parts, the first of the form
If x, y  Expr then x ◦ y  Expr
• The basis step of the proof is to show that P(a) is
true
• The induction hypothesis is that x, y  Expr and that
P(x) and P(y) are true
• The first case of the induction step is to show that
P(x ◦ y) is true. (Similarly for the other 2 operations)
Introduction to Computation
44
Structural Induction (cont’d.)
• For example, suppose P(x) is “x has odd length”
• The basis step of the proof is to show that a has odd
length, and this is clearly true: |a| = 1
• The induction hypothesis is that x, y  Expr and that x
and y have odd length (i.e., |x| and |y| are odd)
• The three cases in the induction step are to show that
x ◦ y, x • y, and ◊(x ) have odd length
• These are all true, because
| x ◦ y| = | x+y | = |x| + |y| + 1, and odd + odd + 1 = odd
| x • y| = | x*y | = |x| + |y| + 1
| ◊(x)| = |(x)| = |x| + 2, and odd + 2 = odd
Introduction to Computation
45
Structural Induction (cont’d.)
• Mathematical induction is simply structural
induction based on the recursive definition of ℕ
given earlier
• This is used to prove statements of the form
“for every integer n ≥ n0, P(n)”
– Basis step: prove the statement P(n) for n = n0
– Induction hypothesis: k is an integer ≥ n0 and P(k) is
true
– Induction step: show using the induction hypothesis
that P(k+1) is true
Introduction to Computation
46
Structural Induction (cont’d.)
• Prove: For every n  ℕ, every set A with n
elements, 2A has exactly 2n elements
– Basis: for every set A with 0 elements, 2A has 20
elements; this is true because only  has zero
elements, and 2 = {}, which has one element
– Induction hypothesis: k  ℕ, and for every set A
with k elements, 2A has 2k elements
– Induction step: to show that for every set A with
k+1 elements, 2A has 2k+1 elements
Introduction to Computation
47
Structural Induction (cont’d.)
• Prove: For every n  ℕ, and every set A with n
elements, 2A has exactly 2n elements (cont’d.)
– Proof of induction step
• Let A be a set with k + 1 elements, and let a be any
element of A (there is one, since k + 1 ≥ 1)
• Then A - {a} has k elements, and 2A-{a} has 2k
elements by the induction hypothesis; therefore, A
has 2k subsets that do not contain a and 2k subsets
that do contain a, for a total of 2k+1 subsets
Introduction to Computation
48
Structural Induction (cont’d.)
• Strong induction is another form of induction
• Example: To prove that for every n ≥ 2, n is either
prime or a product of two or more primes
– Let us strengthen the statement: for n ≥ 2, every
number m such that 2 ≤ m ≤ n is either prime or a
product of two or more primes.
– The reason for this, as we’ll see, is that it gives us a
stronger induction hypothesis but doesn’t actually
require that we prove any more than we would have
anyway.
Introduction to Computation
49
Structural Induction (cont’d.)
• Proof:
– Basis step: To show that every m satisfying
2 ≤ m ≤ 2 is either prime or a product of primes.
This reduces to showing that 2 is, and 2 is prime
– Induction hypothesis: k ≥ 2, and for every m
satisfying 2 ≤ m ≤ k, m is either a prime or a
product of primes
– Statement to prove in induction step: For every m
satisfying 2 ≤ m ≤ k + 1, m is either prime or a
product of primes.
Introduction to Computation
50
Structural Induction (cont’d.)
• Proof of induction step: For every m with 2 ≤ m ≤ k,
we already have the conclusion we want, from the
induction hypothesis. The only additional statement
we need to prove is that k + 1 is either prime or a
product of primes
• If k + 1 is prime, we’re done; if not, k + 1 is the
product of two smaller numbers, both bigger than 1.
By the (stronger) induction hypothesis, both are
prime or the product of primes. Therefore (in either
case), k + 1 is the product of primes
Introduction to Computation
51
Structural Induction (cont’d.)
• The language Balanced was defined as follows:
–   Balanced
– If x, y  Balanced, then xy  Balanced and
(x)  Balanced
• Let’s prove that x  Balanced if and only if B(x) is
true, where B(x) is “x contains equal numbers of left
and right parentheses, and no prefix of x contains
more right than left”
• The “only if” part is straightforward; use structural
induction, based on the definition of Balanced
Introduction to Computation
52
Structural Induction (cont’d.)
• The basis step is to show that B() is true. This is
clear, because  has no symbols and no nonnull
prefixes
• The induction hypothesis is that x, y  Balanced and
both B(x) and B(y) are true
• We must show that B(xy) and B( (x) ) are true. Both
xy and (x) have equal numbers of left and right
parentheses, because x and y do. A prefix of xy is
either a prefix of x, or xz for some prefix z of y; a
prefix of (x) other than (x) is  or (z, for some prefix z
of x. In all these cases, the statement is true by the
induction hypothesis
Introduction to Computation
53
Structural Induction (cont’d.)
• For the reverse (the “if” part), use induction on the
length of the string
• Prove: For every n  ℕ, if x is a string of parentheses
so that |x| = n and B(x) is true then x  Balanced
– Basis: if |x| = 0 then x = , so x  Balanced
– Induction hypothesis: k  ℕ, and for every string x of
parentheses, if |x| ≤ k and B(x), then x  Balanced
(since we say |x| ≤ k, we’re using strong induction)
– To prove: if |x| = k + 1 and B(x), then x  Balanced
– Proof: the details get a little involved; see book
Introduction to Computation
54
Structural Induction (cont’d.)
• Sometimes making a statement stronger makes it
easier to prove. Let’s prove that no string in Expr
contains ++ as a substring
• The basis step is easy to prove, and so are the
induction steps for x*y and (x)
• In the case of x+y, what if x ends with + or y begins
with +?
• Strengthen the statement: prove that no string
contains ++ as a substring, or begins or ends with +
– Now the stronger induction hypothesis makes it
possible to complete the proof
Introduction to Computation
55
Structural Induction (cont’d.)
• Recursive definitions of sets lead naturally to ways of
defining functions on those sets
• Example: if we define a function f at 0, and then
define f (n+1) assuming that f (n) is defined, then f is
effectively defined over ℕ
• The factorial function is defined this way:
f(0) = 1; f(n+1) = (n+1) * f(n)
• Definitions like these are particularly well suited to
induction proofs of properties of the corresponding
functions
Introduction to Computation
56