# DFA

DFA or deterministic finite automata has one output for one input alphabet from a state.

### Classification of Finite Automata

We will explain DFA for now, rest of them we will talk about later.

Below are symbols used in the DFA:

- Q - Finite set of states
- Σ - Input alphabet
- δ - QxΣ → Q
- q0 - start state
- F - Final State

**Note:**Q ⊇ F

#### Question 1

Construct a DFA which accepts set of all strings over Σ={a,b} of length 2 , L = {aa, ab, ba, bb}**Ans**

**Explanation**

- We have to create DFA which will accept string of length 2 on alphabet {a, b}
- So first thing about creating DFA which will accept of 1 length string, that is pretty simple
- You just take 2 states 1 length string can be accepted. Now take 2 length stirng for that we need 3 states
- And third state will be final state.
- But what if we came up with a string of length 3, it should
**not**be accepted in our DFA, right ? - So we have to attach one more state to take care of string greater than 2.
- State 'D' is called dead state.

### Languages Acceptance

**When** we say language is accepted in DFA then all the strings belonging to language should be accepted

in the DFA and the strings which are not part of the language should not be accepted in the DFA.

**Question 2**Construct a DFA which accepts set of all strings over Σ={a,b} of length ≥2

**Explanation**

We have to create DFA which will accept string of length ≥ 2 on alphabet {a, b}.

- So again first thing about creating DFA which will accept of 1 length string, that is pretty simple.
- You just take 2 states 1 length string can be accepted.
- Now take 2 length stirng for that we need 3 states.
- And third state will be final state.
- And if input comes over state C then it will go to C itself to accept strings greater than 2
- Now if string < 2 will not reach final state, so will not be accepted.

**Testing**

- Suppose we have abab, so take alphabets from left to right
- firstly 'a' came on start state A, then we will go to state B
- then input came as 'b', so on state B we will go to state C.
- then 'a', we go to C itself
- then 'b', we go to C itself
- At the end of the string we are at final state, so we say that string is accepted
- And for the strings which are less than 2, you will not reach final state.

**Question 3**Construct a DFA which accepts set of all strings over Σ={a,b} of length ≤2

**Explanation**

We have to create DFA which will accept string of length ≤ 2 on alphabet {a, b}.

- Think about the language content = {∈,a, b, ab, ba, bb, aa}
- So again first thing about creating DFA which will accept of ∈, that is first state will be final state
- Then we have to accept single letter that is two states will be required and second state also will be final state
- Now we need to accept 2 input symbols one after another in case of aa or ab or bb or ba
- And strings which are greater than 3 should not be accepted.
- So we have to attach 1 more state as dead state.

**Testing Example 1**

- Suppose we have ∈ (empty string), so take alphabets from left to right
- It will accepted as first state is final state and that means on nothing you are at final state.

**Testing Example 2**

- Suppose string is ab
- firstly 'a' came on start state A, then we will go to state B
- then input came as 'b', so on state B we will go to state C.
- At the end of the string we are at final state C, so we say that string is accepted

**And for the strings which are less than 2, you will not reach final state.**

So in general we can say that:

n+2Length of String | No of states |

|w|=n | n+2 |

|w|≥n | n+1 |

|w|≤n |

#### Example 4

Create a DFA which accepts strings of even length**Explanation**

As we can see that length of string should be even for that language will be = {Σab, aa, bb, ba, aaaa, bbbb, ….}

And the language is infinite.

#### Testing

- Take a string ‘abbb’ to test whether it is accepted in the above DFA
- Scan string from left to right
- first input we get is
**a**and from start state on ‘a’ we go to the second state - second input is b, from second state on b we will go to start state again.
- third input is b, from start state we go to second state.
- from second state we go to first state on fourth input symbol.
- Now we reached end of string and we are standing on final state.

### Examples

- DFA which accepts strings of odd length
- Design a DFA over w ∈ {a,b}
^{*}such that number of a = 2 and there is no restriction over length of b - DFA for Number of a(w) mod 2 = 0 and Number of b(w) mod 2 = 0
- DFA for Number of a(w) mod 2 = 0
**or**Number of b(w) mod 2 = 0 - DFA for Number of a(w) mod 2 != 0
**and**Number of b(w) mod 2 != 0 - DFA for Number of a(w) mod 3= 0
**and**Number of b(w) mod 3= 0 - DFA for Number of a(w) mod 3 > Number of b(w) mod 3
- DFA for binary number divisible by 2
- DFA for binary number divisible by 3
- DFA in which set of all strings can be accepted which start with a
- DFA in which set of all strings can be accepted which contains ‘a’
- DFA in which set of all strings can be accepted which end with ‘a’
- DFA in which set of all strings can be accepted which start with ab
- DFA in which set of all strings can be accepted which contain ab
- DFA in which set of all strings can be accepted which ends with ab
- DFA in which start and end symbol must be different
- Design a DFA in which start and end symbol must be same
- DFA in which every 'a' should be followed by 'b'
- DFA in which every 'a' should never followed by 'b'
- DFA in which every 'a' should followed by 'bb'
- DFA in which every 'a' should never followed by 'bb'
- DFA for a
^{n}b^{m}| n,m ≥ 1 - DFA for a
^{n}b^{m}| n,m ≥ 0 - DFA for a
^{n}b^{m}c^{l}| n,m,l ≥ 1 - DFA for a
^{n}b^{m}c^{l}| n,m,l ≥ 0 - DFA such that second sybmol from L.H.S. should be 'a'