Clarification regarding PDA for L = {a^nb^(2n) | n>=1} - stack

The solution says for every 'a' you read, push 2 a's into the stack . Finally when you encounter 'b' , pop an 'a'. But won't this give the output as a^n b^n?
For example:
Input = aabbbb
On reading the a's , the stack will have four 4 a's , hence on popping one 'a' for every 'b' encountered , won't you get aaaabbbb?

These a are different. One is from the input, another is for the stack. They are probably with different font in your document. The push-down automaton has a stack. In this stack (Last In First Out: LIFO) it remembers information that it uses for guide of how to accept the input: Wikipedia.
The idea is as follows:
for every input character a push into the stack two t and move to the next character
for every character b one t from the stack has to be popped.
constraints: you cannot have a after b, and you need at least one input character
acceptance: no more input and an empty stack
Here the stack is used to remember how many b to pop: two times more then a.

The strings which are generated by the given language are:
L={abb,aabbbb,aaabbbbbb,….}
Here a’s are followed by double the b’s
Whenever ‘a’ comes, push any character here let ‘t’ two times in the stack and if ‘a’ comes again then the same process is repeated.
When ‘b’ comes then pop one ‘t’ from the stack each time.
Finally at the end of the string, if nothing is left in the STACK, then we can declare that language is accepted in the PDA.
The PDA for the problem is as follows:
The transition functions are:
δ(q0, a, Z) = (q0,ttZ)
δ(q0, a, t) = (q0,ttt)
δ(q0, b, t) = (q1,ε)
δ(q1, b, t) = (q1,ε)
δ(q1, ε, Z) = (qf,Z)
Explanation:
Step 1 : Consider the string: "aabbbb" which satisfies the given condition.
Step 2 : For input 'a' and STACK alphabet Z, push two t's into the stack.
Step 3 : For input 'a' and STACK alphabet 't', again push two t's into the stack.
Push the two 't's into STACK: (a,t/ttt) and state will be q0. Now the STACK has "tttt".
Step 4 : For input 'b' and STACK alphabet 't', then
Pop one 't' from STACK: (b,t/ε) and state will be q1.
Step 5 : For input 'b' and STACK alphabet 't' and state q1, then
Pop one 't' from STACK: (b,t/ε) and state will remain q1
Step 6 : For input 'b' and STACK alphabet 't', then
Pop one 't' from STACK: (b,t/ε) and state will be q1
Step 7 : For input 'b' and STACK alphabet 't' and state q1, then
Pop one 't' from STACK: (b,t/ε) and state will remain q1
Step 8 : We reached end of the string, for input ε and STACK alphabet Z,
Go to final state(qf): (ε, Z/Z)

Related

"How to make a turing machine of {w ∈ {a,b}∗ | 2na(w) = 3nb(w)}. My question is in how to apply the condition"

This is an assignment I have for a module. I understand turing machines, the problem for me is how do I make sure the ratio is maintained. I can see how one would check this if we can check for every 5 digits without being intermixed (e.g. {aababaabab}) but for the words like: {aaaaaabbbb}. Very lost.
Any tips/help?
I assume this means that the ration n/m = 3/2 where n is the number of occurrences of a and m is the number of occurrences of b.
To solve the general case, lots of solutions are possible; here is one that might be easy to understand.
scanning left to right, find 3 occurrences of a. Skip over A, B and b. Change the three a you find to A and go back to the beginning of the tape. If you hit the end of the tape without crossing over any a or b, halt-accept. If you hit the end of the tape having seen some a and b but not at least three a, halt-reject. Otherwise, continue to step 2.
scanning left to right, find 2 occurrences of b. Skip over A, B and a. Change the two b you find to B and go back to the beginning of the tape. If you hit the end without seeing at least two b, halt-reject. Repeat starting from step 1 until you halt-accept or halt-reject.
Examples:
aababaabab aaaaaabbbb aaaaabbbb
AAbAbaabab AAAaaabbbb AAAaabbbb
AABABaabab AAAaaaBBbb AAAaaBBbb
AABABAAbAb AAAAAABBbb halt_reject
AABABAABAB AAAAAABBBB
halt_accept halt_accept

