CS 501 TOC Lab Manual

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 28

INDEX

S. No. Name of Experiment Date Sign Remark

1. Design a Finite Automata for creating machine that accepts three


consecutive one.
2. Design a Finite Automata for creating machine that accepts the
string always ending with 101.
3. Design a Finite Automata for Mod 3 Machine

4. Design a C program for accepting decimal number divisible by 2.

5 Design a machine which accepts string having equal no. of 1’s


and 0’s.
6 Design a C program for creating a machine which count number
of 1’s and 0’s in a given string.
7 Design a C Program to find 2’s complement of a given binary
number.
8 Design a C Program which will increment the given binary
number by 1.
9 Design a Program to convert NDFA to DFA.

10 Design a 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 n n n
Design a Turing machine that’s accepts a b c |n>=0
EXPERIMENT-1
Unit/Topic: 1/ Examples of Finite Automata

OBJECTIVE:
Design a Finite Automata for creating machine that accepts three consecutive one.

THEORY:
o Finite automata are used to recognize patterns.
o It takes the string of symbol as input and changes its state accordingly. When the
desired symbol is found, then the transition occurs.
o At the time of transition, the automata can either move to the next state or stay in the
same state.
o Finite automata have two states, Accept state or Reject state. When the input string
is processed successfully, and the automata reached its final state, then it will accept.

A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F), where:

 Q: finite set of states  
 ∑: finite set of the input symbol  
 q0: initial state   
 F: final state  
 δ: Transition function  

INPUT SET:
 Here, Q is q0,q1,q2 and q3 set of states
 ∑ = {0,1} set of input symbols
 q0 is initial state
 q3 is the final state

 δ: Transition function

Transition function(∂) is a function which maps Q * ∑ into Q . Here ‘Q’ is set


of states and ‘∑’ is input of alphabets. To show this transition function we
use table called transition table. The table takes two values a state and a
symbol and returns next state.
A transition table gives the information about –
 Rows represent different states.
 Columns represent input symbols.
 Entries represent the different next state.
 The final state is represented by a star or double circle.
 The start state is always denoted by an small arrow.

Present state Next state for input 0 Next state for input 1

q0 q0 q1

q1 q0 q2

q2 q0 q3
q3 q3 q3

OUTPUT SET:
The strings that will be generated for this particular languages are 111, 0111, 1110, 01110,
0111.... in which 0 always appears in a clump of 3. The transition graph is as follows:

Examples of Deterministic finite automata .


EXPECTED VIVA QUESTIONS:
What is finite automata?
Finite Automata(FA) is the simplest machine to recognize patterns. The finite
automata or finite state machine is an abstract machine that has five elements or
tuples. It has a set of states and rules for moving from one state to another but it
depends upon the applied input symbol. Basically, it is an abstract model of a
digital computer. The following figure shows some essential features of general
automation.

Figure: Features of Finite Automata

The above figure shows the following features of automata:


1. Input
2. Output
3. States of automata
4. State relation
5. Output relation
A Finite Automata consists of the following: 
Q : Finite set of states.
Σ : set of Input Symbols.
q : Initial state.
F : set of Final States.
δ : Transition Function.

Formal specification of machine is 


{ Q, Σ, q, F, δ }
FA is characterized into two types: 
1) Deterministic Finite Automata (DFA):
DFA consists of 5 tuples {Q, Σ, q, F, δ}.
Q : set of all states.
Σ : set of input symbols. ( Symbols which machine takes as input )
q : Initial state. ( Starting state of a machine )
F : set of final state.
δ : Transition Function, defined as δ : Q X Σ --> Q.

In a DFA, for a particular input character, the machine goes to one state only. A
transition function is defined on every state for every input symbol. Also in DFA
null (or ε) move is not allowed, i.e., DFA cannot change state without any input
character. 
For example, below DFA with Σ = {0, 1} accepts all strings ending with 0. 
 

Figure: DFA with  Σ = {0, 1} 

One important thing to note is, there can be many possible DFAs for a pattern.
A DFA with a minimum number of states is generally preferred. 
2) Nondeterministic Finite Automata(NFA): NFA is similar to DFA except
following additional features: 
1. Null (or ε) move is allowed i.e., it can move forward without reading
symbols. 
2. Ability to transmit to any number of states for a particular input. 
However, these above features don’t add any power to NFA. If we compare both
in terms of power, both are equivalent. 
Due to the above additional features, NFA has a different transition function, the
rest is the same as DFA. 
δ: Transition Function
δ:  Q X (Σ U ε ) --> 2 ^ Q.

As you can see in the transition function is for any input including null (or ε), NFA
can go to any state number of states. For example, below is an NFA for the above
problem. 

NFA

