0% found this document useful (0 votes)
11 views13 pages

Toc Unit I.2

The document outlines the components and functioning of Deterministic Finite Automata (DFA) and Non-Deterministic Finite Automata (NDFA), including their states, transition rules, and examples for recognizing specific patterns in strings. It also compares DFA and NDFA, highlighting their differences in state transitions and construction complexity. Additionally, it discusses applications of DFA and NDFA in real-world scenarios such as lexical analysis and pattern matching.

Uploaded by

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

Toc Unit I.2

The document outlines the components and functioning of Deterministic Finite Automata (DFA) and Non-Deterministic Finite Automata (NDFA), including their states, transition rules, and examples for recognizing specific patterns in strings. It also compares DFA and NDFA, highlighting their differences in state transitions and construction complexity. Additionally, it discusses applications of DFA and NDFA in real-world scenarios such as lexical analysis and pattern matching.

Uploaded by

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

Components of the DFA

States, Q: {q₀, q₁, q₂, q₃}

 q₀: Initial state. Represents the scenario where no part of the substring ‘101’ has been
identified yet.
 q₁: Represents that the input has ended with ‘1’.
 q₂: Represents that the input has ended with ’10’.
 q₃: Accept state. Represents that the substring ‘101’ has been detected.

Alphabets which are Input, Σ: {0, 1}

 The set of characters the DFA can read: Σ = {0, 1}.

Start State (q₀):

 The state where the DFA begins processing.

Accept State F: {q₃}

 q₃: The state where the DFA ends if the input string contains '101'.

Transition Rules (δ):

These rules define how the DFA moves between states based on the input:

δ(q₀, '1') = q₁
δ(q₀, '0') = q₀
δ(q₁, '0') = q₂
δ(q₁, '1') = q₁
δ(q₂, '1') = q₃
δ(q₂, '0') = q₀
δ(q₃, '0') = q₃
δ(q₃, '1') = q₃

 δ(q₀, '1') = q₁: Transition from q₀ to q₁ on seeing ‘1’.


 δ(q₀, '0') = q₀: Stay in q₀ on seeing ‘0’ (no part of ‘101’ detected).
 δ(q₁, '0') = q₂: Transition from q₁ to q₂ on seeing ‘0’.
 δ(q₁, '1') = q₁: Stay in q₁ on seeing ‘1’.
 δ(q₂, '1') = q₃: Transition from q₂ to q₃ on seeing ‘1’ (completes ‘101’).
 δ(q₂, '0') = q₀: Transition from q₂ to q₀ on seeing ‘0’ (reset; no ‘101’).
 δ(q₃, '0') = q₃: Stay in q₃ on seeing ‘0’.
 δ(q₃, '1') = q₃: Stay in q₃ on seeing ‘1’.

State Diagram
Example 2: DFA for Even Length Inputs

Let’s create a DFA to recognise strings over the alphabet Σ = {a, b} that have an even
length.

We need to construct a Deterministic Finite Automaton (DFA) to recognise strings


over the alphabet Σ = {a, b} that have an even number of characters. A string is
considered accepted if it contains an even number of symbols, regardless of the order.
For example:

 Accepted Strings: '' (empty string), 'ab', 'aa', 'abab', 'aabb'


 Rejected Strings: 'a', 'b', 'aaa', 'aab', 'abb'

The DFA must correctly handle:

1. Start state: Where the computation begins.


2. Transitions: Moves between states based on the input character.
3. Accept state: Where the DFA ends if the string is valid.

Components of the DFA

States, Q: {q₀, q₁}

 q₀: Initial state. Represents strings with an even length.


 q₁: Intermediate state. Represents strings with an odd length.

Alphabets which are Input, Σ: {a, b}

 The set of characters the DFA can read: Σ = {a, b}.

Start State (q₀):

 The state where the DFA begins processing.

Accept State F: {q₀}

 q₀: The state where the DFA ends if the string has an even number of characters.

Transition Rules (δ):


These rules define how the DFA moves between states based on the input:

δ(q₀, 'a') = q₁
δ(q₀, 'b') = q₁
δ(q₁, 'a') = q₀
δ(q₁, 'b') = q₀

 δ(q₀, 'a') = q₁: Transition from q₀ to q₁ on seeing ‘a’.


 δ(q₀, 'b') = q₁: Transition from q₀ to q₁ on seeing ‘b’.
 δ(q₁, 'a') = q₀: Transition from q₁ to q₀ on seeing ‘a’.
 δ(q₁, 'b') = q₀: Transition from q₁ to q₀ on seeing ‘b’.

State Diagram

Example 3: DFA for Alternate ‘a’ and ‘b’

Let’s create a DFA to recognise strings over the alphabet Σ = {a, b} where ‘a’ and ‘b’
strictly alternate.

