Finite state machines how to implement minimum and maximum hits - algorithm

I'm trying to implement a fsm and it's going fine. I can enter strings and see if they are valid and all that kind of stuff.
However regular expressions (which are fsms) have this feature where you can specify how many times a certain character may occur for example a{2,4} would accept "aa" and "aaa" but not "aaaaa" and "a"
I can imagine having a counter on edges that count how many times they have been hit and use this to deny any characters after the counter has hit a certain number but you can't implement minimum this way because it would always block the first character (unless minimum is 0).
Does anyone know a way to implement this feature?
it also has to work for really big numbers like a{1,99999999999}

To my understanding, this kind of constraint cannot be implemented dynamically in a finite state machine; parts of the FSM would have to be statically expanded. In your example, for a{2,3} three different separate FSMs would have to be built, one accepting aa, the second one accepting aaa and the third one accepting aaaa; these would then have to be made alternatives in the final FSM via some empty transitions. The reason for this is the fact that the FSM itself does not store the path by which its current state was reached, which means that a paremeterized form of the pattern a{i,j} cannot be checked.

Related

Is this NFA correctly accepting inputs that end with a 00?

In a lecture it was said that this NFA accepts inputs ending with two zeros or inputs=0: https://ibb.co/9Wt0j7J .
The alphabet is {0,1}
But if the input would be 001 we would on some path also end up in the acceptance state (z2) but it would not be possible to go back to another state when reading the last character, the one. That would mean, a wrong input was accepted. So, my question is: Is the NFA really constructed correctly without changing anything? And if yes, why? Can I just assume that we go to an "empty (invisible) state (error state without mentioning it explicitly)" or something like that if there is no other arrow to another state?
Yes, it is a correct NFA for the given definition.
But if the input would be 001 we would on some path also end up in the acceptance state (z2) but it would not be possible to go back to another state when reading the last character, the one. That would mean, a wrong input was accepted.
If you put 001, it will not accept it since NFAs checks all possible paths and eliminates the paths which stuck. So you will go to z2 after the first two 0s but after reading the 1, it will stuck and be eliminated.
Edit:
... an NFA accepts a string w if it is possible to make any
sequence of choices of next state, while reading the characters of w and go from the start state to any accepting state.
from the book Introduction to Automata Theory, Languages, and Computations by John E. Hopcroft, Rajeew Motwani, Jeffret D. Ullman, (2006, p.59).

Could anybody explain me the independence in bayesian nets?

Could anybody explain me conditional independence in the following cases? Could you give me any other appropriate examples for each case?
First and third examples fall under rule, that says if a variable's all parents are known, it should care only about its children and it is conditionally independent of all other variables.
In the first example the random variable JohnCalls(child) is conditionally independent of the random variable Burglary(grandpa), which means that, if we know the state of random variable Alarm(parent), Johncalls will act accordingly regardless whether there was a Burglary or not.
The similar example would be WasPartying -> HomeworkWasntCompleted -> ReceivedBadGrade. Here, regardless whether you were partying or not, if homework wasn't completed (the parent is known), you gonna receive bad grade. So if we have a value of HomeworkWasntCompleted, learning value of WasPartying doesn't give us any new information about ReceivedBadGrade.
In the third example it's the same: if we know that Alarm is on, Marycalls won't give us any new hint about JohnCalls, so JohnCalls is conditionally independent of MaryCalls given the value of Alarm.
The second example is a little bit trickier. Although we know all the parents of Burglary (obviously, cause it doesn't have any parents), we can't say that Burglary is conditionally independent of Earthquake. Cause if we know that Alarm is on, and we received an information about Earthquake, we would guess that the Alarm was triggered by Earthquake and the chances of Burglary is considerably lower. So, in this case Earthquake gives us some information about Burglary. This example doesn't fall under the rule described above, cause the variables questioned upon conditional independence share the same descendant.
The similar example would be WasPartying -> HomeworkWasntCompleted <- DidntUnderstandTopic (pay attention to arrow directions).
Here you can find a nice lecture about conditional independence.

Interview Q: Detecting a fighting game moveset

I got this as an interview question and I'm wondering what the optimal way of designing this system would be. The problem:
Say you have a fighting game where certain button combinations represent a special move. Implement 2 functions register_move([button combo],movename) which takes in a list of button inputs and a movename string and on_keypress(button) which registers the current keypress and prints a movename if a button combo has been activated. The buttons are represented as characters: 'U','D','L','R','A','B'
Example:
register_move(['A','B','U'],"Uppercut")
on_keypress('A')
on_keypress('B')
on_keypress('U') -> print "Uppercut"
you can assume moves are registered before on_keypress so you don't have to retroactively look back at the previous keypresses. You can use any language you like
Build a Deterministic Finite State Automaton. The initial state is "no keys recognised". On each keypress, transition into a new state; if it is a final state you have a move. All undefined transitions transition into the starting state. For your example,
S --(a)--> A
A --(b)--> AB
AB --(u) --> ABU: process "Uppercut", move to S
X --(x)--> S
where X is any state, x is any input not otherwise covered by the rules.
More practically and less theoretically, you will end up with a trie, so using a trie library should be sufficient. Root is "no input", walk it until a leaf, or restart on a mispress.
Considering the limited number of moves, you don't need a super efficient finite state machine to handle this.
You could simply store the strings in register_move, and have on_keypress memorize the last potentially valid sequence.
If the current key sequence is the prefix of at least one move (for instance "AB" being a prefix of "ABU"), you're done (just wait for the next keypress to see if a combo is reached).
If the sequence is no prefix, reset the sequence to the last keypress (for instance "ABD" -> "D"). This clears previous keypresses that correspond to no moves.
If the sequence corresponds to a move, perform the move (well, print it at least) and reset the sequence.
This would require to do a prefix search on every possible move combo, which is very quick if you have only a dozen or so. If for some reason you want to be quicker, you can indeed turn your list of combos into a prefix tree, but it would require a lot more code for little gain.

How does a finite state machine perform division?

I am taking a course on models of computation and currently we are doing finite state machines. One my tasks is to draw out a FSM that performs division of 3; to simplify the model the machine only accepts numbers multiple of 3. I am not sure how this exactly works, especially since I imagine FSM putting out only single binary values. Could you guys give examples (division by 2 or 4) or hints on how to approach this?
This is what you need, I think (sorry about the bad picture). The 'E' represents epsilon/lambda/no-output. The label of the edges denotes 'input/output'. For each symbol read there is also a corresponding output which may be lambda (no output).

Why do people choose random filename for file-uploads instead of a running counter?

For example, you get a random string 6dtgw.png for your filename.
I understand a combination of alpha and numeric can tolerate more combinations. But that doesn't stop a running counter from using alpha numeric.
0001..0008,0009,000a,000A,000b
(Note: I apologize if I made a wrong assumption, as I am assuming image-hosting use a random string as filename. This question will not make sense if my assumption is incorrect.)
I am not sure but i think it has thing to do with decentralized is better, counter is a bottleneck that restricts scalability.
Using a sequence would make it very easy for someone else to scan all uploads, which they probably don't want. Using a sequence also requires synchronization between different processes (possibly on different servers!) to decide who gets to use the next number.

Resources