0% found this document useful (0 votes)
82 views

Python Cheat Sheet

1) Theory of Computation Cheat Sheet provides definitions and examples related to sets, functions, languages, and proofs by induction and diagonalization. 2) Proofs by induction have two steps - proving the base case and inductive step. Diagonalization shows a set is uncountable by creating a new element not in the enumeration. 3) The document defines sets, operations on sets like union and intersection, properties like commutativity, and cardinality. It also defines functions, languages over alphabets, and special languages.

Uploaded by

Sajjad Salaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Python Cheat Sheet

1) Theory of Computation Cheat Sheet provides definitions and examples related to sets, functions, languages, and proofs by induction and diagonalization. 2) Proofs by induction have two steps - proving the base case and inductive step. Diagonalization shows a set is uncountable by creating a new element not in the enumeration. 3) The document defines sets, operations on sets like union and intersection, properties like commutativity, and cardinality. It also defines functions, languages over alphabets, and special languages.

Uploaded by

Sajjad Salaria
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Theory of Computation Cheat Sheet

3) f (i) ∈ N but not in enumeration (differs in some


column with every row). ∴ the set of monotone-increasing
Induction
functions is uncountably infinite.
Proof by induction has two steps: proving the base
E.g. Prove, using diagonalization, that the power set
case and the inductive step. These two parts should
convince us that S(n) is true for every integer n that is of natural numbers (2N ) is uncountable.
equal to or greater than the basis.
1) Assume 2N is countable. Enumerate all subsets
n
E.g. Prove S(n) = n! > 2 for n ≥ 4 1 2 3 ··· N
S1 0 0 0 ···
Step 1: Basis S2 1 1 1 ···
(in binary representation) S3 1 0 1 ···
S(4) = 4! > 24 = 24 > 16.
..
.
Step 2: Induction Si
Inductive hypothesis: assume true for k ≥ 4. 2) Create a new subset S(i) = 1 − Si (i), S 6= Si ∀ i.
If S(k) = k! > 2k then S(k + 1) = (k + 1)! > 2k+1 . S(1) = 1 − S1 (1) = 1 − 0 = 1
Rewrite S(k + 1) so it can make use of S(k). S(2) = 1 − S2 (2) = 1 − 1 = 0
S(k + 1) = (k + 1) k! > 2 · 2k S(3) = 1 − S3 (3) = 1 − 1 = 0
S(n) tells us that k! > 2k . If we remove that assumed truth ..
from the above statement, we only need to show k + 1 > 2. .
Since n ≥ 4, we get 4 + 1 > 2 which holds.
3) S(i) ∈ 2N (because the power set contains all sub-
Technique of Diagonalization sets of set N ) but not in enumeration. ∴ the power set of
all natural numbers is uncountably infinite.
Diagonalization is used to show that a set is uncountable.
An uncountable set is an infinite set that contains too Set Theory
many elements to be countable (i.e. it is not enumerable).
The uncountability of a set is closely related to its cardinal Sets are (1) well defined, (2) have no ordering of ele-
number: a set is uncountable if its cardinal number is ments, and (3) contain no duplicates – but multi-sets do.
larger than that of the set of all natural numbers. The set A : set A is a set
of natural numbers (and any other countably infinite set) x ∈ A x is an element/member of A
has cardinality aleph-null (ℵ0 ). ∅, {} empty set
∅⊆A empty set is a subset
1: Assume set is countable, enumerate all subsets A⊆A set is a subset of itself
Definitions
2: Create new subset using elements from existing subsets A⊆B A is a subset of B if a ∈ A, a ∈ B
3: Show new subset is in set but not in enumeration A⊂B proper subset if a ∈ A, a ∈ B, A 6= B
2A power set (all subsets of set A)
E.g. A function f : N → N is monotone-increasing if |A| cardinality of A (number of elements)
f (i) < f (i + 1) ∀ i ∈ N . Prove, using diagonalization, that 2|A| number of subsets of A
the set of monotone-increasing functions is uncountable. Union A ∪ B = {x | x ∈ A or x ∈ B}
e.g. {1, 2} ∪ {2, 3} = {1, 2, 3}
1) Assume set of all monotone-increasing functions is Intersection A ∩ B = {x | x ∈ A and x ∈ B}
1 2 3 ··· i e.g. {1, 2} ∩ {2, 3} = {2}
f1 1 2 3 · · · Operations
Set Difference A − B = {x |, x ∈ A, x ∈ / B}
f2 2 4 6 · · · e.g. {1, 2} \ {2, 3} = {1}
countable f3 3 5 7 · · ·
Cartesian Product A × B = {(a, b) | a ∈ A, b ∈ B}
..
. Complement everything not in the set
fi Commutative A∪B =B∪A
2) Create a new monotone-increasing function A∩B =B∩A
f (i) = f (i − 1) + fi (i), f 6= fi ∀ i. Associative A ∪ (B ∪ C) = (A ∪ B) ∪ C
Properties
f (1) = 0 + f1 (1) = 0 + 1 = 1 A ∩ (B ∩ C) = (A ∩ B) ∩ C
f (2) = 1 + f2 (2) = 1 + 4 = 5 Distribution A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
f (3) = 5 + f3 (3) = 5 + 7 = 12 A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
.. E.g. Is the set of all functions f : {0, 1} → N countable?
.

