0% found this document useful (0 votes)
35 views22 pages

Temp Theory of Computation Assign 2

The document discusses several examples of using deterministic finite automata (DFAs) to model scenarios: 1) A DFA is constructed to model a protocol that uses bit stuffing to transmit data frames, with states representing prefixes/suffixes and data bits. 2) A DFA with a binary tree structure and 233-1 states is proposed to model a cache memory system where the third bit from the right must be 1. 3) DFAs are described to model languages where strings are (i) multiples of 5 in binary, and (ii) have blocks of 3 with at least two 0s. The key aspects summarized are the construction of DFAs to model given scenarios involving strings, bits,

Uploaded by

Wolf shadow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views22 pages

Temp Theory of Computation Assign 2

The document discusses several examples of using deterministic finite automata (DFAs) to model scenarios: 1) A DFA is constructed to model a protocol that uses bit stuffing to transmit data frames, with states representing prefixes/suffixes and data bits. 2) A DFA with a binary tree structure and 233-1 states is proposed to model a cache memory system where the third bit from the right must be 1. 3) DFAs are described to model languages where strings are (i) multiples of 5 in binary, and (ii) have blocks of 3 with at least two 0s. The key aspects summarized are the construction of DFAs to model given scenarios involving strings, bits,

Uploaded by

Wolf shadow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Theory of Computation

Sem 4 (2022 Jan-May)

Draft

Roll no :
CS20B1012
CS20B1037
CS20B1064
CS20B1068
CS20B1094
CS20B1125
CS20B1127
1. (3 marks) A Model is a representative of a system under study. Models give
abstraction and many models (FA,PDA,TM) exist in computer science to model
problems that arise in various CS domains, the popular domains are computer
networks, computer organization, operating systems, big-data anal-ysis, etc.,

Case Study: High level Data link control protocol uses bit stuffing technique for
each data transfer. i.e., bits 11 is stuffed as prefix and suffix with every frame.
Frame contains data and CRC. For example, to transmit a frame 1101 with CRC
bits 10, HDLC actually transmits 1111011011. Receiver is synchronized to this
scheme so that when the data reaches the receiver the prefix and suffix are
removed and the data part is taken for CRC verification. Model the above scenario
using a deterministic finite automaton. i.e., the set of all finite strings over {0,1}
beginning with 11 and ending with 11.

Cyclic Redundancy Check - 10


High Level Data Link Control - 11-*-10-11 // where * is the data-frame string to be
transmitted

DFA:
● Q = {q0,q1,q2,q3,q4,q5,q6,q7}
● Σ = {0,1}
● ઠ (see below)
● start state = q0
● F = {q6}

ઠ:

● ઠ(q0,0) = q7
● ઠ(q0,1) = q1
● ઠ(q1,0) = q7
● ઠ(q1,1) = q2
● ઠ(q2,0) = q2
● ઠ(q2,1) = q3
● ઠ(q3,0) = q4
● ઠ(q3,1) = q3
● ઠ(q4,0) = q2
● ઠ(q4,1) = q5
● ઠ(q5,0) = q2
● ઠ(q5,1) = q6
● ઠ(q6,0) = q2
● ઠ(q6,1) = q3
● ઠ(q7,0) = q7
● ઠ(q7,1) = q7

q7 is a state to define whether the prefix is correct or not.


2. (3 marks) Consider a Cache memory system with set-associative mapping. The
TAG fields follow a variable length scheme with the size ranging from 8 bits to 32
bits. The third bit from the right is a VALID bit which indicates whether a block
contains valid data or not. If VALID=1 then the block is valid, otherwise it is
invalid. The above scenario can be modelled using deterministic finite automata.
i.e., the set of all finite strings over {0, 1} such that the third bit from the right is 1.

Since we know the size of input is always going to be between 8 to 32 bits, a DFA of a
binary tree structure would be suitable for the above case since there will be finite
number of states.

The depth of the root in this binary tree structure is 0 and is the initial state of the
DFA as well. Number of states are also finite - (1+2+4+8+...232 = 233 - 1)

As the state’s parent’s parent being invoked due to bit 1 which is the VALID bit (third bit
from right) its four children will be final states for every “valid” subtree.
3. The following two DFA models find applications in Bioinformatics.
i) (3 marks) L = {x | x when interpreted as a binary string is a multiple of 5 }.
. x ∈ {0, 1}∗

ii) (3 marks) L = {x | each block of 3 contains at least two 0’s }. x ∈ {0, 1}∗

i)

For divisibility by 5 the binary string must leave a remainder 0 when divided by 5 and
there are 5 possible remainders when divided by 5 = {0,1,2,3,4}. So let us have 5 states
named q0,q1,q2,q3,q4 where the qi state indicates the remainder of the binary string
given when divided by 5. And thus having the final state as q0 we can say that the given
binary string is divisible by 5.

