You are on page 1of 10

F2016 Section 5-3

1. Given three sets A, B and C. Suppose the union of the three sets has 4
cardinality 280. Suppose also that |A|=100, |B|=200, |C|=150. And suppose we 4! / (2! 2!) = 6
also know | A  B | 50, | A  C | 80, and | B  C | 90. aabb, abab, abba, bbaa, baba, baab
Find the cardinality of the intersection of the three given sets. 8. Use Quine’s method to give a formal proof for the following tautology.
(A B) ( (B C) (C A))
Section 1-2 Section 6-2
18. 50. 8.h
2. Find the number of strings over the alphabet a, b, c, d , e that
satisfy the given condition.
Length 6, contains at least one a and at least one b .
Section 1-3
9. Give a formal proof for the following tautology.
Section 6-2
14.d. 9.f

A  a, b
A B C ( A B) (A C)
3. Find an inductive definition for the set S of all lists over

that alternate a’s and b’s. For example, the list , a , b , a, b, a and
F2017
b, a are in S. But a, a is not in S. 1

Section 3-1 – REF (12)

4. Find a grammar for the following language.


a m
bc n | m, n  N 
Section 3-3

4. Find an optimal pan-balance algorithm to find a bad coin, if it exists,


from 12 coins, where at most one coin is bad (i.e., heavier or lighter than the 3
others). Hint: Once you’ve decided on the coins to weight for the root of the
tree, then the coins that you choose at the second level should be the same coins
for all three branches of the tree.

Find a grammar for the following language.


a m
bc n | m, n  N 

6. Given the following algorithm, find a formula in terms of n for each


case:
for i :=1 to n do
for j :=1 to i do x :=x+f(x) od;
x :=x+g(x)
od
a. Find the number of times that the assignment statement (:=) is executed
during the running of the program.
b. Find the number of times that the addition operation (+) is executed
during the running of the program.

Section 5-2

13.a.

7. Given the bag B = [a, a, b, b], write down all the bag permutations of B, and
verify with a formula that you wrote down the correct number.
2. Find the number of strings over the alphabet a, b, c, d , e that
satisfy the given condition.
Length 6, contains at least one a and at least one b .

Section 1.1 A Proof Primer Example 5. If x is even then x2 is even.


A proof is a demonstration that some statement is true. We normally demonstrate proofs Proof: Class do as one minute quiz.
by writing English sentences mixed with symbols.
Example 6. If x2 is odd then x is odd.
We’ll consider statements that are either true or false. If A and B are statements, then
Proof: The contrapositive of this statement is “if x is even, then x2 is even,” which is
“not A,” “A and B,” and “A or B,” are called negation, conjunction, and disjunction,
true by Example 5. QED.
respectively. “not A” is opposite in truth value from A. “A and B” is true exactly when
both A and B are true “A or B” is true except when both A and B are false. Example 7. If x2 is even then x is even.
Proof: This is the contrapositive of Example 4, which has been shown to be true. QED.
Conditionals: “if A then B” (or “A implies B”) is a
A B if A then B if not B then not A
conditional statement with antecedent A and consequent
T T T T If And Only If (Iff) Proofs
B. It’s contrapositive is “if not B then not A” and it’s
T F F F A statement of the form “A if and only if B” means “A implies B” and “B implies A.” So
converse is “if B then A”. Statements with the same truth
F T T T there are actually two proofs to give. Sometimes the proofs can be written as a single
table are said to be equivalent. The table shows that a
F F T T proof of the form “A iff C iff D iff … iff B,” where each iff statement is clear from
conditional and it’s contrapositive are equivalent.
previous information.
A conditional is vacuously true if its antecedent is false.
Example 8. x is even if and only if x2 – 2x + 1 is odd.
A conditional is trivially true if its consequent is true.
Proof: x is even iff x = 2k for some integer k (definition)
Proof Techniques: We’ll give sample proofs about numbers. Here are some definitions. iff x – 1 = 2k – 1 for some integer k (algebra)
• integers: …, -2, -1, 0, 1, 2, … iff x – 1 = 2(k – 1) + 1 for some integer k – 1 (algebra)
• odd integers: …, -3, -1, 1, 3, … (have the form 2k + 1 for some integer k). iff x – 1 is odd (definition)
• even integers:…, -4, -2, 0, 2, 4, … (have the form 2k for some integer k). iff (x – 1)2 is odd (Examples 4 and 6)
• m | n (read m divides n) if m ≠ 0 and n = km for some integer k. iff x2 – 2x + 1 is odd (algebra). QED.
• p is prime if p > 1 and its only divisors are 1 and p. 1 3

Exhaustive Checking
Some statements can be proven by exhaustively checking a finite number of cases. Proof By Contradiction
A false statement is called a contradiction. For example, “S and not S” is a
Example 1. There is a prime number between 200 and 220. contradiction for any statement S. A truth table will show us that “if A then B,” is
Proof: Check exhaustively and find that 211 is prime. QED. equivalent to “A and not B implies false.” So to prove “if A then B,” it suffices to
Example 2. Each of the numbers 288, 198, and 387 is divisible by 9. assume A and also to assume not B, and then argue toward a false statement. This
Proof: Check that 9 divides each of the numbers. QED. technique is called proof by contradiction or reductio ad absurdum.

Example 9. If x2 is odd then x is odd.


Conditional Proof Proof: Assume, BWOC, that x2 is odd and x is even. Then x = 2k for some integer k.
Most statements we prove are conditionals. We start by assuming the antecedent is true. So we have
Then we try to find a statement that follows from the assumption and/or known facts. We
x2= (2k)2 = 4k2 = 2(2k2),
continue in this manner until we reach the consequent.
which is even since 2k2 is an integer. So we have
Example 3. If x is odd and y is even then x – y is odd. x is odd and x2 is even, a contradiction. So the statement is true. QED.
2

Proof: Assume x is odd and y is even. Then x = 2k + 1 and y = 2m for some


integers k and m. So we have Example 10. If 2 | 5n then n is even.
x – y = 2k + 1 – 2m = 2(k – m) + 1, Proof: Assume, BWOC, that 2 | 5n and n is odd. Since 2 | 5n, we have 5n = 2d for some
integer d. Since n is odd, we have n = 2k + 1 for some integer k. Then we have
which is an odd integer since k – m is an integer. QED.
2d = 5n = 5(2k + 1) = 10k + 5.
Example 4. If x is odd then x2 is odd. So 2d = 10k + 5. Solve for 5 to get
Proof: Assume x is odd. Then x = 2k + 1 for some integer k. So we have 5 = 2d – 10k = 2(d – 5k).
x2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1, But this says that 5 is an even number, a contradiction. So the statement is true. QED.
which is an odd integer since 2k2 + 2k is an integer. QED. 2 4
Section 1.2 Sets Section 1.3 Ordered Structures
A set is a collection of things. Tuples: Have order and can have repetitions. For example, (6, 7, 6) is a 3-tuple and ( ) is
• If S is a set and x is a member or element of S we write x Î S. Othewise we write x Ï S. the empty tuple. We write (x1, …, xn) = (y1, …, yn) to mean xi = yi for 1 ≤ i ≤ n.
• The set with elements x1, …, xn is denoted {x1, …, xn}. Cartesian Product: A ´ B = {(x, y) | x Î A and y Î B}. The definition extends to more than
• The empty set with no elements is denoted { } or Æ. two sets. For example, A ´ B ´ C = {(x, y, z) | x Î A, y Î B, z Î C}.
• A set with one element is called a singleton. e.g., {a} is a singleton.
• Notation: A0 = {( )}, A1 = {(x) | x Î A}, and in general, An = {(x1, …, xn) | xi Î A}.
• The set of integers is denoted by Z, the natural numbers {0, 1, …} by N, the rational
numbers by Q, and the real numbers by R. Quiz (1 minute). Does (A ´ B) ´ C = A ´ (B ´ C)?
Equal Sets Lists: Are like tuples but there is no random access. For example, áa, b, a, cñ is a list with 4
Two sets A and B are equal, denoted A = B if they have the same elements. elements and á ñ is the empty list.
e.g., {a, b, c} = {c, b, a} (no ordering). • The operations on lists are head, tail, and cons. For example,
e.g., {a, a, b, c} = {a, b, c} (no repetitions) head(áa, b, a, cñ) = a, tail(áa, b, a, cñ) = áb, a, cñ, cons(a, áb, a, cñ) = áa, b, a, cñ.
• Sets can be described by properties that the elements satisfy. If P is a property, then the • The set of lists whose elements are in A is denoted by lists(A).
expression {x | P} denotes the set of all x that satisfy P. Quiz (1 minute). For L = á áañ, b, ác, dñ ñ, find head(L) and tail(L).
e.g., The set of odd natural numbers can be represented by the following equal sets. Strings: Are like lists, but are represented as juxtaposed elements from a given alphabet.
For example, if A = {a, b}, then some strings over A are: a, b, aa, ab, ba, bb, aaa, bbb.
{x | x = 2k + 1 for some k Î N} = {1, 3, 5, …}.
• The empty string is denoted by L.
Subsets
The set A is a subset of B, denoted A Í B, means every element of A is an element of B. • The concatenation of two strings is their juxtaposition. For example, the concatenation of
ab and bab is abbab.
e.g., N Í Z Í Q Í R.
e.g., S Í S for any set S. • For any string s we have sL = Ls = s.
e.g., Æ Í S for any set S. 5
• If s is a string, sn denotes the concatenation of s with itself n times. Also s0 = L. For 9
example, (ab)3 = ababab.

