SlideShare ist ein Scribd-Unternehmen logo
1 von 115
Pushdown
Automata
PDAs
Pushdown Automaton --
PDA Input String
                 Stack


 States
Initial Stack Symbol


     Stack               Stack



      $                       z

               bottom
             special symbol
The States
   Input          Pop            Push
   symbol         symbol         symbol



                 a, b   c
            q1              q2
a, b   c
                      q1              q2


input
           a                                a   

   stack
        b       top                        c
        h                  Replace         h
        e                                  e
        $                                  $
a,      c
                      q1               q2


input
           a                                 a   

   stack                                    c
        b       top                         b
        h                   Push            h
        e                                   e
        $                                   $
a, b
                      q1           q2


input
           a                             a   

   stack
        b       top
        h                    Pop        h
        e                               e
        $                               $
a,
                      q1               q2


input
           a                                 a   

   stack
        b       top                         b
        h                  No Change        h
        e                                   e
        $                                   $
Non-Determinism

 a, b    c    q2

q1                             , b      c
                          q1                q2

 a, b    c                     transition
             q3

     These are allowed transitions in
     a Non-deterministic PDA (NPDA)
NPDA: Non-Deterministic
PDA
Example:




           a,       a     b, a


q0   ,          q1 b, a      q2   ,$   $ q
                                          3
Execution Example: Time 0
  Input
   a a a b b b
                                          $
                                      Stack


current       a,       a     b, a
state
          ,           b, a           ,$   $ q
  q0               q1           q2           3
Time 1
Input
 a a a b b b
                                       $
                                   Stack


         a,        a      b, a


q0   ,        q1   b, a      q2   ,$   $ q
                                          3
Time 2
Input
 a a a b b b                           a
                                       $
                                   Stack

         a,        a      b, a


q0   ,        q1   b, a      q2   ,$       $ q
                                              3
Time 3
Input                                  a
 a a a b b b                           a
                                       $
                                   Stack


         a,        a      b, a


q0   ,        q1   b, a      q2   ,$   $ q
                                          3
Time 4
                                       a
Input
                                       a
 a a a b b b                           a
                                       $
                                   Stack

         a,        a      b, a


q0   ,        q1   b, a      q2   ,$   $ q
                                          3
Time 5
                                     a
Input
                                     a
 a a a b b b                         a
                                     $
                                 Stack

         a,       a     b, a


q0   ,        q1 b, a      q2   ,$       $ q
                                            3
Time 6
Input                                a
 a a a b b b                         a
                                     $
                                 Stack

         a,       a     b, a


q0   ,        q1 b, a      q2   ,$   $ q
                                        3
Time 7
Input
 a a a b b b                          a
                                      $
                                     Stack


         a,       a     b, a


q0   ,        q1 b, a      q2   ,$     $ q
                                          3
Time 8
Input
 a a a b b b
                                      $
                                     Stack


         a,       a     b, a
                                       accept

q0   ,        q1 b, a      q2   ,$     $ q
                                          3
The Language of PDA
A string is accepted if there is
a computation such that:

     • All the input is consumed

     • The last state is a final state

At the end of the computation,
we do not care about the stack contents
Formal Definition Automaton
Non-Deterministic Pushdown
                 NPDA
         M   (Q, Σ, Γ, δ, q0 , z, F )   Final
States                                  states

Input                                   Stack
alphabet        Transition Initial      start
       Stack
                function   state        symbol
       alphabet
Instantaneous Description
            ( q, u , s )

  Current                  Current
             Remaining
  state                    stack
             input
                           contents
Acceptance by Final State


L(M)= set of all strings w such that starting
  from initial ID machine consumes w from
  input and enters an accepting state.
