SlideShare a Scribd company logo
Mohammad JAWAD

Theory Of Automata And
 Complier Construction
Deterministic
Finite Automata
   COMPSCI 102 Lecture 2
Let me show you a
 machine so simple
    that you can
understand it in less
  than two minutes
11             1
           0
                                             0,1
                       1
0111      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
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 the
start state (q0) ends in 3a double circle
         process       q                states
                      Steven Rudich:
                   www.cs.cmu.edu/~rudich
Anatomy of a Deterministic Finite
            Automaton

                        q1             1
         0
                                 0,1
                 1
          q0                            q2

                   0                      0
The alphabet of a finite automaton is the set
where the symbols come from: 1              {0,1}
                             q3
The language of a finite automaton is the set
of strings that it accepts
                       Steven Rudich:
                    www.cs.cmu.edu/~rudich
q0                  0,1


          L(M) = ∅ strings of 0s and 1s
                 All


The Language of Machine M




              Steven Rudich:
           www.cs.cmu.edu/~rudich
0                             0


                            1
           q0                            q1
                            1

L(M) = { w | w has an even number of 1s}




                   Steven Rudich:
                www.cs.cmu.edu/~rudich
Notation
An alphabet Σ is a finite set (e.g., Σ = {0,1})

A string over Σ is a finite-length sequence of
elements of Σ

For x a string, |x| is the length of x

The unique string of length 0 will be denoted
by ε and will be called the empty or null string

A language over Σ is a set of strings over Σ
                       Steven Rudich:
                    www.cs.cmu.edu/~rudich
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
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
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
A language is regular if it is
    recognized by a deterministic
          finite automaton

L = { w | w contains 001} is regular
L = { w | w has an even number of 1s} is regular




                     Steven Rudich:
                  www.cs.cmu.edu/~rudich
Union Theorem
Given two languages, L1 and L2, define
the union of L1 and L2 as
    L1 ∪ L2 = { w | w ∈ L1 or w ∈ L2 }

Theorem: The union of two regular
languages is also a regular language




                 Steven Rudich:
              www.cs.cmu.edu/~rudich
Theorem: The union of two regular
    languages is also a regular language

Proof Sketch: Let
                  1
M1 = (Q1, Σ, δ1, q0, F1) be finite automaton for L1
                           and
                  2
M2 = (Q2, Σ, δ2, q0, F2) be finite automaton for L2

We want to construct a finite automaton
M = (Q, Σ, δ, q0, F) that recognizes L = L1 ∪ L2



                       Steven Rudich:
                    www.cs.cmu.edu/~rudich
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
Theorem: The union of two regular
languages is also a regular language

   0
                         0

           1
  q0                    q1
           1                                    1
                                        1

                                            0
                                p0              p1
                  Steven Rudich:
                                            0
               www.cs.cmu.edu/~rudich
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
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
Theorem: The union of two regular
languages is also a regular language

Corollary: Any finite language is
regular




                Steven Rudich:
             www.cs.cmu.edu/~rudich
The Regular Operations
Union: 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
Regular Languages Are
  Closed Under The
 Regular Operations
  We have seen part of the proof for
Union. The proof for intersection is very
similar. The proof for negation is easy.


                 Steven Rudich:
              www.cs.cmu.edu/~rudich
The “Grep” Problem
Input: Text T of length t, string S of length n
Problem: Does string S appear inside text T?
Naïve method:

        a1, a2, a3, a4, a5, …, at

Cost: Roughly nt comparisons
Automata Solution
Build a machine M that accepts any string
with S as a consecutive substring

Feed the text to M

Cost: t comparisons + time to build M

As luck would have it, the Knuth, Morris,
Pratt algorithm builds M quickly
Real-life Uses of DFAs
 Grep

 Coke Machines

 Thermostats (fridge)

 Elevators

 Train Track Switches

 Lexical Analyzers for Parsers
Are all
languages
 regular?
Consider the language L = { anbn | n > 0 }
i.e., a bunch of a’s followed by an
equal number of b’s
No finite automaton accepts this language
Can you prove this?
anbn is not regular.
No machine has
enough states to
keep track of the
number of a’s it
might encounter
That is a fairly weak
argument

Consider the following
example…
L = strings where the # of occurrences of
the pattern ab is equal to the number of
occurrences of the pattern ba


   Can’t be regular. No machine has
   enough states to keep track of the
   number of occurrences of ab
b
           a
                           b
       a        a

       b        a
                           a
           b
                b

M accepts only the strings with an
 equal number of ab’s and ba’s!