We need to construct a Deterministic Finite Automaton (DFA) to recognise strings


where each ‘a’ is followed by ‘b’ and vice versa. For example:

 Accepted Strings: '' (empty string), 'ab', 'ba', 'abab', 'baba'


 Rejected Strings: 'a', 'b', 'aa', 'bb', 'abaa'

The DFA must correctly handle:

1. Start state: Where the computation begins.


2. Transitions: Moves between states based on the input character.
3. Accept state: Where the DFA ends if the string is valid.

Components of the DFA

States, Q: {q₀, q₁, q₂}

 q₀: Initial state. Represents an empty string or one that follows the alternating pattern.
 q₁: Represents that the string ends with ‘a’ and is expecting ‘b’.
 q₂: Represents that the string ends with ‘b’ and is expecting ‘a’.

Alphabets which are Input, Σ: {a, b}

 The set of characters the DFA can read: Σ = {a, b}.


Start State (q₀):

 The state where the DFA begins processing.

Accept State F: {q₀}

 q₀: The state where the DFA ends if the alternating pattern is preserved.

Transition Rules (δ):

These rules define how the DFA moves between states based on the input:

δ(q₀, 'a') = q₁
δ(q₀, 'b') = q₂
δ(q₁, 'a') = q₁
δ(q₁, 'b') = q₀
δ(q₂, 'a') = q₀
δ(q₂, 'b') = q₂

 δ(q₀, 'a') = q₁: Transition from q₀ to q₁ on seeing ‘a’.


 δ(q₀, 'b') = q₂: Transition from q₀ to q₂ on seeing ‘b’.
 δ(q₁, 'a') = q₁: Stay in q₁ on seeing ‘a’ (violation).
 δ(q₁, 'b') = q₀: Transition from q₁ to q₀ on seeing ‘b’ (valid alternation).
 δ(q₂, 'a') = q₀: Transition from q₂ to q₀ on seeing ‘a’ (valid alternation).
 δ(q₂, 'b') = q₂: Stay in q₂ on seeing ‘b’ (violation).

State Diagram

Applications of DFA

DFAs are used in various real-world scenarios, such as:

 Lexical Analysis: Recognizing tokens in programming languages.


 Pattern Matching: Searching for specific patterns in text.
 Network Protocols: Modeling state transitions in communication protocols.
NON-DETERMINISTIC FINITE AUTOMATON.

In NDFA, for a particular input symbol, the machine can move to any combination of
the states in the machine. In other words, the exact state to which the machine moves
cannot be determined. Hence, it is called Non-deterministic Automaton. As it has
finite number of states, the machine is called Non-deterministic Finite
Machine or Non-deterministic Finite Automaton.

o NFA stands for non-deterministic finite automata. It is easy to construct an


NFA than DFA for a given regular language.
o The finite automata are called NFA when there exist many paths for specific
input from the current state to the next state.
o Every NFA is not DFA, but each NFA can be translated into DFA.
o NFA is defined in the same way as DFA but with the following two
exceptions, it contains multiple next states, and it contains ε transition.

Formal Definition of an NDFA

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,

q0 is the initial state from where any input is processed (q0 ∈ Q).
transition can occur to any combination of Q states)

F is a set of final state/states of Q (F ⊆ Q).

Graphical Representation of an NDFA: (same as DFA)

An NDFA is represented by digraphs called state diagram.

 The vertices represent the states.


 The arcs labeled with an input alphabet show the transitions.
 The initial state is denoted by an empty single incoming arc.
 The final state is indicated by double circles.

Example

Let a non-deterministic finite automaton be →

Q = {a, b, c}
∑ = {0, 1}
q0 = {a}
F = {c}

The transition function δ as shown below −

Present Next State for Input Next State for Input


State 0 1
a a, b B
b c a, c
c b, c C

Its graphical representation would be as follows −

DFA vs NDFA

The following table lists the differences between DFA and NDFA.

Deterministic Finite Automata Non-Deterministic Finite Automata

Each transition leads to exactly one A transition leads to a subset of states i.e.
state called as deterministic some transitions can be non-deterministic.
Accepts input if one of the last states is in
Accepts input if the last state is in Final
Final.
Backtracking is allowed in DFA. Backtracking is not always possible.
Requires more space. Requires less space.
Empty string transitions are not seen in
Permits empty string transition.
DFA.
For a given state, on a given input we For a given state, on a given input we reach
reach a deterministic and unique state. more than one state.
Need to convert NFA to DFA in the design
DFA is a subset of NFA.
of a compiler.
δ:Q×Σ→Q δ : Q × Σ → 2Q
For example − δ(q0,a)={q1} For example − δ(q0,a)={q1,q2}
DFA is more difficult to construct. NFA is easier to construct.
NFA is understood as multiple small
DFA is understood as one machine.
machines computing at the same time.
Deterministic Finite Automata Non-Deterministic Finite Automata

