finite automatagoodrich/teach/cs162/notes/fa1.pdf2 informal explanation • finite automata are...
TRANSCRIPT
![Page 1: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/1.jpg)
1
Finite Automata
Motivation Examples
![Page 2: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/2.jpg)
2
Informal Explanation
• Finite automata are finite collections of states with transition rules that take you from one state to another.
• Original application was sequential switching circuits, where the “state” was the settings of internal bits.
• Today, several kinds of software can be modeled by Finite Automata.
![Page 3: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/3.jpg)
3
Representing Finite Automata
• Simplest representation is often a graph. • Nodes = states. • Arcs indicate state transitions. • Labels on arcs tell what causes the
transition.
![Page 4: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/4.jpg)
4
Example: Recognizing Strings Ending in “ing”
Saw i i
Not i
Saw ing g
i
Not i or g
i
Saw in n
Not i or n
nothing
Start i
Not i
![Page 5: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/5.jpg)
5
Automata to Code (by hand)
• In C/C++/Java: 1. Initialize state q to start state. 2. Loop through the string one character at
a time. 3. Make a switch statement with a case for
each state for q, where each case sets q according to the transitions for that state.
4. Accept if you end in a final state.
![Page 6: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/6.jpg)
6
Example in Java Scanner scan = new Scanner(System.in); String s = scan.next(); int q = 0; for (char c : s.toCharArray()) { switch (q) { case 0: q = (c=='i')? 1 : 0; break; case 1: q = (c=='n')? 2 : ((c=='i')? 1 : 0); break; case 2: q = (c=='g')? 3 : ((c=='i')? 1 : 0); break; case 3: q = (c=='i')? 1 : 0; } } if (q==3) System.out.println("accept."); else System.out.println("reject.");
0 1 2 3
Start state
Final state
Loop through string s
Transitions
![Page 7: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/7.jpg)
7
Automata to Code – General
• It would be nice to have an automatic way to generate such code…
• Rather than do it by hand, a code generator takes a “regular expression” describing the pattern(s) you are looking for and produces the code for it. • Example: .*ing works in grep.
![Page 8: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/8.jpg)
8
Example: An Even Number of 1’s
even odd 1
0
Start
0
1
• How would it look to accept a number of 1’s that is a multiple of 3?
![Page 9: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/9.jpg)
Password/Keyword Example
9
This is sometimes called a dead state.
BTW, there is a potential security risk on the password application if this finite automaton reports failure too quickly.
any character
any character
![Page 10: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/10.jpg)
10
Exactly Two a’s
![Page 11: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/11.jpg)
11
At Least Two b’s
![Page 12: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/12.jpg)
12
Exactly two a’s and at least two b’s
![Page 13: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/13.jpg)
Containing Substrings or Not
• Contains baba:
• Does not contain baba:
13
![Page 14: Finite Automatagoodrich/teach/cs162/notes/fa1.pdf2 Informal Explanation • Finite automata are finite collections of states with transition rules that take you from one state to another](https://reader030.vdocuments.net/reader030/viewer/2022021718/5b827f7a7f8b9a32738eb9e5/html5/thumbnails/14.jpg)
General Comments
• Some things are easy with finite automata: • Substrings (…abcabc…) • Subsequences (…a…b…c…b…a…) • Modular counting (odd number of 1’s)
• Some things are impossible with finite automata (we will prove this later): • An equal number of a’s and b’s • More 0’s than 1’s
• But when they can be used, they are fast. 14