Complexity and Computability Theory I
Lecture #4
Rina Zviel-Girshin
Leah Epstein
Winter 2002-2003
Rina Zviel-Girshin @ASC 2
Overview
NondeterminismExamplesEquivalence of the nondeterministic model
to the deterministic model
Rina Zviel-Girshin @ASC 3
Nondeterminism (briefly)
• Each state can be accepting or not.• The automaton reads one input word symbol at
each time unit.• An NFA also can change it state without reading
an input - transition• If a state in which it stops reading an input
(reading it till the end) is an accepting state- the automaton accepts (recognizes) the input.
Rina Zviel-Girshin @ASC 4
Nondeterminism (briefly)• On each symbol:
– it moves to a new state (defined by the transition function) as a function of:
• the current state
• the symbol read or an transition
qj qkqj
qk
qi
• or halts if no valid move definedqj ?
Rina Zviel-Girshin @ASC 5
Formal Definition
A finite automaton is a 5-tuple (Q, , , q0, F), where:
• Q - a finite set of states - alphabet - transition function - Q()2|Q| or P(Q)(given a
state and an input symbol - what can be the next state)
• q0 - q0Q is the start state
• F - FQ the set of accept states
Rina Zviel-Girshin @ASC 6
DFA: ExampleConsider the following language: L = (ab aba)*
The deterministic automaton :
q0 a q1
q4
a
a,b
q0
b
b q1q2a
q1q3
b
b
a
Rina Zviel-Girshin @ASC 7
NFA: ExampleThe nondeterministic automaton :
q0a
q1
q2
b
q0
a
b
This simplifies the deterministic automaton.
The basic idea of the construction:
•An aba path
•An ab path
Rina Zviel-Girshin @ASC 8
How does an NFA compute?
• A nondeterministic computation is a tree of possibilities.
• The root of the tree is a start state of the automaton.
• Each branching point is a computation at which automaton has multiple choices.
• An NFA accepts an input string if there exists at least one computation that leads to an accept state.
Rina Zviel-Girshin @ASC 9
NFA vs. DFA
• The difference between a deterministic finite automaton DFA and a non-deterministic one NFA is the number of paths in a computation tree.
• In DFA’s we have only one path – one chain.
• In NFA’s we have a tree.
Rina Zviel-Girshin @ASC 10
Deterministiccomputation
Nondeterministiccomputation
accept orreject
accept
reject
.
.
....
.
.
.
Rina Zviel-Girshin @ASC 11
The language of an NFA • Informally:
The words that an NFA A accepts (recognizes), i.e. when reading them it stops in an accepting state.
• Formally:
L(A)={w | w*, ’( q0,w)F}
means:
There exist a path from q0 to qiF for
the input string w.
Rina Zviel-Girshin @ASC 12
Example
Construct a nondeterministic finite automaton for the following language:
L = { w1 | w is over *={0,1}*}
A:
q0
0,1
q1
Rina Zviel-Girshin @ASC 13
What does A do on the input w=110?
q01
q1q1
1
q01
q1q1
1
q0
0
q0
no movefor 1
no movefor 0
Rina Zviel-Girshin @ASC 14
What does A do on the input w=101?
q01
q1q1
1
q0
0
q0
1
no movefor 0
q1q1
Rina Zviel-Girshin @ASC 15
Additional examples
Construct a nondeterministic finite automaton recognizing the following language:
L={w | w(={0,1,2})*,
w starts with 01 and ends with 1}
q0 0 q1 q4q2
0,2
11
q4
0,2
1
Rina Zviel-Girshin @ASC 16
Additional examples
Construct a nondeterministic finite automaton recognizing the following language:
L={0i1j | i mod 3 = 1, j mod 2 = 1}
q0 0 q1 0 q2
0
0i
i mod 3 = 1
Rina Zviel-Girshin @ASC 17
Additional examples
q0 0 q1
q4q3
1
1
q4
0 q2
0
1
L={0i1j | i mod 3 = 1, j mod 2 = 1}
0i1j i mod 3 = 1j mod 2 = 1
Rina Zviel-Girshin @ASC 18
Equivalence of FA
Definition:
Two automata are equivalent if they
recognize the same language.
Rina Zviel-Girshin @ASC 19
Example
q0
0,1
q2
0,1
q4q1
0,1
0,1
q0
0,1
0,1
q4q1
A:
B:
L(A)=L(B)
Rina Zviel-Girshin @ASC 20
Equivalency
Theorem
Every NFA has an equivalent DFA.
Proof idea:
• We will give a constructive proof - by giving an algorithm to build a DFA equivalent to a given NFA.
Rina Zviel-Girshin @ASC 21
How can we do it?• Consider an example in which we can get from some
state q on an input letter to more than one state.
q
qj
qkqi
• The next state - (q,) - is one of {qi, qj, qk}.
Rina Zviel-Girshin @ASC 22
How can we do it?
• That means that the next state in an NFA is one state among several states.
• But in a DFA only one state can be the next state.
Conclusion:
One state in a DFA = A set of states in an NFA
q
qj
qkqi
Rina Zviel-Girshin @ASC 23
How can we do it?
• Consider an example. What states can we reach starting from some state q on two letters input 1.
q
qj
qkqi
qn
qmql
qp
Rina Zviel-Girshin @ASC 24
How can we do it?
Conclusion:
’(q, 1)= ({qi,qj,qk},1) =
(qi,1) (qj,1) (qk,1)
Informally: The next state in a DFA = A set
of all the states in an NFA to which you can get from the set of all the current states in an NFA.
q
qj
qkqi
qn
qmql
qp
Rina Zviel-Girshin @ASC 25
The - closure
Formally:The - closure of a state is a set of all the states
reached from the current state using -transitions only.
E(q)= { pQ | (q,i) = p, i>=0 }Informally: Use all the transitions you can. Add the states
you reach to -closure. Try to reach as many states as you can.
Rina Zviel-Girshin @ASC 26
The - closure
q0 q1 q2 q3 q4
E(q0) = {q0, q1, q2}
E(q1) = {q1, q2}
E(q2) = {q2}
E(q3) = {q3, q4}
E(q4) = {q4}
Rina Zviel-Girshin @ASC 27
A DFA construction algorithm Let A be an NFA where A = (QA, , A, q0A, FA) .We construct a DFA M equivalent to A,
where M = ( Q, , , q0, F).
• Q = P(QA) = • For each R in Q and in , (R,) is
)....,())),((( qqEERqRq
•q0 = E(q0A)
•F = { RQ | there exists rR such that rFA}
Rina Zviel-Girshin @ASC 28
Explanation• Create all the subsets of set of states of A. These
subsets will become the states of M.• The alphabet remains the same.• Transition function : for each state m in M and a letter find what are
the states qi..qj in A included in m are:
qi
qk
qlqi,qk,qlm:
Rina Zviel-Girshin @ASC 29
Explanation
for each qim find a set R of states which you can reach in NFA A using -closure, the letter and the -closure
qiqt
qp
R
i j
k l
Rina Zviel-Girshin @ASC 30
Explanation
the next state of m on in M is S.
qt
qpRi
qi,qk,qlm:qs
qfRk
qt
qpRl
S
.. ..
unite all the sets R you reach into one set : S
Rina Zviel-Girshin @ASC 31
Explanation
• The initial state of M is the set which includes only E(q0A)
• The final states of M are all the sets in which at least one state is a final (accepting) state of A.
• Eliminate all the unreachable states in M - states to which the is no path from the initial state of M.
• The automaton you have is a deterministic automaton equivalent to A.
Rina Zviel-Girshin @ASC 32
Example
Convert a given nondeterministic finite automaton into a deterministic finite automaton.
q0 q1
q4q3
a
a,b
a,
b
q2q0a
Rina Zviel-Girshin @ASC 33
Example
Construction of the DFA:({q0},a)={q2,q3}({q0},b)={q2}({q1},a)={q2,q3}({q1},b)={q2} Therefore:({q0,q1},a)={q2,q3}({q0,q1},b)={q2}
q2
q2,q3
q0q0
b
aq0,q1
Rina Zviel-Girshin @ASC 34
Example
({q2},a)={q1}
({q2},b)={q2}
q2
q2,q3
q0q0
b
a
b
q1
a
q0,q1
Rina Zviel-Girshin @ASC 35
Example
({q1},a)={q2,q3}
({q1},b)={q2}
q2
q2,q3
q0q0
b
a
b
q1
b
a
a
q0,q1
Rina Zviel-Girshin @ASC 36
Example
({q2,q3},a)={q1,q2}
({q2,q3},b)={q2}
q2
q2,q3
q0q0
b
a
b
b
aq1,q2
q1
b
a
a
q0,q1
Rina Zviel-Girshin @ASC 37
Example
({q1,q2},a)={q1,q2,q3}
({q1,q2},b)={q2}
q2
q2,q3
q1,q2,q3
q0q0
b
a
b
b
aq1,q2
aq1
b
a
b
a
q0,q1
Rina Zviel-Girshin @ASC 38
({q1,q2,q3},a)={q1,q2,q3}
({q1,q2,q3},b)={q2}
q2
q2,q3
q1,q2,q3
q0q0
b
a
b
b
aq1,q2
aq1
b
a
ab
b
a
q0,q1
Rina Zviel-Girshin @ASC 39
Any Questions?