I've read a text file of symbols, states and transitions and put it all in a table. It looks like this:
symbols a, b
states q1, q2, q3, q4
start state q4
final state q2, q3
transition state:
q4, epsilon, q1
q1, a, q2
q3, a, q3
q3, b, q1
I've read an algorithm on how to convert NFA to DFA but I don't really understand the algorithm. How would I create transition methods and what should I have state class?
i have a nifty link right here:
JFLAP
JFLAP is an Java JAR which has some nice visualization included. You can test and transform NFAs/DFAs, do Pummping Lemma Stuff and check various grammars and such...
You might give it a try!
Do you have a programming problem, or an problem with Automata?
Because programming this it seems very simple. Yes, you'd have a state class, with 4 possible values q1-a4. The state class has a single constructor initializing it to q4. It has an Accept(symbol) function that modifies the state object, and possibly an IsEndState() function that returns true for states q2 and q3.
The NFA/DFA part comes into play in the implementation of the Accept() method. Now it coukd be that you are asked for a programmtic solution to convert an NFA-based implementation of Accept() to a DFA-based implementation of Accept().
These two links should help you:
http://www.win.tue.nl/prose/pres/ploeger-19-10-06.pdf
http://www.cs.sun.ac.za/rw711/documentation/hopcroft2.pdf
If you want to do it automatically without having to download something like JFLAP, give the Online NFA to DFA conversion tool a spin.
ProfBrown's "Convert NFA to DFA" video on YouTube examines how to do it in some rigor, though IMO it's impractical and unnecessarily tedious to have to write the tables up by hand. As you get used to doing the process, you'll be able to eyeball it for small graphs. In the case of large graphs, you'd use a tool to automate it, anyway.
Related
I was working on an automat.
Then, I have a question about the loop. Normally, I was working with three alpabets but I'm completed with this problem. The problem is:
{w | w starts with 0 and has odd length, or starts with 1 and has even length}
I want to covert this algorithm to the Deterministic Finite Automaton (DFA) How to solve this problem?
Have you found the solution in NFA? As you’re asking about how to convert the solution to DFA.
Anyway, let me propose a solution.
NFA:
States= {A,B,C}
Alphabet= {0,1,2,3}
Start state= A
Final states= {A}
Transitions=
(A,0,A)
(A,1,A)
(A,2,C)
(A,3,A)
(B,1,A)
(C,3,A)
If you look at that automata, it’s been actually in a form of DFA, right?
So you don’t need to convert it. You might make it complete by adding transitions to the death state.
States= {A,B,C,D}
Alphabet= {0,1,2,3}
Start state= A
Final states= {A}
Transitions=
(A,0,A)
(A,1,A)
(A,2,C)
(A,3,A)
(B,0,D)
(B,1,A)
(B,2,D)
(B,3,D)
(C,0,D)
(C,1,D)
(C,2,D)
(C,3,A)
(D,0,D)
(D,1,D)
(D,2,D)
(D,3,D)
Task: Build NFA from a given regular expression.
I decided to push some of my old programs to GitHub. Specifically problems regarding Theory of formal languages. After testing code I had this result and I can't really tell if this a wrong or correct output. It is kindaaa looks right but not something Thompson's algo would output. Also those little loops look suspicious. They basically do nothing though.
Definitely wrong.
The epsilon-self-loops look to me like a bug in the handling of the union operator. There should be an epsilon transition from each end state in the union to a new end state, so my guess is that you have mixed up the epsilon links. I'm not sure how you end up with the correct epsilon transition on a in one case and b in the other, so perhaps the bug is more complicated.
You're right that in this case, there is no harm in the epsilon self-loop. But it is quite possible that the absence of an epsilon link from the end of the union leg to the union's end state will cause a problem with (a*|b) or (a|b*). One of those might actually turn out to recognize (a|b)+.
Also, your Kleene star implementation does not allow zero repetitions. What you have is (a|b)+, not (a|b)*, because there is no epsilon transition from the start state to the state of the star subconstruction.
My C# implementation of Brzozowski's algorithm for DFA minimization gives the DFA below. (0) is initial state, (2) and (3) are final states.
Consider the DFA :
What will be δ(A,01) equal to ?
options:
A) {D}
B) {C,D}
C) {B,C,D}
D) {A,B,C,D}
The correct answer is option B) but I don't get how. Please some one explain me the steps to solve it and also in general how do we solve sit for any DFA and any transition?
Thanks.
B) Option is not Correct answer! for this transition graph.
In Transition Graph(TG) symbol ε means NULL-move (ε-move). There is two NULL-moves in TG.
One: (A) --- `ε` ---->(B)
Second: (A) --- `ε` ---->(C)
A ε-move means without consume any symbol you can change state. In your diagram from A to B, or A to C.
What will be δ(A,01) equal to ?
Question asked "what is path from state A if input is 01". (as I understand because there is only one final state)
01 can be processed in either of two ways.
(A) -- ε --->(B) -- 0 --> (B) -- 1 --> (D)
(A) -- ε --->(C) -- 0 --> (B) -- 1 --> (D)
Also, there is no other way to process string 01 even if you don't wants to reach final state.
[ANSWER]
So there is misprinting in question (or either you did).
You can learn how to remove NULL-move from transition graph. Also HOW TO WRITE REGULAR EXPRESSION FOR A DFA
If you remove null-moves from TG you will get three ways to accept 01.
EQUIVALENT TRANSITION GRAPH WITHOUT NULL-MOVE
Note there is three start-states in Graph.
Three ways:
{ABD}
{CBD}
{BBD}
In all option state-(B) has to be come.
Also, you have written Consider the DFA : is wrong. The TG is not deterministic because there is there is non-deterministic move δ(A,ε) and next states are either B or C.
The obvious choice is to exhaust all possible inputs. I think I did. But I am not very sure whether it's valid and I didn't break any rules of non-deterministic finite automata.
My NFA is given by: (ab u aab u aba)* and below is my diagram.
Have I missed something?
You aren't missing anything, but the NFA could be simplified considerably by collapsing paths and eliminating λ-rules. In order to determine whether your NFA decides the language denoted by the regular expression, you can argue informally by chasing states in the transition graph. In order to talk about the NFA, I'll use the following definition of the δ function with final states {q0,q3,q4} and initial state q0
δ(q0,a) = {q1,q2}
δ(q1,a) = {q2}
δ(q2,b) = {q3}
δ(q3,a) = {q4}
δ(q3,λ) = {q0}
δ(q4,λ) = {q-}
The goal is to show that the NFA accepts precisely the language (ab U aab U aba)*. To this end, we can consider the strings accepted by beginning with λ at q0 and exhausting all possible transitions through the graph, recording the strings built by concatenating the symbols transitioned on, and noting whether such a string is accepted or not. Paths in the graph indicate concatenation; final states indicate acceptance or disjunction; loops indicate Kleene star.
From q0 and λ we can transition on a to q1 or q2. On q1 and a we can transition to q2. Hence, on q2 we have either a or aa and nothing else. From q2 and a or aa we can transition to q3 on b. Hence, at q3 we have either ab or aab and nothing else. From q3 and ab or aab we can transition to q0 on λ or q4 on a. Hence, on q3 and ab or aab we have either ab or aab or aba and nothing else. Finally, on q4 and aba we can transition to q0 on λ. Since we have ab or aab or aba and transitioned to the start state, meaning our derivation can repeat itself zero or more times, and we have exhausted the possible transitions, we conclude that the NFA decides the Kleene closure of ab or aab or aba.
There are more formal methods of showing that a given NFA decides a language. But the easiest way is to exhaust all possible paths through the NFA, introducing Kleene closures on cycles. An example of a formal method would be to convert the NFA to a regular expression, then derive the equality of the obtained expression and the target expression axiomatically. This is largely unnecessary.
You probably have done precisely what I just wrote, however, making this entire post unnecessary. If not, I hope it shows the kind of informal reasoning you can use to convince yourself that an NFA decides a language.
Since many of the Project Euler problems require you to do a divisibility check for quite a number of times, I've been trying to figure out the fastest way to perform this task in ZX81 BASIC.
So far I've compared (N/D) to INT(N/D) to check, whether N is dividable by D or not.
I have been thinking about doing the test in Z80 machine code, I haven't yet figured out how to use the variables in the BASIC in the machine code.
How can it be achieved?
You can do this very fast in machine code by subtracting repeatedly. Basically you have a procedure like:
set accumulator to N
subtract D
if carry flag is set then it is not divisible
if zero flag is set then it is divisible
otherwise repeat subtraction until one of the above occurs
The 8 bit version would be something like:
DIVISIBLE_TEST:
LD B,10
LD A,100
DIVISIBLE_TEST_LOOP:
SUB B
JR C, $END_DIVISIBLE_TEST
JR Z, $END_DIVISIBLE_TEST
JR $DIVISIBLE_TEST_LOOP
END_DIVISIBLE_TEST:
LD B,A
LD C,0
RET
Now, you can call from basic using USR. What USR returns is whatever's in the BC register pair, so you would probably want to do something like:
REM poke the memory addresses with the operands to load the registers
POKE X+1, D
POKE X+3, N
LET r = USR X
IF r = 0 THEN GOTO isdivisible
IF r <> 0 THEN GOTO isnotdivisible
This is an introduction I wrote to Z80 which should help you figure this out. This will explain the flags if you're not familiar with them.
There's a load more links to good Z80 stuff from the main site although it is Spectrum rather than ZX81 focused.
A 16 bit version would be quite similar but using register pair operations. If you need to go beyond 16 bits it would get a bit more convoluted.
How you load this is up to you - but the traditional method is using DATA statements and POKEs. You may prefer to have an assembler figure out the machine code for you though!
Your existing solution may be good enough. Only replace it with something faster if you find it to be a bottleneck in profiling.
(Said with a straight face, of course.)
And anyway, on the ZX81 you can just switch to FAST mode.
Don't know if RANDOMIZE USR is available in ZX81 but I think it can be used to call routines in assembly. To pass arguments you might need to use POKE to set some fixed memory locations before executing RANDOMIZE USR.
I remember to find a list of routines implemented in the ROM to support the ZX Basic. I'm sure there are a few to perform floating operation.
An alternative to floating point is to use fixed point math. It's a lot faster in these kind of situations where there is no math coprocessor.
You also might find more information in Sinclair User issues. They published some articles related to programming in the ZX Spectrum
You should place the values in some pre-known memory locations, first. Then use the same locations from within Z80 assembler. There is no parameter passing between the two.
This is based on what I (still) remember of ZX Spectrum 48. Good luck, but you might consider upgrading your hw. ;/
The problem with Z80 machine code is that it has no floating point ops (and no integer divide or multiply, for that matter). Implementing your own FP library in Z80 assembler is not trivial. Of course, you can use the built-in BASIC routines, but then you may as well just stick with BASIC.