Languages
A language is a set of strings, usually taken over some alphabet.
Notation: If A is an alphabet, then the set of all strings over A is denoted by A*.
Example. Some languages over A are: Æ, {L}, A, and A*.
The power set of a set S, denoted power(S) is the set of all subsets of S.
Example. {abna | n Î N} = {aa, aba, abba, abbba, … } is a language over {a, b}.
e.g., power({a, b}) = {Æ, {a}, {b}, {a, b}}.
Language Operations
Example(Comparing sets). Let A = {2k + 7 | k Î Z} and B = {4k + 3 | k Î Z}. Let L and M be languages. The product of L and M, denoted LM, is the language
Quiz. Is A Í B? LM = {st | s Î L and t Î M}.
Answer: No. For example, 9 Î A but 9 Ï B. • Notation: L0 = {L}; and Ln = {s1…sn | si Î L}.
Quiz. Is B Í A? Quiz (1 minute). What are the products LÆ and L{L}?
Answer: Yes. Let x Î B. Then x = 4k + 3 for some k Î Z. But we can write Quiz (1 minute). Solve for L in the equation {L, a, b}L = {L, a, b, aa, ba, aba, bba}.
x = 4k + 3 = 4k – 4 + 7 = 2(2k – 2) + 7. • The closure L* is the set of all possible concatenations of strings in L. So
Since 2k – 2 Î Z, it follows that x Î A. Therefore B Í A. QED. L* = L0 È L1 È … È Ln È …
Equality in terms of subsets: A = B iff A Í B and B Í A. Quiz (1 minute). What are {L}* and Æ*?
Example. Let A = {2k + 5 | k Î Z} and B = {2k + 3 | k Î Z}.
Example. Examine the structure of an arbitrary string x Î L*(ML)*.
Show that A = B.
A solution: Use the definitions to write x in terms of strings in L and M.
Proof: First show A Í B. Let x Î A. Then x = 2k + 5 for some k Î Z. So we have
1. Since x Î L*(ML)*, it follows that x = uv where u Î L* and v Î (ML)*.
x = 2k + 5 = 2k + 2 + 3 = 2(k + 1) + 3.
2. Since u Î L*, either u = L or u = s1…sn for some n where si Î L.
Since k + 1 Î Z, it follows that x Î B. Therefore A Í B. 3. Since v Î (ML)*, either v = L or v = r1t1 …rktk for some k where ri Î M and ti Î L.
Now show the other direction B Í A.
So x has one of four forms: L, s1…sn, r1t1 …rktk, or s1…snr1t1 …rktk. 10
…….Class fill in the proof (2 minute quiz).
Since A Í B and B Í A it follows that A = B. QED.
6
Relations.
A relation is a set of tuples. If R is a relation and (x1, …, xn) Î R, we write R(x1, …, xn).
We can usually represent a relation as a subset of some cartesian product.
Example. Let R = {(0, 0), (1, 1), (4, 2), (9, 3), …, (n2, n), …} = {(n2, n) | n Î N}. We might
Operations on Sets
call R the “is square of” relation on N. Notice also that R Í N ´ N.
Union: A È B = {x | x Î A or x Î B}.
Notation: If R is binary, we can use infix to represent pairs in R. For example, from the
Intersection: A Ç B = {x | x Î A and x Î B}. previous example, we have (9, 3) Î R. So we can also write
Difference: A – B = {x | x Î A and x Ï B}. R(9, 3) or 9 R 3 or 9 is square of 3.
Symmetric Difference: A Å B = {x | x Î A or x Î B but not both} Relational Databases
Note: A Å B = (A – B) È (B – A) = (A È B) – (A Ç B ) . A relational database is a relation where the indexes of a tuple have associated names
Universal Complement: Given a universe U and A Í U, we write called attributes.
A' = U – A. Example. Let Students = {(x, y, z) | x is a Name, y is a Major, and z is Credits}.
Example. For each n Î N let Dn = {x Î N | x divides n}. So Dn is the set of positive Who are the people majoring in CS?
divisors of n. Here are some expressions involving these sets. {x | (x, CS, z) Î Students, for some z}.
• D0 = {1, 2, 3, … } = N – {0}, D5 = {1, 5}, D6 = {1, 2, 3, 6}, and D9 = {1, 3, 9}. Note: We need “for some z” to indicate that z is a variable.
• D5 È D6 = {1, 2, 3, 5, 6} How many math majors are upper division students?
• D5 Ç D6 = {1} | {x | (x, math, z) Î Students and z ≥ 90} |.
• D9 – D6 = {9} What is the major of AbeLincoln?
{y | (AbeLincoln, y, z) Î Students, for some z}.
• D5 Å D6 = {2, 3, 5, 6}
What is the history department database of names and their credits?
• Let N be the universe. Then D0' = N – D0 = {0}, and {0}' = D0.
{(x, z) | (x, history, z) Î Students}. 11

Quiz (2 minutes). Draw a Venn diagram for three sets A, B, C with some areas shaded.
Then find an expression to represent the shaded area. Counting Tuples (or strings or lists)
7
Product Rule: | A ´ B | = | A | | B | and | An | = | A |n.
Example. If A = {a, b} and B = {1, 2, 3}, then
Properties of Set Operations A ´ B = {(a, 1), (a, 2), (a, 3), (b, 1), (b, 2), (b, 3)}.
Union and intersection are commutative, associative, and distribute over each other. There
are many other properties too. For example, So | A ´ B | = 6 = (2)(3) = | A | | B |.
• Absorption: A È (A Ç B) = A and A Ç (A È B) = A. Example. Count the number of strings of length 8 over A = {a, b, c} that begin with either
• De Morgan’s Laws: (A È B)' = A' Ç B' and (A Ç B)' = A' È B'. a or c and have at least one b.
Counting Sets A Solution: Split the problem up into easier problems and combine
U–B U
The cardinality of a set S is denoted by | S |. Two useful rules for counting finite sets are: the results (divide and conquer). Let U be the universe consisting
• Inclusion-Exclusion or Union Rule: | A È B | = | A | + | B | – | A Ç B |. of the strings over A of length 8 that begin with either a or c. Let B
be the subset of U consisting of strings with no b’s. Then the set of B
• Difference Rule: | A – B | = | A | – | A Ç B |.
Quiz (2 minutes). Find a rule for the union of three sets: | A È B È C | = ? strings to count is U – B, as pictured.
Quiz (3 minutes). Three programs use a collection of processors in the following way, It is easy to calculate the cardinality of U – B:
where A, B, and C represent the sets of processors used by the three programs:
| U – B | = | U | – | U Ç B | = | U | – | B | (since B is a subset of U)
| A | = 20, | B | = 40, | C | = 60, | A Ç B | = 10, | A Ç C | = 8, | B Ç C | = 6.
It is also easy to count U because it has the same cardinality as the set {a, c} ´ A7, which is
If there are 100 processors available, what could | A Ç B Ç C | be?
| {a, c} ´ A7 | = | {a, c} | | A7 | = | {a, c} | | A |7 = (2)37.
Answer: 100 ≥ | A È B È C | = 20 + 40 + 60 – 10 – 8 – 6 + | A Ç B Ç C | . So
| A Ç B Ç C | ≤ 4. It is also easy to count B because it has the same cardinality as the set {a, c}8, which is
Bags (Multisets) are like sets but can contain repeated elements. e.g., [t, o, o, t] = [o, t, t, o]. | {a, c}8 | = | {a, c} |8 = 28.
Union and intersection can be defined by taking the maximum and minimum occurrences of So we have the answer:
12
each element, respectively. | U – B | = | U | – | U Ç B | = | U | – | B | = (2)37 – 28, which is 4118.
Quiz (2 minutes). Let A = [m, i, s, s, i, s, s, i, p, p, i] and B = [s, i, p, p, i, n, g].
What are A È B and A Ç B?
Section 1.4 Graphs and Trees
Answer: A È B = [m, i, s, s, i, s, s, i, p, p, i, n, g] and A Ç B = [s, i, p, p, i]. 8
A graph is set of objects called vertices or nodes where some pairs of objects may be
connected by edges. (A directed graph has edges that point in one direction.)
Example. Draw a graph of the South American countries that touch the Pacific Ocean and
their neighbors, where the vertices are countries and an edge indicates a common border.
Vertices = {Co, V, E, Br, P, Bo, Ch, A}
Co V Edges = {{Co, V}, {Co, E}, ….}.
A path from vertex x0 to xn is a sequence of edges
E Br that we denote by vertices x0, x1, …, xn, where
P there is an edge from xi–1 to xi for 1≤ i ≤ n.
The length of a path is the number of edges.
Bo
Ch A cycle is a path with distinct edges that begins
A and ends at the same vertex.
Example. A, Bo, A, is not a cycle since the edge
{A, Bo} occurs twice. A, Bo, Br, A, is a cycle.

Quiz (1 minute). What is a longest path from A to V with distinct edges and no cycles?
Answer: The length is 6. For example, A, Bo, Br, P, E, Co, V.
A graph is n-colorable if it’s vertices can be colored with n colors with distinct colors for
adjacent vertices. The chromatic number of a graph is the smallest such n.
13
Quiz (1 minute). What is the chromatic number of the example graph?
Graph Traversals
A graph traversal starts at some vertex v and visits all vertices x that can be reached by a Notation for Binary Trees
path from v to x. But don’t visit any vertex more than once. Let t(L, x, R) denote the tree with root x, left subtree L, and right subtree R. Let á ñ denote the
empty binary tree. If T = t(L, x, R), then root(T) = x, left(T) = L, and right(T) = R.
Breadth-First: If the graph has n vertices then start with a vertex v and do the following:
Example 10. Describe the set S defined inductively as follows:
for k := 0 to n – 1 do visit(v, k) od
Basis: t(á ñ, •, á ñ) Î S.
where visit(v, k) visits all x not visited if there is a path from v to x of length k.
Induction: T Î S implies t(T, •, t(á ñ, •, á ñ)) Î S.
Use the pictured graph for the following quizzes.
A B Solution (picture): The first few trees constructed from the definition are pictured as follows:
Quiz (1 minute). Find a breadth-first traversal that starts at F.
C D
One answer: F, H, D, G, B, A, E, C.
and so on.
Quiz (1 minute). Find a breadth-first traversal that starts at C. E
F
One answer: C, A, E, D, B, F, H, G. G
H
Depth-First: Start at a vertex v and call the procedure D(v), which is defined as follows: Example 11. Find an inductive definition for the set S of binary trees indicated by the
D(v): if v has not been visited then following picture.
visit(v);
for each edge from v to x do D(x) od
fi and so on.
Quiz (1 minute). Find a depth-first traversal of the pictured graph that starts at F.
One answer: F, H, G, D, B, A, C, E.
Solution: Basis: t(á ñ, •, á ñ) Î S.
Quiz (1 minute). Find a depth-first traversal of the pictured graph that starts at E.
Induction: T Î S implies t(t(left(T), •, á ñ), •, t(á ñ, •, right(T))) Î S.
One answer: E, D, F, H, G, A, C, B. 14 3

Trees Example 12. Find an inductive definition for the set S = {a}* ´ N.
A
A tree is a connected graph (a path between any two points) with no Solution: Basis: (L, 0) Î S.
cycles. Most trees are oriented so that they look like upside-down Induction: (s, n) Î S implies (as, n), (s, n + 1) Î S.
B C D
trees, such as the tree pictured.
Example 13. Find an inductive definition for the set S = {(x, –y) | x, y Î N and x ≥ y}.
The top node is the root, the nodes directly below a node are its
E F G Solution: To get an idea about S we can write out a few tuples:
children, the node directly above a node is the parent, the bottom
nodes are leaves, and the height or depth of the tree is the length of (0, 0), (1, 0), (1, –1), (2, 0), (2, –1), (2, –2), and so on.
H I
the longest path of distinct edges from root to a leaf. We can also get an idea about S by graphing a few points,
as indicated in the picture.
Example. For this tree the root is A. The children of A are B, C, D. D is the
parent of G. The height or depth of the tree is 3. The leaves are E, F, C, H, I. One solution can be written as follows:
Any node of a tree is the root of a subtree. One way to represent a tree is as a Basis: (0, 0) Î S.
list whose head is the root of the tree and whose tail is the list of subtrees, where Induction: (x, y) Î S implies (x + 1, y), (x + 1, y – 1) Î S.
each subtree is represented in the same way. Notice that this definition constructs some repeated points.
Example. The pictured tree can be represented by the list For example, (2, –1) is constructed twice.
á A, á B, á E ñ, á F ñ ñ, á C ñ, á D, á G, á H ñ, á I ñ ñ ñ ñ.
+ Quiz (2 minutes). Try to find a solution that does not construct repeated elements.
Any algebraic expression can be represented as a tree. For
Solution: We might use two separate rules. One rule to construct the diagonal points and one
example, the tree for the expression (x – y) + log(z + w) is _
pictured to the right. log rule to construct horizontal lines that start at the diagonal points.
Basis: (0, 0) Î S.
Quiz (1 minute). Do a depth-first (left to right) traversal. x y + Induction: 1. (x, y) Î S implies (x + 1, y) Î S.
Answer: + – x y log + z w. This is the prefix form of the 2. (x, –x) Î S implies (x + 1, – (x + 1)) Î S.
expression. z w 15 4

