Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles of Computing – UFCFA3-30-1

Similar presentations


Presentation on theme: "Principles of Computing – UFCFA3-30-1"— Presentation transcript:

1 Principles of Computing – UFCFA3-30-1
Week-7 Pushdown Automata Instructor : Mazhar H Malik Global College of Engineering and Technology

2 Stack and Queue

3 Stack Overview Stack Basic operations of stack
Pushing, popping etc. Implementations of stacks using array linked list

4 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

5 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

6 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

7 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

8 Queue Overview Queue Basic operations of queue Implementation of queue
Enqueuing, dequeuing etc. Implementation of queue Array Linked list

9 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.

10 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

11 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

12 Pushdown Automata PDAs

13 Pushdown Automaton -- PDA
Input String Stack States

14 Stack

15 Initial Stack Symbol $ Stack Stack stack head top
bottom special symbol Appears at time 0

16 The States Pop symbol Input symbol Push symbol a, b  c q1 q2

17   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

18 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

19 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

20 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

21 ,    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

22 ,    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

23 ,    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

24 ,    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

25 ,    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

26 ,    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

27 ,    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

28 ,    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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 With Empty Input

38 With Empty Input

39 With Input (aaabbb) a b

40 With Input (aaabbb) a b

41 With Input (aaabbb) a b

42 With Input (aaabbb) a b

43 With Input (aaabbb) a b

44 With Input (aaabbb) a b

45 With Input (aaabbb) a b

46 With Input (aaabbb) a b

47 With Input (aaabbb) a b

48 With Input (aaabbb) a b

49 constructing a deterministic NPDA for the language L = {anbn : n > 0}.

50


Download ppt "Principles of Computing – UFCFA3-30-1"

Similar presentations


Ads by Google