Acceptors, Classifiers, and Transducers

Acceptor (Recognizer)

An automaton that computes a Boolean function is called an acceptor. All the states
of an acceptor is either accepting or rejecting the inputs given to it.

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.

Acceptability by DFA and NDFA

A string is accepted by a DFA/NDFA iff the DFA/NDFA starting at the initial state
ends in an accepting state (any of the final states) after reading the string wholly.

A string S is accepted by a DFA/NDFA (Q, ∑, δ, q0, F), iff

δ*(q0, S) ∈ F

The language L accepted by DFA/NDFA is

{S | S ∈ ∑* and δ*(q0, S) ∈ F}

A string S′ is not accepted by a DFA/NDFA (Q, ∑, δ, q0, F), iff

δ*(q0, S′) ∉ F

The language L′ not accepted by DFA/NDFA (Complement of accepted language L)


is
{S | S ∈ ∑* and δ*(q0, S) ∉ F}

Example

Let us consider the DFA shown in Figure 1.3. From the DFA, the acceptable strings
can be derived.

Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}

Strings not accepted by the above DFA: {1, 011, 111, ........}

Example 1:

. Q = {q0, q1, q2}


. ∑ = {0, 1}
. q0 = {q0}
. F = {q2}
Solution:

Transition diagram:

Transition Table:

Present State Next state for Input Next State of Input


0 1
→q0 q0, q1 q1
q1 q2 q0
*q2 q2 q1, q2
In the above diagram, we can see that when the current state is q0, on input 0, the next
state will be q0 or q1, and on 1 input the next state will be q1. When the current state
is q1, on input 0 the next state will be q2 and on 1 input, the next state will be q0.
When the current state is q2, on 0 input the next state is q2, and on 1 input the next
state will be q1 or q2.

Example 2:

NFA with ∑ = {0, 1} accepts all strings with 01.

Solution:

Transition Table:

Present State Next state for Input Next State of Input


0 1
→q0 q1 Ε
q1 ε q2
*q2 q2 q2

Example 3:

NFA with ∑ = {0, 1} and accept all string of length atleast 2.

Solution:

Transition Table:

Present State Next state for Input Next State of Input


0 1
→q0 q1 q1
q1 q2 q2
*q2 ε Ε
Example 1:

Design a NFA for the transition table as given below:

Present State 0 1
→q0 q0, q1 q0, q2
q1 q3 Ε
q2 q2, q3 q3
→q3 q3 q3
Solution:

The transition diagram can be drawn by using the mapping function as given in the
table.

Here,

δ(q0, 0) = {q0, q1}


δ(q0, 1) = {q0, q2}
Then, δ(q1, 0) = {q3}
Then, δ(q2, 0) = {q2, q3}
δ(q2, 1) = {q3}
Then, δ(q3, 0) = {q3}
δ(q3, 1) = {q3}

Example 2:

Design an NFA with ∑ = {0, 1} accepts all string ending with 01.

Solution:
Hence, NFA would be:

Example 3:

Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.

Solution:

The FA with double 1 is as follows:

Advertisement

It should be immediately followed by double 0.

Then,

Now before double 1, there can be any string of 0 and 1. Similarly, after double 0,
there can be any string of 0 and 1.

Hence the NFA becomes:

Now considering the string 01100011

. q0 → q1 → q2 → q3 → q4 → q4 → q4 → q4
Example 4:

Design an NFA in which all the string contain a substring 1110.

Solution:

The language consists of all the string containing substring 1010. The partial
transition diagram can be:

Now as 1010 could be the substring. Hence we will add the inputs 0's and 1's so that
the substring 1010 of the language can be maintained. Hence the NFA becomes:

Transition table for the above transition diagram can be given below:

Present State 0 1
→q1 q1 q1, q2
q2 q3
q3 q4
q4 q5
*q5 q5 q5
Consider a string 111010,

δ(q1, 111010) = δ(q1, 1100)


= δ(q1, 100)
= δ(q2, 00)
Got stuck! As there is no path from q2 for input symbol 0. We can process string
111010 in another way.

δ(q1, 111010) = δ(q2, 1100)


= δ(q3, 100)
= δ(q4, 00)
= δ(q5, 0)
= δ(q5, ε)
As state q5 is the accept state. We get the complete scanned, and we reached to the
final state.
Example 5:

Design an NFA with ∑ = {0, 1} accepts all string in which the third symbol from the
right end is always 0.

Solution:

Thus we get the third symbol from the right end as '0' always. The NFA can be:

The above image is an NFA because in state q0 with input 0, we can either go to state
q0 or q1.

You might also like