Let me show you a
professional strength
proof that anbn is not
      regular…
Pigeonhole principle:
Given n boxes and m > n
objects, at least one box
must contain more than
one object



Letterbox principle:
If the average number of
letters per box is x, then
some box will have at
least x letters (similarly,
some box has at most x)
Theorem: L= {anbn | n > 0 } is not regular
Proof (by contradiction):
Assume that L is regular
Then there exists a machine M with k states
that accepts L
For each 0 ≤ i ≤ k, let Si be the state M is in
after reading ai
∃i,j ≤ k such that Si = Sj, but i ≠ j
M will do the same thing on aibi and ajbi
But a valid M must reject ajbi and accept aibi
Deterministic Finite
              Automata
              • Definition
              • Testing if they accept a string
              • Building automata

              Regular Languages
              • Definition
              • Closed Under Union,
Here’s What
You Need to     Intersection, Negation
  Know…       • Using Pigeonhole Principle to
                show language ain’t regular

More Related Content

PPT
Moore and mealy machines
lavishka_anuj
 
PDF
Fsm sequence detector
lpvasam
 
PPT
Analysis of state machines & Conversion of models
Abhilash Nair
 
PPT
Lect21 Engin112
John Williams
 
PPT
1 introduction1
Sunil Kumar
 
POTX
sistem non linier Lyapunov Stability
ardian gundrunk pratama
 
PPTX
Markov process
Nadeem Hashmi
 
Moore and mealy machines
lavishka_anuj
 
Fsm sequence detector
lpvasam
 
Analysis of state machines & Conversion of models
Abhilash Nair
 
Lect21 Engin112
John Williams
 
1 introduction1
Sunil Kumar
 
sistem non linier Lyapunov Stability
ardian gundrunk pratama
 
Markov process
Nadeem Hashmi
 

What's hot (10)

PDF
Yan maturita 2021
cristinayanying
 
PDF
Yan maturita 2021_
cristinayanying
 
PPTX
lecture 1 content
Cleveland Community College
 
PDF
Lesson 11: Markov Chains
Matthew Leingang
 
PPT
Markov chain intro
2vikasdubey
 
PPT
Real and complex
SumitaGulati
 
PPTX
Markov chain
Luckshay Batra
 
PPTX
6 truyen nhiet
Cang Nguyentrong
 
PDF
99 0771
Pedro León
 
PPTX
LYAPUNOV STABILITY PROBLEM SOLUTION
rohit kumar
 
Yan maturita 2021
cristinayanying
 
Yan maturita 2021_
cristinayanying
 
lecture 1 content
Cleveland Community College
 
Lesson 11: Markov Chains
Matthew Leingang
 
Markov chain intro
2vikasdubey
 
Real and complex
SumitaGulati
 
Markov chain
Luckshay Batra
 
6 truyen nhiet
Cang Nguyentrong
 
99 0771
Pedro León
 
LYAPUNOV STABILITY PROBLEM SOLUTION
rohit kumar
 
Ad

Viewers also liked (9)

PPTX
Theory of computation Lec3 dfa
Arab Open University and Cairo University
 
PPT
Introduction to fa and dfa
deepinderbedi
 
PPTX
Theory of change
Maram Barqawi
 
PPTX
The Theory of Change Approach
Eric Graig
 
PDF
THEORY OF CHANGE
ICCO Cooperation
 
PDF
Lecture: Automata
Marina Santini
 
PPT
Theory of Automata
Farooq Mian
 
PPT
NFA or Non deterministic finite automata
deepinderbedi
 
PDF
Complexity Thinking
Jurgen Appelo
 
Theory of computation Lec3 dfa
Arab Open University and Cairo University
 
Introduction to fa and dfa
deepinderbedi
 
Theory of change
Maram Barqawi
 
The Theory of Change Approach
Eric Graig
 
THEORY OF CHANGE
ICCO Cooperation
 
Lecture: Automata
Marina Santini
 
Theory of Automata
Farooq Mian
 
NFA or Non deterministic finite automata
deepinderbedi
 
Complexity Thinking
Jurgen Appelo
 
Ad

