Lab Manual: Theory of Computation Cs-501
Lab Manual: Theory of Computation Cs-501
Lab Manual: Theory of Computation Cs-501
EXP.NO AIM
1. Design a Program for creating machine that accepts three consecutive one.
2. Design a Program for creating machine that accepts the string always ending with
101.
5. Design a program for creating a machine which accepts string having equal no. of
1’s and 0’s.
6. Design a program for creating a machine which count number of 1’s and 0’s in a
given string.
10. Design a Program to create PDA machine that accept the well-formed parenthesis.
11. Design a PDA to accept WCWR where w is any string and WR is reverse of that
string and C is a Special symbol.
12. Design a Turing machine that’s accepts the following language a n b n c n where
n>0
Experiment 01
AIM -Design a Program for creating machine that accepts three consecutive one
CONCEPT:DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts
or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects
otherwise.
Here, according to this problems tuples are
Q={q0,q1,q2,q3}
∑= {0,1}
q ={q0}
F={q3}
Input : 0101
Output : No
Explanation : does not end with 101
OUTPUT
FIG 1.1
RESULT- The above is the desired finite automata which accepts three consecutive 1’s
Experiment 02
AIM -Design a Program for creating machine that accepts the string always ending with 101
CONCEPT:DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts
or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects
otherwise.
∑= {0,1}
q ={q0}
F={q3}
Examples:
Input : 010101
Output : Yes
Explanation : end with 101
Input : 10100010
Output : No
Explanation : end with 010
OUTPUT -
FIG 2.1
FIG 2.2 The results accepted by the automata in FIG 2.1
RESULT – The above is the required automata which accepts the string ending with 101.
Experiment 03
AIM -Design a Program for Mode 3 Machine
CONCEPT:DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts
or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects
otherwise.
∑= {0,1,2,3,4,5,6,7,8,9}
q ={q0}
F={q0}
Δ ={(q0,0)=q0, ( q0,1)=q1, ( q1,0)=q2, (q1,1)=q1, ( q2,0)=q0, (q2,1)=q3, (q3,0)=q2, (q3,1)=q1 }
Examples:
Input: 14
Output: NOT ACCEPTED
Input: 63
Output: ACCEPTED
OUTPUT
FIG 3.1
FIG 3.2 the strings accepted by the automata in FIG 3.1
RESULT – The automata is the mod 3 machine.
Experiment 04
AIM - Design a program for accepting decimal number divisible by 2
THEORY:DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts or
rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects otherwise.
Examples:
Input : 010101
Output : Yes
Explanation :
divisible by 2
Input : 10100010
Output : No
Explanation : not divisible by 2
Here, according to this problems tuples are
Q={q0,q1 }
∑= {0,1,2,3,4,5,6,7,8,9}
q ={q0}
F={q3}
Δ ={(q0,0,2,4,6,8)=q0, ( q0,1,3,5,7,9)=q1,
(q1,0,2,4,6,8)=q0, ( q1,1,3,5,7,9)=q1,
OUTPUT –
FIG 4.1
FIG 4.2 Numbers Accepted by the Finite Automata in FIG 4.1
RESULT - The above is the required finite automata which accepts decimal numbers divisible by 2 .
Experiment05
AIM- Design a program for creating a machine which accepts the string having equal number of 0’s
and 1’s.
THEORY: -
The language generated by the above problem will be- L = {01, 10, 1100, 0011, 100110, …………}.
Here, the number of 0’s and 1’s is same in all the string, but we need not to maintain any order of
0’s and 1’s. Thus, our state diagram will contain only an initial state and a final state. The count
of 0’s and 1’s is maintained by stack. We will take 3 stack alphabets: τ = {0, 1, Z}
Where, τ is the set of all stack symbols
Z = Stack start symbol
Now, if ‘0’ is present in the top of the stack and ‘1’ comes then pop the ‘0’ from the stack. And if ‘1’
present in the top of the stack and ‘0’ comes then pop the ‘1’ from the stack.
So, at the end if the stack becomes empty then we can say that the string is accepted by the PDA.
OUTPUT
FIG 5.1
FIG 5.2 Diagram represents the strings accepted by the machine in FIG 5.1
RESULT – The above is the desired machine which accepts equal number of 0’s and 1’s.
Experiment06
AIM - Design a program for creating a machine which counts number of 1’s and 0’s in a given string.
THEORY - Moore machine is a 6-tuple machine: -
M = (Q, ∑, Δ, , q0, λ)
∑: - finite set of input symbols = {0, 1}
Δ: - finite set of output state = {a, b} Q-
finite non-empty set of states = {q0, q1, q2}
q0 : - initial state
: - transition function
:Qx∑->Q
λ: - output function : Q x ∑ - > Δ
In the diagram, the initial state ‘q0’ on getting ‘0’ as the input it goes to state q1 and print ‘a’ as the
output and on getting ‘1’ as the input in q0 state it goes to q2 state and print ‘b’ as the output.
The state ‘q1’ on getting ‘0’ as the input it remains in the state of itself and prints ‘a’ as the output
and on getting ‘1’ as the input it goes to ‘q2’ state and prints ‘b’ as the output.
The state ‘q2’ on getting ‘1’ as the input remains in the same state and prints ‘b’ as the output and
on getting ‘0’ as the input it goes to ‘q1’ state and prints ‘a’ as the output.
Thus, finally above Moore machine can easily count the number of 0’s and 1’s i.e., on getting ‘0’ and
‘1’ as the input it gives ‘a’ and ‘b’ resp. as the output thus on counting the outputs ‘a’ and ‘b’, we
can easily count the number of 0’s and 1’s in the given string.
OUTPUT
FIG 6.1
FIG 6.2 Diagram represents the strings accepted by the Moore Machine in FIG 6.1
Using state ‘q0’ we reach end of the string, when BLANK is reached move towards left. Using
state ‘q1’ we pass all 0’s and move left first 1 is found. Pass single ‘1’ and move left. Using state ‘q2’
we complement each digit and move left. When BLANK is reached move towards right and reaches
the final state q2.
Steps:
Step-1. First ignore all 0’s and 1’s and go to right & then if B found go to left.
Step-2. Then ignore all 0’s and go left, if 1 found go to left.
Step-3. Convert all 0’s into 1’s and all 1’s into 0’s and go to left & if B found go to right
and stop the machine.
OUTPUT
FIG 7.1
Here, q0 shows the initial state and q1 and q2 shows the transition state and q3 shows the final
state.
0, 1 are the variables used and R, L shows right and left
FIG 7.2 Diagram shows the input and their 2’s complement
RESULT – The FIG 7.1 is the required Machine for the conversion of binary number into @’s
complement.
Experiment08
AIM - Design a Program which will increment the given binary number by 1.
THEORY- Incrementing machine (to increment the given binary number by 1) are here Turing
machine.
A TM is expressed as a 7-tuple (Q, T, B, ∑, δ, q0, F) where:
Q: - a finite set of states = {q0, q1, q2, q3, q4, q5, q6, q7, q8}
T: - the tape alphabet = {□,0,1}
□: - blank symbol (every cell is filled with □ except input alphabet initially)
∑ is the input alphabet = {0,1}
δ is a transition function which maps Q × T → Q × T × {L, R}. Depending on its present state
and present tape alphabet (pointed by head pointer), it will move to new state, change the
tape symbol (may or may not) and move head pointer to either left or right.
q0: - the initial state
F: - the set of final states.
Approach
1. Add a 0 at the beginning of the string.
2. Traverse through the string to get to the LSB.
3. Traversing backwards (LSB to MSB); use NOT operator on every 1 until you reach a 0 (Since we
added a 0 at the beginning, we are certain that we will eventually reach a 0)
4. use NOT operator on said 0.
5. Traverse backwards until you reach the beginning of string.
6. If the first bit is equal to 0, you have to remove it with blank.
7. If the first bit is equal to 1, it means that overflow has occurred. (Nothing needs to be done in if
you reach this step).
OUTPUT
FIG 8.1
FIG 8.2 Testing results shows the binary number and their incrementation by 1
RESULT – FIG 8.1 represents the required Machine which increments the binary numbers by 1.
Experiment 09
AIM- Design a Program to convert NDFA to DFA.
THEORY -
Non-deterministic Finite Automaton.
An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −
Q is a finite set of states.
∑ is a finite set of symbols called the alphabets.
δ is the transition function where δ: Q × ∑ → 2Q
(Here the power set of Q (2Q) has been taken because in case of NDFA, from a state,
transition can occur to any combination of Q states)
q0 is the initial state from where any input is processed (q0 ∈ Q).
F is a set of final state/states of Q (F ⊆ Q).
In DFA, for each input symbol, one can determine the state to which the machine will move.
Hence, it is called Deterministic Automaton. As it has a finite number of states, the machine is
called Deterministic Finite Machine or Deterministic Finite Automaton.
contains:
((s, (, **), (s, ( ))
If input is) and (is on stack top, then (is popped and nothing is pushed to stack.
If input is (, what’s on stack top doesn’t matter and a (is pushed to stack.
OUTPUT-
FIG 10.1 (left) represents the automata for parenthesis (right) string acceptance
RESULT- The above is the required machine for the well-formed parenthesis.
Experiment 11
AIM – Design a PDA to accept WCWRwhere W is any string and WRis the reverse of the string and C
is a special symbol.
THEORY - a pushdown automaton (PDA) is a type of automaton that employs a stack. Pushdown
automata are used in theories about what can be computed by machines. They are more capable
than finite-state machines but less capable than Turing machines.
For the given problem:
The PDA can be defined as:
Q – {qo , q1 , q2}
∑ - {a, b, C}
S is stack symbol
δ is the transition function: Q × (∑ ∪ {ε}) × S × Q × S*
q0 is the initial state (q0 ∈ Q)
I is the initial stack top i.e. Z0
F is a set of accepting states {q2∈ Q)
Explanation
String will come followed by one 'c', followed by reverse of the string before 'c'.
So we get to know that 'c' will work as an alarm to starting popping STACK.
So we will pop every 'a' with 'a' and every 'b' with 'b'.
For every two a's and b's push them into STACK
When 'c' comes do nothing.
Starting popping STACK: 'a' for 'a' and 'b' for 'b'.
State Transition functions:
δ(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)
// this is decision step
δ(q0, c, a) = (q1, a)
δ(q0, c, b) = (q1, b)
δ(q1, b, b) = (q1, ε)
δ(q1, a, a) = (q1, ε)
δ(q1, ε, Z) = (qf, Z)
OUTPUT –
FIG 11.1
Experiment 12
AIM – Design a Turing Machine that accepts the following language a nbncn where n>0.
THEORY - A Turing machine consists of a tape of infinite length on which read and writes operation
can be performed. The tape consists of infinite cells on which each cell either contains input symbol
or
a special symbol called blank. It also consists of a head pointer which points to cell currently being
read and it can move in both directions. For the given experiment TM is expressed as a 7-tuple (Q,
T, B, ∑, δ, q0, F) where:
Q is a finite set of states
T is the tape alphabet {X,Y,Z}
B is blank symbol (every cell is filled with B except input alphabet initially)
∑ is the input alphabet {a,b,c}
δ is a transition function which maps Q × T → Q × T × {L,R}. Depending on its present state
and present tape alphabet (pointed by head pointer), it will move to new state, change the
tape symbol (may or may not) and move head pointer to either left or right.
q0 is the initial state
F is the set of final state {q5}
State Transition for the given experiment
We have designed state transition diagram for anbncn | n ≥ 1 as follows:
Mark 'a' with 'X' and move towards unmarked 'b'
Move towards unmarked 'b' by passing all 'a's
To move towards unmarked 'b' also pass all 'Y's if exist.
Mark 'b' with 'Y' and move towards unmarked 'c'.
Move towards unmarked 'c' by passing all 'b's
To move towards unmarked 'c' also pass all 'Z's if exist
Mark 'c' with 'Z' and move towards first 'X' (in left)
Move towards first 'X' by passing all 'Z's, 'b's, 'Y's and 'a's
When 'X' is reached just move one step right by doing nothing.
To check all the 'a's, 'b's and 'c's are over add loops for checking 'Y' and 'Z' after "we get 'X'
followed by 'Y'"
To reach final state(qf) just replace BLANK with BLANK and move either direction
OUTPUT
FIG 12.1