One important thing to note is, in NFA, if any path for an input string leads to a
final state, then the input string is accepted. For example, in the above NFA,
there are multiple paths for the input string “00”. Since one of the paths leads to a
final state, “00” is accepted by the above NFA. 
Some Important Points: 
 Justification:
Since all the tuples in DFA and NFA are the same except for one of the tuples,
which is Transition Function (δ) 
In case of DFA
δ : Q X Σ --> Q
In case of NFA
δ : Q X Σ --> 2Q
Now if you observe you’ll find out Q X Σ –> Q is part of Q X Σ –> 2 Q.
On the RHS side, Q is the subset of 2 Q which indicates Q is contained in 2 Q or Q is
a part of 2Q, however, the reverse isn’t true. So mathematically, we can conclude
that every DFA is NFA but not vice-versa. Yet there is a way to convert an NFA
to DFA, so there exists an equivalent DFA for every NFA. 
 
1. Both NFA and DFA have the same power and each NFA can be translated
into a DFA. 
2. There can be multiple final states in both DFA and NFA. 
3. NFA is more of a theoretical concept. 
4. DFA is used in Lexical Analysis in Compiler. 
5. If the number of states in the NFA is N then, its DFA can have maximum
2N number of states.
What are the applications of finite automata?
Automata is a machine that can accept the Strings of a Language L over an input
alphabet  .
So far we are familiar with the Types of Automata . Now, let us discuss the
expressive power of Automata and further understand its Applications.
Expressive Power of various Automata :
The Expressive Power of any machine can be determined from the class or set of
Languages accepted by that particular type of Machine. Here is the increasing
sequence of expressive power of machines :

As we can observe that FA is less powerful than any other machine. It is important
to note that DFA and NFA are of same power because every NFA can be
converted into DFA and every DFA can be converted into NFA .
The Turing Machine i.e. TM is more powerful than any other machine.
(i) Finite Automata (FA) equivalence:
Finite Automata
≡ PDA with finite Stack
≡ TM with finite tape
≡ TM with unidirectional tape
≡ TM with read only tape

(ii) Pushdown Automata (PDA) equivalence:


PDA ≡ Finite Automata with Stack

(iii) Turing Machine (TM) equivalence:


Turing Machine
≡ PDA with additional Stack
≡ FA with 2 Stacks

The Applications of these Automata are given as follows:


1. Finite Automata (FA) –
 For the designing of lexical analysis of a compiler.
 For recognizing the pattern using regular expressions.
 For the designing of the combination and sequential circuits using Mealy
and Moore Machines.
 Used in text editors.
 For the implementation of spell checkers.
2. Push Down Automata (PDA) –
 For designing the parsing phase of a compiler (Syntax Analysis).
 For implementation of stack applications.
 For evaluating the arithmetic expressions.
 For solving the Tower of Hanoi Problem.

3. Linear Bounded Automata (LBA) –


 For implementation of genetic programming.
 For constructing syntactic parse trees for semantic analysis of the compiler.

4. Turing Machine (TM) –


 For solving any recursively enumerable problem.
 For understanding complexity theory.
 For implementation of neural networks.
 For implementation of Robotics Applications.
 For implementation of artificial intelligence.
EXPERIMENT-2
Unit/Topic: 1/ Examples of Finite Automata

OBJECTIVE:
Design a Finite Automata for creating machine that accepts the string always ending with 101.

THEORY:
o Finite automata are used to recognize patterns.
o It takes the string of symbol as input and changes its state accordingly. When the
desired symbol is found, then the transition occurs.
o At the time of transition, the automata can either move to the next state or stay in the
same state.
o Finite automata have two states, Accept state or Reject state. When the input string
is processed successfully, and the automata reached its final state, then it will accept.

A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F), where:

 Q: finite set of states  
 ∑: finite set of the input symbol  
 q0: initial state   
 F: final state  
 δ: Transition function  

INPUT SET:
 Here, Q is q0,q1,q2 and q3 set of states
 ∑ = {0,1} set of input symbols
 q0 is initial state
 q3 is the final state

 δ: Transition function

Transition function(∂) is a function which maps Q * ∑ into Q . Here ‘Q’ is set


of states and ‘∑’ is input of alphabets. To show this transition function we
use table called transition table. The table takes two values a state and a
symbol and returns next state.
A transition table gives the information about –
 Rows represent different states.
 Columns represent input symbols.
 Entries represent the different next state.
 The final state is represented by a star or double circle.
 The start state is always denoted by an small arrow.

Present state Next state for input 0 Next state for input 1

q0 q0 q1

q1 q2 q1

q2 q0 q3
q3 q2 q1

INPUT SET:
Basically we need to design an automata that accepts language containing strings which have
‘101’ as substring. This means that we can reach final state in DFA only when ‘101’ occur in

succession.
Where, q0 = Initial State