1
E.g. N × N × N is countable. 3-tuple (i, j, k).
This function has two cases: fi (0) → N and fi (1) → N . If a = 1, there are a3 tuples (finite) such that
|N | = ℵ0 which means the set of NS is countable. 1 ≤ i ≤ a, 1 ≤ j ≤ a, 1 ≤ k ≤ a. Finite if a = 2, 3, · · ·
f : {0, 1} → N is equivalent to f : {0} → N f : {1} → N .
TheS union of two countable sets results in a countable set, E.g. |(0, 1)| = 2N = ℵ1 = uncountable. (ℵ0 is countable)
N N = N thus f : {0, 1} → N is countably infinite.
E.g. Set of binary functions f : N → {0, 1} is un-
One-to-one and Onto Functions countable.
• A onto B ⇒ every element in B is mapped
• A 1-1 B ⇒ every element in A has a unique mapping Languages
• 1-1 correspondence ⇒ bijective (1-1 and onto). i.e. No • An alphabet Σ is a finite set of symbols, e.g. Σ = {0, 1}
two values map to the same value. Every element in the • A language over alphabet Σ is a set of strings, each
codomain is mapped. |A| = |B|, same cardinality. having its characters drawn from Σ
• Identity function ⇒ input parameter is the same as the • Length of a string s, |s|, is the # of symbols in string s
output value. • Empty string  has length 0
• A function has a single outcome for each parameter • Substrings of abc = a, b, c, ab, bc, abc
• Prefix is any # of leading symbols, e.g. , a, ab, abc
E.g. functions f : N → N where N = {1, 2, 3, . . . } • Postfix is any # of trailing symbols, e.g. , c, bc, abc
• Reversal is the string in reverse, e.g. s = abc, sR = cba
1. f is 1-1 but not onto: Special languages:
f (x) = x + 1
f (1) = 2 1. ∅ is the empty language
f (2) = 3
f (3) = 4 2. {} language that contains empty string
..
. 3. Σ∗ (Kleene Closure) contains all strings over Σ

2. f is onto but not 1-1: 4. Σ+ (Positive Closure) contains all strings over Σ except
f (x) = d x2 e the empty string
f (1) = 1 Concatenation {a} · {b} = {a}{b} = {ab}
f (2) = 1
Union {a} ∪ or + or | {b} = {a, b}
f (3) = 2 Operations
Kleene Closure {a}∗ = {, a, aa, aaa, · · · }
f (4) = 2
Positive Closure {a}+ = {a, aa, aaa, · · · }
f (5) = 3
E.g. First 5 strings of language L = {x ∈ {a, b, c}∗ : x
f (6) = 3
.. contains at least one a and at least one b} in lexicographical
. order. ⇒ ab, ba, aab, aba, abb

