Chapter 1 Automata and Complexity Theory
Chapter 1 Automata and Complexity Theory
Institute of Technology
Department of Computer Science
Automata and Complexity Theory
(Theory of Computation)
2016 E.C
Theory of Computation
Automata Theory
computational complexity.
The type of computational problem:
The most commonly used problems are decision problems.
based on
non-deterministic Turing machines,
The resource (or resources) that is being bounded and the bound: These two
properties are usually stated together, such as "polynomial time", "logarithmic space",
"constant depth", etc.
The set of decision problems solvable by a deterministic/non deterministic
Turing machine within time f(n). (This complexity class is known as P and NP
class with PTIME(f(n)) and NTIME(f(n)) respectively.)
Major Classes
• All Languages
Decidable
Languages
All
Languages
Decidable
Languages
O (n ) O (n 2 ) O (2n )
O ( n log n )
FA<PDA<LBA<TM
FA≡ PDA with finite Stack ≡ TM with finite tape ≡ TM with
unidirectional tape ≡ TM with read only tape
state
• 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.
Turing Machine ≡ PDA with additional Stack ≡
• For implementation of neural networks. FA with 2 Stacks
• For implementation of Robotics Applications.
• For implementation of artificial intelligence.
The main motivation behind developing the automata theory was to develop
by arrows.
Automata is the kind of machine which takes some string as input and this input
goes through a finite number of states and may enter in the final state.
Examples:
Binary: ∑ = {0,1}
All lower case letters: ∑ = {a,b,c,..z}
Alphanumeric: ∑ = {a-z, A-Z, 0-9}
DNA molecule letters: ∑ = {a,c,g,t}
…
∑* = ∑0 U ∑1 U ∑2 U …
∑ + = ∑1 U ∑ 2 U ∑ 3 U …
NO
Tuesday, 17 October, 2023 48
The Membership Problem
Given a string w ∑*and a language L over ∑, decide
whether or not w L.
Example:
Let w = 100011
Q) Is w the language of strings with equal number of
0s and 1s?
one of the final states; we indicate this by drawing two concentric circles.
As always, the string to be examined is read from left to right, one character at each step; when
the first symbol is a letter, the automaton goes into state 2, after which the rest of the string can
be either letter or digit.
Finally, when the entire string is processed the state 2 “accepts” the string or represents the
“yes” state of the automaton.
Conversely, if the first symbol is a digit, the automaton will go into state 3, the “reject” or “no”
state, and remain there.
In our solution we assume that no input other than letters or digits is possible.
Tuesday, 17 October, 2023 53
Deterministic Finite Automata - Definition
A Deterministic Finite Automaton (DFA) consists of:
Q ==> a finite set of states
∑ ==> a finite set of input symbols (alphabet)
q0 ==> a start state
F ==> set of accepting states
δ ==> a transition function, which is a mapping between Q x ∑ ==> Q
A DFA is defined by the 5-tuple:
{Q, ∑ , q0,F, δ }
54
What does a DFA do on reading an input
string?
Input: a word w in ∑*
Question: Is w acceptable by the DFA?
Steps:
Start at the “start state” q0
For every input symbol in the sequence w do
Compute the next state from the current state, given the current input symbol in
w and the transition function
If after all symbols in w are consumed, the current state is one of the
accepting states (F) then accept w;
Otherwise, reject w.
55
Regular Languages
Let L(A) be a language recognized by a DFA A.
Then L(A) is called a “Regular Language”.
56
The Chomsky Hierachy
• A containment hierarchy of classes of formal languages
Regular Context-
(DFA) free Context-
Recursively-
(PDA) sensitive
enumerable
(LBA)
(TM)
57
Example #1
Build a DFA for the following language:
L = {w | w is a binary string that contains 01 as a substring}
Steps for building a DFA to recognize L:
∑ = {0,1}
Decide on the states: Q
Designate start state and final state(s)
δ: Decide on the transitions:
“Final” states == same as “accepting states”
Other states == same as “non-accepting states”
58
Regular expression: (0+1)*01(0+1)*
1 0,1 • ∑ = {0,1}
0
• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Accepting • Transition table
state symbols
0 1
q0 q1 q0
states
• What if the language allows q1 q1 q2
empty strings? *q2 q2 q2
59
Example #2
Clamping Logic:
A clamping circuit waits for a ”1” input, and turns on forever. However, to avoid
clamping on spurious noise, we’ll design a DFA that waits for two consecutive 1s in
a row before clamping on.
Build a DFA for the following language:
L = { w | w is a bit string which contains the substring 11}
State Design:
q0 : start state (initially off), also means the most recent input was not a 1
q1: has never seen 11 but the most recent input was a 1
q2: has seen 11 at least once
60
Example #3
Build a DFA for the following language:
L = { w | w is a binary string that has even number of 1s
and even number of 0s}
?
61
Extension of transitions (δ) to Paths (δ)
δ (q,w) = destination state from state q on input string
w
δ (q,wa) = δ (δ(q,w), a)
δ (q0,wa) = ?
62
Language of a DFA
A DFA A accepts string w if there is a path from q0 to an
accepting (or final) state that is labeled by w
63
Deterministic Finite Automata(DFA)
The first type of automaton we study in detail are Finite Automata
that are deterministic in their operation.
A DFA is defined by the Quintuple (5-Tuples)
M = (Q, Σ, δ, q0, F)
Where, Q is finite set of internal states
Σ is a finite set of symbols called the input alphabet
δ : QX∑ → Q is a total function called the transition
function
q0 ∈ Q is the initial state
Tuesday, 17 October, 2023
F ⊆ Q is a set of Final states 64
Deterministic Finite Automaton (DFA)
Input Tape
String
Output
“Accept”
Finite or
Automaton “Reject”
c b c
• Its graphical representation would be as follows −
72
Languages and DFA
Having made a precise definition of an automaton, we are now ready to define formally what we
mean by an associated language.
The association is obvious: the language is the set of all strings recognized by the automaton.
The language accepted by a DFA M = (Q, Σ, δ, q0, F) is the set of all strings on Σ accepted by M.
In formal notation, L(M) = {w ∈ Σ* : δ*(q0, w) ∈ F}
A DFA will process every string in Σ* and either accept it or not accept it.
Non acceptance means that the DFA stops in a non-final state, so that, L(M) = {w ∈ Σ* : δ*(q0, w)
∉ F}
Example: Consider the DFA shown in the figure below:
a b b
1 a, b 1
Start 0 b a Start 0 a
a 2 b 2 a, b
The Execution of DFA for input string w A* begins at the start state and follows a path whose edges concatenate to w.
The DFA accepts w if the path ends in a final state. Otherwise the DFA rejects w. The language of a DFA is the set of
accepted strings.
Example. The example DFA accepts the strings
a, b, ab, bb, abb, bbb, …, abn, bbn, ….
So the language of the DFA is given by the regular expression (a + b)b*.
Tuesday, 17 October, 2023 74
Languages and DFA …Cont’d
In the above figure, two labels were allowed on a single edge. Such multiple labeled edges are short
hand for two or more distinct transitions: the transition is taken whenever the input symbol matches
any of the edge labels.
For example, the automaton in the above figure remains in its initial state q0 until the first ‘b’ is
encountered.
If this is also the last symbol of the input, then the string is accepted.
If not, the DFA goes into state q2, from which it can never escape. Such a state is called a trap
state.
We can see clearly from the graph that the automaton accepts all strings which have exactly one b,
located at the rightmost position of the string.
All other input strings are rejected. In set notation, the language accepted by this automaton is
L = {an b| n ≥ 0}
1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk
79
Non-deterministic Finite Automata (NFA)
A Non-deterministic Finite Automaton (NFA) consists of:
Q ==> a finite set of states
∑ ==> a finite set of input symbols (alphabet)
q0 ==> a start state
F ==> set of accepting states
δ ==> a transition function, which is a mapping between Q x ∑ ==>
subset of Q
An NFA is also defined by the 5-tuple:
{Q, ∑ , q0,F, δ }
80
How to use an NFA?
Input: a word w in ∑*
Question: Is w acceptable by the NFA?
Steps:
Start at the “start state” q0
For every input symbol in the sequence w do
Determine all possible next states from all current states, given the current input symbol
in w and the transition function
If after all symbols in w are consumed and if at least one of the current states is a
final state then accept w;
Otherwise, reject w.
81
Regular expression: (0+1)*01(0+1)*
states
an input of 0 is received? q1 Φ {q2}
*q2 {q2} {q2}
82
Note: Omitting to explicitly show error states is just a matter of design convenience
(one that is generally followed for NFAs), and
w h i l e
q0 q1 q2 q3 q4 q5
w h i l e
q0 q1 q2 q3 q4 q5
84
Extension of δ to NFA Paths
Basis: δ (q,) = {q}
Induction:
Let δ (q0,w) = {p1,p2…,pk}
δ (pi,a) = Si for i=1,2...,k
Then, δ (q0,wa) = S1 U S2 U … U Sk
85
Language of an NFA
An NFA accepts w if there exists at least one path
from the start state to an accepting (or final) state that
is labeled by w
L(N) = { w | δ(q0,w) ∩ F ≠ Φ }
86
Advantages & Caveats for NFA
Great for modeling regular expressions
String processing - e.g., grep, lexical analyzer
87
Technologies for NFAs
88
But, DFAs and NFAs are equivalent in their power to capture langauges !!
89
Equivalence of DFA & NFA
Theorem:
Should be A language L is accepted by a DFA if and only if it is accepted by an NFA.
true for Proof:
any L 1. If part:
Prove by showing every NFA can be converted to an equivalent DFA (in the next few slides…)
90
Nondeterministic Finite Automata (NFA)
State State 0 1
s -At each state there are many
next states and many
a {a, b} {b} outputs(paths) at same time.
b {c} {a, c}
c {b, c} {c}
q1 {q0, q1} {}
q2 {} {q1, q2}
{q2} {q2}
T a b L
start 0 {1, 2} {1}
1 {1} {2}
final 2
q1 {} {q2}
q2 {q2} {q2}
q3 {q4} {}
q4 {q4} {q4}
Note that there are two major differences between the above NFA
definition and the definition of a DFA.
1. In a NFA, the range of δ is the power set 2Q, so that its value is
not a single element of Q, but a subset of it. This subset defines
the set of all possible states that can be reached by the transition.
Example, δ(q1, a) = {q0, q2}.
2. Also we allow ε (null) as the second argument of δ. This means
that the NFA can make a transition without consuming an input
symbol.
Tuesday, 17 October, 2023 97
NFA Vs. DFA
Like DFAs, NFA can also be represented by transition graphs.
The vertices are determined by Q, while an edge (qi, qj) with label ‘a’ is in the graph if and only if δ(qi, a) contains
qj.
Note that since ‘a’ may be the empty string, there can be some edges labeled ε.
A string is accepted by an NFA if there is any sequence of possible moves that will put the machine in a final state
at the end of the string.
Non-determinism can therefore be viewed as involving “intuitive” insight by which the best move can be chosen
at every stage (assuming that the NFA wants to accept every string).
For example consider the transition graph shown in the figure below. It describes a nondeterministic automaton
since there are two transitions labeled ‘a’ out of q0.
For any given language, we can usually find an unlimited number of equivalent
automata – deterministic or not.
The DFA shown in Fig.(a) is equivalent to the NFA shown in Fig.(b) since,
they both accept the language {(10)n/n≥0}.
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}
• Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}
• Strings not accepted by the above DFA: {1, 011, 111, ........}
quite limited.
A language L is called regular if and only if there exists some deterministic
for 2023
Tuesday, 17 October, it. 107
Regular Languages …
For example: Show that the language L={awa|w ∈ {a,b}*} is regular.
Algorithm
Input − An NDFA
Output − An equivalent DFA
Step 1 − Create state table from the given NDFA.
Step 2 − Create a blank state table under possible input alphabets for the equivalent
DFA.
Step 3 − Mark the start state of the DFA by q0 (Same as the NDFA).
Step 4 − Find out the combination of States {Q0, Q1,... , Qn} for each possible input
alphabet.
Step 5 − Each time we generate a new DFA state under the input alphabet columns,
we have to apply step 4 again, otherwise go to step 6.
Step 6 − The states which contain any of the final states of the NDFA are the final
states of the equivalent DFA.
below.
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Tuesday, 17 October, 2023 110
Conversion NFA to DFA
• Using the above algorithm, we find its equivalent DFA. The state table of the DFA is shown in
below.
q δ(q,0) δ(q,1)
[a] [a,b,c,d,e] [d,e]
[a,b,c,d,e] [a,b,c,d,e] [b,d,e]
[d,e] [e] ∅
[b,d,e] [c,e] [e]
[e] ∅ ∅
[c, e] ∅ [b]
[b] [c] [e]
[c] ∅ [b]