# Deterministic Finite Automata – DFA

A Deterministic Finite Automaton (DFA) is a theoretical model used in computer science and mathematics to recognize and process strings of symbols based on a set of rules. It is a finite state machine that operates in a deterministic manner, meaning that for each input symbol, there is exactly one transition to the next state.

Here are the key components of a DFA:

1. States (Q): A DFA has a finite set of states, often represented as Q. Each state represents a unique condition or situation the machine can be in.
2. Alphabet (Σ): The alphabet is the set of input symbols that the DFA can read. It is denoted as Σ.
3. Transition Function (δ): The transition function δ specifies the rules that dictate how the DFA moves from one state to another in response to input symbols. It is defined as δ: Q × Σ → Q, which means that for each state and input symbol, there is exactly one next state.
4. Start State (q0): The start state q0 is the initial state where the DFA begins processing the input string.
5. Accept States (F): The accept states, also known as final states, are a subset of the states in Q. When the DFA reaches an accept state after processing the entire input string, it accepts the string as part of the language the DFA recognizes.

The operation of a DFA is straightforward:

• It starts in the initial state (q0).
• For each input symbol, it follows the transition function to move to the next state.
• After processing the entire input string, if the DFA is in an accepted state, it accepts the string; otherwise, it rejects it.

DFAs are used in various applications, such as lexical analysis in compilers, string matching, and pattern recognition. They are particularly suitable for recognizing regular languages, which are a class of formal languages defined by regular expressions. Regular languages are characterized by their simplicity and can be described using DFAs.

## Deterministic Finite Automaton representation

A Deterministic Finite Automaton (DFA) can be represented visually and formally using several components:

1. State Diagram or Transition Diagrams: A common way to represent a DFA is through a state diagram which is also called Transition Diagrams, which is a graphical depiction of the DFA’s states and transitions. In a state diagram:
• States are represented as circles or nodes, often labeled with names or identifiers (e.g., q0, q1, q2).
• Transitions between states are represented as arrows or edges, labeled with the input symbols that trigger the transitions (e.g., ‘a’ or ‘b’).
• The start state is indicated by an arrow pointing to it from nowhere, or it may be labeled explicitly.
• Accept states (or final states) are typically marked with a double circle or another distinguishing feature.

Here’s an example of a simple DFA represented as a state diagram:

   State Diagram:

--> q0 (Start)
|   /  \
a  /    \ b
| /      \
v        v
q1 (Accept) q2

1. Transition Table: Another way to represent a DFA is through a transition table. The table lists all the states, the alphabet symbols, and the next state for each combination of state and symbol. It is a concise way to define the DFA formally. Here’s an example of a transition table for the DFA represented in the state diagram above:
   State |   a   |   b
-------|-------|-------
q0    |   q1  |   q2
q1    |   q1  |   q2
q2    |   q2  |   q2

1. Formal Definition: A DFA can also be formally defined using mathematical notation. This includes specifying the set of states, alphabet, transition function, start state, and set of accept states. For example:
• Set of states (Q): {q0, q1, q2}
• Alphabet (Σ): {a, b}
• Transition function (δ):
• δ(q0, a) = q1
• δ(q0, b) = q2
• δ(q1, a) = q1
• δ(q1, b) = q2
• δ(q2, a) = q2
• δ(q2, b) = q2
• Start state (q0)
• Accept states (F): {q1}

These representations help describe and define the behavior of a DFA, making it easier to understand, analyze, and implement the automaton for various applications in computer science and formal language theory.

## Extended transition function

The extended transition function, often denoted as δ*, is a concept used in automata theory and formal language theory to describe the behavior of finite automata (including Deterministic Finite Automata or DFAs and Non-Deterministic Finite Automata or NFAs) over strings of input symbols. The extended transition function allows us to determine the resulting state of an automaton after processing a sequence of input symbols.

The extended transition function δ* is defined as follows:

• δ*(q, ε) = q, where ε represents the empty string, and q is the initial state.
• δ*(q, w) = p, where q is the initial state, w is a string of input symbols (a sequence of characters), and p is the state reached after processing the entire string w.

In simpler terms, δ*(q, w) provides the state that the automaton will be in after reading the input string w, starting from the initial state q.

Here’s a step-by-step explanation of how to compute δ*:

2. For each symbol in the input string w, apply the transition function δ to determine the next state. Repeat this process for each symbol in the string.
3. After processing the entire string w, the resulting state is the output of δ*.

For example, consider a simple DFA with the following transition table:

State |   a   |   b
-------|-------|-------
q0   |   q1  |   q2
q1   |   q1  |   q2
q2   |   q2  |   q2


Let’s compute δ*(q0, “aab”):

• Read ‘a’: δ(q0, ‘a’) = q1.
• Read ‘a’: δ(q1, ‘a’) = q1.
• Read ‘b’: δ(q1, ‘b’) = q2.

After processing the entire string “aab,” the resulting state is q2. Therefore, δ*(q0, “aab”) = q2.

The extended transition function δ* is a fundamental concept in automata theory as it allows us to determine the final state of an automaton when processing strings, which is crucial for recognizing and accepting strings in formal language theory.

Example

Let’s consider an example of a Deterministic Finite Automaton (DFA) and calculate the extended transition function δ* for a given input string.