3. f is a 1-1 correspondence and not an identity function: E.g. Let X = {aa, bb} and Y = {, b, ab}.
f (x) = x − (x + 2 mod 3) + (x mod 3)
f (1) = 2 1. List the strings in the set XY .
f (2) = 3 XY = X · Y = {aa, bb}{, b, ab}
f (3) = 1 = {aa, aab, aaab, bb, bbb, bbab}
f (4) = 5
f (5) = 6 2. List the strings of the set Y ∗ of length three or less.
f (6) = 4 Y ∗ = {, b, ab}∗ of length 3 or less
.. = {, b, bb, bbb, ab, bab, abb}
.
Conditional functions would also work. 3. How many strings of length 6 are there in X ∗ ?
X ∗ = {aa, bb}∗ of exactly length 6
Countability Each symbol has length 2, so there have to be 3 symbols
Set A is countable if ∃ a 1-1 correspondence between A in each string. 23 = 8 strings.
and N . i.e. Can systematically enumerate all elements in
A eventually, labeling each with a unique natural number. (a) 000 → aaaaaa
You might have to find a creative pattern to list them 1-by-1. (b) 001 → aaaabb
(c) 010 → aabbaa
E.g. Set of ± rational numbers is countable if you
enumerate as x1 , −x1 , x2 , −x2 , · · · (d) 011 → aabbbb
(e) 100 → bbaaaa

2
S
(f) 101 → bbaabb 1. L1 is regular, L2 is nonregular, and L1 S L2 is regular.
(g) 110 → bbbbaa L1 = {a, b}∗ , L2 = {an bn |n ≥ 0} → LS
1 L2 = a∗ b∗
∗ i
L1 = a , L2 = {a | i is prime} → L1 L2 = L1
(h) 111 → bbbbbb S
2. L1 is regular, L2 is nonregular, and L1 L2 is nonreg-
E.g. Let L1 = {aaa}∗ , L2 = {a, b}{a, b}{a, b}{a, b}, and ular.
L3 = L∗2 . Describe the strings that are in the languages. L1 = {a∗ }, L2 = {an bn |n ≥ 0} → L1 LS2
S

1. L2 = strings of length 4 that are any combination of L1 = {aa}, L2 = {ai | i is prime} → L1 L2 = L2


a’s and b’s. 3. L1 is regular, L2 is nonregular, and LT
T
L2 is regular.
1

2. L3 = closure of L2 , 0 or more occurrences of L2 . i.e. L1 = {a∗ }, L2 = {an bn |n ≥ 0} → L1 LT2 = a∗


empty string and strings that are a multiple of 4 with L1 = {aa}, L2 = {ai | i is prime} → L1 L2 = L1
any combination of a’s and b’s. E.g. , aaab, bbbbaaaa, S
4. L1 is nonregular, L2 is nonregular, and L1 L2 is reg-
etc. ular.
T
3. L1 L3 = intersection of L1 and L3 , which areTstrings L1 = {ai |i >S0, i is prime}, L2 = {ai |i > 0, i is not
of symbol aaa that are multiples of 12. L1 L3 = prime} → L1 L2 = a+
{aaa aaa aaa aaa}∗ L1 S= {ai |i is prime}, L2 = {ai |i is not prime} →
L1 L2 = a∗
To show a language is regular: show one option exists
E.g. Prove or disprove the following:
1. regular expression
1. If L∗ is regular, then L must be regular.
2. DFA i
False. For example, L = {a2 |i ≥ 0} is nonregu-
3. NFA lar, but L∗ is. Or L = {ai |i is prime}
4. NFA w/ -moves 2. For any language L, L∗ must be regular (discuss both
cases when L is finite and infinite).
If L is finite, it is regular.
If L is finite, then L must be regular (you can
To show a language is irregular:
find a FSM or regex). And if L is regular ⇒ L∗ is
1. Pumping lemma regular, because the Kleene star/closure is a regular
operation under closure properties. Therefore, if L is
2. Show DFA that would require infinite states finite, L∗ must be regular.
If L is infinite, then it is nonregular because you would
3. Use closure properties that relate to other nonregular
need an infinite number of states (you cannot find a
languages
FSM or regex). Therefore, if L is infinite, L∗ can be
Closure Properties of regular languages: regular or nonregular (i.e. it does not say anything).
Regular languages are closed under certain operations
Regular Expressions
(i.e. if L1 , L2 are regular, then so is resulting language).
Regular expressions (regex) are sets in a nicer notation.
Union L1 ∪ L2 = r + s
01 = 0 · 1 = {01} Concatenation: 0 followed by 1
Concatenation L1 · L2 = rs
0 + 1 = {0, 1} Union: 0 or 1
Closure L∗1 = r∗ , L+
2 =s
+
∗ ∗
0 = {0} Kleene closure: zero or more 0’s
Complement L̄ = Σ∗ − L, (L regular, so is L̄)
0+ = {0}+ Positive closure: one or more 0’s
Intersection L1 ∩ L2 = L1 ∪ L2 0? = {, 0} zero or one 0
Set difference L1 − L2 = L1 ∩ L2 (0 + 1)∗ = {0, 1}∗ all strings over {0, 1}
Sn
E.g. For any fixed n, is i=1 Li regular, where each
0∗ 10∗ 10∗ = 0∗ · 1 · 0∗ · 1 · 0∗ strings containing exactly two 1’s
Li is regular? (0 + 1)∗ 11 = {0, 1}∗ · {1} · {1} strings ending with 11
0(1 + 10)∗ + (1 + 10)∗ strings of 0’s and 1’s
∗ ∗
True.
Sn L i is regular so has regex ri . Finite languages, = {0} · {1, 10} ∪ {1, 10} not containing substring 00
so i=1 Li has regex r1 + r2 + · · · + rn .
Deterministic Finite Automata (DFA)
DFA & NFA are Finite State Machines (FSM). They can
E.g. Give examples of languages L1 and L2 over al-
only scan input once (i.e. new string requires new scan
phabet {a, b} that satisfy1 : process) and have finite memory. Unlike Turing machines.
1 Hints: Known regular languages: Σ∗ , , a∗ , a∗ b∗ , etc. Known non-