So for a binary string abcde… the remainder of that string by 5 is the sum of the
remainder of ( a*(2^n) % 5 + b*(2^(n-1)) % 5 + c*(2^(n-2)) % 5 +...) and taking the
remainder of the final number and recursing it till we get one of the 5 remaining
numbers. So in the above expression we are reading abcde… one by one and then
recursion as abcde… -> (a%5 state)bcde…. -> … and so on.

Number Binary Remainder (%5) State


(state)sub-binary

0 000 → (q0)0 0 q0

1 001 → (q0)1 1 q1

2 010 → (q1)0 2 q2

3 011 → (q1)1 3 q3

4 100 → (q2)0 4 q4

With all the states in hand we shall continue till we have all ઠ relations in hand
5 101 → (q2)1 0 q0

6 110 → (q3)0 1 q1

7 111 → (q3)1 2 q2
8 1000→ (q4)0 3 q3

9 1001 → (q4)1 4 q4

With the above information in hand:

ઠ:

● ઠ(q0,0) = q0
● ઠ(q0,1) = q1
● ઠ(q1,0) = q2
● ઠ(q1,1) = q3
● ઠ(q2,0) = q4
● ઠ(q2,1) = q0
● ઠ(q3,0) = q1
● ઠ(q3,1) = q2S
● ઠ(q4,0) = q3
● ઠ(q4,1) = q4

F = {q0}

Σ = {0,1}

Initial state = q0

Q = {q0,q1,q2,q3,q4}

When an empty string is given it is considered the same as 0{0}* string.


ii) Considering two cases:
First case where blocks are in a manner of :
● aaa-aaa-aaa
● aaa-aaa-aa
● aaa-aaa-a

Therefore the DFA:

Where e means є (empty string)

Here q2 acts as a garbage collector. Whenever there is a block which does not have at
least two 0s (that is→ when it reads two 1s) then it goes to q2 and stays there for the
rest of the reading of inputs.

Second case considering substring of size 3:


abcdefgh….. → abc - bcd - cde - def - efg - fgh - …

In the below model


● qs is the initial state
● qe is the end state (garbage collector)
● qab is the state when its previous read two digits are ab in order
● qab’ is the state when its previous read two digits are ab in order but only two
digits had been read.
String ‘00’ is not part of the language in this model since the question asked for the
block of size 3.

If the last two digits read is:


● ‘11’ no matter what the next digit is it will not be a language
● ‘01’ or ‘10’ the next digit must be a ‘0’ for it to stay as a language
● ‘00’ no matter the next digit it will stay as language

Shifting of states be like:


● 00-0 → (q00)-0 → q00
● 00-1 → (q00)-1 → q01
● 01-0 → (q01)-0 → q10
● 01-1 → (q01)-1 → qe (end)
● 10-0 → (q10)-0 → q00
● 10-1 → (q10)-1 → qe (end)

And this is the model above


4. (3 marks) Construct a DFA if exists: L = {s ∈ (0 + 1)∗| |n0(s) − n1(s)| ≤ 4}, n0(s)
represents the number of 0’s in the string s. If DFA does not exist, present an
intuitive argument for non-existence of FA.

Let us start with an general state automation:

Here the initial state is q0 and the final states are q0,q1,q2,q3,q4,q5,q6,q7,q8. The
working of the above model is stated as: whenever the reader reads 1 the automaton
will move to its right state, else if it reads 0 then move to the left state.

The drawback of this automation is that this does not have finite number of states,
therefore not a FSA thus not a DFA (rather a Deterministic Infinite State Automaton).

To make a DFA such that |n0(s) − n1(s)| ≤ 4 is not possible because of the fact that
DFA or simple FSA cannot record nor store its previous states during the execution of
the automaton. That is, FSA cannot count the number of 1s and 0s since in a string ‘s’
there could be infinite 1s and 0s.

But the above model could work given that the string ‘s’ is less than a certain length,
thus giving us an idea of how many states we need.

String ‘s’ could have infinite length of 1s and 0s thus making the automaton not
finite therefore no DFA.
5. Let Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Consider the base 10 numbers formed by
strings from Σ∗

Let L2 = {x ∈ Σ∗| the numbers represented by x are exactly divisible by 5 }.

Main idea is for the decimal string to end at 5 or 0.


Therefore consider the below DFA model:

When an empty string is given it is considered the same as 0{0}* string.

● Q = {q0,q1,q2,q3,q4,q5,q6,q7,q8,q9}
● Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
● ઠ (see below)
● start state = q0
● F = {q0,q5}

ઠ:

● ઠ(qi,j) = qj
○ i : 0 <= i <= 9
○ j : 0 <= j <= 9
○ i,j ∈ natural numbers

