Theory of Computation
Theory of Computation
COMPUTATION
Mosul University/College of Computer and Math sciences / Department of Cyber Security
Subject: Theory of computation
Class: 2nd
Semester:1st
Year: 2023-2024
Prepared by
Dr. Ayad Alezzi
Outline
• Set, Strings, alphabets and language.
• Regular expression.
• Grammars: Definition, The Chomsky hierarchy of languages.
• The regular grammars and regular languages.
• closure properties of regular sets (union, catenation and kleene closure).
• Finite state automata ,deterministic and nondeterministic finite state automata.
• Equivalence between deterministic and nondeterministic finite state automata.
• Finite state automata with output(The Moore and Mealy Machine).
• Context-free grammars and languages, derivation trees.
• Simplification of context-free grammars, the Chomsky and Greibach normal
form.
• Pushdown Automata
Introduction to Set Theory
Vocabulary
• A set is any well define collection of Objects
A= 1,2,3,4,5,6
Give a verbal description
“A is the set of all integers from 1 to 6, inclusive”
• Give a mathematical inclusion rule
A= Integers x 1 x 6
Some Special Sets
• The Null Set or Empty Set. This is a set with no elements, often
symbolized by
or {}
• The Universal Set. This is the set of all elements currently under
consideration, and is often symbolized by
U وﻏﺎﻟًﺑﺎ ﻣﺎ ﯾﺗم ﺗرﻣﯾزھﺎ ﺑـ،ھذه ھﻲ ﻣﺟﻣوﻋﺔ ﻛل اﻟﻌﻧﺎﺻر اﻟﺗﻲ ﯾﺗم اﻟﻧظر ﻓﯾﮭﺎ ﺣﺎﻟًﯾﺎ
U
Universal Sets
• The universal set is the set of all things pertinent to a given discussion
and is designated by the symbol U
Example: اﻟﻣﺟﻣوﻋﺔ اﻟﺷﺎﻣﻠﺔ ھﻲ ﻣﺟﻣوﻋﺔ ﻛل اﻷﺷﯾﺎء ذات اﻟﺻﻠﺔ ﺑﻣﻧﺎﻗﺷﺔ ﻣﻌﯾﻧﺔ وﯾﺗم اﻹﺷﺎرة إﻟﯾﮭﺎ ﺑﺎﻟرﻣزU
U = {all students at Brandeis}
Some Subsets:
A = {all Computer Technology students}
B = {freshmen students} طﻼب اﻟﺳﻧﺔ اﻷوﻟﻰ
C = {sophomore students} طﻼب اﻟﺳﻧﺔ اﻟﺛﺎﻧﯾﺔ
Find the Subsets
• What are all the subsets of {3, 4, 5}
{} or Ø
{3,4,5}
Venn Diagrams
• Venn diagrams show relationships between sets and their elements
Sets A & B
Universal Set
Venn Diagram Example
Set Definition
U={1, 2, 3, 4, 5, 6, 7, 8}
Set Complement
~A or A′
• “A complement,” or “not A” is the set of all elements not in A.
Universal set U =
What is the complement of set A?
Combining Sets – Set Union
A B
Combining Sets – Set Intersection
• A B
• “A intersect B” is the set of all elements that are in
both A and B.
• This is similar to the logical “and”
Venn Diagrams
• Venn Diagrams use topological areas to stand for sets. I’ve done this one for
you.
A B
A B
Venn Diagrams
A B
A B
Examples
A {1, 2,3} B {3, 4,5,6}
A B {3}
L5= all words over {0,1} such that each word contains an odd number of
0’s.
Ans:
G5= ({E,O}, {0,1}, E , P5}
Where P5 is
E → 0O | 1E
O → 0E |1O | ϵ
Example 2 ……… Cont.
L6= all words over {0,1} such that each word contains an even number of
1’s and an odd number of 0’s
Ans:
• Even Even eg ( 0110, 1001, 00 , 11}
• Even Odd eg (110, 01010, 0 , 01100)
• Odd Even eg (100, 10011, 1101101, 11100 , 1)
• Odd Odd eg (10 , 1101,001011)
Type 3:
A grammar G is of type 3 (regular grammar) if its productions are of the form A →
aB or A → a, where a ∈ T and A, B ∈ N. A production of the form S → ε can also be
accepted if the start symbol S does not occur in the right-hand side of any
production.
1.
G1 = (N1, T1, P1, S1), where N1 = {S1, A, B, C} , T1 ={a, 0,1}.
Elements of P1 are:
S1 → ACA
AC → AACA | ABa |AaB
B → AB | A
A → 0|1
Example: …………….cont.
2.
G2 = (N2, T2, P2, S), where N2 = {S, A, B} , T2 ={a, +, *, (, )}.
Elements of P2 are:
S → S+A |A
A → A*B | B
B → (S) | a
Example: ……………cont.
3.
G3 = (N3, T3, P3, S), where N3 = {S, A, B} , T3 ={a, b}.
Elements of p3 are:
S →aA
A → aB | a
B → aB | bB | a | b
Finite State Automata
• Automata – What is it?
The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An
automaton (Automata in plural) is an abstract self-propelled computing device which follows a
predetermined sequence of operations automatically.
An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine
(FSM).
• Classifier
A classifier has more than two final states and it gives a single output when it terminates.
Transducer
An automaton that produces outputs based on current input and/or previous state is called a
transducer.
Transducers can be of two types -
Mealy Machine - The output depends both on the current state and the current input.
Moore Machine - The output depends only on the current state.
Examples (Finite Automata as an Accepter)
• Example 1:
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.
Solution:
The FA will have a start state q0 from which only the edge with input 1 will go to the
next state.
In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will
reach to state q2 which is the final state. In state q2, if we read either 0 or 1, we will
go to q2 state or q1 state respectively. Note that if the input ends with 0, it will be in
the final state.
• Example 2:
• In the given solution, we can see that only input 101 will be accepted.
Hence, for input 101, there is no other path shown for other input.
• Example 3:
Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1's.
Solution:
This FA will consider four different stages for input 0 and input 1. The stages could
be:
Here q0 is a start state and the final state also. Note carefully that a symmetry of 0's
and 1's is maintained. We can associate meanings to each state as:
q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
• Example 4:
Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive
0's.
Solution:
The strings that will be generated for this particular languages are 000, 0001,
1000, 10001, .... in which 0 always appears in a clump of 3. The transition
graph is as follows:
• Example 5:
• Problem Statement
Let X = (Q , ∑, δ , q , F ) be an NDFA which accepts
the language L(X). We have to design an
equivalent DFA Y = (Q , ∑, δ , q , F ) such that L(Y) =
L(X). The following procedure converts the
NDFA to its equivalent DFA -
• Algorithm
Input - An NDFA
Output - An equivalent DFA
Step 1 - Create state table from the given NDFA.
Step 2 - Create a blank state table under possible input alphabets for
the equivalent DFA.
Step 3 - Mark the start state of the DFA by q0 (Same as the NDFA).
Step 4 - Find out the combination of States {Q , Q ,... , Q } for each
possible input alphabet.
Step 5 - Each time we generate a new DFA state under the input
alphabet columns, we have to apply step 4 again, otherwise go to
step 6.
Step 6 - The states which contain any of the final states of the NDFA
are the final states of the equivalent DFA.
Example
Let us consider the NDFA shown in the figure below.
Step 1 - Create state table from the given NDFA. ∅
Current
State 0 1
a
b
c
d
e
Step 2 - Create a blank state table under possible input
alphabets for the equivalent DFA.
Step 3 - Mark the start state of the DFA by q0 (Same as the
NDFA).
Step 4 - Find out the combination of States {Q , Q ,... , Q } for
each possible input alphabet.
Step 5 - Each time we generate a new DFA state under the
input alphabet columns, we have to apply step 4 again,
otherwise go to step 6.
Step 6 - The states which contain any of the final states of the
Current NDFA are the final states of the equivalent DFA.
state 0 1
State 0 1
[a]
→a { a, b , c , d , e} {d , e}
b {c} {e}
c ∅ {b}
d {e} ∅
+ e ∅ ∅
state 0 1
+ [d,e] [e] ∅
+ [e] ∅ ∅
+ [c, e] ∅ [b]
[c] ∅ [b]
state 0 1
[d,e] [e] ∅
a { a, b , c , d {d , e}
, e} [b,d,e] [c,e] [e]
b {c} {e}
[e] ∅ ∅
c ∅ {b} [c, e] ∅ [b]
d {e} ∅ [b] [c] [e]
e ∅ ∅ [c] ∅ [b]
Finite State Automata with Output
Till now we have seen the finite automata, DFA and NFA. Now we will see finite
automata with output.
It will be clear once you see one example
Moore machine shows output on state itself so if input ε then also we will get one
output.
Below are parameters for Moore machine:
Parameter Name Description
Σ Input Alphabet
q0 Initial State
Δ Output Alphabet
Σ Input Alphabet
q0 Initial State
Δ Output Alphabet
We have taken string 'baabaa'. So as you can see from the below diagram that we
will get '1' twice.
Example:
design mealy machine for 1's complement.
Example:
Design a mealy machine for 2's complement
Top-down Approach –
Starts with the starting symbol S
Goes down to tree leaves using productions
Bottom-up Approach -
Starts from tree leaves
Solution
(1) Since S appears in R.H.S, we add a new state S’ and S’ →S is added to the
production set and it becomes -
S’ →S, S→ ASA | aB, A → B | S, B→b|∈
Current state is q
Current input symbol is a
Symbol currently on top of the stack z
Move to state pi from q
Replace z with γi on the stack (leftmost symbol on top)
Move the input head to the next input symbol
Two types of PDA transitions --------cont.
2nd Type
δ(q, ε, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}
Current state is q
Current input symbol is not considered
Symbol currently on top of the stack z
Move to state pi from q
Replace z with γi on the stack (leftmost symbol on top)
No input symbol is read
:
Example1: 0n1n,
n>=0
M = ({q1, q2}, {0, 1}, {L, #}, δ, q1, #, Ø)
δ:
(1) δ(q1, 0, #) = {(q1, L#)} // stack order: L on top, then # below
(2) δ(q1, 1, #) = Ø // illegal, string rejected, When will it happen?
(3) δ(q1, 0, L) = {(q1, LL)}
(4) δ(q1, 1, L) = {(q2, ε)}
(5) δ(q2, 1, L) = {(q2, ε)}
(6) δ(q2, ε, #) = {(q2, ε)} //if ε read & stack hits bottom, accept
(7) δ(q2, ε, L) = Ø // illegal, string rejected
(8) δ(q1, ε, #) = {(q2, ε)} // n=0, accept
Goal: (acceptance)
Read the entire input string
Terminate with an empty stack
Informally, a string is accepted if there exists a computation that uses up all
the input and leaves the stack empty.
PDA Example: Lwwr {ww | w (0 1) }
R *
S 0 S 0 | 1S1 | e
One PDA for Lwwr is given on the following slide...
Design a non-deterministic PDA for accepting the language L
= {wwR | w ∈ (a, b)*}
L = {aa, bb, abba, aabbaa, abaaba, ......}
δ(q0, a, z)=(q0, az)
δ(q0, a, a)=(q0, aa)
δ(q0, b, z)=(q0, bz)
δ(q0, b, b)=(q0, bb)
δ(q0, a, b)=(q0, ab)
δ(q0, b, a)=(q0, ba)
δ(q0, a, a)=(q1, ∈)
δ(q0, b, b)=(q1, ∈)
δ(q1, a, a)=(q1, ∈)
δ(q1, b, b)=(q1, ∈)
δ(q1, ∈, z)=(qf, z)
Where, q0 = Initial state
qf = Final state
∈ = indicates pop operation
A Graphical Notation for PDA’s
1. The nodes correspond to the states of the PDA.
2. An arrow labeled Start indicates the unique start state.
3. Doubly circled states are accepting states.
4. Edges correspond to transitions in the PDA as follows:
5. An edge labeled (ai, tm)/tn from state q to state p means
that d(q, ai, tm) contains the pair (p, tn), perhaps among
other pairs.
Graphical representation of PDA (wwR )
Design a deterministic PDA for accepting the language
L = { wcwR w ∈ (a, b)+}, i.e.,
L={aca, bcb, abcba, abbcbba, aacaa, bbcbb, .......}
δ(q0, a, z) = (q0, az)
δ(q0, a, a) = (q0, aa)
δ(q0, b, z) = (q0, bz)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
δ(q0, c, a) = (q1, a)
δ(q0, c, b) = (q1, b)
δ(q1, a, a) = (q1, ∈)
δ(q1, b, b) = (q1, ∈)
δ(q1, ∈, z) = (qf, z)
M(Q,1,1)->(Q,e)
M(Q,c,c)->(Q,e)
Convert the following CFG to PDA
• Start P, Final Q, {+,*,i,(,)} , {+,*,i,(,), E,T,A,F,B},
E->TA ɛ, transition function M
M(Q,+,+)->(Q, ɛ) Stack Input State
A->+TA M(P, ɛ, ɛ)->(Q,E)
M(Q,*,*)->(Q, ɛ)
i+i P
E i+iI Q
A->ɛ M(Q, ɛ,E)->(Q,TA) M(Q, i , i )->(Q, ɛ)
AT i+i Q
M(Q, ɛ,A)->(Q,+TA) M(Q,(,( )->(Q, ɛ) ABF i+i Q
T->FB M(Q, ɛ,A)->(Q, ɛ) M(Q,),) )->(Q, ɛ)
ABi i+i Q
AB +i Q
B->*FB M(Q, ɛ,T)->(Q,FB) A +i Q
AT+ +i Q
B-> ɛ M(Q, ɛ,B)->(Q,*FB) AT i Q
ABF i Q
F->i M(Q, ɛ,B)->(Q, ɛ)
ABi i Q
M(Q, ɛ,F)->( Q, i) AB Q
F->(E) M(Q, ɛ,F)->(Q, (E) )
A Q
Q
Thank you