regular languages: an bn or languages with similar dependencies. DFA’s are 5-tuple: M = (Q, Σ, δ, q0 , F )

3
Q finite set of states that L(M ) = L(M 0 ).
Σ finite alphabet
q0 ∈ Q initial state M = (Q, Σ, δ, q0 , F )
F ⊆Q final states Q = {q0 , q1 , q2 , q3 }
δ transition function Q × Σ → Q
Σ = {a, b}
δ(q, a) = p where q, P are states in Q and a is symbol in Σ
L(M ) language accepted by machine M F = {q0 , q2 , q3 }
δ a b
• Deterministic means you can predict the path it
will take (unlike in NFA). q0 {q1 , q3 } ∅
• Every DFA is an NFA. q1 ∅ {q2 }
• Easy method to make a DFA that cannot contain a q2 {q1 } ∅
certain substring, is to make a DFA that has to accept that q3 {q3 } ∅
substring, and then complement the DFA (non-final states
become final and vice-versa: F̄ = Q − F ). Each state in M 0 corresponds to a subset of states from
M.
Nondeterministic Finite Automata (NFA)
NFA is a finite state machine where for each pair of state M0 = (Q0 , Σ, δ 0 , q00 , F 0 )
0
and input symbol there may be several possible next states. Q = 2Q = {∅, [q3 ], [q2 ], [q2 , q3 ], [q1 ], [q1 , q3 ], [q1 , q2 ],
This distinguishes it from DFA, where the next possible [q1 , q2 , q3 ], [q0 ], [q0 , q3 ], [q0 , q2 ], [q0 , q2 , q3 ], [q0 , q1 ],
state is uniquely determined. Although DFA and NFA
[q0 , q1 , q3 ], [q0 , q1 , q2 ], [q0 , q1 , q2 , q3 ]}
have distinct definitions, they are equivalent, in that, for 0
any given NFA, one may construct an equivalent DFA, and F = all elements in 2Q that contain a state in F
vice-versa (powerset construction). Both types of automata = {[q3 ], [q2 ], [q2 , q3 ], [q1 , q3 ], [q1 , q2 ], [q1 , q2 , q3 ], [q0 ],
recognize only regular languages. [q0 , q3 ], [q0 , q2 ], [q0 , q2 , q3 ], [q0 , q1 ], [q0 , q1 , q3 ],
[q0 , q1 , q2 ], [q0 , q1 , q2 , q3 ]}
An extension of NFA is NFA with -moves, which al-
lows a transformation to a new state without consuming
any input symbols (using  as the symbol in the transition). Now the most important part, the transition func-
tion. To complete this part, we look at the NFA state
Accepting an input is similar to that for DFA. When transitions. For example, δ 0 ([q0 ], a) = [q1 , q3 ] because
the last input symbol is consumed, the NFA accepts if and δ(q0 , a) = {q1 , q3 }.
only if there is some set of transitions that will take it to
δ 0 ([q0 ], a) = [q1 , q3 ] ...new state!
an accepting state (it’s allowed to reach a stuck state).
Equivalently, it rejects, if, no matter what transitions are δ 0 ([q0 ], b) = ∅
applied, it would not end in an accepting state. δ 0 ([q1 , q3 ], a) = [q3 ] ...new state!
0
δ ([q1 , q3 ], b) = [q2 ] ...new state!
E.g. Give the state diagram of an NFA (without - 0
δ ([q3 ], a) = [q3 ]
moves) that accepts the language (ab)∗ + a∗ and convert it
to a DFA using the standard algorithm. δ 0 ([q3 ], b) = ∅
δ 0 ([q2 ], a) = [q1 ] ...new state!
0
δ ([q2 ], b) = ∅
0
δ ([q1 ], a) = ∅
δ 0 ([q1 ], b) = [q2 ]