DFA Example:
Suppose we have a DFA that recognizes strings over the alphabet {0, 1} where the string should end with “01”. The DFA has three states: q0, q1, and q2.

Transition Table:

State |   0   |   1
-------|-------|-------
q0   |   q0  |   q1
q1   |   q2  |   q1
q2   |   q0  |   q1

• q0 is the initial state.
• q1 is an accept (final) state.
• q2 is a non-accept state.

Input String: Let’s calculate δ*(q0, “110101”).

Now, let’s compute the extended transition function δ* step by step:

1. Start with the initial state q0: δ*(q0, ε) = q0, where ε represents the empty string.
2. Read the first symbol ‘1’: δ(q0, ‘1’) = q1. So, δ*(q0, “1”) = q1.
3. Read the next symbol ‘1’: δ(q1, ‘1’) = q1. So, δ*(q0, “11”) = q1.
4. Read ‘0’: δ(q1, ‘0’) = q2. So, δ*(q0, “110”) = q2.
5. Read ‘1’: δ(q2, ‘1’) = q1. So, δ*(q0, “1101”) = q1.
6. Finally, read ’01’: δ(q1, ’01’) = q1. So, δ*(q0, “110101”) = q1.

After processing the entire string “110101,” the resulting state is q1. Therefore, δ*(q0, “110101”) = q1.

In this example, the extended transition function δ* helped us determine that the DFA accepts the input string “110101” because it ended in an accept state (q1), which satisfies the criteria of ending with “01.”

## The Language of a DFA

The language of a Deterministic Finite Automaton (DFA) is the set of all strings that the DFA can recognize and accept. In other words, it’s the collection of input strings for which the DFA, when started in its initial state, ends up in an accept state after processing the entire string.

The language of a DFA is formally defined as follows:

Let’s denote the DFA as a tuple (Q, Σ, δ, q0, F), where:

• Q is the set of states.
• Σ is the alphabet, the set of input symbols.
• δ is the transition function, specifying how the DFA moves between states based on input symbols.
• q0 is the initial state where the DFA starts.
• F is the set of accept states or final states.

The language recognized by the DFA is represented as L(DFA) and is defined as:

L(DFA) = {w ∈ Σ* | δ*(q0, w) ∈ F}

In this formula:

• Σ* represents the set of all possible strings over the alphabet Σ.
• δ* is the extended transition function, which determines the final state after processing the string w, starting from the initial state q0.
• δ*(q0, w) represents the state reached by the DFA after processing the string w.
• F represents the set of accept states.

In simpler terms, the language of the DFA is the set of all strings (in Σ*) for which, when you feed them to the DFA starting from the initial state q0, the DFA ends up in one of the accept states (in F).

For example, if you have a DFA that recognizes binary strings that end with ‘0’, its language would be L(DFA) = {w ∈ {0, 1}* | w ends with ‘0’}. This means that the DFA recognizes and accepts all binary strings that end with ‘0’, and that’s its language.

## More Examples of Deterministic Finite Automata

Here are five examples of Deterministic Finite Automata (DFAs) with different purposes and characteristics:

1. Binary String Ends with ’01’ DFA:
• Language Recognized: Binary strings that end with ’01’.
• States: {q0, q1}
• Alphabet: {0, 1}
• Transition Function:
• δ(q0, 0) = q0
• δ(q0, 1) = q1
• δ(q1, 0) = q0
• δ(q1, 1) = q1
• Start State: q0
• Accept State: q1
1. Even Number of ‘0’s and ‘1’s DFA:
• Language Recognized: Strings with an even number of ‘0’s and ‘1’s.
• States: {q0, q1}
• Alphabet: {0, 1}
• Transition Function:
• δ(q0, 0) = q1
• δ(q0, 1) = q0
• δ(q1, 0) = q0
• δ(q1, 1) = q1
• Start State: q0
• Accept State: q0
1. Simple Vending Machine DFA:
• Purpose: Recognizing valid coin inputs for a vending machine (e.g., accepting only quarters and dimes).
• States: {q0, q1}
• Alphabet: {quarter, dime}
• Transition Function:
• δ(q0, quarter) = q1
• δ(q0, dime) = q1
• δ(q1, quarter) = q1
• δ(q1, dime) = q1
• Start State: q0
• Accept State: q1
• Purpose: Validating user passwords that meet certain criteria.
• States: {q0, q1, q2}
• Alphabet: {a-z, A-Z, 0-9}
• Transition Function:
• δ(q0, any symbol) = q1
• δ(q1, any symbol) = q1
• δ(q1, any symbol) = q2
• Start State: q0
• Accept State: q2
1. Traffic Light Control DFA:
• Purpose: Controlling the sequence of traffic lights (e.g., red, green, yellow) in a traffic signal.
• States: {Red, Green, Yellow}
• Alphabet: {Timer Expiry, Manual Control}
• Transition Function:
• δ(Red, Timer Expiry) = Green
• δ(Green, Timer Expiry) = Yellow
• δ(Yellow, Timer Expiry) = Red
• δ(any state, Manual Control) = the corresponding state
• Start State: Red
• Accept State: None (as it’s a control DFA)

These examples showcase DFAs for various applications, including language recognition, vending machines, password validation, and traffic light control. Each DFA has a specific set of states, alphabet, transition rules, and purpose.