Therefore there are 100 transition states in the above model, here is the part of the
model:
ઠ:

A = {1,2,3,4,6,7,8,9}

B = {0,5}

● ઠ(q0,A) = q0

● ઠ(q0,B) = q1

● ઠ(q1,A) = q0

● ઠ(q1,B) = q1
Assignment 2:

1) (1+1+3=5 marks) L: the set of all finite strings over {0, 1} beginning with 11 and ending with
11. Write the regular expression. Construct an equivalent NFA (with epsilon). Use the DFA
constructed in Assignment 1 to obtain the Regular expression using Arden’s Theorem.

Regular expression: 11(0+1)*11

NFA with epsilon

DFA constructed from assignment 1:


Arden’s Theorem → R = Q + RP → R = QP*

To find the regex of q4 (i.e. the final state - therefore language of the model in regex)

q0 = є → (i)
q1 = q0.1 → (ii)
q2 = q1.1 + q2.0 + q3.0 + q4.0 → (iii)
q3 = q2.1 → (iv)
q4 = q3.1 + q4.1 → q4 = (q3.1).1* → (v)

From (i) and (ii) → q1 = 1 → (vi)

From (iii) and (vi) →


q2 = 11 + q3.0 + q4.0 + q2.0
q2 = (11 + q3.0 + q4.0)0* → (vii)

From (iv) and (vii) →


q3 = (11 + q3.0 + q4.0).0*1
q3 = (11 + q4.0)0*1 + q3.(00*1)
q3 = (11 + q4.0)0*1(00*1)* → (viii)
From (v) and (viii) →
q4 = (11 + q4.0)0*1(00*1)*1.1*
q4 = 110*(00*1)*11* + q4.(00*1(00*1)*11*)
q4 = (110*1(00*1)*11*)(00*1(00*1)*11*)*

Let T = 0*1(00*1)*11*

Therefore:
q4 = 11.T.(0.T)*

And we know T ends with 11, q4’s regex is for the language over {0,1}* where it starts with ‘11’
and ends with ‘11’

2) (1+1+3=5 marks) L: the set of all finite strings over {0, 1} such that the third bit from the right
is 1. Write the regular expression. Construct an equivalent NFA (with epsilon). Use the DFA
constructed in Assignment 1 to obtain the Regular expression using Arden’s Theorem.

Regex: (0+1)*1(0+1)(0+1)

Assignment 1 image here:


Since in assignment the size of input is in a fixed range of 8 to 32, the model of a binary tree
with 32 levels (level of root is 0) is made to have a unique state invoked for a unique string of
size 8 to 32.
Therefore the regex is:
(0+1)x1(0+1)2 where 5≤x≤29

5)

There are three states therefore all possible transitions will be like:

ઠ(q0,є) = q0 ઠ(q0,0) = {q0,q1,q2} ઠ(q0,1) = {q0,q1} ઠ(q0,2) = {q2}


Date: 28 Mar 2022
PDA Assignment

Videos to refer: (rough estimation)


Q1) L3, L12
Q2) L17, L18
Q3) L11
Q4) L11, …
Q5) L3, L4, …
Q6) L21, L22
Q7) L21, L22
Q8) L12, L19

1) Find a CFG for the language over {0, 1} consisting of those strings in which the ratio of
the number of 1’s to the number of 0’s is three to two. Also, design a PDA.

PDA:
δ(qi,0,Z0) = (q0,XXXZ0) // starting with 0
δ(q0,0,X) = (q0,XXXX) // already read 0 correspondence to above function
δ(q01,XX) = (q0, є) // popping when reading 1 w.r.t. Above statements
δ(q0,є,Z0) = (qf , Z0) // end of process | Qf = final state
δ(q0,є,X) = (q0 , X)

δ(qi,1,Z0) = (q1,XXZ0) // starting with 1


δ(q1,1,X) = (qi,XXX) // already read 1 w.r.t. above statement
δ(q1,0,XXX) = (q1,є) // popping when reading 0
δ(q1,є,Z0) = (qf , Z0)
δ(q1,є,X) = (q1 , X)

δ(qi,є,Z0) = (qf,Z0)

CFG:

S → 11100
S → 11010
S → 10110
S → 01110

S → 11001
S → 10101
S → 01101
S → 10011
S → 01011

S → 00111

S → SS

S → 11100S
S → 1110S0
S → 111S00
S → 11S100
S → 1S1100
S → S11100

S → 11010S
S → 1101S0
S → 110S10
S → 11S010
S → 1S1010
S → S11010

S → 10110S
S → 1011S0
S → 101S10
S → 10S110
S → 1S0110
S → S10110

S → 01110S
S → 0111S0
S → 011S10
S → 01S110
S → 0S1110
S → S01110