DFA that will accepts the string having odd number of 1's and odd number of 0's

I want the DFA generation, that will accepts the string having odd number of 1's and odd number of 0's.
First, let's build a DFA that accepts an odd number of 0. We need at least one state or else we can't accept anything. That state can't be accepting since the empty string leads there and the empty string doesn't have an odd number of 0. So, we need at least two states - an initial state that is not accepting, and an accepting state. Do we need more?
To answer that question let's start filling in the transitions and see where we get. There must be a transition originating in the accepting state. Where does it go? If it goes to itself, then we don't accept the string 0, which has an odd number (one) of 0. So we need to go to some accepting state on 0 in the initial state. We just so happen to have an accepting state already; let's go there.
Next, we must have a transition from the accepting state. If we return to the accepting state, we would accept the string 00, so we can't do that. We must transition to some non-accepting state. We have a non-accepting state already - our initial state - so that choice might work. Alternatively, if not, we must introduce a new state. Let us first consider whether returning to the initial state works, since in that case we are done.
We have already reasoned that the strings 0 and 00 are handled correctly. From then on, 000 will be handled correctly since we return to the accepting state from the initial state on the subsequent 0; indeed, we return to the initial state on 0^2k and to the accepting state on 0^(2k+1), for k >= 0. Therefore, this DFA is correct for the language of strings of odd numbers of 0. The diagram looks like:
/---0----\
| |
V |
----->(q0)--0-->(q1)
By changing the labels we can get an automaton for the language of strings of odd numbers of 1:
/---1----\
| |
V |
----->(q2)--1-->(q3)
To get an automaton accepting strings containing odd numbers of both 0 and 1, imagine running both automata simultaneously: whenever we see a 0, we pass it to the first one, and whenever we see a 1, we pass it to the second one. Then, we accept if both automata ended up in accepting states. We can represent the combined state of the two automata by considering all four pairs of states from the first and second automata as states of a new, combined automaton, the transitions graph of which looks like this:
/----0----\
| |
V |
----->(q0,q2)--0-->(q1,q2)
^ | ^ |
| 1 | 1
1 | 1 |
| V | V
(q0,q3)--0-->(q1,q3)
^ |
| |
\----0----/
These are the intuitions behind the Myhill-Nerode theorem on regularity of languages and the Cartesian product machine construction for the intersection of regular languages.
MEANS you want a DFA for odd-odd language
here is the DFA that you required
look here when you apply 0 it didn't accept ..then you apply 1 it will reach to final state . As DFA accepting odd number of '0's and odd number of '1's.it will accept strings unless their length is odd .you can run strings on DFA to check it. Hope it would help you
Let q0,q1,q2,q3,q4,q5 are the states of DFA . Also given that to construct a DFA that accepts odd no of 0's and odd no of 1's so that q0 on 0 gives q0 and q0 on q 1 q1 on 0 gives q2 and q1 on 1 q 1 q2 on 0 gives q0 and q2 on 1 q3. q3 on 0 gives q2 and q3 on 1 q4. And q4 on 0 and 1 remains in the same state by checking substring 1011
q0 on 1 goes to q1
q1 on 0 goes to q2
q2 on 1 goes to q3
q3 on 1 goes to q4 the final state . It is the required DFA .https://i.stack.imgur.com/jy109.jpg

Why are the only the states 0 and 2 present in line 8?

LR Parsing:
LR Parsing Table:
In line 7, we reduce by T->T*F.
And State 7 on T does not have any transition.
In line 8, why do we have only the states 0 and 2?
At step 7, we reduce T→T*F, which means that:
We pop the right-hand side off of the stack, leaving only the state 0 corresponding to symbol $.
We consult the goto transitions of state 0 (the new top of the stack) for the left-hand symbol T. That says we should goto state 2.
We push the new state 2 onto the stack along with the associated symbol T.
At the end, the stack is 0 2 with symbols $ T, as shown at step 8.
This is well-described in the text and pseudocode algorithms of the excellent book from which those charts were copied.

Converting NFA to DFA

