Principles of Computing – UFCFA3-30-1 Week-7 Pushdown Automata Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology
Stack and Queue
Stack Overview Stack Basic operations of stack Pushing, popping etc. Implementations of stacks using array linked list
The Stack A stack is a list with the restriction that insertions and deletions can only be performed at the top of the list The other end is called bottom Fundamental operations: Push: Equivalent to an insert Pop: Deletes the most recently inserted element Top: Examines the most recently inserted element
Stack Stacks are less flexible but are more efficient and easy to implement Stacks are known as LIFO (Last In, First Out) lists. The last element inserted will be the first to be retrieved
Push and Pop Primary operations: Push and Pop Push Pop Add an element to the top of the stack Pop Remove the element at the top of the stack empty stack push an element push another pop top B top top A A A top
Implementation of Stacks Any list implementation could be used to implement a stack Arrays (static: the size of stack is given initially) Linked lists (dynamic: never become full) We will explore implementations based on array and linked list Let’s see how to use an array to implement a stack first
Queue Overview Queue Basic operations of queue Implementation of queue Enqueuing, dequeuing etc. Implementation of queue Array Linked list
Queue Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order. Like customers standing in a check-out line in a store, the first customer in is the first customer served.
The Queue ADT Another form of restricted list Basic operations: Insertion is done at one end, whereas deletion is performed at the other end Basic operations: enqueue: insert an element at the rear of the list dequeue: delete the element at the front of the list First-in First-out (FIFO) list
Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear. Enqueue Insert an element at the rear of the queue Dequeue Remove an element from the front of the queue Insert (Enqueue) Remove (Dequeue) front rear
Pushdown Automata PDAs
Pushdown Automaton -- PDA Input String Stack States
Stack
Initial Stack Symbol $ Stack Stack stack head top bottom special symbol Appears at time 0
The States Pop symbol Input symbol Push symbol a, b c q1 q2
transition , b c Non-Determinism PDAs are non-deterministic q2 Allowed non-deterministic transitions q2 a, b c , b c q1 q1 q2 transition a, b c q3
PDA M : L(M ) {anbn : n 0} , q1 b, a , $ $ Example PDA PDA M : L(M ) {anbn : n 0} a, a b, a , q1 b, a , $ $ q0 q2 q3
L(M ) {anbn : n 0} , q1 b, a , $ $ a, a b, a Basic Idea: 1. Push the a’s on the stack Match the b’s on input with a’s on stack Match found a, a b, a , q1 b, a , $ $ q0 q2 q3
q0 , q1 b, a , $ $ b $ a, a b, a q2 q3 a Time 0 Execution Example: Input Time 0 a b $ Stack current state a, a b, a q0 , q1 b, a , $ $ q2 q3
, b, a q2 , $ $ b $ a, a b, a q0 q1 q3 a Time 1 Input a b $ Stack a, a b, a , b, a q2 , $ $ q0 q1 q3
, b, a q2 , $ $ b $ a, a b, a q0 q1 q3 a a Time 2 Input a b a $ Stack a, a b, a , b, a q2 , $ $ q0 q1 q3
, b, a q2 , $ $ $ b a, a b, a q0 q1 q3 a a a Time 3 Input a a $ Stack a b a, a b, a , b, a q2 , $ $ q0 q1 q3
, b, a q2 , $ $ b $ a, a b, a q0 q1 q3 a a a a Time 4 a a Input a b a $ Stack a, a b, a , b, a q2 , $ $ q0 q1 q3
, q1 b, a , $ $ b $ a, a b, a q0 q2 q3 a a a a Time 5 a a a Input a b $ Stack a, a b, a , q1 b, a , $ $ q0 q2 q3
, q1 b, a , $ $ $ b a, a b, a q0 q2 q3 a a a Time 6 Input a a $ Stack a b a, a b, a , q1 b, a , $ $ q0 q2 q3
, q1 b, a , $ $ $ b a, a b, a q0 q2 q3 a a Time 7 Input a $ Stack a b a, a b, a , q1 b, a , $ $ q0 q2 q3
, q1 b, a , $ $ b $ a, a b, a q0 q2 q3 a Time 8 Input a b $ Stack a, a b, a accept , q1 b, a , $ $ q0 q2 q3
A string is accepted if there is a computation such that: All the input is consumed AND The last state is an accepting state we do not care about the stack contents at the end of the accepting computation
q0 , q1 b, a , $ $ b $ a, a b, a q2 q3 a Time 0 Rejection Example: Input Time 0 a b $ Stack current state a, a b, a q0 , q1 b, a , $ $ q2 q3
q0 , q1 b, a , $ $ b $ a, a b, a q2 q3 a Time 1 Rejection Example: Input Time 1 a b $ Stack current state a, a b, a q0 , q1 b, a , $ $ q2 q3
q0 , q1 b, a , $ $ $ b a, a b, a q2 q3 a a Rejection Example: Input Time 2 a $ Stack a b current state a, a b, a q0 , q1 b, a , $ $ q2 q3
q0 , q1 b, a , $ $ b $ a, a b, a q2 q3 a a a Rejection Example: Input Time 3 a a a b $ Stack current state a, a b, a q0 , q1 b, a , $ $ q2 q3
q0 , q1 b, a , $ $ b $ a, a b, a q2 q3 a a a Rejection Example: Input Time 4 a a a b $ Stack current state a, a b, a q0 , q1 b, a , $ $ q2 q3
q0 , q1 b, a , $ $ b $ reject b, a a, a q2 q3 Rejection Example: Input Time 4 a a a b $ Stack reject b, a current state a, a q0 , q1 b, a , $ $ q2 q3
There is no accepting computation for aab The string aab is rejected by the PDA a, a b, a , q1 b, a , $ $ q0 q2 q3
With Empty Input
With Empty Input
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
With Input (aaabbb) a b
constructing a deterministic NPDA for the language L = {anbn : n > 0}.