Deterministic Finite Automata

8,006 views

Published on

0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,006
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
361
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Deterministic Finite Automata

  1. 1. Mohammad JAWADTheory Of Automata And Complier Construction
  2. 2. DeterministicFinite Automata COMPSCI 102 Lecture 2
  3. 3. Let me show you a machine so simple that you canunderstand it in less than two minutes
  4. 4. 11 1 0 0,1 10111 111 1 0 0 1 The machine accepts a string if the process ends in a double circle Steven Rudich: www.cs.cmu.edu/~rudich
  5. 5. Anatomy of a Deterministic Finite Automaton 1 0 0,1 states accept states (F) 1 q1 0 0 q0 q1 2 The machine accepts a string if thestart state (q0) ends in 3a double circle process q states Steven Rudich: www.cs.cmu.edu/~rudich
  6. 6. Anatomy of a Deterministic Finite Automaton q1 1 0 0,1 1 q0 q2 0 0The alphabet of a finite automaton is the setwhere the symbols come from: 1 {0,1} q3The language of a finite automaton is the setof strings that it accepts Steven Rudich: www.cs.cmu.edu/~rudich
  7. 7. q0 0,1 L(M) = ∅ strings of 0s and 1s AllThe Language of Machine M Steven Rudich: www.cs.cmu.edu/~rudich
  8. 8. 0 0 1 q0 q1 1L(M) = { w | w has an even number of 1s} Steven Rudich: www.cs.cmu.edu/~rudich
  9. 9. NotationAn alphabet Σ is a finite set (e.g., Σ = {0,1})A string over Σ is a finite-length sequence ofelements of ΣFor x a string, |x| is the length of xThe unique string of length 0 will be denotedby ε and will be called the empty or null stringA language over Σ is a set of strings over Σ Steven Rudich: www.cs.cmu.edu/~rudich
  10. 10. A finite automaton is a 5-tuple M = (Q, Σ, δ, q0, F) Q is the set of states Σ is the alphabet δ : Q × Σ → Q is the transition function q0 ∈ Q is the start state F ⊆ Q is the set of accept states L(M) = the language of machine M = set of all strings machine M accepts Steven Rudich: www.cs.cmu.edu/~rudich
  11. 11. M = (Q, Σ, δ, q0, F) Q = {q0, q1, q2, q3} where Σ = {0,1} δ : Q × Σ → Q transition function * q0 ∈ Q is start state F = {q1, q2} ⊆ Q accept states 0 q1 0,1 1 * δ q0 0 q0 1 q1 1 q1 q2 q2 q0 M q2 0 0 q2 q3 q2 1 q3 q0 q2 q3 Steven Rudich: www.cs.cmu.edu/~rudich
  12. 12. Build an automaton that accepts all and only those strings that contain 001 0,1 1 0 0 0 1 q q0 q00 q001 1 Steven Rudich: www.cs.cmu.edu/~rudich
  13. 13. A language is regular if it is recognized by a deterministic finite automatonL = { w | w contains 001} is regularL = { w | w has an even number of 1s} is regular Steven Rudich: www.cs.cmu.edu/~rudich
  14. 14. Union TheoremGiven two languages, L1 and L2, definethe union of L1 and L2 as L1 ∪ L2 = { w | w ∈ L1 or w ∈ L2 }Theorem: The union of two regularlanguages is also a regular language Steven Rudich: www.cs.cmu.edu/~rudich
  15. 15. Theorem: The union of two regular languages is also a regular languageProof Sketch: Let 1M1 = (Q1, Σ, δ1, q0, F1) be finite automaton for L1 and 2M2 = (Q2, Σ, δ2, q0, F2) be finite automaton for L2We want to construct a finite automatonM = (Q, Σ, δ, q0, F) that recognizes L = L1 ∪ L2 Steven Rudich: www.cs.cmu.edu/~rudich
  16. 16. Idea: Run both M1 and M2 at the same time!Q = pairs of states, one from M1 and one from M2 = { (q1, q2) | q1 ∈ Q1 and q2 ∈ Q2 } = Q 1 × Q2 Steven Rudich: www.cs.cmu.edu/~rudich
  17. 17. Theorem: The union of two regularlanguages is also a regular language 0 0 1 q0 q1 1 1 1 0 p0 p1 Steven Rudich: 0 www.cs.cmu.edu/~rudich
  18. 18. Automaton for Union 1 q0,p0 q1,p0 1 0 0 0 0 1 q0,p1 q1,p1 1 Steven Rudich: www.cs.cmu.edu/~rudich
  19. 19. Automaton for Intersection 1 q0,p0 q1,p0 1 0 0 0 0 1 q0,p1 q1,p1 1 Steven Rudich: www.cs.cmu.edu/~rudich
  20. 20. Theorem: The union of two regularlanguages is also a regular languageCorollary: Any finite language isregular Steven Rudich: www.cs.cmu.edu/~rudich
  21. 21. The Regular OperationsUnion: A ∪ B = { w | w ∈ A or w ∈ B }Intersection: A ∩ B = { w | w ∈ A and w ∈ B }Reverse: AR = { w1 …wk | wk …w1 ∈ A }Negation: ¬A = { w | w ∉ A }Concatenation: A ⋅ B = { vw | v ∈ A and w ∈ B }Star: A* = { w1 …wk | k ≥ 0 and each wi ∈ A } Steven Rudich: www.cs.cmu.edu/~rudich
  22. 22. Regular Languages Are Closed Under The Regular Operations We have seen part of the proof forUnion. The proof for intersection is verysimilar. The proof for negation is easy. Steven Rudich: www.cs.cmu.edu/~rudich
  23. 23. The “Grep” ProblemInput: Text T of length t, string S of length nProblem: Does string S appear inside text T?Naïve method: a1, a2, a3, a4, a5, …, atCost: Roughly nt comparisons
  24. 24. Automata SolutionBuild a machine M that accepts any stringwith S as a consecutive substringFeed the text to MCost: t comparisons + time to build MAs luck would have it, the Knuth, Morris,Pratt algorithm builds M quickly
  25. 25. Real-life Uses of DFAs Grep Coke Machines Thermostats (fridge) Elevators Train Track Switches Lexical Analyzers for Parsers
  26. 26. Are alllanguages regular?
  27. 27. Consider the language L = { anbn | n > 0 }i.e., a bunch of a’s followed by anequal number of b’sNo finite automaton accepts this languageCan you prove this?
  28. 28. anbn is not regular.No machine hasenough states tokeep track of thenumber of a’s itmight encounter
  29. 29. That is a fairly weakargumentConsider the followingexample…
  30. 30. L = strings where the # of occurrences ofthe pattern ab is equal to the number ofoccurrences of the pattern ba Can’t be regular. No machine has enough states to keep track of the number of occurrences of ab
  31. 31. b a b a a b a a b bM accepts only the strings with an equal number of ab’s and ba’s!
  32. 32. Let me show you aprofessional strengthproof that anbn is not regular…
  33. 33. Pigeonhole principle:Given n boxes and m > nobjects, at least one boxmust contain more thanone objectLetterbox principle:If the average number ofletters per box is x, thensome box will have atleast x letters (similarly,some box has at most x)
  34. 34. Theorem: L= {anbn | n > 0 } is not regularProof (by contradiction):Assume that L is regularThen there exists a machine M with k statesthat accepts LFor each 0 ≤ i ≤ k, let Si be the state M is inafter reading ai∃i,j ≤ k such that Si = Sj, but i ≠ jM will do the same thing on aibi and ajbiBut a valid M must reject ajbi and accept aibi
  35. 35. Deterministic Finite Automata • Definition • Testing if they accept a string • Building automata Regular Languages • Definition • Closed Under Union,Here’s WhatYou Need to Intersection, Negation Know… • Using Pigeonhole Principle to show language ain’t regular

×