I'm having trouble understanding how to convert.
If 2 gets an input of 'a' would it become (1,4) or (1,2,4) because of the empty string?
Thanks!
If state Q2 gets an input of 'a' next states may be either Q1,Q2, 0r Q4.
In your NFA your get final state Q4
Its equivalent DFA is as below:
a-
||
▼|
--►(Q0)---a---►((Q1))---b----►((Qf))
▲-----a--------|
Where Q1 and Q2 are final state.
And its Regular Expression is: a (a + ba)* (b + ε )
Where ε is null symbol (epsilon)
We begin to convert NFA to DFA with identifying empty-input-closure sets (starting from here i will denote empty-input-closure by L-closure).
L(1)=(1,2) We can visit 2 from 1 on empty input.
L(2)=(2) There is no empty input edge out from 2.
L(3)=(3) There is no empty input edge out from 3.
L(4)=(1,2,4) We can visit 1 from 4 and 2 from 1.
If 2 gets an input of 'a' would it become (1,4) or (1,2,4) because of
the empty string?
If 2 gets an input of 'a', it would become L(1)UL(4)=(1,2,4).
Since our start node is 1 in NFA, in DFA it will be L(1) which is (1,2).
T((1,2),a)=L(1)UL(3)UL(4)=(1,2,3,4)
T((1,2),b)=F
T((1,2,3,4),a)=L(1)UL(3)UL(4)=(1,2,3,4)
T((1,2,3,4),b)=L(4)=(1,2,4)
T((1,2,4),a)=L(1)UL(3)UL(4)=(1,2,3,4)
T((1,2,4),b)=F
Since 4 is an accept node in NFA, in DFA nodes including 4 will be accept nodes, which are (1,2,3,4) and (1,2,4).

What do Push and Pop mean for Stacks?