For an NFA with n states, the DFA could have up


to 2n states. In our case, the DFA will have 5 states
(instead of 16).
Figure 1: NFA without -moves for (ab)∗ + a∗
Pumping Lemma
E.g. Show that each of the following languages is not regular:

1. (ab)∗ +a∗ = (a·b)∗ | a∗ = 0 or more ab’s or 0 or more a’s. 1. {ai bj |i > j}

For an NFA M , there exists a DFA M 0 such Proof by contradiction (using pumping lemma):

4
Select z = an bn c2n ∈ L = uvw.
|uv| ≤ n so v consists of a’s.
u = aq , v = ar , w = as bn c2n .
q+r+s=n
Find an i such uv i w ∈/ L.
i = n + 1 → uv n+1 w = q + (n + 1)r + s + n + 2n =
q + r + s + n + 2n + nr = n + n + 2n + nr
Which is not in L because then there would be more
a’s than c’s. Thus L is not regular.
Figure 2: DFA converted from NFA
4. The set of nonpalindromes over {a, b}.

Step 1: Assume L is regular, then let n be the constant If L is regular, then so is L (the set of palin-
in the lemma. dromes).

Step 2: Select a specific string z ∈ L such that L= {w | w ∈ {a, b}∗ , w = wR }. Use pumping lemma.
|z| ≥ n. Select z = an ban = uvw. u = ai , v = aj , w = ak ban .
z = an+1 bn i+j+k =n
i+k <n
Step 3: Split z into uvw. |uv| ≤ n
By the lemma, |uv| ≤ n, thus v appears within i + j ≤ n, so n ≥ j ≥ 1.
the first n characters (v consists only of a’s). Pump v 0 times (i = 0), we get ai+k ban ∈ / L.

z = uvw = aq ar as bn where u = aq , v = ar , and Therefore L is not regular and thus neither is L.
as bn = w.
From the lemma we know: Arbitrary
q+r+s=n+1 Prove false: Show a single example where it’s false (proof
0 ≤ |q| < n by contradiction).
1 ≤ |r| ≤ n because |v| ≥ 1
0 ≤ |s| Cantor’s Theorem: For any set A, |A| < |2A |
q+s<n+1
Sufficient & Necessary Conditions: P ⇒ Q: If
Step 4: Find an i such that uv w ∈ i
/ L, violating P , then Q (P iff Q). P is a sufficient condition for Q to be
the necessary condition. true. Q is a necessary condition for P to be true.
The lemma states that for L to be regular, uv w ∈ L. • If Q is false, we can say P is false.
i

If i = 0, then uv 0 w = uw = aq + as < n + 1 ∈ / L • If Q is true, we cannot say anything about P .