S → 11001S
S → 1100S1
S → 110S01
S → 11S001
S → 1S1001
S → S11001
S → 10101S
S → 1010S1
S → 101S01
S → 10S101
S → 1S0101
S → S10101

S → 01101S
S → 0110S1
S → 011S01
S → 01S101
S → 0S1101
S → S01101

S → 10011S
S → 1001S1
S → 100S11
S → 10S011
S → 1S0011
S → S10011

S → 01011S
S → 0101S1
S → 010S11
S → 01S011
S → 0S1011
S → S01011

S → 00111S
S → 0011S1
S → 001S11
S → 00S111
S → 0S0111
S → S00111

2) Convert the following to CNF. S → ABA, A → aA, A → ε, B → bB, B → ε

ε statements:
A→ε
B→ε
S → ABA / BA/ AA / AB / A / B / ε
A → a / aA
B → b / bB // removed epsilon

S → ABA / BA/ AA / AB / a / aA / b / bB / ε
A → a / aA
B → b / bB // removed unit productions

CNF is not possible because ” S → ε “ is part of the grammar . Therefore, NO CNF EXISTS.

3) Is the following grammar Ambiguous. S → Sb, S → aSb, S → Sa, S → a.

S → Sb / aSb / Sa / a

Let W = aab | Using leftmost derivation:

Derivation 1:
S → Sb | S → Sb
S → Sab | S → Sa
S → aab |S→a

Derivation 2:
S → aSb | S → aSb
S → aab |S→a

Therefore the given CFG is an ambiguous grammar:


∃W ∊ L(G) ∍ W has at least two leftmost / rightmost derivations

4) What is L(G), S → aS | aSbS | ε. Is G ambiguous. If so, find an equivalent unambiguous


grammar. Also, design a PDA.

S → aS | aSbS | ε

Let W = aaabb | Using leftmost derivation:

Derivation 1:
S → aS | S → aS
S → aaSbS | S → aSbS
S → aaaSbSbS | S → aSbS
S → aaabSbS |S→ε
S → aaabbS |S→ε
S → aaabb |S→ε

Derivation 2:
S → aSbS | S → aSbS
S → aaSbS | S → aS
S → aaaSbSbS | S → aSbS
S → aaabSbS |S→ε
S → aaabbS |S→ε
S → aaabb |S→ε

Therefore the given CFG is an ambiguous grammar:


∃W ∊ L(G) ∍ W has at least two leftmost / rightmost derivations

The unambiguous grammar for the given language is


S → aSS’ / ε
S’ → bS / ε

Let's check with the same string W, i.e., aaabb ,

Derivation :
S → aSS’ | S → aSS’
S → aaSS’S’ | S → aSS’
S → aaaSS’S’S’ | S → aSS’
S → aaaS’S’S’ |S→ε
S → aaaS’S’ | S’ → ε
S → aaabSS’ | S’ → bS
S → aaabS’ |S→ε
S → aaabbS | S’ → bS
S → aaabb | S’ → ε

Therefore the given CFG is not an ambiguous grammar:


∃W ∊ L(G) ∍ W has only one leftmost / rightmost derivations

5) Find PDA, CFG; L = {am bn | m ≤ n ≤ 2m}.

Given language , L = {am bn | m ≤ n ≤ 2m}

CFG :
S → ab / abb / aSb / aSbb

PDA :
δ(q0,ε,Z0) = (q1,SZ0) // push ‘start’ symbol
// Non Deterministic PDA
δ(q1,ε,S) = (q1,aSb) // for S → aSb , push ‘aSb’
δ(q1,ε,S) = (q1,aSbb) // for S → aSb , push ‘aSbb’
δ(q1,ε,S) = (q1,ab) // for S → aSb , push ‘ab’
δ(q1,ε,S) = (q1,abb) // for S → aSb , push ‘abb’

δ(q1,b,b) = (q1,ε) // pop


δ(q1,a,a) = (q1,ε) // pop
δ(q1,ε,Z0) = (qf,Z0) // final state

6) Show that the language ap , p is prime, is not context free.

∃∈
∝𝛼ɣβ

7) Is L = {an bm ck | n, m, k ≥ 1, 2n = 3k or 5n = 7m} context free.

8) For the language of equal no of 0’s and 1’s, write the Greibach Normal Form.

CFG :
A1 → 0A11 / 1A10 / A101 / A110 / 01A1 / 10A1
A1 → 01 / 10

( Assume ‘ε’ is not a part of the language. If ‘ε’ is part of language CNF and GNF are not
possible. )

Consider the following:


A2 → 0
A3 → 1

Then:
A1 → 0A1A3 / 1A1A2 / A1A2A3 / A1A3A2 / 0A3A1 / 1A2A1 / A2A3 / A3A2
A2 → 0
A3 → 1

You might also like