Upcoming SlideShare
×

# Theory of Computation: Lecture 34

1,775
-1

Published on

1) Pigeonhole Principle
2) Pumping Lemma for Regular Languages
3) Finite Automata Applications
4) Finite Automata in Robotics
5) Finite Automata in Text Processing
6) Grammars and Derivations

Published in: Education, Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
1,775
On Slideshare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
0
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Theory of Computation: Lecture 34

1. 1. CS 5000: Theory of Computation Lecture 34 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco
2. 2. Outline ● Review ● Pumping Lemma for Regular Languages ● Finite Automata Applications – Finite Automata in Robotics – EGREP and Java Patterns ● Grammars and Derivationswww.youtube.com/vkedco
3. 3. Review: Pigeon Hole 9 students take a test. Student X makes 7 errors. Each of the other students makes fewer errors. Did at least two students make the same number of errors?www.youtube.com/vkedco
4. 4. Review: Pigeon Hole X 0 1 2 3 4 5 6 7 7 holes 8 studentswww.youtube.com/vkedco
5. 5. Pumping Lemma for Regular Languageswww.youtube.com/vkedco
6. 6. Processing State Sequence 0 1 q0 q1 A processing state sequence is a sequence of states that an FA uses (enters and leaves or just enters in the case of final states) to process a given input. Let x = 001. Then the processing path of the above FA is q0, q0, q0, q1: from q0 to q0 on the first 0, on to q0 on the second 0, and on to q1 on 1. 0 0 1 q0 q0 q0 q1www.youtube.com/vkedco
7. 7. Pumping Lemma Let L = L( M ) , where M is a DFA with n states. Let x ∈ L and x ≥ n. Then we can write x = uvw, where 1 ≤ v and uv w ∈ L, for i ≥ 0. iwww.youtube.com/vkedco
8. 8. Proof ● The string x has at least n symbols ● To process n symbols the DFA M needs a processing state sequence of n+1 states ● Since DFA M has exactly n states, to process x, it must enter the same state at least twice, due to the Pigeonhole Principle ● Leaving and re-entering the same state constitutes a non-empty loop string that can be eliminated or repeated (pumped) as many times as we want ● Elimination of the loop string or a repeat (pump) of the loop string gives us a string that is still in the languagewww.youtube.com/vkedco
9. 9. Pictorial Insight v w u q0 q q’ M x = uvw, x ≥ nwww.youtube.com/vkedco
10. 10. Proof δ * ( q0 , u ) = q δ * ( q, v ) = q δ * ( q, w) = q∈ Fwww.youtube.com/vkedco
11. 11. Proof 1. v ≠ ε . 2. δ ( q0 , uvw) = δ ( q0 , uw) ∈ F . * * 3. δ ( q 0 , uvw) = δ ( q 0 , uvvw) = δ ( q 0 , uv w) ∈ * * * 2 F. 4. v can be pumped 0, 1, 2, 3... times and the i string uv w will still be in L.www.youtube.com/vkedco
12. 12. Pumping Lemma Example: Proof 1 { } Claim : L = a k b k | k ∈ N is not regular. Proof 1 : Let n be the constant of the Pumping Lemma. Let z ∈ L, | z |≥ n. Then z = uvw, 1 ≤ v . Then + z = a b , m > 0. There are 3 choices for v : v = a ; m m v = a + b + ; v = b + . If v = a + , uv 0 w = a k b m , k < m. Thus, uv 0 w ∉ L. If v = a + b + , uv 2 w is a string where a s follow b s. Thus, uv 2 w ∉ L. If v = b + , uv 0 w = a m b k , m > k . Thus, uv 0 w ∉ L. In each of the 3 cases, we achieve a contradiction.www.youtube.com/vkedco
13. 13. Pumping Lemma Example: Proof 2 { } Claim : L = a k b k | k ∈ N is not regular. Proof 2 : Let n be the constant of the Pumping Lemma. Let z = a b . Then z ∈ L, | z |≥ n. By the Pumping Lemma, n n z = uvw,1 ≤ v . To process a n , the DFA with n states needs + a processing sequence of n + 1 states. Thus, v = a is the only + choice for v. Consider y = uv w = uvvw. Since v = a , 2 y ∉ L, because the number of a s in y is strictly greater than the number of b s. Hence, L is not regular.www.youtube.com/vkedco
14. 14. Pumping Lemma Use ● Pumping Lemma is not used to prove that languages are regular ● Pumping Lemma states that if a language is regular, then its strings have a specific property (pumping property) ● Pumping Lemma is used to show that languages are not regular: assume that a language is regular and then show that a specific string does not satisfy the pumping propertywww.youtube.com/vkedco
15. 15. Main Implication of the Pumping Lemma There are languages that are not regular, i.e., languages for which it is impossible to construct DFAs, NFAs or regular expressionswww.youtube.com/vkedco
17. 17. Finite Automata Applications ● Text Processors – Computational Linguistics – Compilers – Bioinformatics ● Roboticswww.youtube.com/vkedco
18. 18. Finite Automata in Roboticswww.youtube.com/vkedco
19. 19. Robots & Finite Automata Junun Mark III Robotwww.youtube.com/vkedco
20. 20. Line Following How can we make Mark III follow a black line?www.youtube.com/vkedco
21. 21. Line Following Middle Sensor Right Sensor Left Sensor Left Sensor returns black_on_left when it detects blackness. Middle Sensor returns black_on_center when it detects blackness. Right Sensor returns black_on_right when it detects blackness.www.youtube.com/vkedco
22. 22. Line Following if ( black_on_center ) We assume that the robot move_forward; starts with the middle sensor aligned with the line. So, if the middle sensor detects blackness, the robot moves forward. LEFT CENTER RIGHTwww.youtube.com/vkedco
23. 23. Line Following if ( black_on_left ) If the left sensor detects turn_left; blackness, the robot turns left to align the middle sensor with the line. LEFT CENTER RIGHTwww.youtube.com/vkedco
24. 24. Line Following if ( black_on_right ) If the right sensor detects turn_right; blackness, the robot turns right to align the middle sensor with the line. LEFT CENTER RIGHTwww.youtube.com/vkedco
25. 25. An FA for Line Following turn_right black_on_right move_completed turn_left move_completed black_on_center/no_black black_on_left move_forward read_sensors move_completedwww.youtube.com/vkedco
26. 26. Finite Automata in Text Processing EGREP and JAVA.UTIL.REGEXwww.youtube.com/vkedco
27. 27. egrep Example \$ egrep a file1.txt barney wilma file1.txt \$ egrep e file1.txt fred fred barney barney wilma betty betty \$ egrep a.*y file1.txt barney \$ egrep b.*y file1.txt barney bettywww.youtube.com/vkedco
28. 28. egrep Example \$ egrep ^(0|1(01*0)*1)*\$ file2.txt file2.txt 0 0 1 11 10 11 110 100 101 1001 110 111 1000 1001 1010www.youtube.com/vkedco
29. 29. Patterns in Java ● Java has the package java.util.regex that allows the programmer to work with patterns, including regular expressions ● java.util.regex package has two major classes: Pattern and Matcher ● Pattern compiles patterns into finite state automata ● Matcher uses the compiled pattern to find substrings that match the patternwww.youtube.com/vkedco
30. 30. Java Pattern Example ● This is how you compile a regular expression: – Pattern pat = Pattern.compile(“abc”); ● This is how you create a matcher object, essentially an NFA that can be used to find matches: – Matcher match = pat.matcher(str); ● This is how you can test for a match: – match.matches() is a boolean predicatewww.youtube.com/vkedco
31. 31. Java Pattern Example import java.io.*; import java.util.regex.*; public class RegexDemo { public static void main(String[] args) throws IOException { Pattern pat = Pattern.compile(args[0]); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = in.readLine(); while ( str != null ) { Matcher match = pat.matcher(str); if ( match.matches() ) { System.out.println(str); } str = in.readLine(); } } }www.youtube.com/vkedco
32. 32. Java Pattern Example ● This is how you can call RegexDemo from the command line: – java RegexDemo ^(0|1(01*0)*1)*\$ ● Then you can give it strings to match ● You can also redirect a file to RegexDemo: – java RegexDemo reg-exp < numbers.txtwww.youtube.com/vkedco