Q = Set of all states {q0, q1, q2, q3}

q3 = Final State

0,1 are input alphabets


OUTPUT SET:
Let us understand the above solution step by step -

The smallest possible valid string is ‘101’ so from initial state q0, upon giving ‘101’ we should
reach final state q3.

If suppose any string starts with '0′ then we need the entire ‘101’ sequence to reach the final
state(to ensure ‘101’ is substring as given in question). Hence we will apply a self loop at state
q0 upon giving ‘0’ (transition into the same state q0).

If suppose at state q1, the input is ‘1’ i.e. string is of type ‘11’ then we only need ‘01’ sequence
to satisfy the criteria of ‘101’ substring. Hence we will apply self loop at state q1 upon giving
‘1’

If suppose at state q2, the input is ‘0’ i.e. string is of type ‘100’ then we need entire ‘101’
sequence to satisfy the criteria of ‘101’ as substring. Hence at state q2, upon giving ‘0’ we will
transition to state q0.

Finally at state q3, we have already reached by giving strings having ‘101’ as substring. Hence
for inputs ‘0’ and ‘1’ we will just self loop them (transition them to same state) and criteria will
still be fulfilled.

EXPECTED VIVA QUESTIONS:


What is DFA ?
o DFA refers to deterministic finite automata. Deterministic refers to the uniqueness
of the computation. The finite automata are called deterministic finite automata if
the machine is read an input string one symbol at a time.
o In DFA, there is only one path for specific input from the current state to the next
state.
o DFA does not accept the null move, i.e., the DFA cannot change state without any
input character.
o DFA can contain multiple final states. It is used in Lexical Analysis in Compiler.
In the following diagram, we can see that from state q0 for input a, there is only one path
which is going to q1. Similarly, from q0, there is only one path for input b going to q2.

Formal Definition of DFA


A DFA is a collection of 5-tuples same as we described in the definition of FA.

1. Q: finite set of states  
2. ∑: finite set of the input symbol  
3. q0: initial state   
4. F: final state  
5. δ: Transition function  
Transition function can be defined as:

1. δ: Q x ∑→Q  

Graphical Representation of DFA


A DFA can be represented by digraphs called state diagram. In which Play Video

1. The state is represented by vertices.


2. The arc labeled with an input character show the transitions.
3. The initial state is marked with an arrow.
4. The final state is denoted by a double circle.

What are 5 tuples of DFA?

A Deterministic Finite Automaton (DFA) is defined as a 5-tuple (Q, Σ, δ, s, F) consisting of

 A finite set Q (the set of states)


 A finite set of symbols Σ (the input alphabet)
 A transition function δ: Q × Σ → Q mapping the current state q ∈ Q and input
symbol a ∈ Σ to a new state δ(q, a) ∈ Q
 An initial state s ∈ Q (the start state)
 A set of accepting states F (the final states)

A DFA is a mathematical model of a simple computational device that reads a string of


symbols over the input alphabet Σ, and either accepts or reject the input string. We would
like to turn this mathematical definion into a working program, so that we can run DFAs on
our computer.
EXPERIMENT-3
Unit/Topic: 1/ Language acceptor

OBJECTIVE:
Design a Finite Automata for Mode 3 Machine

THEORY:
o Finite automata are used to recognize patterns.
o It takes the string of symbol as input and changes its state accordingly. When the
desired symbol is found, then the transition occurs.
o At the time of transition, the automata can either move to the next state or stay in the
same state.
o Finite automata have two states, Accept state or Reject state. When the input string
is processed successfully, and the automata reached its final state, then it will accept.

A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F), where:

 Q: finite set of states  
 ∑: finite set of the input symbol  
 q0: initial state   
 F: final state  
 δ: Transition function  

INPUT SET:
Basically we need to design an automata that accepts language containing strings which have

‘mod 3’ . This means that we can reach final state in DFA only when ‘mod 3’ occur in
succession.

Where, q0 = Initial State

Q = Set of all states {q0, q1, q2}

q0 = Final State

0,1 are input alphabets


 δ: Transition function

Transition function(∂) is a function which maps Q * ∑ into Q . Here ‘Q’ is set


of states and ‘∑’ is input of alphabets. To show this transition function we
use table called transition table. The table takes two values a state and a
symbol and returns next state.
A transition table gives the information about –
 Rows represent different states.
 Columns represent input symbols.
 Entries represent the different next state.
 The final state is represented by a star or double circle.
 The start state is always denoted by an small arrow.

Present state Next state for input 0 Next state for input 1

q0 q0 q1

q1 q2
q0

q2
q1 q2

OUTPUT SET:
Basically we need to design an automata that accepts language containing strings which
have ‘mod 3’ . This means that we can reach final state in DFA only when ‘mod 3’ occur in

succession.
EXPECTED VIVA QUESTIONS:

