DFA creation and minimization - automata

Task: Draw an DFA that accepts words from the alphabet {0,1} where the last character is not repeated anywhere else in the word.
(example: words 0, 1, 00001, 111110, ε are in the language of this NFA, while words 010, 111, 0010, 101 are not).
I think I got the DFA right but I can't minimize it because I have a trap state which I can't get rid of whatever I do. Any advices or tips?

Your DFA is correct (except the initial state q0 should be accepting since the empty string is in the language). It is also minimal; we can show that the sets of strings that lead to each state are all distinguishable w.r.t. the language according to the Myhill-Nerode indistinguishability relation.
q0: any string in L can be appended to the strings that lead here (the empty string) to get another string in L
q1: appending the string 0 in L to 0 gives 00 not in L, so q1 is distinct from q0.
q2: appending the string 1 in L to 1 gives 11 not in L, so q2 is distinct from q0 and from q1 (since appending 1 to 0 gives 01 which is in L also)
strings that lead to q3 are distinct from q1 in that you cannot append the empty string (i.e., q1 is accepting and q3 isn't), but otherwise identical, and therefore q3 is also distinct from q0-q2
strings that lead to q4 are distinct from q2 in that you cannot append the empty string (i.e., q2 is accepting and q4 isn't), but otherwise identical, and therefore q4 is also distinct from q0-q2
appending anything but the empty string to strings that lead to q5 leads to a string not in the language, so it is distinct from q0-q4
appending anything to a strings that lead to q6 leads to a string not in the language, so it is distinct from q0-q5 (these strings differ from strings that lead to q5 only in that you cannot even append the empty string to them to get a string in L, i.e., q6 is not accepting and q5 is).
Therefore, your DFA is minimal. You can show this by attempting to run a minimization algorithm and noting that no states get removed.
Note: it depends on your definitions but it is a normal (and I would say preferred) definition of DFAs that they define all transitions, which means that dead (or as you call them, trap) states are required to be shown. Minimization algorithms probably don't remove them, but you can remove them if you like (though I'd call the resulting automaton nondeterministic since deterministic behavior is not specified for some transitions).

Related

How to construct a PDA for n(a) is less than or equal to 2n(b)

So here is where I am stuck, I have to construct a PDA that would accept words from {a,b}* with the condition n(a) is less than or equal to 2n(b)
Put yourself into the frame of mind of a pushdown automaton. All you know how to do is read input and the stack, and then push/pop and change state based on what you see. If you start reading a string and need to tell whether it's in the language, what can you do?
It seems to me the best we can do starting out is to remember how many as we are reading. Until we see bs, there's no reason to do anything else. That is, simply read as and push them on the stack. The following rules should suffice:
Q i s Q' s'
-- -- -- -- --
q0 a Z q0 aZ
q0 a a q0 aa
Now, what happens when we see a b? If we want at least twice as many bs as as, we can't just cross of as for each b, since that would give at least as many bs as as but not at least twice as many.
What if we cross off two as for each b? Well, that gives us at least half as many bs as as, which is the wrong direction. This suggests crossing off one a for every two bs, which turns out to be correct. The rules:
Q i s Q' s'
-- -- -- -- --
q0 b a q1 a
q1 b a q2 -
q2 b a q1 a
Note we created a new state q2 which has one rule in common with q0 but not the ones for reading as. Indeed, once we start reading bs, we don't want to allow any more as to be read. Leaving out the rules will crash the automaton and reject the string.
If we have exactly twice as many bs as as, we will end up in state q2 with no more input and an empty stack. If we have additional bs, we need a rule to allow them. It suffices to add a self loop on state q2:
Q i s Q' s'
-- -- -- -- --
q2 b Z q2 Z
Taken together, these states and transitions should be pretty close to a working PDA for your language. Other options would have been to write a CFG first and then apply an algorithm to convert the CFG to a PDA, for example, a top-down or bottom-up parser.

DFA minimization in F#

I'm completely new to functional programming and have elected to use F# for a project which entails the parsing and minimization of a DFA.
I currently have my parser completed and am able to format each element of the DFA's tuple (states, alphabet, transition function, start state, final states) in whatever way I'd like and I have reached the point where I need to implement the minimization algorithm. The algorithm being used is:
For some DFA (Q, Σ, δ, S, F) where
Q: The set of states
Σ: The alphabet
δ: The transition function
S: The start state
F: The set of final states
Step 1. For each pair of states
(p, q) ∈ Q x Q
If p ∈ F and q ∉ F (or vice versa), then set distinct(p, q) = 1.
Step 2. Loop until there is no change in the table contents:
For each pair of states (p, q) ∈ Q x Q:
For each alphabet symbol a ∈ alphabet:
If distinct(p, q) = 0 and distinct(δ(p, a), δ(q, a)) = 1, then set
distinct(p, q) = 1.
I have the DFA tuple elements formatted like so:
States:
["0";"1";"2";"3"]
Alphabet:
["a";"b"]
Transition Function (ie: ["0";"a";"1"] is read as "0 on an 'a' goes to 1"]
[["0";"a";"1"];["1";"a";"1"];["1";"b";"2"];["2";"a";"0"];...;["5";"a";"4"]
Start State:
["0"]
Final States:
["1";"5"]
I also have have a distinct table formatted. It's basically the Cartesian product of States x States (QxQ from the above minimization algorithm) with any repeated products and duplicate elements ignored:
[["0";"1"];["0";"2"];["0";"3"];["0";"4"];["0";"5"];["1";"2"];
["1";"3"];["1";"4"];["1";"5"];["2";"3"];["2";"4"];["2";"5"];
["3";"4"];["3";"5"];["4";"5"]]
My initial strategy was to make a new list with only pairs which are either both non-final, or both final. (The only two conditions failing the 'Step 1' condition).
My problem is this: I am having a difficult time coming up with a way to compare the resulting list to the transition function of each pair of states. For example, take the pair of states ["1";"5"]. As the algorithm states, we must compare what happens to '1' for each alphabet character to what happens to '5' for each alphabet character. In this case, the transition function states:
For 1:
["1";"a";"1"];["1";"b";"2"]
-'1' on an 'a' goes to '1'
-'1' on a 'b' goes to '2'
And for 5:
["5";"a";"4"]
-'5' on an 'a' goes to '4'
Because both states, '5' and '1', behave differently when passed the same alphabet character, they are distinct. But, as I've stated, I'm not at all clear as to how to implement this comparison.
Any help would be greatly appreciated. Take care!
If your transition function is stored as triples as you show above:
Sort them into separate lists of state pairs according to letter
Make a random access array (or associative map) out of each such list
Since you are inverting the transition function, the index/key of each mapping will be the destination state, and the contents/value will be a list of zero or more origin states that the letter maps to it.
Make sure that all elements of your "distinct" list have their first element less than their second (by swapping the two if necessary), and make sure the list itself is sorted. Then, for each array or map:
Apply the mapping to your "distinct" list as follows:
look up both elements of each "distinct" pair
perform the cartesian product of the two "origin state" lists from a given pair
combine the resulting pairs from all cartesian products into a single list
For this new list:
Filter out any resulting tuples with identical elements
Swap any tuples whose first element is greater than the second
Sort the result
Eliminate neighboring duplicates
Do a merge pass with the "distinct" list:

Is a Pushdown Automaton with an Epsilon Transition a NDPA?

Let's suppose we have this PA:
-> q0 (e, e -> $) --> q1
Where:
q0 is a final and initial state;
e is epsilon (empty); and q1 is another state.
If the automaton were to read the e word, it could either make the transition to q1 or stop in q0.
So, would this PA be Non-Deterministic?
My teacher says it wouldn't because, in reality, there's only one path for the automaton to follow: since the word is empty and all symbols had already been consumed in q0, it would make no transition whatsoever; however, we're not sure if he's right (by the way, he says that in order for a PA to recognize a word it needs not only to be in a final state but also all the word's symbols must have been consumed).
For a PA to be deterministic it must follow at least the following rule:
If there is an epsilon transition from a state q, there must not be any alphabet transition from that state.
So in your case, if there isn't any other rule, the PA is deterministic.

Automata and Formal Languages

Showing that the reverse of a word for a regular language L is also regular
I am confused as to how I am to approach this question, i've been stuck for hours: For a word x, we use x^r to denote its reverse. For a language L, we use L^r to denote {x^r where x is in the set of L}. Show that if L is regular then so is L^r
If L is regular, then there exists some regular grammar which generates it. It can be always represented as either a left-regular grammar, or a right-regular grammar. Let's assume that it's left-regular grammar G_l(the proof for right-regular grammar is analogous).
This grammar has productions of two types; the terminating-type:
A -> a, where A is non-terminal and a is either a terminal or empty string (epsilon)
or the chaining type:
B -> Ca, where B, C are non-terminals and a is a terminal
When we apply reverse to a regular language, we basically also apply it to the tails of productions (since heads are just single non-terminals). It's going to be proved later on. So we get a new grammar G_r, with productions:
A -> a, where A is non-terminal and a is either a terminal or empty string (epsilon)
B -> aC, where B, C are non-terminals and a is a terminal
But hey, it's a right-regular grammar! So the language it accepts is also regular.
There is one thing to do - to show that reversing tails actually does the thing it's supposed to. We're going to prove it very simply:
If L contains \epsilon, then there is production 'S -> \epsilon' in G_l. Since we don't touch productions like that, it's also present in G_r.
If L contains a, a word composed of a single terminal, then it's similar to the above
If L contains aZ, where a is a terminal and Z is a word from the language constructed from chopping off the first terminals out of words in L, then L^r contains (because of changes to the chaining productions) (Z^r)a. Z is also a regular language, since it can be constructed by dropping the first "level" of left-productions from G_l, which leaves us with a regular grammar.
I hope it helped. There's also an arguably easier way of doing that by reversing edges of the relevant finite automata and changing accepting and entry states a bit.

Testing intersection of two regular languages

I want to test whether two languages have a string in common. Both of these languages are from a subset of regular languages described below and I only need to know whether there exists a string in both languages, not produce an example string.
The language is specified by a glob-like string like
/foo/**/bar/*.baz
where ** matches 0 or more characters, and * matches zero or more characters that are not /, and all other characters are literal.
Any ideas?
thanks,
mike
EDIT:
I implemented something that seems to perform well, but have yet to try a correctness proof. You can see the source and unit tests
Build FAs A and B for both languages, and construct the "intersection FA" AnB. If AnB has at least one accepting state accessible from the start state, then there is a word that is in both languages.
Constructing AnB could be tricky, but I'm sure there are FA textbooks that cover it. The approach I would take is:
The states of AnB is the cartesian product of the states of A and B respectively. A state in AnB is written (a, b) where a is a state in A and b is a state in B.
A transition (a, b) ->r (c, d) (meaning, there is a transition from (a, b) to (c, d) on symbol r) exists iff a ->r c is a transition in A, and b ->r d is a transition in B.
(a, b) is a start state in AnB iff a and b are start states in A and B respectively.
(a, b) is an accepting state in AnB iff each is an accepting state in its respective FA.
This is all off the top of my head, and hence completely unproven!
I just did a quick search and this problem is decidable (aka can be done), but I don't know of any good algorithms to do it. One is solution is:
Convert both regular expressions to NFAs A and B
Create a NFA, C, that represents the intersection of A and B.
Now try every string from 0 to the number of states in C and see if C accepts it (since if the string is longer it must repeat states at one point).
I know this might be a little hard to follow but this is only way I know how.

Resources