Professional Documents
Culture Documents
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 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 .
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.
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
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
(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
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.
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).
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
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
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
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
16