Binary Trees
A binary tree is either empty, denoted by á ñ, or each node has two subtrees that are binary Recursively Defined Functions and Procedures
trees and are called the left and right subtrees of the node. If a binary tree is not empty, A function ƒ is recursively defined if at least one value ƒ(x) is defined in terms of another
we’ll represent it as a list of the form áL, x, Rñ, where x is the root and L and R are the left value ƒ(y), where x ≠ y. Similarly, a procedure P is recursively defined if the action of P(x)
and right subtrees, respectively. is defined in terms of another action P(y), where x ≠ y.
Example. The binary tree with a single node x is denoted by áá ñ, x, á ññ. 7 Technique for recursive definitions when the argument domain is inductively defined.
A binary search tree represents ordered information, where the predecessors 3 11 1. Specify a value ƒ(x), or action P(x), for each basis element x of S.
and successors of a node are in its left and right subtrees, respectively. 2. Specify rules that, for each inductively defined element x in S, define the value ƒ(x), or
Example. A binary search tree for the first six prime numbers is pictured. 2 5 13 action P(x), in terms of previously defined values of ƒ, or actions of P.
Spanning Trees Example 1. Find a recursive definition for the function ƒ : N ® N defined by
A spanning tree for a connected graph is a tree whose nodes are the nodes of the graph and ƒ(n) = 0 + 3 + 6 + … + 3n.
whose edges are a subset of the edges of the graph. A minimal spanning tree minimizes the Solution: Notice that N is an inductively defined set: 0 Î N; n Î N implies n + 1 Î N. So
sum of weights on the edges of all spanning trees. 3
we need to give ƒ(0) a value in N and we need to define ƒ(n + 1) in terms of ƒ(n). The given
A B definition of ƒ tells us to set ƒ(0) = 0. To discover a definition for ƒ(n + 1) we can write
Example. Use Prim’s algorithm to construct a minimal spanning tree 1
3 ƒ(n + 1) = (0 + 3 + 6 + … + 3n) + 3(n + 1)
for the pictured graph, starting with node D. 1
Solution: A minimal spanning tree is constructed in 4 steps:
2 D = ƒ(n) + 3(n + 1).
C 2 So we have a recursive definition for ƒ:
B B A B A B 3
E ƒ(0) = 0
1 1 1 1
1 1 ƒ(n + 1) = ƒ(n) + 3(n + 1).
D 2 D 2 D 2 D Two alternative definitions:
C C C 2 • ƒ(0) = 0
16 ƒ(n) = ƒ(n – 1) + 3n (n > 0). 5
E
• (if-then-else form): ƒ(n) = if n = 0 then 0 else ƒ(n – 1) + 3n.