(because there must be more a’s than b’s, and if one a
/ L and L is • ∅ is a language, but not a string
is missing, then it’s false). Therefore uw ∈
nonregular. •  is a not a language, but it is a string
• every language is infinite or has an infinite complement
2. The set of strings over {0, 1} with an equal number of • some languages are infinite and have an infinite comple-
0’s and 1’s. ment L = {w ∈ {0, 1} | |w| is odd}
L = {w|w ∈ {0, 1}∗ and # of 0’s = # of 1’s in w} • empty set ∅ is a subset of every language
• kleene closure of a language is not always infinite, ∅ and
Intuition says we would need infinite states and {}
thus a DFA would be impossible to build. Try pump- • concatenation of infinite language and finite language is
ing lemma. not always infinite

Select z = 0n 1n ∈ L = uvw ∈ L Context-Free Grammar (CFG)


|uv| ≤ n so v consists of 0’s. u = 0q , v = 0r , w = 0s 1n . CFG’s are used for describing the structure of programming
We know q + r + s = n and n ≥ |v| ≥ 1, so q + s < n. languages and other artificial languages. The idea is to use
Lemma says uv i w ∈ L if regular. But for “variables” for sets of strings (i.e. languages). Variables
i = 0, we have too few 0’s in our string, since are defined recursively. Every production rule is of the
uv 0 w = uw = 0q 0s 1n ∈
/ L because q + s < n. Therefore form V → w, where V is a nonterminal symbol (variable)
L is not regular. and w is a string on terminals and/or non-terminals (can
be empty).
3. {ai bj c2j |i ≥ 0, j ≥ 0}
E.g. Find CFG’s for the following languages:

5
(start w/ base case & use as many variables as needed) Leftmost derivation of string aaabb

{an bn |n ≥ 1} {an bn |n ≥ 0} S ⇒ ASB (1)


S → ab | aSb S → aSb |  S ⇒ aASB (4)
S ⇒ aaASB (4)
(a + b)∗ a {w|w ∈ (a + b) and a0 s 6= b0 s} S ⇒ aaSB = aaSB (5)
S → a | aS | bS a = b, a > b, or a < b S ⇒ aaabB (2)
S → U |V S ⇒ aaabbB (6)
Set of all production T → aT bT | bT aT |  S ⇒ aaabb = aaabb (7)
rules for CFG’s with U → T aT | T aU Rightmost derivation of the same string.
T = Σ = {a, b}, and V → T bT | T bV
S ⇒ ASB (1)
V = {A, B, C}
S ⇒ ASbB (6)
S → X→Y Set of strings over
S ⇒ ASb = ASb (7)
X → A|B |C Σ = {a, b, +, (, )}
S ⇒ Aabb (2)
Y →  | aY | bY | AY | BY | CY S → a | b | S + S | (S)
S ⇒ aAabb (4)
S ⇒ aaAabb (4)
{ai bj ck |i 6= j orj 6= k} {ai bj ck |i, j, k > 0, i = j or i = k}
S ⇒ aaabb = aaabb (5)
i > j, j < i, j > k, or j < k S → XY
S → ABC | DEF | GHI | JKL X → aXb | ab A context-free grammar is said to be ambiguous if there
exists a string that can be generated by the grammar
A → aA | a Y → cY | c
in more than one way (i.e. the string admits more than
B → aBb | ab |  Similar for i = k one parse tree or, equivalently, more than one leftmost
C → cC |  derivation). {ai bj ck |i = j or i = k} is inherently ambiguous.
Similar for i < j, j > k, j < k {an wwR an |n ≥ 0, w ∈ {a, b}∗ }
E.g. Give an unambiguous grammar equivalent to G.
It’s a palindrome
Matching (nested) paranthesis S → aSa | bSb | 
Some ambiguous grammars can be converted into un-
S → SS | (S) | () ambiguous grammars, but no general procedure for doing
this is possible just as no algorithm exists for detecting
Parse trees have the start symbol as the root, terminals as ambiguous grammars. So instead we describe what type of
leaves, and variables as interior nodes. Reading the leaves strings G produces and then try to find a grammar that (1)
from left-to-right (preorder traversal) yields the derived is equivalent to G (i.e. generates the same language), and
string. (2) is unambiguous (i.e. for every sentence of the language,
the parse tree is correct).
A derivation of a string for a grammar is a sequence
of grammar rule applications, that transforms the start G produces strings that start with an a and end with
symbol into the string. A derivation proves that the string a b and have any combination of a’s and b’s in between.
belongs to the grammar’s language.
In a leftmost derivation, the next nonterminal to S ⇒ aT b
rewrite is always the leftmost nonterminal; in a rightmost T ⇒ aT | bT | 
derivation, it is always the rightmost nonterminal.
Useless Symbols
E.g. Let G be the grammar Sometimes S derives nothing which means the language is
empty. This is the case when you cannot reach a terminal
string. To reach a terminal string you need a production of
S → ASB (1)
the form A → w.
S → ab (2) To eliminate useless symbols (1) Eliminate symbols that
S → SS (3) derive no terminal string, (2) Eliminate unreachable sym-
A → aA (4) bols.
A →  (5)
Context-Free Language (CFL)
B → bB (6) CFL is a language generated by a CFG. If CFL, give a
B →  (7) PDA or CFG. If not CFL, show with pumping lemma.

6
E.g. Show that the following languages over Σ = {a, b} are Push Down Automata (PDA)
not CFL (using pumping lemma). NOTE: Have to prove A PDA is a stack with operations push & pop. It models
for every case it can violate (not done here). a parser. In language-defining power, it’s equivalent to a
CFG. You can only have a single stack, which limits the
1. The set of strings of a’s, b’s, and c’s, with an equal power of PDA. The moves for a PDA are determined by
number of each. (1) the current state, (2) the current input symbol, and
(3) the current symbol at the top of the stack. PDA’s are
Intuition tells us it is not CFL because you would non-deterministic, so it can have a choice of next moves. In
need to count, which we cannot do. So we should with each choice, the PDA can (1) change state, and (2) change
pumping lemma that it is not CFL. There are many the top stack symbol.
cases where it will violate the language property, we M = {Q, Σ, Γ, δ, q0 , Z0 , F }
just need one. Q : finite set of states
Σ : input alphabet
Let z = an bn cn
Γ : stack alphabet
= uv i wxi y δ: transition function
|vwx| ≤ n q0 : start state (q0 ∈ Q)
|vx| ≥ 1 Z 0 : start symbol (Z0 ∈ Γ )
F : set of final states (F ⊆ Q)
v, x contains a0 s :
n n n
E.g. Construct PDA for the following languages and
z }| { z }| { z }| { state if “accept by empty stack” or “accept by final state.”
|a ·{z
· · a} b · · · b c · · · c
TIP: draw out a sample string and corresponding stack.
uvwxy

uv wxi y ∈
i
/ L if i ≥ 2 1. {w|w ∈ (a + b)+ and contains the same number of a’s
and b’s}
2. {wwR w|w ∈ Σ∗ }
We match the a’s and b’s by pushing and pop-
Intuition tells us it is not CFL, because we would need ping. If the stack ever becomes empty, we have the
more than one stack to match. So we try disproving same numbers of a’s and b’s. We will accept this
with pumping lemma. language by empty stack, so the stack will start out
with empty stack symbol Z0 and finish with no symbols.
Let z = an an an
= uv i wxi y
|vwx| ≤ n δ(q0 , a, Z0 ) = {(q0 , AZ0 )} push A on empty stack
|vx| ≥ 1 δ(q0 , b, Z0 ) = {(q0 , BZ0 )} push B on empty stack
v, x contains a s : 0 δ(q0 , a, A) = {(q0 , AA)} keep pushing A
n n n δ(q0 , b, B) = {(q0 , BB)} keep pushing B
z }| { z }| { z }| {
|a ·{z
· · a} a · · · a a · · · a δ(q0 , a, B) = {(q0 , )} ‘a’ encountered, pop B
uvwxy δ(q0 , b, A) = {(q0 , )} ‘b’ encountered, pop A
uv i wxi y ∈
/ L if i ≥ 2 δ(q0 , , Z0 ) = {(q0 , )} end of input, pop Z0

3. {ap |p is a prime}
2. {wwR |w ∈ (a + b)+ }
Intuition says it is not CFL because we would
need to calculate primes. If |Σ| = 1, then both versions This PDA will recognize the language of even
of the pumping lemma (CFL and regular languages) length palindromes (with length greater than 0) over
become the same, and we already proved it in class for Σ = {a, b}. This PDA pushes the input symbols on
the regular language example. So we can safely say the stack until it guesses that it is in the middle and
that this is not CFL. then it compares the input with what is on the stack,
popping off symbols from the stack as it goes. If it
Grammar G = (V, Σ, P, S) reaches the end of the input precisely at the time when
type 0 (unrestricted): α → β (e.g. aAa → aa) the stack is empty, it accepts. Thus it accepts by
type 1 (context-sensitive): α → β and |α| ≤ |β| iff β 6=  empty stack, and it is non-deterministic. We need two
type 2 (context-free): α is a single variable (e.g. A → ...) states (q0 for pushing, q1 for popping) because we need
type 3 (regular grammar): A → aB | a |  the same number of a’s and b’s (like in the previous

7
language), and it must be a palindrome. 1. Kleene closure (L∗ of L)

δ(q0 , a, Z0 ) = {(q0 , AZ0 )} push A 2. concatenation (L · P )


δ(q0 , b, Z0 ) = {(q0 , BZ0 )} push B 3. union (L ∪ P )
δ(q0 , a, A) = {(q0 , AA), (q1 , )} push or pop A
4. intersection (L ∩ P )
δ(q0 , b, B) = {(q0 , BB), (q1 , )} push or pop B
δ(q0 , a, B) = {(q0 , AB), (q1 , )} push or pop A They are not closed (i.e. not guaranteed to be RE) under:
δ(q0 , b, A) = {(q0 , BA), (q1 , )} push or pop B 1. set difference (L − P )
δ(q1 , a, A) = {(q1 , )} pop A
2. complementation (L̄ of L)
δ(q1 , b, B) = {(q1 , )} pop B
δ(q1 , a, B) = {(q1 , )} pop A L1 = {ai bj ck |i, j, k > 0, i = j or i = k} CFL
δ(q1 , b, A) = {(q1 , )} pop B L2 = {ai bj ck |i = k, j > 2} CFL
L3 = {0n |n is multiple of 101} REG
δ(q1 , , Z0 ) = {(q1 , )} end of input, pop Z0 L4 = {0i 1j 2k |i + j = k} CFL
L5 = {< M, w > |w ∈ Σ∗ , M = DF A, M accepts w} REC
Turing Machine (TM) L6 = Σ REG
# of TM is infinitely countable, # of languages is infinitely L7 = AT M Halting Problem R.E.
uncountable, so there are language that have no TM (i.e. L8 = finite set REG
uncountable unsolvable problems). L9 = union of any finite # of R.E. languages R.E.
TODO: Creating a TM and formal definitions L10 = {wxwR |w, x ∈ (0 + 1)+ } REG
Undecidable Problems L11 = {} = ∅ = empty language REG
E.g. Let ALLCF G = {< G > | G is a CFG and L(G) = Σ∗ }. L12 = {ap+1 |p is prime} REC
It is known that this language is undecidable. De- L13 = {xy|x ∈ L and y ∈ / Σ∗ − L}, L is regular REG
fine EQCF G = {< G, H > | G and H are CFG’s and L14 = intersection of any finite # of R.E. languages R.E.
L(G) = L(H)}. Show that EQCF G is undecidable. L15 = {ai bj |i, j > 0} REG
L16 = {w|w ∈ {a, b}∗ , w is a palindrome} CFL
Use reduction. Show we can solve ALLCF G , then we can L17 = {< M > |M is a TM, M does not accept < M >} N.R.E.
solve EQCF G . But ALLCF G is known to be unsolvable, so L18 = L(G), G : S → Sa|Sb = ∅ (no terminal string) REG
then EQCF G is also unsolvable since ALLCF G ≤ EQCF G . L19 = {w|w{a, b}∗ and # a0 s =# b0 s} CFL
ALLCF G reduces to EQCF G , so EQCF G is at least as hard L20 = {ai bj ak |i = j = k} REC
as ALLCF G .

E.g. Given A = {< M > | M is a finite automaton


and L(M ) = Φ} where M is some encoding of the machine
M . Is A Turing-decidable?

A FSM of n states accepts strings of length < n, so


brute force! Generate all strings of length < n, see if any is
accepted. Guaranteed to halt (Turing-decidable).

TODO: Formal proofs


Chomsky Hierarchy of Languages
Regular: · regular expression
· FSM (DFA, NFA, NFA w/ -moves)
· closure properties
Not Regular: · pumping lemma (uv i w)
· DFA w/ infinite states
· intuition says calculate or inf. memory
CFL: · PDA (e.g. palindromes)
· CFG (easier)
Not CFL: · pumping lemma (uv i wxi y)
Recursive: · decidable (algorithm/TM always terminates)
Rec. Enumerable: · recognizable (terminates iff legal input)
Non-Rec. Enum.: · unsolvable (doesn’t fit elsewhere)
Recursively Enumerable languages are closed under:

You might also like