The contents of the stack at that time is
  irrelevant.
{w | (q0, w, Z) * (q, , )
For some final state q of F and any stack
  staring .
Acceptance by empty
stack
 N(M) = (w | (q0,w,Z)    * (q,   ,   )

 That is N(M) is the set of all strings w such
  that M can consume w and at the same
  time empties its stack.Here we do not
  care whether q is a final state or not.
The input string aaabbb
         is accepted by the NPDA:



             a,       a     b, a


q0   ,            q1 b, a      q2   ,$   $ q
                                            3
In general,
                      n n
              L {a b : n 0}

is the language accepted by the NPDA:



              a,       a     b, a


 q0   ,            q1 b, a      q2   ,$   $ q
                                             3
Another NPDA example
                   NPDA      M
                                 R
                  L( M ) {ww }

a,        a           a, a
b,        b           b, b


     q0       ,           q1         ,$   $   q2
Execution Example:   Time 0
  Input
       a b b a
                                         $
                                       Stack
  a,        a        a, a
  b,        b        b, b


       q0       ,       q1    ,$   $     q2
Time 1
Input
     a b b a
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 2
Input
                                      b
     a b b a
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 3
Input
                                           b
     a b b a
                  Guess the middle         a
                  of string                $
                                         Stack
a,        a         a, a
b,        b         b, b


     q0       ,        q1     ,$     $     q2
Time 4
Input
                                      b
     a b b a
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 5
Input
     a b b a
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,            ,$   $     q2
                     q1
Time 6
Input
     a b b a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b
                                    accept
     q0       ,      q1    ,$   $     q2
Rejection Example:   Time 0
  Input
       a b b b
                                         $
                                       Stack
  a,        a        a, a
  b,        b        b, b


       q0       ,       q1    ,$   $     q2
Time 1
Input
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 2
Input
                                      b
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 3
Input
                                           b
     a b b b
                  Guess the middle         a
                  of string                $
                                         Stack
a,        a         a, a
b,        b         b, b


     q0       ,        q1     ,$     $     q2
Time 4
Input
                                      b
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 5
Input             There is no possible transition.

     a b b b            Input is not          a
                        consumed
                                              $
                                            Stack
a,        a              a, a
b,        b              b, b


     q0       ,                   ,$    $     q2
                            q1
Another computation on same string:
  Input             Time 0
       a b b b
                                        $
                                      Stack
  a,        a       a, a
  b,        b       b, b


       q0       ,      q1    ,$   $     q2
Time 1
Input
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 2
Input
                                      b
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 3
Input                                 b
                                      b
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 4              b
Input                                 b
                                      b
     a b b b
                                      a
                                      $
                                    Stack
a,        a       a, a
b,        b       b, b


     q0       ,      q1    ,$   $     q2
Time 5                b
Input                                    b
                  No final state         b
     a b b b      is reached             a
                                         $
                                       Stack
a,        a        a, a
b,        b        b, b


     q0       ,       q1    ,$     $     q2
There is no computation
          that accepts string abbb

                  abbb L(M )


a,        a          a, a
b,        b          b, b


     q0       ,         q1     ,$   $   q2
Pushing Strings
    Input          Pop            Push
    symbol         symbol         string



                  a, b   w
             q1              q2
Example:
                           a, b    cdf
                      q1                 q2

input
   
           a    
                                        
                                                 a   
                                                      


                                              c       pushed
   stack                                      d
                top                                   string
        b                                     f
        h                   Push              h
        e                                     e
        $                                     $
Another NPDA example
                  NPDA   M
        L( M ) {w : na            nb }

 a, $   0$        b, $   1$
 a, 0   00        b, 1   11
 a, 1             b, 0


                         ,$   $      q2
             q1
Execution Example:            Time 0
  Input
   a b       b a a b

                                                $
     a, $    0$        b, $    1$
                                           Stack
     a, 0    00        b, 1    11
     a, 1              b, 0
   current
   state                       ,$      $
                  q1                       q2
Time 1
Input
 a b     b a a b
                                            0
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                            ,$     $   q2
              q1
Time 3
Input
 a b     b b a a
                                            0
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                           ,$      $   q2
              q1
Time 4
Input
 a b     b b a a
                                            1
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                           ,$      $   q2
              q1
Time 5
Input
 a b     b b a a                            1
                                            1
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                           ,$      $   q2
              q1
Time 6
Input
 a b     b b a a                            1
                                            1
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                           ,$      $   q2
              q1
Time 7
Input
 a b     b b a a
                                            1
                                            $
  a, $   0$        b, $    1$          Stack
  a, 0   00        b, 1    11
  a, 1             b, 0


                           ,$      $   q2
              q1
Time 8
Input
 a b     b b a a

                                             $
  a, $   0$        b, $    1$
                                        Stack
  a, 0   00        b, 1    11
  a, 1             b, 0
                                       accept
                           ,$      $    q2
              q1
Formalities for
NPDAs
a, b   w
               q1              q2

Transition function:


              (q1, a, b) {(q2 , w)}
a, b    w   q2

              q1

               a, b    w   q3

Transition function:

        (q1, a, b) {(q2 , w), (q3 , w)}
Formal Definition Automaton
Non-Deterministic Pushdown
                 NPDA
         M   (Q, Σ, Γ, δ, q0 , z, F )   Final
States                                  states

Input                                   Stack
alphabet        Transition Initial      start
       Stack
                function   state        symbol
       alphabet
Instantaneous Description
            ( q, u , s )

  Current                  Current
             Remaining
  state                    stack
             input
                           contents
Example:      Instantaneous Description
                    (q1, bbb, aaa$)
                                              a
Time 4:       Input                           a
                   a a a b b b                a
                                              $
                                            Stack
              a,        a      b, a


  q0      ,        q1   b, a      q2   ,$   $ q
                                               3
Example:      Instantaneous Description
                        (q2 , bb, aa$)
                                                a
Time 5:       Input                             a
                   a a a b b b                  a
                                                $
                                              Stack
              a,        a       b, a


  q0      ,        q1    b, a      q2    ,$   $ q
                                                 3
We write:



    (q1, bbb, aaa$)  (q2 , bb, aa$)
      Time 4              Time 5
A computation:

(q0 , aaabbb,$)  (q1, aaabbb,$) 
(q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) 
(q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$)


                a,        a      b, a


   q0    ,           q1   b, a      q2    ,$     $ q
                                                    3
(q0 , aaabbb,$)  (q1, aaabbb,$) 
(q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) 
(q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$)


    For convenience we write:


           (q0 , aaabbb ,$)  (q3 , ,$)
Formal Definition
Language of NPDA M :


   L( M ) {w : (q0 , w, s )  (q f , , s ' )}


     Initial state                Final state
Example:
            (q0 , aaabbb,$)  (q3 , ,$)


                      aaabbb L(M )

 NPDA M :

            a,        a      b, a


  q0   ,         q1   b, a      q2   ,$   $ q
                                             3
n n
           (q0 , a b ,$)  (q3 , ,$)

                          n n
                         a b       L(M )

NPDA M :

           a,        a          b, a


 q0   ,         q1   b, a          q2   ,$   $ q
                                                3
Therefore:                  n n
                L( M ) {a b : n 0}




NPDA M :

           a,        a      b, a


 q0   ,         q1   b, a      q2   ,$   $ q
                                            3
NPDAs Accept
Context-Free
Languages
Theorem:

  Context-Free    Languages
    Languages    Accepted by
   (Grammars)      NPDAs
Proof - Step 1:

  Context-Free            Languages
    Languages            Accepted by
   (Grammars)              NPDAs


 Convert any context-free grammar G
 to a NPDA M with: L (G ) L( M )
Proof - Step 2:

  Context-Free           Languages
    Languages           Accepted by
   (Grammars)             NPDAs


 Convert any NPDA M to a context-free
 grammar G with: L (G ) L( M )
Converting
Context-Free
Grammars
to
NPDAs
An example grammar:   S   aSTb
                      S   b
                      T   Ta
                      T

What is the equivalent NPDA?
Grammar:
S   aSTb
S   b                NPDA:
T   Ta
               ,S   aSTb
T
               ,S   b
               ,T   Ta   a, a
               ,T        b, b


    q0     ,    S            ,$   $
                      q1              q2
The NPDA simulates
leftmost derivations of the grammar




       L(Grammar) = L(NPDA)
Grammar: S     aSTb
          S    b
          T    Ta
          T


A leftmost derivation:

    S   aSTb       abTb   abTab   abab
NPDA execution: Time 0

Input   a b a b

              ,S   aSTb               $
              ,S   b                Stack
              ,T   Ta   a, a
current       ,T        b, b
state
          ,    S           ,$   $
   q0                q1               q2
Time 1

Input   a b a b
                                      S
             ,S    aSTb               $
             ,S    b                Stack
             ,T    Ta   a, a
             ,T         b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 2             a
                                     S
Input   a b a b
                                     T
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 3             a
                                     S
Input   a b a b
                                     T
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 4
                                     b
Input   a b a b
                                     T
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 5
                                     b
Input   a b a b
                                     T
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 6
                                     T
Input   a b a b                      a
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 7
                                     T
Input   a b a b                      a
                                     b
             ,S   aSTb               $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 8

Input   a b a b                       a
                                      b
             ,S   aSTb                $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 9

Input   a b a b
                                      b
             ,S   aSTb                $
             ,S   b                 Stack
             ,T   Ta   a, a
             ,T        b, b


   q0    ,    S            ,$   $
                     q1               q2
Time 10

Input   a b a b

             ,S   aSTb                 $
             ,S   b                  Stack
             ,T   Ta   a, a
             ,T        b, b
                                     accept

   q0    ,    S             ,$   $
                     q1                q2
In general:

    Given any grammar    G

    We can construct a NPDA    M

     With     L(G )   L( M )
Constructing NPDA         M from grammar G :

 For any production                   For any terminal
         A       w                             a


                     ,A   w        a, a


    q0       ,        S               ,$   $
                              q1                   q2
Grammar     G generates string w

if and only if

NPDA      M accepts w



     L(G)    L( M )
Therefore:


     For any context-free language
     there is an NPDA
     that accepts the same language
Converting
NPDAs
to
Context-Free
Grammars
For any NPDA    M

we will construct

a context-free grammar    G with

             L( M )   L(G )
Intuition: The grammar simulates the machine


A derivation in Grammar   G:

   S        abc ABC                 abc



    Current configuration in NPDA   M
A derivation in Grammar    G:
           terminals      variables
    S         abc ABC                 abc


    Input processed          Stack contents

                 in NPDA     M
Some Necessary
Modifications
First, we modify the NPDA:
    • It has a single final state q f
    • It empties the stack
      when it accepts the input


   Original NPDA              Empty Stack

                                        qf
Second, we modify the NPDA transitions:

     all transitions will have form

                        a, B       qj
                   qi

                           or

                   qi   a, B    CD q
                                     j


B, C , D : stack symbols
Example of a NPDA in correct form:

           L( M ) {w : na         nb }

    $ : initial stack symbol

    a, $    0$        b, $   1$
    a, 0    00        b, 1   11
    a, 1              b, 0


                             ,$          qf
                 q0
The Grammar
 Construction
In grammar G :
                   Stack symbol

          Variables:   (qi Bq j )

                        states



          Terminals:
          Input symbols of NPDA
For each transition
                            a, B       qj
                       qi




We add production     (qi Bq j )   a
For each transition   qi   a, B      CD q
                                          j




We add production (qi Bqk )       a(q j Cql )(ql Dqk )

                      For all states qk , ql
Stack bottom symbol


Start Variable:     (qo $q f )


      Start state                final state
Example:
    a, $   0$        b, $   1$
    a, 0   00        b, 1   11
    a, 1             b, 0


                            ,$         qf
                q0



Grammar production:         (q01q0 )   a
Example:
     a, $    0$        b, $   1$
     a, 0    00        b, 1   11
     a, 1              b, 0


                              ,$          qf
                  q0
Grammar productions:
(q0 $q0 )    b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )   b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
Example:
     a, $   0$        b, $   1$
     a, 0   00        b, 1   11
     a, 1             b, 0


                             ,$   qf
                 q0


Grammar production: (q0 $q f )
Resulting Grammar:          (q0 $q f ) : start variable


(q0 $q0 )    b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )   b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )

(q01q0 )     b(q01q0 )(q01q0 ) | b(q01q f )(q f 1q0 )
(q01q f )    b(q01q0 )(q01q f ) | b(q01q f )(q f 1q f )

(q0 $q0 )    a (q0 0q0 )(q0 $q0 ) | a (q0 0q f )(q f $q0 )
(q0 $q f )    a (q0 0q0 )(q0 $q f ) | a (q0 0q f )(q f $q f )
(q0 0q0 )    a (q0 0q0 )(q0 0q0 ) | a (q0 0q f )(q f 0q0 )
(q0 0q f )   a (q0 0q0 )(q0 0q f ) | a (q0 0q f )(q f 0q f )

       (q01q0 )     a
       (q0 0q0 )    b

       (q0 $q f )
Derivation of string         abba

(q0 $q f )    a(q0 0q0 )(q0 $q f )

                      ab(q0 $q f )

             abb(q01q0 )(q0 $q f )

                   abba(q0 $q f )       abba
In general, in Grammar:

            (q0 $q f )    w

              if and only if


      w is accepted by the NPDA
Explanation:

   By construction of Grammar:

               (qi Aq j )   w

               if and only if

    in the NPDA going from qi to q j
    the stack doesn’t change below A
    and A is removed from stack

Weitere ähnliche Inhalte

Was ist angesagt?

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDAAshish Duggal
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata TheorySaifur Rahman
 
Finite State Machines with Output
Finite State Machines with OutputFinite State Machines with Output
Finite State Machines with OutputMustafa Saeed
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory Rajendran
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata introlavishka_anuj
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
A brief introduction to Gaussian process
A brief introduction to Gaussian processA brief introduction to Gaussian process
A brief introduction to Gaussian processEric Xihui Lin
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraRoshan Tailor
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
Turing Machine
Turing MachineTuring Machine
Turing MachineAyAn KhAn
 

Was ist angesagt? (20)

Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata Theory
 
Finite State Machines with Output
Finite State Machines with OutputFinite State Machines with Output
Finite State Machines with Output
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Push down automata
Push down automataPush down automata
Push down automata
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Finite automata intro
Finite automata introFinite automata intro
Finite automata intro
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
A brief introduction to Gaussian process
A brief introduction to Gaussian processA brief introduction to Gaussian process
A brief introduction to Gaussian process
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 

Mehr von lavishka_anuj

Mehr von lavishka_anuj (9)

Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
One ques
One quesOne ques
One ques
 
Non regular languages
Non regular languagesNon regular languages
Non regular languages
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Grammar
GrammarGrammar
Grammar
 
Flat
FlatFlat
Flat
 
Finite automata
Finite automataFinite automata
Finite automata
 
Turing machines
Turing machinesTuring machines
Turing machines
 

Kürzlich hochgeladen

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Kürzlich hochgeladen (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

PDAs Explained: Pushdown Automata and Their Languages

  • 2. Pushdown Automaton -- PDA Input String Stack States
  • 3. Initial Stack Symbol Stack Stack $ z bottom special symbol
  • 4. The States Input Pop Push symbol symbol symbol a, b c q1 q2
  • 5. a, b c q1 q2 input  a   a  stack b top c h Replace h e e $ $
  • 6. a, c q1 q2 input  a   a  stack c b top b h Push h e e $ $
  • 7. a, b q1 q2 input  a   a  stack b top h Pop h e e $ $
  • 8. a, q1 q2 input  a   a  stack b top b h No Change h e e $ $
  • 9. Non-Determinism a, b c q2 q1 , b c q1 q2 a, b c transition q3 These are allowed transitions in a Non-deterministic PDA (NPDA)
  • 10. NPDA: Non-Deterministic PDA Example: a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 11. Execution Example: Time 0 Input a a a b b b $ Stack current a, a b, a state , b, a ,$ $ q q0 q1 q2 3
  • 12. Time 1 Input a a a b b b $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 13. Time 2 Input a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 14. Time 3 Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 15. Time 4 a Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 16. Time 5 a Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 17. Time 6 Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 18. Time 7 Input a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 19. Time 8 Input a a a b b b $ Stack a, a b, a accept q0 , q1 b, a q2 ,$ $ q 3
  • 20. The Language of PDA A string is accepted if there is a computation such that: • All the input is consumed • The last state is a final state At the end of the computation, we do not care about the stack contents
  • 21. Formal Definition Automaton Non-Deterministic Pushdown NPDA M (Q, Σ, Γ, δ, q0 , z, F ) Final States states Input Stack alphabet Transition Initial start Stack function state symbol alphabet
  • 22. Instantaneous Description ( q, u , s ) Current Current Remaining state stack input contents
  • 23. Acceptance by Final State L(M)= set of all strings w such that starting from initial ID machine consumes w from input and enters an accepting state. The contents of the stack at that time is irrelevant. {w | (q0, w, Z) * (q, , ) For some final state q of F and any stack staring .
  • 24. Acceptance by empty stack N(M) = (w | (q0,w,Z) * (q, , ) That is N(M) is the set of all strings w such that M can consume w and at the same time empties its stack.Here we do not care whether q is a final state or not.
  • 25. The input string aaabbb is accepted by the NPDA: a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 26. In general, n n L {a b : n 0} is the language accepted by the NPDA: a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 27. Another NPDA example NPDA M R L( M ) {ww } a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 28. Execution Example: Time 0 Input a b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 29. Time 1 Input a b b a a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 30. Time 2 Input b a b b a a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 31. Time 3 Input b a b b a Guess the middle a of string $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 32. Time 4 Input b a b b a a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 33. Time 5 Input a b b a a $ Stack a, a a, a b, b b, b q0 , ,$ $ q2 q1
  • 34. Time 6 Input a b b a $ Stack a, a a, a b, b b, b accept q0 , q1 ,$ $ q2
  • 35. Rejection Example: Time 0 Input a b b b $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 36. Time 1 Input a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 37. Time 2 Input b a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 38. Time 3 Input b a b b b Guess the middle a of string $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 39. Time 4 Input b a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 40. Time 5 Input There is no possible transition. a b b b Input is not a consumed $ Stack a, a a, a b, b b, b q0 , ,$ $ q2 q1
  • 41. Another computation on same string: Input Time 0 a b b b $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 42. Time 1 Input a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 43. Time 2 Input b a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 44. Time 3 Input b b a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 45. Time 4 b Input b b a b b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 46. Time 5 b Input b No final state b a b b b is reached a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 47. There is no computation that accepts string abbb abbb L(M ) a, a a, a b, b b, b q0 , q1 ,$ $ q2
  • 48. Pushing Strings Input Pop Push symbol symbol string a, b w q1 q2
  • 49. Example: a, b cdf q1 q2 input   a     a   c pushed stack d top string b f h Push h e e $ $
  • 50. Another NPDA example NPDA M L( M ) {w : na nb } a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 51. Execution Example: Time 0 Input a b b a a b $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 current state ,$ $ q1 q2
  • 52. Time 1 Input a b b a a b 0 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 53. Time 3 Input a b b b a a 0 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 54. Time 4 Input a b b b a a 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 55. Time 5 Input a b b b a a 1 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 56. Time 6 Input a b b b a a 1 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 57. Time 7 Input a b b b a a 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  • 58. Time 8 Input a b b b a a $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 accept ,$ $ q2 q1
  • 60. a, b w q1 q2 Transition function: (q1, a, b) {(q2 , w)}
  • 61. a, b w q2 q1 a, b w q3 Transition function: (q1, a, b) {(q2 , w), (q3 , w)}
  • 62. Formal Definition Automaton Non-Deterministic Pushdown NPDA M (Q, Σ, Γ, δ, q0 , z, F ) Final States states Input Stack alphabet Transition Initial start Stack function state symbol alphabet
  • 63. Instantaneous Description ( q, u , s ) Current Current Remaining state stack input contents
  • 64. Example: Instantaneous Description (q1, bbb, aaa$) a Time 4: Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 65. Example: Instantaneous Description (q2 , bb, aa$) a Time 5: Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 66. We write: (q1, bbb, aaa$)  (q2 , bb, aa$) Time 4 Time 5
  • 67. A computation: (q0 , aaabbb,$)  (q1, aaabbb,$)  (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$)  (q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$) a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 68. (q0 , aaabbb,$)  (q1, aaabbb,$)  (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$)  (q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$) For convenience we write: (q0 , aaabbb ,$)  (q3 , ,$)
  • 69. Formal Definition Language of NPDA M : L( M ) {w : (q0 , w, s )  (q f , , s ' )} Initial state Final state
  • 70. Example: (q0 , aaabbb,$)  (q3 , ,$) aaabbb L(M ) NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 71. n n (q0 , a b ,$)  (q3 , ,$) n n a b L(M ) NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 72. Therefore: n n L( M ) {a b : n 0} NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  • 74. Theorem: Context-Free Languages Languages Accepted by (Grammars) NPDAs
  • 75. Proof - Step 1: Context-Free Languages Languages Accepted by (Grammars) NPDAs Convert any context-free grammar G to a NPDA M with: L (G ) L( M )
  • 76. Proof - Step 2: Context-Free Languages Languages Accepted by (Grammars) NPDAs Convert any NPDA M to a context-free grammar G with: L (G ) L( M )
  • 78. An example grammar: S aSTb S b T Ta T What is the equivalent NPDA?
  • 79. Grammar: S aSTb S b NPDA: T Ta ,S aSTb T ,S b ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 80. The NPDA simulates leftmost derivations of the grammar L(Grammar) = L(NPDA)
  • 81. Grammar: S aSTb S b T Ta T A leftmost derivation: S aSTb abTb abTab abab
  • 82. NPDA execution: Time 0 Input a b a b ,S aSTb $ ,S b Stack ,T Ta a, a current ,T b, b state , S ,$ $ q0 q1 q2
  • 83. Time 1 Input a b a b S ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 84. Time 2 a S Input a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 85. Time 3 a S Input a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 86. Time 4 b Input a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 87. Time 5 b Input a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 88. Time 6 T Input a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 89. Time 7 T Input a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 90. Time 8 Input a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 91. Time 9 Input a b a b b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  • 92. Time 10 Input a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b accept q0 , S ,$ $ q1 q2
  • 93. In general: Given any grammar G We can construct a NPDA M With L(G ) L( M )
  • 94. Constructing NPDA M from grammar G : For any production For any terminal A w a ,A w a, a q0 , S ,$ $ q1 q2
  • 95. Grammar G generates string w if and only if NPDA M accepts w L(G) L( M )
  • 96. Therefore: For any context-free language there is an NPDA that accepts the same language
  • 98. For any NPDA M we will construct a context-free grammar G with L( M ) L(G )
  • 99. Intuition: The grammar simulates the machine A derivation in Grammar G: S  abc ABC   abc Current configuration in NPDA M
  • 100. A derivation in Grammar G: terminals variables S  abc ABC   abc Input processed Stack contents in NPDA M
  • 101. Some Necessary Modifications First, we modify the NPDA: • It has a single final state q f • It empties the stack when it accepts the input Original NPDA Empty Stack qf
  • 102. Second, we modify the NPDA transitions: all transitions will have form a, B qj qi or qi a, B CD q j B, C , D : stack symbols
  • 103. Example of a NPDA in correct form: L( M ) {w : na nb } $ : initial stack symbol a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0
  • 104. The Grammar Construction In grammar G : Stack symbol Variables: (qi Bq j ) states Terminals: Input symbols of NPDA
  • 105. For each transition a, B qj qi We add production (qi Bq j ) a
  • 106. For each transition qi a, B CD q j We add production (qi Bqk ) a(q j Cql )(ql Dqk ) For all states qk , ql
  • 107. Stack bottom symbol Start Variable: (qo $q f ) Start state final state
  • 108. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0 Grammar production: (q01q0 ) a
  • 109. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0 Grammar productions: (q0 $q0 ) b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 ) (q0 $q f ) b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
  • 110. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0 Grammar production: (q0 $q f )
  • 111. Resulting Grammar: (q0 $q f ) : start variable (q0 $q0 ) b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 ) (q0 $q f ) b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f ) (q01q0 ) b(q01q0 )(q01q0 ) | b(q01q f )(q f 1q0 ) (q01q f ) b(q01q0 )(q01q f ) | b(q01q f )(q f 1q f ) (q0 $q0 ) a (q0 0q0 )(q0 $q0 ) | a (q0 0q f )(q f $q0 ) (q0 $q f ) a (q0 0q0 )(q0 $q f ) | a (q0 0q f )(q f $q f )
  • 112. (q0 0q0 ) a (q0 0q0 )(q0 0q0 ) | a (q0 0q f )(q f 0q0 ) (q0 0q f ) a (q0 0q0 )(q0 0q f ) | a (q0 0q f )(q f 0q f ) (q01q0 ) a (q0 0q0 ) b (q0 $q f )
  • 113. Derivation of string abba (q0 $q f ) a(q0 0q0 )(q0 $q f ) ab(q0 $q f ) abb(q01q0 )(q0 $q f ) abba(q0 $q f ) abba
  • 114. In general, in Grammar: (q0 $q f ) w if and only if w is accepted by the NPDA
  • 115. Explanation: By construction of Grammar: (qi Aq j ) w if and only if in the NPDA going from qi to q j the stack doesn’t change below A and A is removed from stack