Inductively Defined Sets Example 2. Find a recursive definition for cat : A* ´ A* ® A* defined by cat(s, t) = st.
To define a set S inductively is to do three things: Solution: Notice that A* is inductively defined: L Î A*; a Î A and x Î A* imply ax Î A*,
where ax denotes the string version of cons. We can define cat recursively using the first
Basis: Specify one or more elements of S.
argument. The definition of cat gives cat(L, t) = Lt = t. For the recursive part we can write
Induction: Specify one or more rules to construct elements of S from existing elements of S.
cat(ax, t) = axt = a(xt) = acat(x, t). So we have a definition:
Closure: Specify that no other elements are in S (always assumed).
cat(L, t) = t
Note: The basis elements and the induction rules are called constructors. cat(ax, t) = acat(x, t).
If-then-else form using head and tail for strings:
Example 1. Find an inductive definition for S = {3, 16, 29, 42, …}.
cat(s, t) = if s = L then t else head(s)cat(tail(s), t).
Solution: Basis: 3 Î S.
Induction: If x Î S then x + 13 Î S. Example 3. Find a definition for ƒ : lists(Q) ® Q defined by ƒ(áx1, …, xnñ) = x1 + … + xn.
The constructors are 3 and the operation of adding 13. Also, without closure, many sets Solution: The set lists(Q) is inductively defined:
would satisfy the basis and induction rule. e.g., 3 Î Z and x Î Z implies x + 13 Î Z. á ñ Î lists(Q); h Î Q and t Î lists(Q) imply h :: t Î lists(Q).
Example 2. Find an inductive definition for S = {3, 4, 5, 8, 9, 12, 16, 17, 20, 24, 33,…} . To discover a recursive definition, we can use the definition of ƒ as follows:
Solution: To simplify things we might try to “divide and conquer” by writing S as the union ƒ(áx1, …, xnñ) = x1 + … + xn
of more familiar sets as follows: = x1 + (x2 + … + xn)
S = {3, 5, 9, 17, 33, …} È {4, 8, 12, 16, 20, 24, …}. = x1 + ƒ(áx2, …, xnñ)
= head(áx1, …, xnñ) + ƒ(tail(áx1, …, xnñ).
Basis: 3, 4 Î S.
Induction: If x Î S then (if x is odd then 2x – 1 Î S else x + 4 Î S). So if we let ƒ(á ñ) = 0, we have a recursive definition:
Example 3. Describe the set S defined inductively as follows: ƒ(á ñ) = 0
Basis: 2 Î S; ƒ(h :: t) = h + ƒ(t).
1 6
Induction: x Î S implies x ± 3 Î S. If-then-else form: ƒ(L) = if L = á ñ then 0 else head(L) + ƒ(tail(L)).
Solution: S = {2, 5, 8, 11, … } È {–1, –4, –7, –10, … }.
Example 4. Find an inductive definition for S = {L, ac, aacc, aaaccc, …} = {ancn | n Î N}. Example 4. Given ƒ : N ® N defined recursively by
Solution: Basis: L Î S. ƒ(0) = 0
Induction: If x Î S then axc Î S. ƒ(1) = 0
Example 5. Find an inductive definition for S = {an+1bcn | n Î N}. ƒ(x + 2) = 1 + ƒ(x).
The if-then-else form for ƒ can be written as follows:
Solution: Basis: ab Î S.
Induction: If x Î S then axc Î S. ƒ(x) = if x = 0 or x = 1 then 0 else 1 + ƒ(x – 2).
Example 6. Describe the set S defined by: Basis: a, b Î S What does ƒ do?
Induction: x Î S implies ƒ(x) Î S. Answer: List a few values to get the idea. For example,
Solution: S = {a, ƒ(a), ƒ(ƒ(a)), …} È {b, ƒ(b), ƒ(ƒ(b)), …}, which could also be written as map(ƒ, á0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ñ) = á0, 0, 1, 1, 2, 2, 3, 3, 4, 4 ñ.
S = {ƒn(a) | n Î N} È {ƒn(b) | n Î N} = {ƒn(x) | x Î {a, b} and n Î N}. So ƒ(x) returns the floor of x/2. i.e., ƒ(x) = ëx/2û.
Example 7. Describe the set S defined by: Basis: á 0 ñ Î S Example 5. Find a recursive definition for ƒ : lists(Q) ® Q defined by
Induction: x Î S implies cons(1, x) Î S. ƒ(áx1, …, xnñ) = x1x2 + x2x3 + … + xn–1xn.
Solution: S = {á 0 ñ, á 1, 0 ñ, á1, 1, 0 ñ, …}. Solution: Let ƒ(á ñ) = 0 and ƒ(áxñ) = 0. Then for n ≥ 2 we can write
Infix notation ƒ(áx1, …, xnñ) = x1x2 + (x2x3 + … + xn–1xn) = x1x2 + ƒ(áx2, …, xnñ).
cons(h, t) = h :: t. Associate to the right. e.g., x :: y :: z = x :: (y :: z). So we have the following recursive definition.
Example 8. Find an inductive definition for S = {á ñ, á a, b ñ, áa, b, a, b ñ, …}. ƒ(á ñ) = 0
Solution: Basis: á ñ Î S. ƒ(áxñ) = 0
Induction: x Î S implies a :: b :: x Î S. ƒ(h :: t) = h · head(t) + ƒ(t).
Example 9. Find an inductive definition for S = {á ñ, á á ñ ñ, á á á ñ ñ ñ, …}. If-then-else form:
Solution: Basis: á ñ Î S. 2 ƒ(L) = if L = á ñ or tail(L) = á ñ then 0 else head(L) · head(tail(L)) + ƒ(tail(L)). 7
Induction: x Î S implies x :: á ñ Î S.
Example 6. Find a recursive definition for isin : A ´ lists(A) ® {true, false} where isin(x, L) Rules for Combining Grammars
means that x is in the list L. Let L and M be two languages with grammars that have start symbols A and B, respectively,
Solution: isin(x, á ñ) = false and with disjoint sets of nonterminals. Then the following rules apply.
isin(x, x :: t) = true • L È M has a grammar starting with S ® A | B.
isin(x, h :: t) = isin(x, t). • LM has a grammar starting with S ® AB.
If-then-else form: • L* has a grammar starting with S ® AS | L.
isin(x, L) = if L = á ñ then false else if x = head(L) then true else isin(x, tail(L)). Example. Find a grammar for {ambmcn | m, n Î N}.
Example 7. Find a recursive definition for sub : lists(A) ´ lists(A) ® {true, false} where Solution: The language is the product LM, where L = {ambm | m Î N} and M = {cn | n Î N}.
sub(L, M) means the elements of L are elements of M. So a grammar for LM can be written in terms of grammars for L and M as follows.
Solution: sub(á ñ, M) = true S ® AB
sub(h :: t, M) = if isin(h, M) then sub(t, M) else false. A ® aAb | L
If-then-else form: B® cB | L.
sub(L, M) = if L = á ñ then true else if isin(head(L), M) then sub(tail(L), M) else false. Example. Find a grammar for the set Odd, of odd decimal numerals with no leading zeros,
Example 8. Find a recursive definition for intree : Q ´ binSearchTrees(Q) ® {true, false} where, for example, 305 Î Odd, but 0305 Ï Odd.
where intree(x, T) means x is in the binary search tree T. Solution: Notice that Odd can be written in the form Odd = (PD*)*O, where
Solution: intree(x, á ñ) = false O = {1, 3, 5, 7, 9}, P = {1, 2, 3, 4, 5, 6, 7, 8, 9} , and D = {0} È P.
intree(x, tree(L, x, R)) = true Grammars for O, P, and D can be written with start symbols A, B, and C as:
intree(x, tree(L, y, R)) = if x < y then intree(x, L) else intree(x, R). A ® 1 | 3 | 5 | 7 | 9, B ® A | 2 | 4 | 6 | 8, and C ® B | 0.
If-then-else form: intree(x, T) = if T = á ñ then false Grammars for D* and PD* and (PD*)* can be written with start symbols E, F, and G as:
else if x = root(T) then true E ® CE | L, F ® BE, and G ® FG | L.
else if x < root(T) then intree(x, left(T)) 8 So a grammar for Odd with start symbol S is 13
else intree(x, right(T)).
S ® GA.

Traversing Binary Trees Example. Find a grammar for the language L defined inductively by,
Basis: a, b, c Î L.
The three standard procedures to traverse a binary tree are defined recursively as follows:
Induction: If x, y Î L then ƒ(x), g(x, y) Î L.
preorder(T): if T ≠ á ñ then visit root(T); preorder(left(T)); preorder(right(T)) fi.
Solution: We can get some idea about L by listing some of its strings.
inorder(T): if T ≠ á ñ then inorder(left(T)); visit root(T); inorder(right(T)) fi
a, b, c, ƒ(a), ƒ(b), …, g(a, a), …, g(ƒ(a), ƒ(a)), …, ƒ(g(b, c)), …, g(ƒ(a), g(b, ƒ(c))), …
postorder(T): if T ≠ á ñ then postorder(left(T)); postorder(right(T)); visit root(T) fi.
So L is the set of all algebraic expressions made up from the letters a, b, c, and the function
Example 9. Traverse the following tree in each of the three orders. symbols ƒ and g of arities 1 and 2, respectively. A grammar for L can be written as
a
Solution: S ® a | b | c | ƒ(S) | g(S, S).
c Preorder: a b c d e
b For example, a leftmost derivation of g(ƒ(a), g(b, ƒ(c))) can be written as
Inorder: badce
Postorder: b d e c a S Þ g(S, S) Þ g(ƒ(S), S) Þ g(ƒ(a), S) Þ g(ƒ(a), g(S, S))
d e Þ g(ƒ(a), g(b, S)) Þ g(ƒ(a), g(b, ƒ(S))) Þ g(ƒ(a), g(b, ƒ(c))).
Example 10. Find a recursive definition for post : binaryTrees(A) ® lists(A) where post(T) is S
A Parse Tree is a tree that represents a derivation. The root is the
the list of nodes from a postorder traversal of T. start symbol and the children of a nonterminal node are the symbols
Solution: post(á ñ) = á ñ (terminals, nonterminals, or L) on the right side of the production g ( S , S )
post(tree(L, x, R)) = cat(post(L), cat(post(R), á x ñ)) used in the derivation step that replaces that node.
where cat concatenates two lists and can be defined by,
Example. The tree shown in the picture is the parse tree for the ƒ ( S ) b
cat(á ñ, L) = L
cat(h :: t, L) = h :: cat(t, L). following derivation:
S Þ g(S, S) Þ g(ƒ(S), S) Þ g(ƒ(a), S) Þ g(ƒ(a), b). a
9 14

Example 11. Find a recursive definition for ƒ : binaryTrees(Q) ® Q where ƒ(T) is the sum Ambiguous Grammar: Means there is at least one string with two distinct parse trees,
of the nodes in T. or equivalently, two distinct leftmost derivations or two distinct rightmost derivations.
Solution: ƒ(á ñ) = 0 Example. Is the grammar S ® SaS | b ambiguous?
ƒ(tree(L, x, R)) = x + ƒ(L) + ƒ(R). Solution: Yes. For example, the string babab has two distinct S S

Infinite Sequences leftmost derivations: S a S S a S


We can construct recursive definitions for infinite sequences by defining a value ƒ(x) in S Þ SaS Þ SaSaS Þ baSaS Þ babaS Þ babab.
S Þ SaS Þ baSÞ baSaS Þ babaS Þ babab. S a S b b S a S
terms of x and ƒ(y) for some value y in the sequence.
Example 12. Suppose we want to represent the infinite sequence ƒ(x) = áx, x2, x4, x8, … ñ. The parse trees for the derivations are pictured. b b b b
Solution: Use the definition to discover a solution as follows: Quiz (2 minutes). Show that the grammar S ® abS | Sab | c is ambiguous.
ƒ(x) = áx, x2, x4, x8, … ñ = x :: áx2, x4, x8, … ñ = x :: ƒ(x2). Solution: The string abcab has two distinct leftmost derivations:
So define ƒ(x) = x :: ƒ(x2). S Þ abS Þ abSab Þ abcab and S Þ Sab Þ abSab Þ abcab.
Example 13. What sequence is defined by g(x, k) = xk :: g(x, k + 1)? Unambiguous Grammar: Sometimes one can find a grammar that is not ambiguous for
the language of an ambiguous grammar.
Answer: g(x, k) = xk :: g(x, k + 1) = xk :: xk+1 :: g(x, k + 2) =… = áxk, xk+1, xk+2, … ñ.
Example. The previous example showed S ® SaS | b is ambiguous. The language of the
Example 14. How do we obtain the sequence áx, x3, x5, x7, … ñ?
grammar is {b, bab, babab, …}. Another grammar for the language is S ® baS | b. It is
A Solution. Define ƒ(x) = h(x, 1), where h(x, k) = xk :: h(x, k + 2). unambiguous because S produces either baS or b, which can’t derive the same string.
Example 15. How do we obtain the sequence á1, x2, x4, x6, x8, … ñ? Example. The previous quiz showed S ® c | abS | Sab is ambiguous. Its language is
A Solution: Use h(x, 0) from Example 14. {(ab)mc(ab)n | m, n Î N}. Another grammar for the language is
S ® abS | cT and T ® abT | L.
It is unambiguous because S produces either abS or cT, which can’t derive the same string.
10
Similarly, T produces either abT or L, which can’t derive the same string. 15

Grammars Analyzing Algorithms


A grammar is a finite set of rules, called productions, that are used to describe the strings of
a language. Let P be a problem and A an algorithm to solve P. The running time of A can be analyzed
by counting the number of certain operations that are performed during its execution.
Notational Example The productions take the form a ® b, where a and b are strings over This count may also depend on the size of an input, which depends on the problem.
an alphabet of terminals and nonterminals. Read a ® b as, “a produces b,” “a derives b,”
or “a is replaced by b.” The following four expressions are productions for a grammar. Example. If P asks whether a given item is in a given list, then we might count the
S ® aSB Alternative Short Form comparison operations executed by A, which will depend on the length of the list.
S®L S ® aSB | L
A worst-case input of size n is an input that causes A to execute the largest number of
B ® bB B ® bB | b.
operations.
B ® b.
Terminals: {a, b}, the alphabet of the language. Example. A worst-case input for an algorithm to search a list for an item would be a list
without the item.
Nonterminals: {S, B}, the grammar symbols (uppercase letters), disjoint from terminals.
Start symbol: S, a specified nonterminal alone on the left side of some production. Let WA(n) denote the maximum running time of A over all inputs of size n. WA is called
the worst-case function for A.
Sentential form: any string of terminals and/or nonterminals.
Derivation: a transformation of sentential forms by means of productions as An algorithm A to solve P is optimal in the worst case if every algorithm B to solve P
follows: If xay is a sentential form and a ® b is a production, then the replacement satisfies the inequality WA(n) ≤ WB(n) for all n > 0.
of a by b in xay to obtain xby is a derivation step, which we denote by xay Þ xby. Method to find optimal algorithm in the worst case:
Example Derivation: S Þ aSB Þ aaSBB Þ aaBB Þ aabBB Þ aabbB Þ aabbb. 1. Find an algorithm A to solve P and do some analysis to find WA(n).
This is a leftmost derivation, where each step replaces the leftmost nonterminal. 2. (lower bound) Find a function F where F(n) ≤ WB(n) for all n > 0 and all B to solve P.
The symbol Þ+ means one or more steps and Þ* means zero or more steps. So we could 3. Compare F and WA. If F(n) = WA(n) for all n, then A is optimal in the worst case.
write S Þ+ aabbb or S Þ* aabbb or aSB Þ* aSB, and so on. 11 Otherwise try to find a better lower bound or a better algorithm. 1

The Language of a Grammar A decision tree for an algorithm is a tree whose nodes represent decision points in the
algorithm and whose leaves represent outcomes.
The language of a grammar is the set of terminal strings derived from the start symbol.
Example/Quiz. Given a set of nine coins, one of which is heavier than the others, use a
Example. Can we find the language of the grammar S ® aSB | L and B ® bB | b?
pan-balance to find the heavy coin.
Solution: Examine some derivations to see if a pattern emerges.
A Solution. Let the coins be 1, 2, …, 9. The numbers on either side of each internal node
SÞL of the decision tree represent coins on the pan balance. The 9 outcomes are 1H, …, 9H.
S Þ aSB Þ aB Þ ab
S Þ aSB Þ aB Þ abB Þ abbB Þ abbb The worst-case performance of this algorithm
1234 5678
S Þ aSB Þ aaSBB Þ aaBB Þ aabB Þ aabb is 3 weighings.
S Þ aSB Þ aaSBB Þ aaBB Þ aabBBÞ aabbBB Þ aabbbBÞ aabbbb. 12 34 56 78 Is the algorithm optimal in the worst case? A
So we have a pretty good idea that the language of the grammar is {anbn+k | n, k Î N}. 9H
ternary decision tree with depth d has at most
Quiz (1 minute). Describe the language of the grammar S ® a | bcS. 1 2 3 4 5 6 7 8 3d leaves. Since there are 9 possible outputs, it
Solution: {(bc)na | n Î N}. follows that 3d ≥ 9. Take log3 to get d ≥ log39.
1H 2H 3H 4H 5H 6H 7H 8H Since d is a natural number, d ≥ élog39ù = 2.
Construction of Grammars So a lower bound is 2.
Example. Find a grammar for {anb | n Î N}.
Solution: We need to derive any string of a’s followed by b. The production S ® aS can be Is there a better lower bound or a better algorithm?
123 456
used to derive strings of a’s. The production S ® b will stop the derivation and produce the Here’s an optimal worst-case algorithm:
desired string ending with b. So a grammar for the language is S ® aS | b.
Quiz (1 minute). Find a grammar for {ban | n Î N}. 1 3 7 9 4 6
Solution: S ® Sa | b.
Quiz (1 minute). Find a grammar for {(ab)n | n Î N}. 12
1H 2H 3H 7H 8H 9H 4H 5H 6H
2
Solution: S ® Sab | L or S ® abS | L.
Example. Given a set of nine coins, one of which is bad, meaning that it is heavier or Example. Let count(n) be the number of times S is executed by the following algorithm as
lighter than the others. Find a good lower bound for a pan-balance algorithm that finds a function of n, where n Î N. Find a closed form for count(n).
the bad coin and states whether it is heavy or light. i := 1;
while i < n do
A Solution. A lower bound: If the coins are numbered 1, 2, …, 9 and we let H mean
i := i + 2;
heavy and let L mean light, then there are 18 possible outputs: 1H, 1L, …, 9H, 9L. A
for j := 1 to i do S od
ternary decision tree with depth d has at most 3d leaves. It follows that 3d ≥ 18. Take
log3 to obtain d ≥ log318. Since d is a natural number, we have d ≥ élog318ù = 3. So 3 is od
Solution: Each time through the while-loop i is incremented by 2. So the values of i at the
a good lower bound on the number of weighings.
start of each for-loop are 3, 5, …, (2k + 1), where i = 2k + 1 ≥ n represents the stopping
Quiz. Find an optimal worst-case pan-balance algorithm to detect the one bad coin among point for the while-loop. So we have
a set of nine coins, where the output states whether the bad coin is heavy or light.
count(n) = 3+ 5+" + (2k +1)
A Solution: k k k

123 456 = å (2i +1) = 2å i + å1


i=1 i=1 i=1
2k(k +1)
= + k = k(k +1) + k = k(k + 2).
123 789 7 8 123 789 !! 2
But we need to write count(n) in terms of n. Since 2k + 1 ≥ n is the stopping point
1 3 4 6 7 9 7 9 7 9 4 6 1 3 for the while-loop it follows that 2k –1< n is the last time the while-condition is
true. In other words, we have the inequality 2k – 1 < n ≤ 2k + 1. Solving for k, we
have 2k – 2 < n – 1 ≤ 2k, which gives k – 1 < (n – 1)/2 ≤ k. Therefore k = é(n – 1)/2ù.
1H 2H 3H 6L 5L 4L 7H 8L 9L 9H 8H 7L 4H 5H 6H 3L 2L 1L Now we can write count(n) in terms of n as
3 count(n) = k(k + 2) = é(n – 1)/2ù(é(n – 1)/2ù + 2). 8

Summations and Closed Forms Permutations and Combinations


A closed form is an expression that can be computed by applying a fixed number of
familiar operations to the arguments. For example, the expression 2 + 4 + … + 2n is not a Permutations (order counts)
closed form, but the expression n(n+1) is a closed form. • A permutation of a set is an arrangement of the objects from a set.
n
Summation Notation: • There are n! permutations of an n-element set, where 0! = 1 and n! = n · (n – 1)!
å ak = a1 +" + an . • The number of permuations of length r chosen from an n-element set (where r ≤ n) is
Summation Facts !k=1
!
n!
(1) å cak = cå ak . (2) å (ak + bk ) = å ak +å bk . P(n,r) = .
(n - r)!
n n+i

(3) å ak x i+k = x i å ak x k . (4) å ak+i = å ak . Example/Quiz. Let S = {a, b, c}. Calculate each expresssion and list the corresponding
k=m k=m+i permutations of S: P(3, 3), P(3, 2), and P(3, 1).
n n Solution.
(5) Collapsing Sums) å(ak - ak-1 ) = an - a0 and å(ak-1 - ak ) = a0 - an . P(3, 3) = 3!/0!= 6 with permutations abc, acb, bac, bca, cab, cba.
k=1 k=1 P(3, 2) = 3!/1! = 6 with permutations ab, ac, ba, bc,ca, cb.
Some Useful Closed Forms P(3, 1) = 3!/2!= 3 with permutations a, b, and c.
Quiz. Find the number of permutations of the letters in the word radon.
n n
n(n +1)
(1) å c = (n - m +1)c. (2) åk = 2
. Answer. 5! = 120.
k=m k=1
Bag Permutations
n
n(n +1)(2n +1) n
a n+1 -1
(3) åk2 = . (4) å a = a -1 (where a ¹ 1).
k
The number of permutations of an n-element bag with k distinct elements, where the ith
k=1 6 k=0 distinct element is repeated mi times is
n
a - (n +1)a n+1 + na n+2 n!
(5) å ka = (where a ¹ 1).
k
.
k=1 (a -1)2 4 !m
!1!" mk ! 9