What is mod operation?


This article is about the binary operation mod(a,n). For the (mod n) notation, see Modular
arithmetic.

In computing, the modulo operation returns the remainder or signed remainder of


a division, after one number is divided by another (called the modulus of the operation).
Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the
remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.

For example, the expression "5 mod 2" would evaluate to 1, because 5 divided by 2 has
a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0, because 9
divided by 3 has a quotient of 3 and a remainder of 0; there is nothing to subtract from 9
after multiplying 3 times 3.

Although typically performed with a and n both being integers, many computing systems


now allow other types of numeric operands. The range of values for an integer modulo
operation of n is 0 to n − 1 inclusive (a mod 1 is always 0; a mod 0 is undefined, possibly
resulting in a division by zero error in some programming languages). See Modular
arithmetic for an older and related convention applied in number theory.

When exactly one of a or n is negative, the naive definition breaks down, and
programming languages differ in how these values are defined.

What is final state?

A final state denotes the end of the execution flow of a state machine or region. It can
have multiple incoming transitions but no outgoing ones. Each region may contain at most
one final state. In the case of orthogonal regions, the execution flow stops when all
regions' final states have been reached.

What is initial state?


When a state machine starts, it starts by entering the machine’s initial state. Likewise,
when a compound state is entered, its initial state is also entered. The initial state is
not a separate state, but more like an indication of which state that the machine should
start in by default.
A transition from a different state can target a sub state of a compound state, thereby
bypassing the state’s initial state. An initial state should be thought of as the default
starting point of a compound state if a transition points directly to the compound state.
Notation
A state machine, or compound state’s initial state is specified by way a black circle with
exactly one transition arrow to a sibling state that should be entered. The black circle
and arrow have no name, it is the target state that is called the ‘initial’ state.

Initial states are impossible to avoid, and follows from the definition of compound
states: For a compound state, whenever it becomes active, exactly one of its direct
children are also active. The “initial” state simply denotes which of the child states
become active when this happens.
Picking the right initial state can help decouple the various parts of the statechart. It
allows transitions to target the compound (parent) state instead of a child directly. This
allows collapsing the view of a compound state to see the bigger picture.
EXPERIMENT-4
Unit/Topic: 1/ Examples of Finite Automata

OBJECTIVE:
Design a C program for accepting decimal number divisible by 2
THEORY:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is decimal number?

NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT-5
Unit/Topic: 2/ DFA

OBJECTIVE:
Design a machine which accepts string having equal no. of 1’s and 0’s.

PROCEDURE:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is regular expression?

NAME OF FACULTY:
SIGNATURE:
DATE:

EXPERIMENT-6
Unit/Topic: 2/ Regular Expression

OBJECTIVE:
Design a C program for creating a machine which count number of 1’s and 0’s in a given string.

THEORY:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is dead state?
2. What is transition table and transition graph?

NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT-7
Unit/Topic: 2/Regular Expression

OBJECTIVE:
Design a C Program to find 2’s complement of a given binary number.

PROCEDURE:
Attached
INPUT SET:

OUTPUT SET

EXPECTED VIVA QUESTIONS:


1. What is 2’s complement?

NAME OF FACULTY:
SIGNATURE:
DATE:

EXPERIMENT-8
Unit/Topic: 2/Regular Expression

OBJECTIVE:
Design a C Program which will increment the given binary number by 1.

PROCEDURE:
Attached

INPUT SET:
OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is binary number?
2. Explain binary addition?
NAME OF FACULTY:
SIGNATURE:
DATE:

EXPERIMENT-9
Unit/Topic: 2/NDFA

OBJECTIVE:
Design a Program to convert NDFA to DFA.

PROCEDURE:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is transition function of NDFA?

NAME OF FACULTY:
SIGNATURE:
DATE:

EXPERIMENT-10
Unit/Topic: 4/PDA

OBJECTIVE:
Design a PDA machine that accept the well-formed parenthesis.

PROCEDURE:
Attached

INPUT SET:

OUTPUT SET:
EXPECTED VIVA QUESTIONS:
1. What is PDA?
2. What are different types of PDA?

NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT-11
Unit/Topic: 4/PDA

OBJECTIVE:
Design a PDA to accept WCWR where w is any string and WR is reverse of that string and C is a
Special symbol.

PROCEDURE:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is palindrome?

NAME OF FACULTY
SIGNATURE:
DATE:

EXPERIMENT-12
Unit/Topic: 5/Turing Machine

OBJECTIVE:
n n n
Design a Turing machine that’s accepts a b c |n>=0

PROCEDURE:
Attached

INPUT SET:

OUTPUT SET:

EXPECTED VIVA QUESTIONS:


1. What is turing machine?
NAME OF FACULTY:
SIGNATURE:
DATE:

You might also like