Upload
khangminh22
View
1
Download
0
Embed Size (px)
Citation preview
Example: Detect Even Number of 1s
Jim Anderson (modified by Nathan Otterness) 2
This is a βtransition diagramβ for a deterministic finite automaton.
Diagrams like this visualize automata like a simple game.
Example: Detect Even Number of 1s
Jim Anderson (modified by Nathan Otterness) 3
In this βgameβ, we will move from circle to circle, following the instructions given by an input string.
Input string: 1111
Example: Detect Even Number of 1s
Jim Anderson (modified by Nathan Otterness) 4
The βplayerβ starts at the indicated circle:
Input string: 1111
Example: Detect Even Number of 1s
Jim Anderson (modified by Nathan Otterness) 5
The βgameβ proceeds by reading one character at a time from the input string and following the path labeled with the character.
Input string: 1111
Current Symbol
Example: Detect Even Number of 1s
Jim Anderson (modified by Nathan Otterness) 6
The βgameβ ends when all input symbols have been read.
Input string: 1111
Current Symbol
Done!
Defining a Deterministic Finite Automaton
We define a deterministic finite automaton (DFA) as a 5-tuple: π, Ξ£, πΏ, π0, πΉ
π: A set of states
Ξ£: A set of input symbols (the alphabet)
π0: The initial state. π0 β π.
πΉ: A set of accepting (βfinalβ) states. πΉ β π.
πΏ: The βtransition functionβ mapping π Γ Ξ£ β π.
Jim Anderson (modified by Nathan Otterness) 7
πΏ must be defined for all symbols in all states.
πΏ returns a single state.
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 8
The circles represent states.
π = π0, π1
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 9
This automaton only handles strings of 1s
Ξ£ = 1
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 10
The initial state is labeled with βStartβ
π0 = π0
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 11
Accepting states have double circles. This automaton only has one.
πΉ = π0
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 12
The arrows connecting the states represent possible transitions.
πΏ is the automatonβs transition function:πΏ ππ’πππππ‘ π π‘ππ‘π, ππ’πππππ‘ π π¦ππππ= πππ π‘ππππ‘πππ π π‘ππ‘π
πΏ can be represented as a table:
πΉ 1
π0 π1
π1 π0
Current state
Current input symbol
Destination state
From the Diagram to π, Ξ£, πΏ, π0, πΉ
Jim Anderson (modified by Nathan Otterness) 13
This automaton in tuple notation:
π = π0, π1
Ξ£ = 1
π0 = π0
πΉ = π0
πΏ = πΉ 1
π0 π1
π1 π0
We use a table here, but πΏ can also be described using words,
mathematical formulas, etc.
βTransition Tableβ Representation
Jim Anderson (modified by Nathan Otterness) 14
The table for πΏ implicitly lists the states and alphabet.
A βtransition tableβ just adds the remaining needed information:
Indicate the start state with β
Indicate accepting states with β
Example: πΉ 1
ββ π0 π1
π1 π0
Notation: Extending πΏ to Strings
An automatonβs standard transition function, πΏ, takes two parameters: a state and a symbol.
The βextended transition functionβ, απΏ, takes a state and a string.
απΏ can be defined in terms of πΏ:
β Assume that π€ is a string, π is a symbol in Ξ£, and π is a state.
β Recursively, απΏ π,π€π = πΏ απΏ π, π€ , π .
Examples from the previous automaton:
β απΏ π0, ν = π0
β απΏ π0, 111 = π1
β απΏ π0, 1111 = π0
Jim Anderson (modified by Nathan Otterness) 15
πΏ π, π = απΏ π, π for DFAs.
Example: Proofs About Automata
Here is a more complex automaton.
Transition table representation:
Jim Anderson (modified by Nathan Otterness) 16
πΉ 0 1
β π0 π1 π0
π1 π0 π2
β π2 π0 π2
Example: Proofs About Automata
Questions:
What language does this automaton represent?
How should we prove itβs correct?
Jim Anderson (modified by Nathan Otterness) 17
Example: Proofs About Automata
πΏ = {π₯|π₯ has an odd number of 0s and ends with a 1}.
We will prove this by induction on the length of an input string, π₯.
Jim Anderson (modified by Nathan Otterness) 18
Example: Proofs About Automata
We start our proof by defining what each state means about the input read so far:
π0: Even # of 0s
π1: Odd # of 0s, and ends with a 0
π2: Odd # of 0s, and ends with a 1
Jim Anderson (modified by Nathan Otterness) 19
Proof obligation:Show that these definitions are correct!
Example: Proofs About Automata
Base case: Prove the definition is correct for a string of length 0 (ν).
The automaton is in state π0 after processing ν.
Since ν contains an even number of 0s, our definition of π0 holds.
Jim Anderson (modified by Nathan Otterness) 20
Example: Proofs About Automata
Inductive step: Assume that απΏ π0, π₯ is correct for string π₯.
We need to prove that απΏ π0, π₯π remains correct for any symbol π.
This requires proving correctness for all possible transitions from all three states (mutual induction).
Jim Anderson (modified by Nathan Otterness) 21
Example: Proofs About Automata
Induction part 1: state ππ
If απΏ π0, π₯ = π0, then we can assume π₯ contained an even number of 0s.
πΏ π0, 1 = π0. Reading a 1 doesnβt change the # of 0s, so this is correct.
πΏ π0, 0 = π1. Weβve read an odd # of zeros, but the string doesnβt end in 1 yet.
Jim Anderson (modified by Nathan Otterness) 22
Example: Proofs About Automata
Induction part 2: state ππ
If απΏ π0, π₯ = π1, then we can assume π₯ contained an odd number of 0s and ends with a 0.
πΏ π1, 1 = π2. The string contains an odd # of 0s, but now ends with 1.
πΏ π1, 0 = π0. Reading an additional 0 means that the string contains an even number of 0s.Jim Anderson (modified by Nathan Otterness) 23
Example: Proofs About Automata
Induction part 3: state ππIf απΏ π0, π₯ = π2, then we can assume π₯ contained an odd number of 0s and ends with a 1.
πΏ π2, 1 = π2. This doesnβt change the # of 0s or the fact that the string ends with a 1.
πΏ π2, 0 = π0. Reading an additional 0 means that the string contains an even number of 0s.
Jim Anderson (modified by Nathan Otterness) 24
Example: Proofs About Automata
Finishing up:Weβve now proven that our claims about the states were correct, but we still need to prove the automaton recognizes the language.
Jim Anderson (modified by Nathan Otterness) 25
The automaton ends in π2 if and only if the string contained an odd number of 0s and ended with 1.
Since π2 is the only accepting state, the automaton accepts strings if and only if they contain an odd number of 0s and end with a 1.
Nondeterministic Finite Automata
Weβve been looking at deterministic finite automata (DFAs) so far.
βπΏ returns exactly one state for every symbol in every state
With nondeterministic finite automata (NFAs), the transition function πΏ returns a set of states.
βπΏ: π Γ Ξ£ β 2π
βThis can include no states at all!
Jim Anderson (modified by Nathan Otterness) 26
2π: The power set of π. (the set of all possible
subsets of π)
Note on βNondeterministicβ Terminology
DFAs always follow the same path for a single input string.
βDFAs accept a string if and only if this path leads to an accepting state.
NFAs may follow one of many different paths for the same input string.
βThis is why they are called nondeterministic.
βNFAs accept strings if and only if it is possible for them to reach an accepting state for a given input string.
Jim Anderson (modified by Nathan Otterness) 27
Extended Transition Function for NFAs
As with DFAs, απΏ for NFAs processes a string rather than a single character.
As with the definition of πΏ for NFAs, απΏ for NFAs returns the set of states an NFA is in after processing a string.
If an NFA has a start state π, then the NFA accepts
string π₯ if and only if απΏ π, π₯ β© πΉ β β
ββThe set of states after processing π₯ contains at least one accepting stateβ
Jim Anderson (modified by Nathan Otterness) 28
Recursive Definition of απΏ for NFAs
απΏ π, ν = π
απΏ π, π€π ={ π | for some state π in απΏ π, π€ , π is in πΏ π, π }
Jim Anderson (modified by Nathan Otterness) 29
q
w
w
a
a
aStates inαπΏ π, π€π .
Note: This isa set.
As with DFAs,
πΏ π, π = απΏ π, π for NFAs.
Definition of πΏ for Sets of States
Since πΏ can return a set of states for NFAs, it can be helpful to define a version of πΏ that takes a set of states rather than a single state.
If π is a set of states,πΏ π, π = βπβππΏ π, π
Jim Anderson (modified by Nathan Otterness) 30
Nondeterministic Finite Automata
Example: Match all strings ending with 01
Jim Anderson (modified by Nathan Otterness) 31
πΉ 0 1
β π0 π0, π1 π0
π1 β π2
β π2 β β
Example NFA Execution
Jim Anderson (modified by Nathan Otterness) 32
Input string: 10101
Current Symbol
Example NFA Execution
Jim Anderson (modified by Nathan Otterness) 33
Input string: 10101
Current Symbol
Done!
Example NFA Execution
Jim Anderson (modified by Nathan Otterness) 34
Input string: 10101
πΉ = π2
απΏ π0, 10101 = π0, π2
π0, π2 β© πΉ = π2
As expected, this NFA accepts 10101, because it ends in a set of states containing an accepting state.
NFAs vs DFAs
NFAs are often more convenient than DFAs
βWrite an NFA that accepts L = {x|x is a string of 0s or 1s that contains 0101000 as a substring}
βNow write a DFA that accepts L
Good news: Any language that can be recognized by an NFA can also be recognized by a DFA.
Jim Anderson (modified by Nathan Otterness) 35
Equivalence of NFAs and DFAs
Let π = π, Ξ£, πΏ, π0, πΉ be an NFA.
We will show how to construct an equivalent DFA, π·, using a technique called subset construction.
Main idea: π· will keep track of the subset of states that π might be in.
βIn other words, each of π·βs states corresponds to a subset of πβs states.
Jim Anderson (modified by Nathan Otterness) 36
Subset Construction
The constructed DFA π· = πβ², Ξ£, πΏβ², π0β² , πΉβ² , where:
βπβ² = 2π = Assuming the states in π are π0, π1, β¦ , ππ , the states in πβ² are all possible
subsets of π0, π1, β¦ , ππ .
βπ0β² = π0
βπΉβ² = {π β πβ² | π = β¦ , ππ , β¦ and ππ β πΉ}
ββπ·βs final states consist of all subsets containing one or more of πβs final states.β
βFor all π and π in πβ², πΏβ² π, π = π iff πΏ π, π = π.
Jim Anderson (modified by Nathan Otterness) 37
Subset Construction: Example
Jim Anderson (modified by Nathan Otterness) 38
Start with the following NFA:
βπ = π0, π1, π2 , 0, 1 , πΏ, π0, π2
βπΏ =
Construct DFA π· = πβ², 0, 1 , πΏβ², π0 , πΉβ² , where
βπβ² =
β , π0 , π1 , π2 , π0, π1 , π0, π2 , π1, π2 , π0, π1, π2
βπΉβ² = q2 , q0, q2 , q1, q2 , q0, q1, q2
βπΏβ² is defined on the following slide.
πΉ 0 1
π0 π1 π0
π1 π0 π1, π2
π2 β β
Subset Construction: Example
πΏβ² =
Jim Anderson (modified by Nathan Otterness) 39
π π
β β β
π0 {π1} π0
π1 π0 π1, π2
π2 β β
π0, π1 π0, π1 π0, π1, π2
π0, π2 π1 π0
π1, π2 π0 π1, π2
π0, π1, π2 π0, π1 π0, π1, π2
Subset Construction: Example
Diagram for π·:
Jim Anderson (modified by Nathan Otterness) 40
πΉβ² π π
β β β
β π0 {π1} π0
π1 π0 π1, π2
β π2 β β
π0, π1 π0, π1 π0, π1, π2
β π0, π2 π1 π0
β π1, π2 π0 π1, π2
β π0, π1, π2 π0, π1 π0, π1, π2
Subset Construction: Example
Jim Anderson (modified by Nathan Otterness) 41
We donβt care about these states; they are unreachablefrom the start state.
Diagram for π·:
Subset Construction: Example
Jim Anderson (modified by Nathan Otterness) 42
Diagram for π· without unreachable states:
Theorem 2.11
Proof: We need to show that απΏβ² π0β² , π₯ = π if and only
if απΏ π0, π₯ = π.
We will prove this by induction on π₯ .
Jim Anderson (modified by Nathan Otterness) 43
Theorem 2.11 (from the textbook):If π is the original NFA and π· is the constructed DFA (as defined earlier), then πΏ π = πΏ π· .
Proof of Theorem 2.11
Base case:
π₯ = 0. (Put another way, π₯ = ν).
Verifying the base case:απΏβ² π0
β² , ν = π0β² = π0
andαπΏ π0, ν = {π0}
by the definition of the extended transition function.
So, πΏ π = πΏ π· holds for the base case.
Jim Anderson (modified by Nathan Otterness) 44
Reminder:πΏβ² is for the DFA π·πΏ is for the NFA π
Proof of Theorem 2.11
Inductive step:
By the inductive hypothesis we assume that:
απΏβ² π0β² , π₯ = π iff απΏ π0, π₯ = π
We now apply the definition of the extended transition function to advance by a single symbol:
απΏβ² π, π = π iff απΏ π, π = π, by the definition of πΏβ².
Therefore απΏβ² π0β² , π₯ = π iff απΏ π0, π₯ = π.
Jim Anderson (modified by Nathan Otterness) 45
Reminder:πΏβ² is for the DFA π·πΏ is for the NFA π
Reminder:For string π€ and symbol π:απΏ π, π€π = πΏ απΏ π, π€ , π
Finishing the Proof of Theorem 2.11
Finally, since απΏβ² π0β² , π₯ is in πΉβ² if and only if απΏ π0, π₯
contains a state in πΉ, πΏ π· = πΏ π .
Note: This construction results in a state explosion.
Jim Anderson (modified by Nathan Otterness) 46
Reminder:πΏβ² is for the DFA π·πΏ is for the NFA π
NFAs with ν-Transitions
NFAs with ν-transitions have all the same rules as regular NFAs, but with additional flexibility.
The transition function for NFAs with ν-transitions:πΏ: π Γ Ξ£ βͺ ν β 2π
Example:
Jim Anderson (modified by Nathan Otterness) 47
ν-Closure
Let πΈπΆπΏπππΈ π β‘π βͺ π | π is reachable from π via ν β transitions
πΈπΆπΏπππΈ π0 = π0, π1, π2
πΈπΆπΏπππΈ π1 = π1, π2
For a set of states π,πΈπΆπΏπππΈ π β‘ βͺπβπ πΈπΆπΏπππΈ(π)
Jim Anderson (modified by Nathan Otterness) 48
Definition of απΏ for ν-NFAs
Recursive definition of απΏ:
απΏ π, ν = πΈπΆπΏπππΈ π
For a string π€ and symbol π: απΏ π, π€π = πΈπΆπΏπππΈ π ,
where π = π | for some π in απΏ π, π€ , π is in απΏ π, π
In other words, απΏ π, π = πΈπΆπΏπππΈ πΏ πΈπΆπΏπππΈ π , π
for a starting state π and a single symbol π.
Jim Anderson (modified by Nathan Otterness) 49
Unlike before, απΏ π, π β πΏ π, π for ν-NFAs!
ν-NFA Example
Jim Anderson (modified by Nathan Otterness) 50
Input string: 011
απΏ π0 , ν = π0, π1, π2
How to simulate an ν-NFA:1. Follow transitions as you
would for a normal NFA.2. Take the ν-closure for
any states you end up in.
ν-NFA Example
Jim Anderson (modified by Nathan Otterness) 51
Input string: 011
Current Symbol
απΏ π0 , 0 = π0, π1, π2
How to simulate an ν-NFA:1. Follow transitions as you
would for a normal NFA.2. Take the ν-closure for
any states you end up in.
ν-NFA Example
Jim Anderson (modified by Nathan Otterness) 52
Input string: 011
Current Symbol
απΏ π0 , 01 = π1, π2
How to simulate an ν-NFA:1. Follow transitions as you
would for a normal NFA.2. Take the ν-closure for
any states you end up in.
ν-NFA Example
Jim Anderson (modified by Nathan Otterness) 53
Input string: 011
Current Symbol
απΏ π0 , 011 = π1, π2
How to simulate an ν-NFA:1. Follow transitions as you
would for a normal NFA.2. Take the ν-closure for
any states you end up in.
ν-NFA Example
Jim Anderson (modified by Nathan Otterness) 54
Input string: 011
Current Symbol
απΏ π0 , 011 = π1, π2
How to simulate an ν-NFA:1. Follow transitions as you
would for a normal NFA.2. Take the ν-closure for
any states you end up in.
Done!
Extending πΏ to Sets of States
This is similar to normal NFAs. If π is a set of states:
πΏ π , π = βͺπβπ πΏ π, παπΏ π ,π€ =βͺπβπ
απΏ π, π€
Jim Anderson (modified by Nathan Otterness) 55
The Language of an ν-NFA
NFAs with ν-transitions define languages similarly to standard NFAs:
If π is an NFA with ν-transitions, then:
πΏ π β‘ π€ | απΏ π0, π€ contains a state in πΉ
More good news: any language that can be recognized by an ν-NFA can also be recognized by an NFA without ν-transitions.
Jim Anderson (modified by Nathan Otterness) 56
Eliminating ν-Transitions
The textbook shows how to transform an NFA with ν-transitions to a DFA.
We will instead show how to transform an NFA with ν transitions into an NFA without ν-transitions.
βYou could then transform such an NFA into a DFA using subset construction.
Jim Anderson (modified by Nathan Otterness) 57
Eliminating ν-Transitions
Let πΈ be an NFA with ν-transitions: π, Ξ£, πΏ, π0, πΉ
Define π to be an NFA without ν-transitions.π = π, Ξ£, πΏβ², π0, πΉ
β² , where:
βπΏβ² π, π β‘ απΏ π, π
βπΉβ² β‘ απΉ βͺ π0πΉ
Jim Anderson (modified by Nathan Otterness) 58
, if πΈπΆπΏπππΈ π0 contained a state in πΉ, otherwise
Eliminating ν-Transitions: Example
Jim Anderson (modified by Nathan Otterness) 59
π = π, Ξ£, πΏβ², π0, πΉβ² , where:
β’ πΏβ² π, π β‘ πΏ πΈπΆπΏπππΈ π , π
β’ πΉβ² β‘ απΉ βͺ π0πΉ
, if πΈπΆπΏπππΈ π0 contained a state in F, otherwise
π π π
π0 π0, π1, π2 π1, π2 π2
π1 β π1, π2 π2
π2 β β π2
πΏβ² = πΉβ² = π0, π2
Theorem 2.22
If: Proving this is easy (see Theorem 2.22 in the book)
Only if: The textbook directly constructs a DFA π·from ν-NFA πΈ, but we will instead construct an ordinary NFA π, and use Theorem 2.11 to conclude that Theorem 2.22 holds. We start by defining π as it was on the preceding slides.
Jim Anderson (modified by Nathan Otterness) 60
Theorem 2.22 (from the textbook):Language πΏ is accepted by an ν-NFA πΈ if and only if it is accepted by some DFA π·.
First Claim in Proof of Theorem 2.22
Rather than starting with a claim about πΏ πΈ or πΏ π , we instead
claim that απΏβ² π0, π₯ = απΏ π0, π₯ for some string π₯.
Jim Anderson (modified by Nathan Otterness) 61
Reminder:πΏβ² is for the NFA ππΏ is for the ν-NFA πΈ
We will prove this claim using induction on π₯ .
Note: This may not hold for π₯ = 0. For example,
in the previous example απΏ π0, ν = {π0, π1, π2}, but απΏβ² π0, ν = π0 .
We instead use π₯ = 1 as our base case (next slide).
π = π, Ξ£, πΏβ², π0, πΉβ² , where:
β’ πΏβ² π, π β‘ πΏ πΈπΆπΏπππΈ π , π
β’ πΉβ² β‘ απΉ βͺ π0πΉ
, if πΈπΆπΏπππΈ π0 contained a state in F, otherwise
Proof of Theorem 2.22: Base Case
Jim Anderson (modified by Nathan Otterness) 62
Base case: π₯ = 1
For any symbol π, απΏβ² π0, π = απΏ π0, π , by the definition of πΏβ².
Reminder:πΏβ² is for the NFA ππΏ is for the ν-NFA πΈ
Claim:απΏβ² π0, π₯ = απΏ π0, π₯
π = π, Ξ£, πΏβ², π0, πΉβ² , where:
β’ πΏβ² π, π β‘ πΏ πΈπΆπΏπππΈ π , π
β’ πΉβ² β‘ απΉ βͺ π0πΉ
, if πΈπΆπΏπππΈ π0 contained a state in F, otherwise
Proof of Theorem 2.22: Inductive Step
Jim Anderson (modified by Nathan Otterness) 63
Reminder:πΏβ² is for the NFA ππΏ is for the ν-NFA πΈ
Claim:απΏβ² π0, π₯ = απΏ π0, π₯
π = π, Ξ£, πΏβ², π0, πΉβ² , where:
β’ πΏβ² π, π β‘ πΏ πΈπΆπΏπππΈ π , π
β’ πΉβ² β‘ απΉ βͺ π0πΉ
, if πΈπΆπΏπππΈ π0 contained a state in F, otherwise
Let π₯ = π€π, where π€ is a string and π is a symbol.
We must show that απΏβ² π0, π₯ =απΏ π0, π₯ .
By the inductive hypothesis, απΏβ² π0, π€ = απΏ π0, π€ .
απΏβ²(π0, π€π)
= πΏβ² απΏβ² π0, π€ , π , by the inductive definition of απΏβ²
= βͺπβπΏβ² π0,π€πΏβ² π, π , by the definition of πΏβ² for sets of states
= βͺπβπΏ π0,π€απΏ π, π , by the inductive hypothesis and definition of πΏβ²
= απΏ απΏ π0, π€ , π , by the definition of απΏ for ν-NFAs and sets of states
= απΏ π0, π€π , by the definition of απΏ
Proof of Theorem 2.22: Finishing Up
To show that πΏ π = πΏ πΈ we must show that απΏβ²(π0, π₯)
contains a state in πΉ iff απΏ π0, π₯ contains a state in πΉ.
Additionally, we need to deal with π₯ = 0.
Case where π = πΊ:
απΏβ² π0, ν = π0
απΏ π0, ν = πΈπΆπΏπππΈ π0
π0 is in πΉβ² if and only if πΈπΆπΏπππΈ(π0) contains a state in πΉ, by the definition of πΉβ².
Jim Anderson (modified by Nathan Otterness) 64
Reminder:πΏβ² is for the NFA ππΏ is for the ν-NFA πΈ
Proof of Theorem 2.22: Finishing Up
Case where π β πΊ:
By the inductive proof earlier, απΏβ² π0, π₯ = απΏ π0, π₯ .
If πΉβ² = πΉ or π0 β απΏβ² π0, π₯ , we are done.
Otherwise, π0 β πΉβ², π0 β πΉ, and π0 β απΏβ² π0, π₯ .
βIn this case, some state in πΈπΆπΏπππΈ π0 is in πΉ.
βBy construction of απΏ, that state is in απΏ π0, π₯ .
Jim Anderson (modified by Nathan Otterness) 65
Reminder:πΏβ² is for the NFA ππΏ is for the ν-NFA πΈ