n
Example. Find a closed form for the expression å (k -1)2 k+1 . The idea behind the formula
n n-1
k=2 The idea is easy to see from an example. Suppose the bag is [a, a, b, b, b]. Then we can
Solution: å (k -1)2 k+1 = å k2 k+2 (Fact 4) think of the letters as distinct elements of a set by placing subscripts on the repeated
elements to obtain the set {a1, a2, b1, b2, b3}. There are 5! permutations of this set. But we
k=2 k=1
n-1 don’t want to count permutations that are repeated if we drop the subscripts. For example,
= 2 2 å k2 k (Fact 3) don’t want to count a1a2b1b2b3 and a2a1b1b2b3 as different. So we need to divide 5! by the
k=1
number of permutations of each subscripted element: 2! for {a1, a2} and 3! for {b1, b2, b3}.
= 2 (2 - n2 n + (n -1)2 n+1 ) (Form 5)
2
This gives 5!/(2!3!) = 10 permutations of the bag [a, a, b, b, b].
= 2 3 - (2 - n)2 n+2. Example. Calculate the number of permutations of [a, a, b, b] and list each permutation.
Answer. 4!/(2!2!) = 6 with permutations aabb, abab, abba, bbaa, baba, baab .
Example. Find a closed form for !2!+ 2 2 × 7 + 2 3 ×14 +" + 2 n (n-1)× 7.
Quiz. Find the number of permutations of the letters in the word babbage.
n
Solution: The sum has the form 2 + å 2 k (k -1) × 7 Answer. 7!/(2!3!) = 420.
k=2
n
Quiz (5 minutes). Find the smallest size n for strings of length n over {a, b, c} that can be
(Fact 1) used as distinct codes for 27 people, where a is repeated k times, b is repeated l
= 2 + 7å (k -1)2 k
k=2 times, c is repeated m times, and k + l + m = n.
n-1
Answer. Use trial and error to solve the following inequality for the smallest n that
= 2 + 7å k2 k+1 (Fact 4)
satisfies the given conditions.
k=1
n-1 n!
= 2 +14å k2 k (Fact 3) ³ 27
k!l!m!
k=1

= 2 +14(2 - n2 n + (n -1)2 n+1 ). The solution is n = 5 with, for example, k = 2, l = 2, and m = 1.


(Form 5) 5 10

Quiz. Use summation facts and forms to prove that 2 + 3+...+ n = (n -1)(n + 2) . Combinations (order does not count)
n n-1
2 The number of combinations of n things taken k at a time is the number of k-element
2 + 3+ ...+ n = å k = å (k +1) æ ö n!
Solution. subsets of an n-element set, and is given by C(n, k) = ç n ÷ = .
k=2 k=1 è k ø k!(n - k)!
n-1 n-1
= å k + å1 The idea: Take the number of permutations of n things taken k at a time. This count includes
k=1 k=1 all the permutations of k elements. So divide by k! to get the count without regard to order.
(n -1)(n) Example/Quiz. Let S = {a, b, c}. Calculate each expression and list the corresponding
= + (n -1)
2 subsets of S. C(3, 3), C(3, 2), C(3, 1), C(3, 0).
(n -1)(n + 2) Solution. C(3, 3) = 1 with the subset {a, b, c}. C(3, 2) = 3 with subsets {a, b}, {a, c}, {b, c}.
= .
2 C(3, 1) = 3 with subsets {a}, {b}, {c}. C(3, 0) = 1 with subset Æ.
Quiz. Suppose there is a set of 5 cans of soda {a, b, c, d, e}. Find the number of
! 7+" + (3+ 4n).
Quiz. Use summation facts and forms to find a closed form for !3+ combinations of 5 cans of soda taken 3 at a time and list each combination.
æ ö 5!
n n n Answer. C(5,3) = ç5 ÷ = = 10.
Solution. å (3+ 4k) = å 3 + å 4k è3ø 3!2!
k=0 k=0 k=0 The 3-element subsets are {a, b, c}, {a, b, d}, {a, b, e}, {a, c, d}, {a, c, e}, {a, d, e},
n n
{b, c, d}, {b, c, e}, {b, d, e}, {c, d, e}.
= å 3 + 4å k
n æ ö æn ö
k=0 k=0 n
4n(n +1) Binomial Theorem. (x + y) n = å ç ÷x n-k y k . Note: ç ÷ is called a binomial coefficient.
= 3(n +1) + k=0 è k ø èk ø
2 Example.
3 æ ö æ 3ö æ 3ö æ 3ö æ 3ö
n
= (3+ 2n)(n +1). (x + y) 3 = å ç ÷x 3-k y k = ç ÷ x 3 + ç ÷x 2 y + ç ÷ xy 2 + ç ÷ y 3 = x 3 + 3x 2 y + 3xy 2 + y 3 .
6 k=0 è ø
k è ø
0 è ø
1 è ø
2 è ø
3 11