long story short my lecturer is crap, and was showing us infix to prefix stacks via an overhead projector and his bigass shadow was blocking everything so i missed the important stuff
he was referring to push and pop, push = 0 pop = x
he gave an example but i cant see how he gets his answer at all,
2*3/(2-1)+5*(4-1)
step 1 Reverse : )1-4(*5+)1-2(/3*2 ok i can see that
he then went on writing x's and o's operations and i got totally lost
answer 14-5*12-32*/+ then reversed again to get +/*23-21*5-41
if some one could explain to me the push pop so i could understand i would be very greatful, i have looked online but alot stuff im finding seems to be a step above this, so i really need to get an understanding here first
Hopefully this will help you visualize a Stack, and how it works.
Empty Stack:
| |
| |
| |
-------
After Pushing A, you get:
| |
| |
| A |
-------
After Pushing B, you get:
| |
| B |
| A |
-------
After Popping, you get:
| |
| |
| A |
-------
After Pushing C, you get:
| |
| C |
| A |
-------
After Popping, you get:
| |
| |
| A |
-------
After Popping, you get:
| |
| |
| |
-------
The rifle clip analogy posted by Oren A is pretty good, but I'll try another one and try to anticipate what the instructor was trying to get across.
A stack, as it's name suggests is an arrangement of "things" that has:
A top
A bottom
An ordering in between the top and bottom (e.g. second from the top, 3rd from the bottom).
(think of it as a literal stack of books on your desk and you can only take something from the top)
Pushing something on the stack means "placing it on top".
Popping something from the stack means "taking the top 'thing'" off the stack.
A simple usage is for reversing the order of words. Say I want to reverse the word: "popcorn". I push each letter from left to right (all 7 letters), and then pop 7 letters and they'll end up in reverse order. It looks like this was what he was doing with those expressions.
push(p)
push(o)
push(p)
push(c)
push(o)
push(r)
push(n)
after pushing the entire word, the stack looks like:
| n | <- top
| r |
| o |
| c |
| p |
| o |
| p | <- bottom (first "thing" pushed on an empty stack)
======
when I pop() seven times, I get the letters in this order:
n,r,o,c,p,o,p
conversion of infix/postfix/prefix is a pathological example in computer science when teaching stacks:
Infix to Postfix conversion.
Post fix conversion to an infix expression is pretty straight forward:
(scan expression from left to right)
For every number (operand) push it on the stack.
Every time you encounter an operator (+,-,/,*) pop twice from the stack and place the operator between them. Push that on the stack:
So if we have 53+2* we can convert that to infix in the following steps:
Push 5.
Push 3.
Encountered +: pop 3, pop 5, push 5+3 on stack (be consistent with ordering of 5 and 3)
Push 2.
Encountered *: pop 2, pop (5+3), push (2 * (5+3)).
*When you reach the end of the expression, if it was formed correctly you stack should only contain one item.
By introducing 'x' and 'o' he may have been using them as temporary holders for the left and right operands of an infix expression: x + o, x - o, etc. (or order of x,o reversed).
There's a nice write up on wikipedia as well. I've left my answer as a wiki incase I've botched up any ordering of expressions.
The algorithm to go from infix to prefix expressions is:
-reverse input
TOS = top of stack
If next symbol is:
- an operand -> output it
- an operator ->
while TOS is an operator of higher priority -> pop and output TOS
push symbol
- a closing parenthesis -> push it
- an opening parenthesis -> pop and output TOS until TOS is matching
parenthesis, then pop and discard TOS.
-reverse output
So your example goes something like (x PUSH, o POP):
2*3/(2-1)+5*(4-1)
)1-4(*5+)1-2(/3*2
Next
Symbol Stack Output
) x )
1 ) 1
- x )- 1
4 )- 14
( o ) 14-
o 14-
* x * 14-
5 * 14-5
+ o 14-5*
x + 14-5*
) x +) 14-5*
1 +) 14-5*1
- x +)- 14-5*1
2 +)- 14-5*12
( o +) 14-5*12-
o + 14-5*12-
/ x +/ 14-5*12-
3 +/ 14-5*12-3
* x +/* 14-5*12-3
2 +/* 14-5*12-32
o +/ 14-5*12-32*
o + 14-5*12-32*/
o 14-5*12-32*/+
+/*23-21*5-41
A Stack is a LIFO (Last In First Out) data structure. The push and pop operations are simple. Push puts something on the stack, pop takes something off. You put onto the top, and take off the top, to preserve the LIFO order.
edit -- corrected from FIFO, to LIFO. Facepalm!
to illustrate, you start with a blank stack
|
then you push 'x'
| 'x'
then you push 'y'
| 'x' 'y'
then you pop
| 'x'
A stack in principle is quite simple: imagine a rifle's clip - You can only access the topmost bullet - taking it out is called "pop", inserting a new one is called "push".
A very useful example for that is for applications that allow you to "undo".
Imagine you save each state of the application in a stack. e.g. the state of the application after every type the user makes.
Now when the user presses "undo" you just "pop" the previous state from the stack. For every action the user does - you "push" the new state to the stack (that's of course simplified).
About what your lecturer specifically was doing - in order to explain it some more information would be helpful..
Ok. As the other answerers explained, a stack is a last-in, first-out data structure. You add an element to the top of the stack with a Push operation. You take an element off the top with a Pop operation. The elements are removed in reverse order to the order they were put inserted (hence Last In, First Out). For example, if you push the elments 1,2,3 in that order, the number 3 will be at the top of the stack. A Pop operation will remove it (it was the last in) and leave 2 at the top of the stack.
Regarding the rest of the lecture, the lecturer tried to describe a stack-based machine that evaluates arithmetic expressions. The machine operates by continuously popping 3 elements from the top of the stack. The first two elements are operands and the third is an operator (+, -, *, /). It then applies this operator on the operands, and pushes the result onto the stack. The process continues until there is only one element on the stack, which is the value of the expression.
So, suppose we begin by pushing the values "+/*23-21*5-41" in left-to-right order onto the stack. We then pop 3 elements from the top. The last in is first out, which means the first 3 element are "1", "4", and "-" in that order. We push the number 3 (the result of 4-1) onto the stack, then pop the three topmost elements: 3, 5, *. Push the result, 15, onto the stack, and so on.
push = add to the stack
pop = remove from the stack
Simply:
pop: returns the item at the top then remove it from the stack
push: add an item onto the top of the stack.
after all these good examples adam shankman still can't make sense of it. I think you should open up some code and try it. The second you try a myStack.Push(1) and myStack.Pop(1) you really should get the picture. But by the looks of it, even that will be a challenge for you!

Resources