Deterministic Finite Automata

  • 1. Mohammad JAWAD Theory Of Automata And Complier Construction
  • 2. Deterministic Finite Automata COMPSCI 102 Lecture 2
  • 3. Let me show you a machine so simple that you can understand it in less than two minutes
  • 4. 11 1 0 0,1 1 0111 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. 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 the start state (q0) ends in 3a double circle process q states Steven Rudich: www.cs.cmu.edu/~rudich
  • 6. Anatomy of a Deterministic Finite Automaton q1 1 0 0,1 1 q0 q2 0 0 The alphabet of a finite automaton is the set where the symbols come from: 1 {0,1} q3 The language of a finite automaton is the set of strings that it accepts Steven Rudich: www.cs.cmu.edu/~rudich
  • 7. q0 0,1 L(M) = ∅ strings of 0s and 1s All The Language of Machine M Steven Rudich: www.cs.cmu.edu/~rudich
  • 8. 0 0 1 q0 q1 1 L(M) = { w | w has an even number of 1s} Steven Rudich: www.cs.cmu.edu/~rudich
  • 9. Notation An alphabet Σ is a finite set (e.g., Σ = {0,1}) A string over Σ is a finite-length sequence of elements of Σ For x a string, |x| is the length of x The unique string of length 0 will be denoted by ε and will be called the empty or null string A language over Σ is a set of strings over Σ Steven Rudich: www.cs.cmu.edu/~rudich
  • 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. 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. 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. A language is regular if it is recognized by a deterministic finite automaton L = { w | w contains 001} is regular L = { w | w has an even number of 1s} is regular Steven Rudich: www.cs.cmu.edu/~rudich
  • 14. Union Theorem Given two languages, L1 and L2, define the union of L1 and L2 as L1 ∪ L2 = { w | w ∈ L1 or w ∈ L2 } Theorem: The union of two regular languages is also a regular language Steven Rudich: www.cs.cmu.edu/~rudich
  • 15. Theorem: The union of two regular languages is also a regular language Proof Sketch: Let 1 M1 = (Q1, Σ, δ1, q0, F1) be finite automaton for L1 and 2 M2 = (Q2, Σ, δ2, q0, F2) be finite automaton for L2 We want to construct a finite automaton M = (Q, Σ, δ, q0, F) that recognizes L = L1 ∪ L2 Steven Rudich: www.cs.cmu.edu/~rudich
  • 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. Theorem: The union of two regular languages 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. 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. 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. Theorem: The union of two regular languages is also a regular language Corollary: Any finite language is regular Steven Rudich: www.cs.cmu.edu/~rudich
  • 21. The Regular Operations Union: 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. Regular Languages Are Closed Under The Regular Operations We have seen part of the proof for Union. The proof for intersection is very similar. The proof for negation is easy. Steven Rudich: www.cs.cmu.edu/~rudich
  • 23. The “Grep” Problem Input: Text T of length t, string S of length n Problem: Does string S appear inside text T? Naïve method: a1, a2, a3, a4, a5, …, at Cost: Roughly nt comparisons
  • 24. Automata Solution Build a machine M that accepts any string with S as a consecutive substring Feed the text to M Cost: t comparisons + time to build M As luck would have it, the Knuth, Morris, Pratt algorithm builds M quickly
  • 25. Real-life Uses of DFAs Grep Coke Machines Thermostats (fridge) Elevators Train Track Switches Lexical Analyzers for Parsers
  • 27. Consider the language L = { anbn | n > 0 } i.e., a bunch of a’s followed by an equal number of b’s No finite automaton accepts this language Can you prove this?
  • 28. anbn is not regular. No machine has enough states to keep track of the number of a’s it might encounter
  • 29. That is a fairly weak argument Consider the following example…
  • 30. L = strings where the # of occurrences of the pattern ab is equal to the number of occurrences of the pattern ba Can’t be regular. No machine has enough states to keep track of the number of occurrences of ab
  • 31. b a b a a b a a b b M accepts only the strings with an equal number of ab’s and ba’s!
  • 32. Let me show you a professional strength proof that anbn is not regular…
  • 33. Pigeonhole principle: Given n boxes and m > n objects, at least one box must contain more than one object Letterbox principle: If the average number of letters per box is x, then some box will have at least x letters (similarly, some box has at most x)
  • 34. Theorem: L= {anbn | n > 0 } is not regular Proof (by contradiction): Assume that L is regular Then there exists a machine M with k states that accepts L For each 0 ≤ i ≤ k, let Si be the state M is in after reading ai ∃i,j ≤ k such that Si = Sj, but i ≠ j M will do the same thing on aibi and ajbi But a valid M must reject ajbi and accept aibi
  • 35. Deterministic Finite Automata • Definition • Testing if they accept a string • Building automata Regular Languages • Definition • Closed Under Union, Here’s What You Need to Intersection, Negation Know… • Using Pigeonhole Principle to show language ain’t regular