Example. Let count(n) be the number of := statements executed by the following Bag Combinations
algorithm as a function of n, where n Î N. Find a closed form for count(n). The number of k-element bags over an n-element set (with k and n positive) is given by
i := 1; (1) æ n + k -1ö
while i < n do ç ÷.
è k ø
i := i + 1; (n – 1)
for j := 1 to i do S od (2 + 3 + … + n) The idea behind the formula
od There is a bijection between the k-element bags over {1, 2, …, n} and the k-element
The expressions in parentheses indicate the number of times that := is executed. subsets of {1, 2, …, n, n + 1, …, n + (k – 1)}. The bijection associates each k-element bag
[x1, x2, …, xk] where xi ≤ xi+1, with the k-element subset {x1, x2+1, x3 + 2, …, xk + (k – 1)}},
Therefore, count(n) is the sum: count(n) = 1+ (n -1) + (2 + 3+" + n) and the number of these k-element subsets is given by desired formula.
= (n -1) + (1+ 2 + 3+" + n)
Example/Quiz. Let S = {a, b, c}. Calculate the number of 3-element bags over S and list
n(n +1)
= (n -1) + . each bag.
!! 2 æ 3+ 3-1ö æ5 ö 5!
Quiz. Let count(n) be the number of executions of S in the preceding algorithm as a Solution. ç ÷= ç ÷= = 10.
è 3 ø è3ø 3!2!
function of n. Find a closed form for count(n).
The 3-element bags are [a, b, c], [a, b, b], [a, c, c], [a, a, b], [a, a, c], [a, a, a], [b, c, c],
Solution. count(n) = (2 + 3+" + n) [b, b, c], [b, b, b], [c, c, c].
= (1+ 2 + 3+" + n) -1 Quiz. Find the number of ways that 5 cans of soda can be chosen from a machine that
n(n +1) dispenses 4 kinds of soda {a, b, c, d}.
= -1.
!! 2 æ 4 + 5 -1ö æ 8 ö 8!
Solution. ç ÷= ç ÷= = 56. For example, [a, a, b, b, d], and so on.
7 è 5 ø è 5 ø 3!5! 12
Discrete Probability Example. Draw a card at random from a deck of 52 cards. Let A mean the card is an Ace
The probability that statement s will be true is a real number, denoted by P(s), in the range and let B mean the card is a Spade. Are A and B independent events?
0 ≤ P(s) ≤ 1. If P(s) = 0, then s will never be true and if P(s) = 1, then s will always be true. Solution. We can represent A and B by
Terminology A = {AS, AH, AD, AC}.
• Sample space: a set of possible outcomes of an experiment (assumed to be finite). B = {2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS, AS}.
• Sample point (or point): an element of a sample space.
• Event: a subset of a sample space. P(A) = 4(1/52) = 1/13 and P(B) = 13(1/52) = 1/4. So P(A)P(B) = (1/13)(1/4) = 1/52. Also,
• Probability distribution on a sample space S: a function P : S ® [0, 1] such that A Ç B = {AS}, so P(A Ç B) = 1/52. Therefore, A and B are independent events.

å P(x) = 1. Repeated Independent Binomial Trials (of experiments with 2 outcomes)


xÎS Let H and T be two outcomes of an experiment with P(H) = p and P(T) = 1 – p. Assume
If E Í S (i.e., E is an event), then the probability of E is P( E) = å P(x). that we perform n trials of the experiment and each trial is independent of the others. For
example, the event “H on the first trial” is independent from the event “H on the second
xÎE
trial.” So both events have probability p. The sample space S can be represented by
Basic Properties S
Let S be a sample space, let P be a probability S = {x1x2...xn | xi Î {H, T}}.
E
distribution on S, and let E, A, and B be events as Since the trials are independent, we assign probabilities to the points in S by

pictured. P(x1x2...xn) = P(x1)P(x2)…P(xn).
Then we have the following properties. S Example. Suppose we perform 3 trials of the experiment. What value should be assigned to
P(S) = 1, A P(HHT)? Let A, B, and C be the events “H on first trial,” “H on second trial,” and “T on
P(Æ) = 0, B third trial.” For example, A = {HHH, HTH, HHT, HTT}. We have
P(E¢) = 1 – P(E), P({HHT}) = P(A Ç B Ç C)
P(A È B) = P(A) + P(B) – P(A Ç B). 13 = P(A)P(B)P(C) (Since A, B, and C are independent) 17
= pp(1 – p).

Example. Two fair dice are tossed with sample space S = {(i, j) | i, j Î {1, 2, 3, 4, 5, 6}}.
Since the dice are fair, P(i, j) = 1/36 for (i, j) Î S. Find the probability for each event. The Question: What is the probability of exactly k successes in n trials of a binomial
1. The sum of dots is a prime number. experiment where P(success) = p and P(failure) = 1 – p?
2. The sum of dots is not a prime number. æ ö
3. The sum of dots is greater than 4. The Answer: P(Exactly k successes in n trials) = ç n ÷ p k (1- p) n-k .
èk ø
Solution.
1. Let E = {(1, 1), (1, 2), (2, 1), (1, 4), (4, 1), (1, 6), (6, 1), (2, 3), (3, 2), (2, 5), (5, 2), Proof idea: If x1x2...xn contains k successes and n – k failures, then we know that
(3, 4), (4, 3), (5, 6), (6, 5)}. So | E | = 15. Therefore P(E) = 15(1/36) = 15/36. P(x1x2...xn) = P(x1)P(x2)…P(xn) = pk(1 – p)n–k.
2. Let E be the event of (1). Then P(E¢) = 1 – P(E) = 1 – 15/36 = 21/36. Now, how many ways can k successes and n – k failures be arranged? For example, how
3. If E denotes the sum of dots greater than 4, then E¢ = {(1, 1), (1, 2), (2, 1), (1, 3), (3, 1), many arrangements are there of 2 H’s and 3 T’s? The answer (by bag permutations) is
(2, 2)}. So P(E) = 1 – P(E¢) = 1 – 6/36 = 30/36. 5!/(2!3!) = 20. So in general there are n!/(k!(n – k)!) different ways to arrange k successes
and n – k failures. So we obtain the desired answer. QED.
Conditional Probability
For events A and B where P(B) ≠ 0, the probability of A given B, written P(A | B), is: Example. Toss a fair die and assume that success means 6 is on top. So P(success) = 1/6
and P(failure) = 5/6.
P( AÇ B) æ10ö
1. P(Exactly 3 successes in 10 trials) = ç ÷(1/ 6) (5/ 6) » 0.155.
3 7
P( A | B) = .
P( B) è3 ø
The idea for P(A | B) is to restrict the sample space to B as pictured. 2. P(Less than 3 successes in 10 trials)
æ ö 10 æ ö 9 æ ö
= ç10÷(1/ 6) (5/ 6) + ç10÷(1/ 6) (5/ 6) + ç10÷(1/ 6) (5/ 6) » 0.775.
0 1 2 8
Note the following special cases: S
è0ø è1ø è2ø
1. If A Ç B = Æ, then P(A | B) = 0. A
2. If B Í A, then P(A | B) = 1. B n
æ ö
Notice (from the binomial theorem): å ç nk ÷ø p k (1- p) n-k = ( p +1- p) n = 1.
3. If A Í B, then P(A | B) = P(A)/P(B). 14 k =0è 18

Example. Two fair dice are tossed with sample space S = {(i, j) | i, j Î {1, 2, 3, 4, 5, 6}}. Expectation (Average Behavior)
Since the dice are fair, P(i, j) = 1/36 for (i, j) Î S. Find the probability for each event. Let P : S ® [0, 1] be a probability distribution and let V : S ® R is an assignment of values
1. The sum of dots is a prime number. to the points in sample space S. The expectation (or expected value) of V is defined by
2. The sum of dots is not a prime number.
3. The sum of dots is greater than 4. E(V ) = åV (x)P(x).
xÎS
Solution.
1. Let E = {(1, 1), (1, 2), (2, 1), (1, 4), (4, 1), (1, 6), (6, 1), (2, 3), (3, 2), (2, 5), (5, 2), Example/Quiz. Two fair dice are tossed. If the total is 7, we win $100; if the total is 2 or 12,
(3, 4), (4, 3), (5, 6), (6, 5)}. So | E | = 15. Therefore P(E) = 15(1/36) = 15/36. we lose $100; otherwise we lose $10. What is the expected value of the game?
2. Let E be the event of (1). Then P(E¢) = 1 – P(E) = 1 – 15/36 = 21/36. Solution. Let S = {(i, j) | i, j Î {1, 2, 3, 4, 5, 6}} and P(i, j) = 1/36 for (i, j) Î S. Let A, B,
and C mean the total is 7, the total is 2 or 12, and the total is not 7, 2, or 12. Then we have
3. If E denotes the sum of dots greater than 4, then E¢ = {(1, 1), (1, 2), (2, 1), (1, 3), (3, 1),
(2, 2)}. So P(E) = 1 – P(E¢) = 1 – 6/36 = 30/36. A = {(1, 6), (6, 1), (2, 5), (5, 2), (3, 4), (4, 3)} and V(i, j) = 100 for (i, j) Î A.
B = {(1, 1), (6, 6)} and V(i, j) = – 100 for (i, j) Î B.
Conditional Probability
For events A and B where P(B) ≠ 0, the probability of A given B, written P(A | B), is: C = S – (A È B) and V(i, j) = – 10 for (i, j) Î C. (Note that | C | = 28.)
So we can calculate the expected value E(V) as follows:
P( AÇ B)
P( A | B) = .
P( B)
The idea for P(A | B) is to restrict the sample space to B as pictured. åV (x) P(x) = (1/ 36) åV (x)
xÎS xÎS
Note the following special cases: æ ö
S = (1/ 36)ç åV (x) + åV (x) + åV (x) ÷
1. If A Ç B = Æ, then P(A | B) = 0. A è xÎ A xÎB xÎC ø
2. If B Í A, then P(A | B) = 1. B æ ö
= (1/ 36)ç å100 - å100 - å10÷
3. If A Í B, then P(A | B) = P(A)/P(B). 14 è xÎ A xÎB xÎC ø 19
= (1/ 36)(6 ×100 - 2 ×100 - 28×10) » 3.33.

Example. Toss two fair dice, as in the previous example. What is the probability that the Markov Chains
top of the first die is 2 given that the sum of the two dice is 7? Suppose a program has one input and one output where the data can be of type A or type B.
After a period of testing it is observed that when the input has type A, the output has a 0.9
Solution. Let A be “the first die is 2” and let B be “the sum is 7”. Calculate P(A | B).
chance of being type A and 0.1 chance of being type B. When the input has type B, the
A = {(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6)}, output has a 0.2 chance of being type A and a 0.8 chance of being type B.
B = {(1, 6), (6, 1), (2, 5), (5, 2), (3, 4), (4, 3)},
Suppose further that the program is part of a loop where the output of each iteration is the
Then A Ç B = {(2, 5)}. So P(A | B) = P(A Ç B)/P(B) = (1/36)/(6/36) = 1/6. input to the next iteration. (This process is an example of a 2-state Markov chain.) We can
Quiz. Toss two fair dice, as in the previous example. What is the probability that the top of picture the situation with a labeled graph, where the nodes are the possible types and the
the first die is less than 4 given that the sum of the two dice is less than 8? edges are labeled with the given probabilities of traveling from one node to another:
Answer. Let A be “the first die is less than 4” and let B be “the sum is less than 8”. 0.1
Calculate P(A | B). B has 21 pairs, so P(B) = 21/36. A Ç B contains those pairs in B that
begin with 1, 2, or 3. So A Ç B = {(1, 1), …, (1, 6), (2, 1), …, (2, 5), (3, 1), …, (3, 4)}, 0.9 A B 0.8
which has 15 pairs. So P(A Ç B) = 15/36. Therefore P(A | B) = 15/21 = 5/7.
0.2
Bayes’ Theorem S B A question: What is the probability that the output is type A after n iterations if the initial
Let S be partitioned by events E1, …, En, and let B be an
input has type A? If the initial input has type B?
event with P(B) ≠ 0 as pictured.
E1 Example. If we start with A the probability of A after two iterations is obtained by traveling
The conditional probability P(Ei | B) can be thought of as E3 along all paths of length 2 that begin at A and end at A and then adding up the product of the
the probability that B is caused by Ei. E2
probabilities on the edges of each path to obtain
P(Ei Ç B) P( Ei Ç B) P(Ei ) P( B | Ei )
P( Ei | B) = = = . P(AAA) + P(ABA) = (0.9)(0.9) + (0.1)(0.2) = 0.83.
!! P(B) P(E1 Ç B) +" + P(En Ç B) P(E1 ) P( B | E1 ) +" + P(En )P(B | En )
Quiz. Find the probability of output A after 3 iterations of the loop with initial input A.
15 Answer: P(AAAB) + P(AABB) + P(ABAB) + P(ABBB) = 0.219. 21

We can represent the given probabilities with a matrix P, where the entry in row i column j
Example. Three chests C1, C2, and C3 each have 2 drawers. There is one coin in each
is the probability that input i results in output j.
drawer as follows: C1: gold and gold; C2: gold and silver; C3: silver and silver. One chest is
picked at random. Given that a gold coin is found in one of its drawers, what is the
probability that there is a gold coin in the other drawer? æ0.9 0.1ö
P =ç ÷.
Solution. Let Ci mean that chest Ci was chosen. So P(Ci) = 1/3. Let G mean that a gold è0.2 0.8ø
coin was found. Since C1 is the only chest with two gold coins, we want to know P(C1 | G).
We know that P(G | C1) = 1, P(G | C2) = 1/2, and P(G | C3) = 0. So we have
The nice part about this representation is that if the input is i we can find the probability that
P(C1 ) P(G | C1 ) the output is j after n stages by examining the (i, j) entry of the product Pn.
P(C1 | G) =
P(C1 )P(G | C1 ) + P(C2 ) P(G | C2 ) + P(C3 ) P(G | C3 )
(1/ 3)(1)
= = 2 / 3.
(1/ 3)(1) + (1/ 3)(1/ 2) + (1/ 3)(0)
Quiz: For the previous example, compute P(C2 | G) and P(C3 | G).
Answer. 1/3 and 0.
Independent Events
Two events A and B are independent if
P(A Ç B) = P(A)P(B).
Consequences
If A and B are independent with nonzero probabilities, P(A | B) = P(A) and P(B | A) = P(B).
If A and B are disjoint with nonzero probabilities, then they are NOT independent.
16 22
Solving Recurrences Step 2 tells us to solve the equation for A(x) and try to transform the resulting expression
Any recursively defined function ƒ with domain N that computes numbers is called a using known generating functions.
recurrence or recurrence relation. We often denoteƒ(n) by ƒn. The goal is to find a closed A(x) = 3+ 5x + 2x(A(x) - 3) + 3x 2 A(x)
formula for the general term ƒn.
= 3- x + 2xA(x) + 3x 2 A(x).
Simple Recurrences
It’s easy to solve simple recurrences of the following form, where ai and bi are Collect terms to obtain A(x)(1- 2x - 3x 2 ) = 3- x.
expressions that do not contain r. 3- x 3- x 1 2
Now solve for A(x): A(x) = = = +
r0 = b0 1- 2x - 3x 2 (1+ x)(1- 3x) 1+ x 1- 3x
rn = an rn–1 + bn 1 1
= +2×
Substitution Method 1- (-x) 1- 3x
Start with the general equation rn = an rn–1 + bn and use the definition to keep substituting ¥ ¥

for r on the right side until we discover a general pattern that allows us to skip ahead and = å (-x)n + 2 å (3x)n
n=0 n=0
substitute for the basis r0 = b0. ¥ ¥
Example. Solve the recurrence = å (-1)n x n + 2 å 3n x n
a0 = 1 n=0 n=0
¥
an = an – 1 + 2n.
= å ((-1)n + 2 × 3n )x n .
Solution. an = an – 1 + 2n n=0
= an – 2 + 2(n – 1) + 2n Step 3 tells us to equate coefficients to get an = (-1)n + 2 × 3n.
= an – 3 + 2(n – 2) + 2(n – 1) + 2n
… Step 4 tells us to check the answer. (Quiz)
= a0+ 2 + 2·2 + … + 2(n – 2) + 2(n – 1) + 2n Answer. a0 and a1 check out OK. Assume n > 1 and ak is OK for k < n. Show an is OK.
= 1 + 2 + 2·2 + … + 2(n – 2) + 2(n – 1) + 2n 23 an = 2an-1 + 3an-2 = 2((-1) n-1 + 2× 3n-1 ) + 3((-1)n-2 + 2 × 3n-2 ) = ((-1) n + 2× 3n ). 28
= 1 + 2(1 + 2 + … + (n – 2) + (n – 1) + n) = 1 + n(n + 1).

Cancellation Method Section 6.1 How Do We Reason?


Start with the general equation rn = an rn–1 + bn. Then write a new equation whose left We make arguments, where an argument is a sequence of statements, called premises,
side is the term involving r on the right side of the given equation. Continue this process followed by a single statement, called the conclusion.
by writing a new equation whose left side is the term involving r on the right side of the
previous equation. Do this until we discover a general pattern that allows us to skip ahead The hope is that we make valid arguments, where an argument is valid if the truth of the
and write the last equation that contains r0 on the right side. Then add up the equations premises implies the truth of the conclusion.
and cancel the like terms to obtain an expression for rn. We can use rules of logic to make valid arguments.
Example. Solve the recurrence The most common rule of logic is modus ponens (mode that affirms). If A and B be are
a0 = 1 statements and “if A then B” and A are both true, then we can conclude that B is true.
an = an – 1 + 2n. Quiz. How did you learn the modus ponens rule as a child?
Solution. an = an – 1 + 2n
When a conclusion is made that does not follow from the premises the reasoning is called a
an–1 = an – 2 + 2(n – 1)
non sequitur (it does not follow).
an–2 = an – 3 + 2(n – 2)
… Quiz. What is a non sequitur that you have observed?
a1 = a0+ 2(1) Some dictionary-type definitions of logic:
Now add up the equations and cancel the like terms on either side to obtain the following • The study of the principles of reasoning, especially of the structure of statements and
equation for an: of methods to determine their validity.
an = a0+ 2(1) + … + 2(n – 2) + 2(n – 1) + 2n • A system of reasoning.
= 1 + 2[1 + … + (n – 2) + (n – 1) + n] • Valid reasoning.
= 1 + n(n+1). A calculus is a language of expressions, where each expression has a value and there are
rules to transform one expression into another that has the same value.
24
1

Divide and Conquer Algorithms. Many divide and conquer algorithms are special cases
of the following general form. To solve a problem with input of size n split it into s smaller Section 6.2 Propositional Calculus
problems each with input of size n/b. To simplify things we assume that n = bk for two Propositional calculus is the language of propositions (statements that are true or false).
positive integers b and k. Assume that for inputs of size n it takes tn operations to split up We represent propositions by formulas called well-formed formulas (wffs) that are
the problem and to do other tasks such as assembling the solution. Let an be the number of constructed from an alphabet consisting of
operations to solve the problem with input size n. Then we have the recurrence
Truth symbols: T (or True) and F (or False)
a1 = t Propositional variables: uppercase letters.
an = san/b + tn. Connectives (operators): ¬ (not, negation)
Solution (by cancellation): an = san / b + tn Ù (and, conjunction)
san / b = s 2an / b + (s / b)tn Ú (or, disjunction)
® (conditional, implication)
2

s 2an / b = s 3an / b + (s / b)2 tn


2 3
Parentheses symbols: ( and ).
" A wff is either a truth symbol, a propositional variable, or if V and W are wffs, then so are
k-1
k -1
k-1
!s! an / b = s an / b + (s / b) tn
k
k ¬ V, V Ù W, V Ú W, V ® W, and (W).
Since n = bk, we stop with the last equation. k-1
Example. The expression A ¬ B is not a wff. But each of the following three expressions
Now add the equations and cancel to obtain an = s k a1 + tnå (s / b)i. is a wff: A Ù B ® C, (A Ù B) ® C, and A Ù (B ® C).
i=0 Truth Tables. The connectives are defined by the following truth tables.
For example, the closed forms for the cases s = b and s ≠ b are as follows:
k-1 P Q ¬P P ÙQ P ÚQ P ® Q
(s = b) : an = b log b n
a1 + å tn = tn + ktn = tn + (logb n)tn = tn(1+ log b n). T T F T T T
i=0
k-1 æ (s / b)log n - 1ö T F F F T F
(s ¹ b) : an = s k a1 + tnå (s / b)i = ts log
b
n
b
+ tnçç ÷÷. F T T F T T
è (s / b) - 1 ø
25
i=0 F F T F F T
2

Generating Functions
The generating function for the infinite sequence a0, a1, …, an, … is given by the following Semantics
expression, where x is an indeterminate symbol. The meaning of T (or True) is true and the meaning of F (or False) is false. The meaning of
¥ any other wff is its truth table, where in the absence of parentheses, we define the hierarchy
å an x n
. of evaluation to be ¬, Ù, Ú, ®, and we assume Ù, Ú, ® are left associative.
n=0
Examples. ¬ A Ù B means (¬ A) Ù B
Other names are formal power series and infinite polynomial. They can be added, A Ú B Ù C means A Ú (B Ù C)
multiplied, divided, and equated just like polynomials. A Ù B ® C means (A Ù B) ® C
Some generating functions have closed forms. For example, the generating function for A ® B ® C means (A ® B) ® C.
the sequence 1, 1, …, 1, … has the following closed form, which can be verified by Three Classes
multiplying both sides by 1 – x. A Tautology is a wff for which all truth table values are T.
¥
1 A Contradiction is a wff for which all truth table values are F.
(Geometric Series Generating Function) å x n = 1- x . A Contingency is a wff that is neither a tautology nor a contradiction.
n=0
Examples. P Ú ¬ P is a tautology. P Ù ¬ P is a contradiction. P ® Q is a contingency.
Generating functions with closed forms can be used to solve recurrences.
Equivalence
Example. Suppose someone tells us that the generating function for an satisfies the equation The wff V is equivalent to the wff W (written V º W) iff V and W have the same truth value
¥
3 for each assignment of truth values to the propositional variables occurring in V and W.
å an x n = 2x +1 .
n=0 Example. ¬ A Ù (B Ú A) º ¬ A Ù B and A Ú ¬ A º B Ú ¬ B.
¥ æ ö ¥ ¥ Equivalence and Tautologies
3 1
Notice that å an x n = = 3ç ÷ = 3å (-2x) = å 3(-2) x .
n n n
We can express equivalence in terms of tautologies as follows:
2x +1 è 1- (-2x) ø n=0
n=0 n=0
V º W iff (V ® W) and (W ® V) are tautologies.
We can equate coefficients to obtain the solution an = 3(–2)n. 26 Proof: V º W iff V and W have the same truth values iff (V ® W) and (W ® V) are
tautologies. QED. 3

Example. Solve the following recurrence.


a0 = 3
a1 = 5
an = 2an–1 + 3an–2.
Solution. Step 1 tells us to use the recurrence to find an algebraic equation in terms of the
generating function A(x).
¥ ¥
å an x = a0 + a1x + å a n x
n n
A(x) =
n=0 n=2
¥
= 3+ 5x + å (2a n-1 +3a n-2 )x
n

n=2
¥ ¥
= 3+ 5x + å 2a n-1 x + å 3a n-2 x
n n

n=2 n=2
¥ ¥
= 3+ 5x + 2x å a n-1 x
n-1
+ 3x 2 å a n-2 x
n-2

n=2 n=2
¥ ¥
= 3+ 5x + 2x å a n x + 3x 2 å a n x
n n

n=1 n=0

= 3+ 5x + 2x(A(x) - a0 ) + 3x 2 A(x)
= 3+ 5x + 2x(A(x) - 3) + 3x 2 A(x).
27
Basic Equivalences that Involve True and False Quizzes (1 minute each). Use known equivalences in each case.
The following equivalences are easily checked with truth tables:
Prove that A Ú B ® C º (A ® C) Ù (B ® C).
A Ù True º A A Ú True º True A ® True º True True ® A º A Prove that (A ® B) Ú (¬ A ® B) is a tautology (i.e., show it is equivalent to true)
A Ù False º False A Ú False º A A ® False º ¬ A False ® A º True Prove that A ® B º (A Ù ¬ B) ® False .
A Ù ¬ A º False A Ú ¬ A º True A ® A º True
Use absorption to simplify (P Ù Q Ù R) Ú (P Ù R) Ú R.
Other Basic Equivalences Use absorption to simplify (S ® T) Ù (U Ú T Ú ¬ S).
The connectives Ù and Ú are commutative, associative, and distribute over each other. These
properties and the following equivalences can be checked with truth tables: Is it a tautology, a contradiction, or a contingency?
If P is a variable in a wff W, let W(P/True) denote the wff obtained from W by replacing all
AÙAºA ¬ (A Ù B) º ¬ A Ú ¬ B A Ù (A Ú B) º A A Ù (¬ A Ú B) º A Ù B occurrences of P by True. W(P/False) is defined similarly. The following properties hold:
AÚAºA ¬ (A Ú B) º ¬ A Ù ¬ B A Ú (A Ù B) º A A Ú (¬ A Ù B) º A Ú B
¬¬AºA A®Bº¬AÚB ¬ (A ® B) º A Ù ¬ B W is a tautology iff W(P/True) and W(P False) are tautologies.
Using Equivalences To Prove Other Equivalences
W is a contradiction iff W(P/True) and W(P/False) are contradictions.
We can often prove an equivalence without truth tables because of the following two facts: Quine’s method uses these properties together with basic equivalences to determine whether
1. If U º V and V º W, then U º W. a wff is a tautology, a contradiction, or a contingency.
2. If U º V, then any wff W that contains U is equivalent to the wff obtained from W by Example. Let W = (A Ù B ® C) Ù (A ® B) ® (A ® C). Then we have
replacing an occurrence of U by V.
W(A/False) = (False Ù B ® C) Ù (False ® B) ® (False ® C)
º (False ® C) Ù True ® True º True .
So W(A/False) is a tautology. Next look at
W(A/True) = (True Ù B ® C) Ù (True ® B) ® (True ® C) º (B ® C) Ù B ® C.
Let X = (B ® C) Ù B ® C. Then we have
4 X(B/True) = (True ® C) Ù True ® C º C Ù True ® C º C ® C º True.
X(B/False) = (False ® C) Ù False ® C º False ® C º True.
5
So X is a tautology. Therefore, W is a tautology.
Quizzes (2 minutes each). Use Quine’s method in each case.
Show that (A Ú B ® C ) Ú A ® (C® B) is NOT a tautology.
Show that (A ® B) ® C is NOT equivalent to A ® (B ® C). Section 6.3 Formal Reasoning
Normal Forms A formal proof (or derivation) is a sequence of wffs, where each wff is either a premise
A literal is either a propositional variable or its negation. e.g., A and ¬ A are literals. or the result of applying a proof rule to certain previous wffs in the sequence.
A disjunctive normal form (DNF) is a wff of the form C1 Ú … Ú Cn, where each Ci is a
Basic Proof Rules
conjunction of literals, called a fundamental conjunction. A conjunctive normal form (CNF)
is a wff of the form D1 Ù … Ù Dn, where each Di is a disjunction of literals, called a Conjunction (Conj) Simplification (Simp)
fundamental disjunction. A, B AÙB AÙB
and
Examples. (A Ù B) Ú (¬ A Ù C Ù ¬ D) is a DNF. (A Ú B) Ù (¬ A Ú C) Ù (¬ C Ú ¬ D) is a AÙB A B
CNF. The wffs A, ¬ B, A Ú ¬ B, and A Ù ¬ B are both DNF and CNF. Why?
Any wff has a DNF and a CNF. Addition (Add) Disjunctive Syllogism (DS)
For any propositional variable A we have True º A Ú ¬ A and False º A Ù ¬ A. Both forms
are DNF and CNF. For other wffs use basic equivalences to: (1) remove conditionals, (2) A
and
B A Ú B, ¬A A Ú B, ¬B
and
move negations to the right, and (3) transform into required form. Simplify where desired. AÚB AÚB B A
Example. (A ® B Ú C) ® (A Ù D)
º ¬ (A ® B Ú C) Ú (A Ù D) (X ® Y º ¬ X Ú Y) Modus Ponens (MP) Conditional Proof (CP)
º (A Ù ¬ (B Ú C)) Ú (A Ù D) (¬ (X ® Y) º X Ù ¬ Y) A ® B, A From A, derive B
º (A Ù ¬ B Ù ¬ C) Ú (A Ù D) (¬ (X Ú Y) º ¬ X Ù ¬ Y) (DNF)
B A®B
º ((A Ù ¬ B Ù ¬ C) Ú A) Ù ((A Ù ¬ B Ù ¬ C) Ú D) (distribute Ú over Ù)
º A Ù ((A Ù ¬ B Ù ¬ C) Ú D) (absorption)
Double Negation (DN) Contradiction (Contr) Indirect Proof (IP)
º A Ù (A Ú D) Ù (¬ B Ú D) Ù (¬ C Ú D) (distribute Ú over Ù) (CNF)
º A Ù (¬ B Ú D) Ù (¬ C Ú D) (absorption) (CNF). 6 ¬¬A A A, ¬A From ¬A, derive False
and
A ¬¬A False A
Constructing Full DNF and Full CNF
9
We can use the technique for truth functions to find a full DNF or full CNF for any wff with
the restriction that a tautology does not have a full CNF and a contradiction does not have a
full DNF. For example,
True º A Ú ¬ A, which is a full DNF and a CNF, but it is not a full CNF. Proof Notation
False º A Ù ¬ A, which is a full CNF and a DNF, but it is not a full DNF. Put each wff on a numbered line along with a reason. Use the letter P for a premise and
Alternative Constructions for Full DNF and Full CNF. Use basic equivalences together follow the proof with QED.
with the following tricks to add a propositional variable A to a wff W: Using CP
W º W Ù True º W Ù (A Ú ¬ A) º (W Ù A) Ú (W Ù ¬ A). If a proof consists of a derivation from a premise A to a conclusion B that does not contain
W º W Ú False º W Ú (A Ù ¬ A) º (W Ú A) Ù (W Ú ¬ A). any uses of CP or IP, then we can apply CP to obtain a tautology A ® B. The reason we
Example. Find a full DNF for (A Ù ¬ B) Ú (A Ù C). obtain a tautology is that the proof rules used in the derivation are valid arguments. So the
Answer. (A Ù ¬ B Ù C) Ú (A Ù ¬ B Ù ¬ C) Ú (A Ù C Ù ¬ B) Ú (A Ù C Ù B), which can be truth of A implies the truth of B, which tells us that A ® B is a tautology.
simplified to: (A Ù ¬ B Ù C) Ú (A Ù ¬ B Ù ¬ C) Ú (A Ù C Ù B),
Quiz (1 minute). Find a full CNF for ¬ A Ù B. When using CP in this way, instead of writing A ® B, we’ll write QED along with the line
Ans. (¬ A Ú B) Ù (¬ A Ú ¬ B) Ù (B Ú A) Ù (B Ú ¬ A) º (¬ A Ú B) Ù (¬ A Ú ¬ B) Ù (B Ú A). numbers of the derivation followed by CP.
Complete Sets of Connectives
A set S of connectives is complete if every wff is equivalent to a wff constructed from S. So
{¬, Ù, Ú, ®} is complete by definition. Using IP
Examples. Each of the following sets is a complete set of connectives. If a proof consists of a derivation from a premise ¬ A to the conclusion False, then we could
{¬, Ù, Ú}, {¬, Ù}, {¬, Ú}, {¬, ®}, {False, ®}, {NAND}, {NOR}.
apply CP to obtain ¬ A ® False. But we also know that A º ¬ A ® False. So the result of
the derivation is A. This is the IP rule.
Quiz (2 minutes). Show that {¬, ®} is a complete.
8
Quiz (2 minutes). Show that {if-then-else, True, False} is a complete. IP is most often used in a subproof setting when proving a conditional of the form V ® W.
Start with V as a premise for a CP proof. Then start an IP subproof with premise ¬ W.
Quiz (2 minutes). Transform (A Ù B) Ú ¬ (C ® D) into DNF and into CNF. When a contradiction is reached, we obtain W by IP. Then CP gives the result V ® W.
Every Truth Function Is a Wff
As with CP subproofs, the result of IP is written with no indentation.
A truth function is a function whose arguments and results take values in {true, false}. So a
truth function can be represented by a truth table. The task is to find a wff with the same
10
truth table. We can construct both a DNF and a CNF.
Technique. To construct a DNF, take each line of the table with a true value and construct a
fundamental conjunction that is true only on that line. To construct a CNF, take each line Subproofs
with a false value and construct a fundamental disjunction that is false only on that line. A subproof is a proof that is part of another proof. It always starts with a new premise
Example. Let ƒ be defined by and always ends by applying CP or IP to the derivation from that premise. When this
A B ƒ(A,B) (DNF Parts) (CNF Parts)
ƒ(A, B) = if A = B then True else False.
T T T AÙB
happens, the premise is discharged and the wffs of the derivation become inactive.
The picture shows the truth table for ƒ
T F F ¬A Ú B Indent the statements of the subproof and write down the result of CP or IP without
together with the fundamental
F T F A Ú¬B indentation.
conjunctions for the DNF and the
fundamental disjunctions for the CNF. F F T ¬ A Ù¬ B

So ƒ(A, B) can be written as follows:


ƒ(A, B) º (A Ù B) Ú (¬ A Ù ¬ B) (DNF)
ƒ(A, B) º (¬ A Ú B) Ù (A Ú ¬ B) (CNF)
Full CNF and Full DNF. A DNF for a wff W is a Full DNF if each fundamental
Derived Rules (they follow from the original rules) 12
conjunction contains the same number of literals, one for each propositional variable of W.
A CNF for a wff W is a Full CNF if each fundamental disjunction contains the same
Modus Tollens (MT)
Proof by Cases (Cases)
number of literals, one for each propositional variable of W. A ® B, ¬B
7 A Ú B, A ® C, B ® C
Example. The wffs in the previous example are full DNF and full CNF. ¬A
C
Hypothetical Syllogism (HS) Constructive Dilemma (CD)
A ® B, B ® C A Ú B, A ® C, B ® D
A®C CÚD

11

13
Example/Quiz. Consider the following argument:
I eat spinach (S) or ice cream (I). If I study logic (L) then I will pass the exam (P).
If I eat ice cream then I will study logic. If I eat spinach then I will play golf (G).
I failed the exam. Therefore, I played golf.
The argument has five premises {S Ú I, L ® P, I ® L, S ® G, ¬ P} and conclusion G.
Prove that the argument is valid.
Alternative Proof:
1. SÚI P 6. I ® P 2, 3, HS
2. L®P P 7. ¬ I 5, 6, MT
3. I®L P 8. S 1, 7, DS
4. S®G P 9. G 4, 8, MP
5. ¬P P QED.
6. ¬L 2, 5, MT Alternative Proof:
7. ¬I 3, 6, MT 6. G Ú L 1, 3, 4, CD
8. S 1, 7, DS 7. ¬ L 2, 5, MT
9. G 4, 8, MP 8. G 6, 7, DS
QED. QED.
Alternative Proof:
6. I ® P 2, 3, HS
7. G Ú L 1, 4, 6, CD
8. G 5, 7, DS
QED. 14

Deduction Theorem (The CP Rule)


If A is a premise in a proof of B, then there is a proof of A ® B that does not use A as a
premise.
Proof Idea: Assume the proof has the form
A = B0, …, Bn = B.
If n = 0, then A = B. So we must find a proof of A ® B = A ® A that does not use A as a
premise. A proof was given in the previous example (lemma). Let n > 0 and assume that for
each k in the range 0 ≤ k < n there is a proof of A ® Bk that does not use A as a premise.
Show that there is a proof of A ® Bn that does not use A as a premise. If Bn is a premise or
an axiom, then we have the following proof that does not use A as a premise:
1. Bn Premise or Axiom
2. Bn ® (A ® Bn) Axiom 1
3. A ® Bn 1, 2, MP QED.
If Bn is neither a premise nor an axiom, then it is inferred by MP from Bi and Bj = Bi ® Bn,
where i < n and j < n. So we obtain the following proof that does not use A as a premise:
1. Proof of A ® Bi not using A as a premise Induction assumption
2. Proof of A ® (Bi ® Bn) not using A as a premise Induction assumption
3. (A® (Bi ® Bn))® ((A ® Bi) ®(A ® Bn)) Axiom 2
4. (A ® Bi) ® (A ® Bn) 2, 3, MP
5. A ® Bn 1, 4, MP QED.
Since Bn = B, we have a proof of A ® B that does not use A as a premise. QED.
15

Section 6.4 Formal Axiom Systems


By a formal axiom system we mean a specific set of axioms (a fixed set of premises) and
proof rules. The aims of a formal axiom system are soundness and completeness::
Soundness: All proofs yield theorems that are tautologies.
Completeness: All tautologies are provable as theorems.
Frege-Lukasiewicz (F-L) Axiom System
Axiom 1: A® (B ® A).
Axiom 2: (A® (B ® C)) ® ((A ® B) ®(A ® C)).
Axiom 3: (¬ A® ¬ B) ® (B ® A).
Proof Rule: MP.
Since the axioms of F-L are tautologies and MP maps tautologies to a tautology, the F-L
system is sound. The F-L system is also complete, but that takes a bit of proof (see the text).

16

You might also like