csc236 week 11236/larry/11/lec11.pdf · 2020-01-10 · here is an nfa. what’s different? from...
TRANSCRIPT
![Page 1: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/1.jpg)
CSC236 Week 11Larry Zhang
1
![Page 2: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/2.jpg)
Logistics
● PS5 due Dec 6
● Next week: exam review
2
![Page 3: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/3.jpg)
Recap
● Last week we learned about Deterministic Finite Automata.
● It is a model of the computation performed when the computer checks if a string belongs to a regular language.
● Every regular language can be recognized by a DFA with a finite number of states.
● An irregular languages would need an infinite number of states in the DFA, so it cannot be recognized by a computer efficiently.
3
![Page 4: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/4.jpg)
Number of DFA States
● Normally, we would expect that the number of states needed by the DFA somehow reflects how complicated it is to describe the regular languages.
● But sometimes, similar languages require quite different numbers of states. For example,
● (0+1)(0+1)1(0+1)*
○ all strings whose third symbol from the left is 1.
○ 5 states needed by the DFA (Home exercise: draw the DFA)
● (0+1)*1(0+1)(0+1)
○ all strings whose third symbol from the right is 1.
○ 8 states needed by the DFA (Home exercise: draw the DFA)
○ Hint: need to remember the last three symbols read, of which there are 8 possibilities.4
![Page 5: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/5.jpg)
Why more states?
● (0+1)(0+1)1(0+1)* vs (0+1)*1(0+1)(0+1)
● This has to do with how DFA works.
○ Read string from the left to the right.
○ It is easier to deal with what’s on the left (making decisions with what has happened already)
○ It is harder to deal with what’s on the right (making decisions for the future!)
○ For the second regex, we don’t know when we are at the third-last symbol, so we have to be prepared for everything that could happen in three steps into the future, therefore needing more states.
● It would be nice to have a simpler model for this.
5
![Page 6: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/6.jpg)
Nondeterministic Finite Automata(NFA)
6
![Page 7: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/7.jpg)
Here is an NFA. What’s different?
● From q0, reading symbol 1 leads to two possible transitions!
● q3 has no outgoing transition at all!
● Basically, scanning a given string will gives a set of multiple possible paths in the diagram, compared to only one path for DFA. (Nondeterministic)
● For NFAs, we say a string w is accepted if one of the possible paths got by scanning the string reaches the accepting state.
● The above NFA actually checks if the third-last symbol is 1.7
![Page 8: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/8.jpg)
Scan string 010110
● Start from q0
● Read 0, reach q0
● Read 01, reach q0, or q1
● Read 010, reach q0, or q2
● Read 0101, reach q0, or q1, or q3 (accepting)
● Read 01011, reach q0, or q1, or q2, or nowhere (from q3)
● Read 010110, reach q0, or q2, or q3 (accepting)
8
![Page 9: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/9.jpg)
NFA: Formal Definition
9
![Page 10: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/10.jpg)
one more thing ...
ε-transition
Transition from one state to another without reading a symbol.
10
● If you are reaching q0, then you are reaching q1 for free (without needing to read a symbol)!
● In other words, reaching state q0 is equivalent to reaching the set of states {q0, q1}
q0 q1ε
![Page 11: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/11.jpg)
another example
● Reaching q0 is equivalent to reaching the set {q0, q1, q2, q3}.
● Reaching q1 is equivalent to reaching the set {q1, q2}.
○ Note: no free ε-transition from q1 back to q0
11
q0 q1ε
q2
q3ε
ε
![Page 12: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/12.jpg)
Exercise
12
![Page 13: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/13.jpg)
Does this NFA accept “abbb”?
13
● Initial state s = q0
● Actual initial set of states δ(s, ε) = {q0, q1, q2}
● δ(s, a) = {q3, q2} # ε-transition q3 to q2
● δ(s, ab) = {q4, q5}
● δ(s, abb) = {q4, q5}
● δ(s, abbb) = {q4, q5}
○ accepting because q4 is an accepting state
ConcepTest
A. AcceptB. Not accept
![Page 14: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/14.jpg)
ConcepTest
14
![Page 15: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/15.jpg)
15
![Page 16: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/16.jpg)
ConcepTest
16
![Page 17: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/17.jpg)
Home Exercise: Develop a NFA
17
![Page 18: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/18.jpg)
18
![Page 19: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/19.jpg)
So, Regex, DFA, NFA, they all recognize regular languages.
How are they related to each other?
19
![Page 20: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/20.jpg)
Equivalence of Representations
Let L be a languages over an alphabet ∑. Then the following are equivalent.
● There is a regular expression that matches L.
● There is a DFA that accepts L
● There is an NFA with ε-transitions that accepts L
● There is an NFA without ε-transitions that accepts L
20
![Page 21: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/21.jpg)
In other words
● Given a regex, we can construct a DFA that accepts the same language
● Given a regex, we can construct an NFA that accepts the same language
● Given a DFA, we can construct a regex that accepts the same language
● Given a DFA, we can construct an NFA that accepts the same language
● Given an NFA, we can construct a regex that accepts the same language
● Given an NFA we can construct a DFA that accepts the same language
21Subset Construction
![Page 22: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/22.jpg)
Subset Constructionan algorithm that converts NFA to DFA
22
![Page 23: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/23.jpg)
Subset Construction● Each state Q in the DFA represents a set of states in the NFA
23
{q0,q2}
The algorithm:
● Let Q0 = δ(s, ε), i.e., the set of states that reachable from initial NFA state s via zero or more ε-transitions
● Repeat until no more new DFA states are added
○ For each state Q in the DFA, for each symbol x, determine the set R of NFAs that are reached from any NFA state in Q via symbol x.
○ For each R from the last step, update R = δ(R, ε), which is the set of all NFA states reachable from some state in R via zero or more ε-transitions.
![Page 24: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/24.jpg)
The initial state and accepting states of the DFA
Initial state of the DFA:
● Q0 = δ(s, ε)
Accepting states of the DFA:
● Any states that contains an accepting state of the NFA.
24
![Page 25: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/25.jpg)
Example
25
![Page 26: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/26.jpg)
Find the equivalent DFA for the following 4-state NFA
26
old state symbol new state
q0 0 q1
q0 1 q0
q0 ε q3
q1 0 q2
q2 ε q1
q2 1 q3
The initial state is q0; the accepting state is q3.
![Page 27: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/27.jpg)
27
How many states in the DFA?● 5● {q0, q3}, {q1}, {q1, q2}, {q3}, ∅
Which state is the initial state?● Q0: {q0, q3}
Which states are accepting states?● any state that contains q3● {q0, q3} and {q3}.
![Page 28: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/28.jpg)
The resulting DFA from subset construction
28
old state symbol new state
{q0, q3} 0 {q1}
{q0, q3} 1 {q0, q3}
{q1} 0 {q1, q2}
{q1} 1 ∅
{q1, q2} 0 {q1, q2}
{q1, q2} 1 {q3}
{q3} 0 ∅
{q3} 1 ∅
∅ 0 ∅
∅ 1 ∅
Initial state: {q0, q3}Accepting states: {q0, q3}, {q3}
{q0,q3}
{q1}
{q1,q2}
{q3}
∅
1
0
01
010, 1
0, 1
![Page 29: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/29.jpg)
Equivalence between DFA, NFA and Regex
● Given a regex, we can construct a DFA that accepts the same language
● Given a regex, we can construct an NFA that accepts the same language
● Given a DFA, we can construct a regex that accepts the same language
● Given a DFA, we can construct an NFA that accepts the same language
● Given an NFA, we can construct a regex that accepts the same language
● Given an NFA we can construct a DFA that accepts the same language
29
State Elimination
![Page 30: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/30.jpg)
State Eliminationconvert DFA to regular expression
30
![Page 31: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/31.jpg)
State Elimination: Steps● If the initial state q1 has incoming edges, create a new start state s and add
an ε-transition to q1.
● If there are multiple accepting states or if the final state qn has outgoing edges, create a new accepting state f and add ε-transition(s) to f from all former accepting states. (Former accepting states become non-accepting.)
● Eliminate trap states
● Eliminate state by state until only the initial and the accepting state remain.
○ When eliminating each state, take care of all the paths that bypass the eliminated state
○ change all paths that used to “transfer” at the eliminated state to “direct flights”, by applying the following rules...
31
![Page 32: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/32.jpg)
Rules of elimination
32
Remove all original edges and remove the state q once it is isolated.
![Page 33: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/33.jpg)
What happens when eliminating q1 ?
33
![Page 34: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/34.jpg)
34
ConcepTest
Exercise credit: Sadia Sharmin
![Page 35: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/35.jpg)
35
What can we reduce between q1 and q2?
![Page 36: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/36.jpg)
36
How to eliminate q2 ?
![Page 37: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/37.jpg)
37
Eliminate q3
![Page 38: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/38.jpg)
38
![Page 39: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/39.jpg)
Handout Exercise
39
![Page 40: CSC236 Week 11236/larry/11/lec11.pdf · 2020-01-10 · Here is an NFA. What’s different? From q0, reading symbol 1 leads to two possible transitions! q3 has no outgoing transition](https://reader033.vdocument.in/reader033/viewer/2022041614/5e39ce7bf545891a0541ff09/html5/thumbnails/40.jpg)
40