Day 4 - Finite Automata
Day 4 - Finite Automata
Day 4 - Finite Automata
Yared Y.
automata
• Automata (plural of automaton) are mathematical
models of computation that describe how a system
moves between different states in response to
inputs.
• They are abstract machines that can be in one of a
finite number of states at any given time.
• Automata are used to define formal languages, which
are sets of strings over a given alphabet.
• A language can be considered as a set of valid
sequences that a machine (automaton) can recognize
or generate.
• Automata theory helps us understand how machines
process inputs & recognize patterns, which is crucial in
designing compilers and other software that interprets
and processes programming languages.
Finite Automata
Finite Automata (FA) are the simplest type of automaton,
consisting of:
1.Finite Set of States: The automaton can only be in one state at a
time, and there are a limited number of states it can be in.
2.Alphabet: A finite set of symbols (inputs) that the automaton can process.
3.Transition Function: Rules that describe how the automaton moves
from one state to another based on the input symbol.
4.Start State: The state in which the automaton begins processing.
5.Accept States: States that represent a successful recognition of a valid
string in the language. If the automaton ends in an accept state after processing
an input string, the string is considered valid (or accepted).
Types of FA
• Deterministic Finite Automata (DFA):
• In a DFA, for each state and input symbol, there is exactly one transition to
another state.
• This means that given a state and an input symbol, the next state is uniquely
determined.
• No epsilon (ε) transitions (transitions that do not consume input symbols).
δ :Q x Σ → Q
• A Finite automation has a set of states, and its "control"
moves from one state to state in response to external
"inputs."
• One of the crucial distinctions among classes of finite
automata is whether that control is "deterministic, "
meaning that the automation cannot be in more than
one state at any one time, or "nondeterministic,"
meaning that it may be in several states at once.
Non-deterministic Finite Automata
(NFA)
• More than one transition occurs for any input symbol
from a state.
• Transition can occur even on empty string (Ɛ).
Deterministic Finite Automata (DFA)
• For each state and for each input symbol, exactly one
transition occurs from that state.
• Finite automata is a state machine that takes a string
of symbols as input and changes its state accordingly.
Finite automata is a recognizer for regular expressions.
• When a regular expression string is fed into finite
automata, it changes its state for each literal. If the
input string is successfully processed and the automata
reaches its final state, it is accepted, i.e., the string just
fed was said to be a valid token of the language in
hand.
• The mathematical model of finite automata consists of:
• Finite set of states (Q)
• Finite set of input symbols (Σ)
• One Start state (q0)
• Set of final states (qf)
• Transition function (δ)
• The transition function (δ) maps the finite set of state
(Q) to a finite set of input symbols (Σ), Q × Σ ➔ Q
Finite Automata Construction
• Let L(r) be a regular language recognized by some finite automata (FA).
• States : States of FA are represented by circles. State names are written inside
circles.
• Start state : The state from where the automata starts, is known as the start state.
Start state has an arrow pointed towards it.
• Intermediate states : All intermediate states have at least two arrows; one pointing
to and another pointing out from them.
• Final state : If the input string is successfully parsed, the automata is expected to be
in this state. Final state is represented by double circles. It may have any odd number
of arrows pointing to it and even number of arrows pointing out from it. The number
of odd arrows are one greater than even, i.e. odd = even + 1.
• Transition : The transition from one state to another state happens when a desired
symbol in the input is found. Upon transition, automata can either move to the next
state or stay in the same state. Movement from one state to another is shown as a
directed arrow, where the arrows points to the destination state. If automata stays on
the same state, an arrow pointing from a state to itself is drawn.
regexps
• A regular expression is a sequence of symbols used to
specify lexeme patterns. A regular expression is helpful
in describing the languages that can be built using
operators such as union, concatenation, and closure
over the symbols.
• A regular expression ‘r’ that denotes a language L(r) is
built recursively over the smaller regular expression
using the rules given below.
• The following rules define the regular expression over some alphabet Σ
and the languages denoted by these regular expressions.
( a + b )2 corresponds to the language {aa, ab, ba, bb}, that is the set of strings of length 2 over
the alphabet {a, b}.
In general ( a + b )k corresponds to the set of strings of length k over the alphabet {a, b}. ( a +
b )* corresponds to the set of all strings over the alphabet {a, b}.
• a*b* corresponds to the set of strings consisting of zero or more a's followed by zero or more b's.
• a*b+a* corresponds to the set of strings consisting of zero or more a's followed by one or more
b's followed by zero or more a's.
• ( ab )+ corresponds to the language {ab, abab, ababab, ... }, that is, the set of strings of repeated
ab's.
FINITE AUTOMATA in compiler
design
• It’s the recognizer for the language.
• Finite automata (FA) can be defined as a recognizer that
identifies whether the input string represents the
regular language.
• The finite automata accept the input string if the input string
is a regular expression representing the regular language
else it rejects it.
• Types of FA:
Deterministic FA
Non-deterministic FA
DFA
• In deterministic finite automata in response to a single
input alphabet, the state transits to only one state. In
DFA, no null moves are accepted.
• The DFA have five tuples as discussed below:
{Q, Σ, q0, F, δ}
Q = set of all states
Σ = Set of all input alphabets
q0 = initial state
F = Set of Final State
δ = Transition function
Q = a finite number of states (set of all states)
Σ = set of symbols (inputs)
q0 = an initial start state where the first input is given or
processed
F = a final state or states, known as accepting states.
δ = a function that operates the transition between
states of each
symbol
In the table, you can see a transition function that maps
Q x Σ Q
Write the 5 tuples of the following DFA
diagram
(states, transitions, inputs)
a transition function that maps Q x
Σ Q
the 5 tuples of the DFA diagram
• Q = {A, B, C, D} ---set of all states
• Σ = {0, 1}
• q0 = A
• F = {D}
•δ=
Design DFA
• L1 = Set of all strings that start with 0
Solution
• Alphabet: {0,1} (binary alphabet).
• Elements {0, 00, 01, 000, 010, 011, 0000, ....}
Solution
• Construct a DFA that accepts sets of all strings over
{0,1} of length 2
• Solutions:
alphabet = {0,1}
set of strings = {00, 01, 10, 11}
Design a FA with ∑ = {0, 1} accepts those
string which starts with 1 and ends with 0.
• Solution:
Example: {10, 100, 110, 1100, …. 11110000, }
The FA will have a start state q0 from which only the
edge with input 1 will go to the next state.
In state q1, if we read 1, we will be in state q1, but if we
read 0 at state q1, we will reach to state q2 which is the
final state. In state q2, if we read either 0 or 1, we will go
to q2 state or q1 state respectively. Note that if the input
ends with 0, it will be in the final state.
Tabular representation of the FA that
accepts strings over Σ={0,1} that
start with '1’ & end with '0'
Design a FA with ∑ = {0, 1} accepts
the only input 101.
Design FA with ∑ = {0, 1} accepts
the set of all strings with three
consecutive 0's.
Design a FA with ∑ = {0, 1} accepts the strings
with an even number of 0's followed by single 1.
• Construct a DFA that accepts any strings over {a, b}
that contains the string aabb in it.
solution
alphabet = {a, b}
Tabular representation
• Construct a DFA that accepts any strings over {a, b}
that does not contain the string aabb in it.
• solution
• alphabet = {a, b}
• From the previous problem,
• flip the states
• make the final state into non-final state and make the
non-final states into final states
Figure out what the ff DFA
recognizes
solution
• The DFA can be represented by the transition graph. In
the transition graph, the nodes of the graph represent
the states and the edges represent the transition of one
state to another.
Example of DFA (Graphical one)
• In the below example of DFA (see graph), you can notice
that with a single input ‘a’ state ‘0’ transit to only one
state ‘1’.
The transition table for the above DFA is as below:
Th DFA to recognize the language
(a|b)* ab
• 0 is the start state q0
• {2} is the set of final states f
• Σ = {a, b}
• Q = {0, 1, 2}
Transition Function
a b
0 1 0
1 1 2
2 1 0
Group Assignment: In a week: 10
marks
(Every Group should do this)
• 1.
Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1’s.
• 2.
Design a Finite Automaton (FA) that accepts strings over the alphabet Σ={a,b} where
the string contains the substring "ab" exactly twice, and no other substring "ab".
• 3.
Design a Finite Automaton (FA) that accepts strings over the alphabet Σ={0,1} where
the string contains an even number of 0's and an odd number of 1's, and the
substring "101" appears at least once.
• 4.
Design a Finite Automaton (FA) that accepts strings over the alphabet Σ={0,1} where
the number of 0's and 1's are both equal, and the string ends with the substring
"110".
How to do and deliver it?
• Use drawing tools: Lucidchart, Creatly, Visio, Figma
• Make it in pdf format and attach it in google classroom
Non-deterministic Finite
Automata
• In non-deterministic finite automata, in response to a
single input alphabet, a state may transit to more than
one state. The NFA also accept the NULL move.
• In the above example of NFA, you can notice that the
state ‘0’ with the input symbol ‘a’ can either transit to
itself or to state ‘1’. Now let us create a transition table
for the NFA above.
Examples: NFA
0 is the start state q0
{2} is the set final states F
Σ = {a, b}
Q = {0, 1, 2}
Transition Function
A b
0 {0, 1} 0
1 {} 2
2 {} {}
Example: Draw the state transition
diagram for the following DFA
• Q = { 0, 1, 2 }, Σ= { a }, F = { 1 }, the initial state is 0
and δ is as shown in the following table.