Converting NFA to DFA - automata

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

Related

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

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)

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

Deterministic Finite Automata divisibility problem

Design a DFA that accepts the string given by L = { w has number of 'a' divisible by 3 and number of 'b' divisible by 2 over the alphabet {a,b} }
Realize that we should have 3 * 2 = 6 states in the DFA. Why? Because one has 3 choices for the number of a's (0 or 1 or 2) [think in terms of remainders] and 2 choices for number of b's (0 or 1 similarly).
Let us name the states axby which means I have found x number of a's and y number of b's till now. For example, if we are in a2b0 and we encounter an a, then we go to a0b0 (hope you see why?). Similarly a1b1 ---b---> a1b0 and a1b1 ---a---> a2b1.
Needless to say a0b0 is the accepting state.
Now, all you have to do is draw the states and keep joining them. I have drawn them on a paper here.

Calculating ISIN checksum

HI I know there have been may question about this here but I wasn't able to find a detailed enough answer, Wikipedia has two examples of ISIN and how is their checksum calculated.
The part of calculation that I'm struggling with is
Multiply the group containing the rightmost character
The way I understand this statement is:
Iterate through each character from right to left
once you stumble upon a character rather than digit record its position
if the position is an even number double all numeric values in even position
if the position is an odd number double all numeric values in odd position
My understanding has to be wrong because there are at least two problems:
Every ISIN starts with two character country code so position of rightmost character is always the first character
If you omit the first two characters then there is no explanation as to what to do with ISINs that are made up of all numbers (except for first two characters)
Note
isin.org contains even less information on verifying ISINs, they even use the same example as Wikipedia.
I agree with you; the definition on Wikipedia is not the clearest I have seen.
There's a piece of text just before the two examples that explains when one or the other algorithm should be used:
Since the NSIN element can be any alpha numeric sequence (9 characters), an odd number of letters will result in an even number of digits and an even number of letters will result in an odd number of digits. For an odd number of digits, the approach in the first example is used. For an even number of digits, the approach in the second example is used
The NSIN is identical to the ISIN, excluding the first two letters and the last digit; so if the ISIN is US0378331005 the NSIN is 037833100.
So, if you want to verify the checksum digit of US0378331005, you'll have to use the "first algorithm" because there are 9 digits in the NSIN. Conversely, if you want to check AU0000XVGZA3 you're going to use the "second algorithm" because the NSIN contains 4 digits.
As to the "first" and "second" algorithms, they're identical, with the only exception that in the former you'll multiply by 2 the group of odd digits, whereas in the latter you'll multiply by 2 the group of even digits.
Now, the good news is, you can get away without this overcomplicated algorithm.
You can, instead:
Take the ISIN except the last digit (which you'll want to verify)
Convert all letters to numbers, so to obtain a list of digits
Reverse the list of digits
All the digits in an odd position are doubled and their digits summed again if the result is >= 10
All the digits in an even position are taken as they are
Sum all the digits, take the modulo, subtract the result from 0 and take the absolute value
The only tricky step is #4. Let's clarify it with a mini-example.
Suppose the digits in an odd position are 4, 0, 7.
You'll double them and get: 8, 0, 14.
8 is not >= 10, so we take it as it is. Ditto for 0. 14 is >= 10, so we sum its digits again: 1+4=5.
The result of step #4 in this mini-example is, therefore: 8, 0, 5.
A minimal, working implementation in Python could look like this:
import string
isin = 'US4581401001'
def digit_sum(n):
return (n // 10) + (n % 10)
alphabet = {letter: value for (value, letter) in
enumerate(''.join(str(n) for n in range(10)) + string.ascii_uppercase)}
isin_to_digits = ''.join(str(d) for d in (alphabet[v] for v in isin[:-1]))
isin_sum = 0
for (i, c) in enumerate(reversed(isin_to_digits), 1):
if i % 2 == 1:
isin_sum += digit_sum(2*int(c))
else:
isin_sum += int(c)
checksum_digit = abs(- isin_sum % 10)
assert int(isin[-1]) == checksum_digit
Or, more crammed, just for functional fun:
checksum_digit = abs( - sum(digit_sum(2*int(c)) if i % 2 == 1 else int(c)
for (i, c) in enumerate(
reversed(''.join(str(d) for d in (alphabet[v] for v in isin[:-1]))), 1)) % 10)

How to count locals in ANS-Forth?

While developing BigZ, mostly used for number theoretical experiments, I've discovered the need of orthogonality in the word-set that create, filter or transform sets. I want a few words that logically combinated cover a wide range of commands, without the need to memorize a large number of words and ways to combinate them.
1 100 condition isprime create-set
put the set of all prime numbers between 1 and 100 on a set stack, while
function 1+ transform-set
transform this set to the set of all numbers p+1, where p is a prime less than 100.
Further,
condition sqr filter-set
leaves the set of all perfect squares on the form p+1 on the stack.
This works rather nice for sets of natural numbers, but to be able to create, filter and transform sets of n-tuples I need to be able to count locals in unnamed words. I have redesigned words to shortly denote compound conditions and functions:
: ~ :noname ;
: :| postpone locals| ; immediate
1 100 ~ :| p | p is prime p 2 + isprime p 2 - isprime or and ;
1 100 ~ :| a b | a dup * b dup * + isprime ;
Executing this two examples gives the parameter stack ( 1 100 xt ) but to be able to handle this right, in the first case a set of numbers and in the second case a set of pairs should be produced, I'll have to complement the word :| to get ( 1 100 xt n ) where n is the numbet of locals used. I think one could use >IN and PARSE to do this, but it was a long time ago I did such things, so I doubt I can do it properly nowadays.
I didn't understand (LOCALS) but with patience and luck I managed to do it with my original idea:
: bl# \ ad n -- m
over + swap 0 -rot
do i c# bl = +
loop negate ;
\ count the number of blanks in the string ad n
variable loc#
: locals# \ --
>in # >r
[char] | parse bl# loc# !
r> >in ! ; immediate
\ count the number of locals while loading
: -| \ --
postpone locals#
postpone locals| ; immediate
\ replace LOCALS|
Now
: test -| a b | a b + ;
works as LOCALS| but leave the number of locals in the global variable loc#.
Maybe you should drop LOCALS| and parse the local variables yourself. For each one, call (LOCAL) with its name, and end with passing an empty string.
See http://lars.nocrew.org/dpans/dpans13.htm#13.6.1.0086 for details.

Resources