Fait Ull
Fait Ull
Arabian proverb
Chapter 1: Sets, Relations and
Languages
Recommended Readings: Textbook + A Basis for Theo-
retical Computer Science by M. A. Arbib, A. J. Kfoury,
R N. Moll.
If A ⊆ B and B ⊆ A then A = B.
Set operations: Union, Difference, In-
tersection
Idempotency A∪A=A
A∩A=A
Distributivity A ∪ (B ∩ C) = (A ∪ B) ∩
(A ∪ C)
A∩(B ∪C) = (A∩B)∪(A∩
C)
Proof
1. To show A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C), we
show A ∪ (B ∩ C) ⊆ (A ∪ B) ∩ (A ∪ C) and
(A ∪ B) ∩ (A ∪ C) ⊆ A ∪ (B ∩ C).
(a) Let x ∈ A ∪ (B ∩ C).
From x ∈ A ∪ (B ∩ C), it follows: x ∈ A or
x ∈ B ∩ C.
If x ∈ A then x ∈ A ∪ B and x ∈ A ∪ C.
Hence x ∈ (A ∪ B) ∩ (A ∪ C).
If x ∈ B ∩ C then x ∈ B and x ∈ C. If
x ∈ B then x ∈ A ∪ B. If x ∈ C then
x ∈ A ∪ C. Therefore if x ∈ B ∩ C then
x ∈ (A ∪ B) ∩ (A ∪ C).
Hence from x ∈ A or x ∈ B ∩ C we can
conclude x ∈ (A ∪ B) ∩ (A ∪ C). Therefore
we have proved that if x ∈ A ∪ (B ∩ C) then
x ∈ (A ∪ B) ∩ (A ∪ C).
(b) Let x ∈ (A ∪ B) ∩ (A ∪ C). To show x ∈
A ∪ (B ∩ C).
From x ∈ (A ∪ B) ∩ (A ∪ C), it follows: x ∈
A ∪ B and x ∈ A ∪ C. Hence (x ∈ A or
x ∈ B) and (x ∈ A or x ∈ C). Hence there
are four cases: (x ∈ A) or (x ∈ A and x ∈ B)
or (x ∈ A and x ∈ C) or (x ∈ B and x ∈ C).
It follows that x ∈ A or (x ∈ B and x ∈ C).
That means x ∈ A ∪ (B ∩ C).
2. Exercise.
Absorption A ∩ (A ∪ B) = A
A ∪ (A ∩ B) = A
Proof Exercise.
DeMorgan’s Laws
Proof
1. Suppose there is a set U such that all sets A,B,C
are subsets of U. Hence
A \ (B ∪ C) = A ∩ (B ∪ C) = A ∩ (B ∩ C)
= (A ∩ B) ∩ (A ∩ C) = (A \ B) ∩ (A \ C)
2. Exercise.
S
If S is a collection of sets then S is the set
whose elements are the elements of the sets in S.
Example S = {a, b, c}
R×R
where R is the set of all real numbers.
Example of functions:
The integer addition +: N×N → N is a func-
tion from N×N to N
Proof Exercise.
A relation R ⊆ A × A is antisymmetric if
whenever (a, b) ∈ R and a 6= b then (b, a) 6∈ R
g(n + 1) f (n + 1)
≤
g(n) f (n)
Then for any n ≥ 0: g(n) ≤ f (n).
The pigeon principle If A, B are finite sets
and |A| > |B|. Then there is no one-to-one func-
tion from A to B.
Algorithms
What is an algorithm
Example of an algorithm:
Initially S := ∅
for i = 1,..., n do
for each i-tuple (b1, . . . , bi) ∈ Ai do
if (b1, . . . , bi) is a path in R then add (b1, bi)
to S
n + n2 + . . . + nn
that belongs to O(nn+1) where n is the number
of elements of A. This algorithm is therefore not
efficient.
An efficient algorithm:
Definition
1. Let f be a function from N into N . The order
of f, denoted by O(f ) is the set of all functions
g : N → N such that there are positive natural
numbers c, d > 0 such that for each n ∈ N
g(n) ≤ c.f (n) + d
^
2. We write f _ g iff f ∈ O(g) and g ∈ O(f ).
^
Lemma _ is a equivalence relation.
Proof Exercise.
g(n + 1) f (n + 1)
≤
g(n) f (n)
Then g ∈ O(f )
Proof Let c be a number such that
g(n0) ≤ cf (n0)
From an earlier exercise:
∀n ≥ n0 : g(n) ≤ c.f (n)
Let d = g(n0). We can prove that for each n ∈ N :
g(n) ≤ cf (n) + d
Theorem Let r > 1. Then
1. ni ∈ O(rn)
2. rn 6∈ O(ni)
Proof
1. It follows immediately from lemma 1. (Elabo-
rate)
^ ^
2. Suppose rn ∈ O(ni). Hence ni _ rn _ ni+1.
Contradiction to lemma 1. (Elaborate)
Alphabet and Languages
An alphabet is a finite set of symbols.
L1 ◦ L2 = {x ◦ y | x ∈ L1, y ∈ L2}
– Denote
1. L0 = {e},
2. L1 = L
3. L2 = L ◦ L
4. Ln+1 = Ln ◦ L
The Kleen star of a language L, denoted by
L∗, is
L∗ = {w1 ◦ w2 ◦ . . . ◦ wn | wi ∈ L, n ≥ 0}
= L0 ∪ L1 ∪ L2 ∪ . . . ∪ Ln ∪ . . .
Notes ∅∗ = {e}
Define
L+ = {w1 ◦ w2 ◦ . . . ◦ wn | wi ∈ L, n ≥ 1}
Regular Languages
∗
Since Σ ∗ is countable infinite, 2Σ is uncountable
infinite.
Question: w ∈ L?
Algorithm:
s : x := get-next-symbol;
if x := end-of-file then accept;
else if x := a then goto s;
else if x := b then goto q;
q : x := get-next-symbol;
if x := end-of-file then reject;
else if x := a then goto s;
else if x := b then goto q;
s : x := get-next-symbol;
if x := end-of-file then accept;
else if x := a then goto s;
else if x := b then goto q;
q : x := get-next-symbol;
if x := end-of-file then reject;
else if x := a then goto s;
else if x := b then goto q;
Fig. 1.
Fig. 2.
Fig. 3.
1. K = {s, q}
2. Σ = {a, b}
3. F = {s}
4. δ : K × Σ −→ K
– Σ is an alphabet
Fig. 4.
Fig. 5.
M = (K, Σ, δ, s, F ):
– K = {s, q}
– Σ = {a, b}
– F = {s}
– ∆ ⊆ K × (Σ ∪ {e}) × K:
– K 0 = 2K
– s0 = E(s)
–
δ 0 : K 0 × Σ −→ K 0
[
0
δ (Q, σ) = {E(p) | p ∈ K and (q, σ, p) ∈ ∆}
q∈Q
– F 0 = {Q ⊆ K | Q ∩ F 6= ∅ }
Property For each w ∈ Σ ∗:
(q, w) `∗M (p, e) iff (E(q), w) `∗M 0 (P, e)
for some P ⊆ K such that p ∈ P .
w ∈ L(M ) iff
w ∈ L(M 0).
Example Let Σ = {a1, . . . , an}, n ≥ 2 and
M = (K, Σ, ∆, s, F ) where
– F = {q1, . . . , qn}
– ∆ = {(s, e, qi) | 1 ≤ i ≤ n} ∪
{(qi, σ, qi) | 1 ≤ i ≤ n, σ 6= ai }
2. concatenation
3. Kleen star
4. complementation
5. intersection
Theorem 7. A language is regular iff it is ac-
cepted by a finite automata
Proof From the definition of regular languages,
it is clear that every regular language is accepted
by a finite automata (exercise).
It is clear
[
L(M ) = R(1, j, n)
qj ∈F
Now we want to show that R(i, j, k) are regular.
We prove this by induction on k.
R(i, j, k) =
R(i, j, k−1) ∪ R(i, k, k−1)◦R(k, k, k−1)∗◦R(k, j, k−1)
Pumping Theorem
Example
Example
is not regular.
STATE MINIMIZATION
Problem: Given a regular language L, design
a finite automaton M such that L = L(M) and M
has as few states as possible, i.e. for each finite au-
tomaton M 0 that accepts L, the number of states
of M’ is greater or equal the number of states of M
xz ∈ L iff yz ∈ L
.
It is obvious that for all x, y ∈ Σ ∗ and a ∈ Σ,
x ≈L y implies xa ≈L ya
Theorem 9. For any deterministic finite au-
tomaton M = (K, Σ, δ, s, F ) and for any string
x, y ∈ Σ ∗, if x ∼M y then x ≈L(M ) y.
Let M = (K, Σ, δ, s, F ) be a deterministic fi-
nite automaton and q ∈ K and Lq be the set of
all strings x such that (s, x) `∗M (q, e). Further for
each x ∈ Σ ∗, let [x]M be the equivalent class of x
wrt ∼M .
Theorem 10.
L = L(M )
Question Given a deterministic finite automa-
ton M = (K, Σ, δ, s, F ), how to construct a deter-
ministic finite automaton M 0 such that L(M ) =
L(M 0) and M 0 has a minimal number of states.
– Define
– For p, q ∈ K, define
p ≡n q iff L0p,n = L0q,n
It is obvious that
≡0 ⊇ ≡1 ⊇ . . . ⊇ ≡n ⊇ . . . ⊇ ≡
Because K is finite, there is n such that
≡n = ≡n+1 = ≡
Lemma 2. For any two states p,q and any num-
ber n ≥ 0, p ≡n+1 q iff following conditions
hold:
– p ≡n q
– for each a ∈ Σ: δ(p, a) ≡n δ(q, a)
ALGORITHMS FOR FINITE
AUTOMATA
Theorem 11. 1. There is an exponential algo-
rithm which given a nondeterministic finite
automaton, constructs an equivalent determin-
istic finite automaton.
2. There is a polynomial algorithm which given
a deterministic finite automaton, constructs
an equivalent deterministic finite automaton
with a minimal number of states.
3. There is a polynomial algorithm which given
two deterministic finite automata, decides whether
they are equivalent.
S0 := E(s); n := 0;
if σ 6= end-of-file then
S
Sn := { E(q) | ∃p ∈ Sn−1 : (p, σ, q) ∈ ∆ }
}
until σ = end-of-file
– We write
G ∗
w =⇒ v
iff there is a derivation of v from w.
2. If A −→ e is a rule in G, then
Fig. 6.
3. If
Fig. 7.
L
– We write x =⇒ y if the nonterminal symbol
being replaced is the leftmost nonterminal sym-
bol in the string, i.e
x = wAv, y = wuv where w ∈ Σ ∗, v ∈ V ∗,
A → u ∈ R.
1. A ⇒∗ w
– G1 = (V1, Σ, R1, E)
• V1 = Σ ∪ {T, F, E}
• R1 = { E −→ E + T E −→ T
T −→ T ∗ F T −→ F
F −→ (E) F −→ x }
– G2 = (V2, Σ, R2, E)
• V2 = Σ ∪ {E}
• R2 = { E −→ E + E E −→ E ∗ E
E −→ (E) E −→ x }
G1 is unambiguous in the sense that for each
string w ∈ L(G1), there is exactly one parse tree
that yields w. On the contrary, G2 is ambiguous
as there are distinct parse trees yielding the same
string.
– K = {s, p, f }, F = {f }
Fig. 9.
A configuration of a pushdown automata is a
triple
(q, w, v)
where q ∈ K, w ∈ Σ ∗ and v ∈ Γ ∗
We write
(q, w, v) `∗M (q 0, w0, v 0)
if there is a sequence
(q1, w1, v1) `M . . . `M (qn, wn, vn)
such that (q, w, v) = (q1, w1, v1), (qn, wn, vn) =
(q 0, w0, v 0).
A string w is said to be accepted by M =
(K, Σ, δ, s, F ) if and only if there exists a state
q ∈ F such that (s, w, e) `∗M (q, e, e)
– Construct E = {A | A ⇒∗ e} as follows:
• E := ∅
• While there is a rule A → α with α ∈ E ∗ and
A 6∈ E
do E := E ∪ {A}.
– Removing e-rules:
• Delete all rules of the form A → e
• For each rule of the form
A → BC or A → CB where B ∈ E
add the rule A → C
– Construct for each A, D(A) = {B | A ⇒∗ B}
as follows:
• D(A) = {A}
• While there is a rule B → C with B ∈ D(A)
and C 6∈ D(A)
do D(A) := D(A) ∪ {C}.
– N [i, i] = {xi}
C ∈ N [s + 1, j] }
w ∈ L(G) iff w ∈ N [1, n].
Example Translate the grammar
S → (S) S → SS S→e
into Chomski normal form
S → SS S → ()
S → (S1 S1 → S)
Fig. 10.
TURING MACHINES
M = (K, Σ, δ, s, H) with
– K = {s, q0, q1, y, n}, H = {y, n}
– Σ = {a, b}
– δ : (K − H) × Σ −→ K × (Σ ∪ {←, →})
q σ δ(q, r)
s t q0 , →
q0 t y, t
q0 a or b q1, →
q1 t n, t
q1 a or b q0, →
M accepts aa:
M rejects a:
Notations:
We often write
– a for Ma
– L for M←
– R for M→
– Lt: Finds the first blank square to the left of the
currently scanned symbol
– Rt: Finds the first blank square to the right of
the currently scanned symbol
An initial configuration is of the form (s, .tw)
Definition 10. Given is a Turing machine M =
(K, Σ, δ, s, H) with H = {y, n}. Any halting
configuration whose state is y is said to be an
accepting state while a halting configuration whose
state is n is said to be a rejecting state.
Fig. 11.
Fig. 12.
Example Eeach deterministic finite automaton could
be ”simulated” by a TM.
| B | t | t | ...
↑
σ stands for a or b
x stands for a or b or t
y stands for a or b or t
Extending Turing machines with more than one
tape does not increase the power of the machines.
G
– We write u −→ v for any rule (u, v) ∈ R.
– We write
G ∗
w =⇒ v
iff there is a derivation of v from w.
G = (V, Σ, R, S) where
– V = {S, A, B, C, Ta, Tb, Tc, a, b, c}
– Σ = {a, b, c}
S → ABCS S → Tc
CA → AC BA → AB CB → BC
CTc → Tcc
Ta → e
s q00
p q01
q q10
h q11
t a000
B a001
← a010
→ a011
0 a100
1 a101
The representation ”M ” is the following string:
(q00, a000, q01, a011)(q01, a100, q01, a011)(q01, a101, q01, a011)
(q01, a000, q10, a100)(q10, a100, q10, a011)(q10, a101, q10, a011)
(q10, a000, q11, a000)
The universal Turing machine U is a ma-
chine which uses the encodings of other machine to
direct its operations.
p(|x|)+1
(s, .tx) `M C
Reachability Problem
Given a directed graph G ⊆ V × V where V =
{v1, . . . , vn} and two nodes vi, vj , is there a path
from vi to vj ?
Independent Set
Given an undirected graph G and an integer
K ≥ 2, is there a set of nodes C with |C| ≥ K
such that for all vi, vj ∈ C, there is no edge be-
tween vi and vj ?
Boolean Satisfiability
2-satisfiability is in P.
p(|x|)+1
(s, .tx) `M C
Theorem 31. P ⊆ NP
Open Problem P = NP ?
N P - Completeness
Fig. 14.