Do epsilon transitions in an nfa accept any input? - state-machine

If i have this transition in my nfa
epsilon
( q1 ) ----------------> ( q2 )
for the alphabet {a,b}
does that mean that there is a transtion from q1 to q2 when either b or a is read as input when the nfa is in state q1? Or are there no transitions defined from q1 to q2 on inputs a and b

No. They don't consume any input. That is anytime you are in state q1 you can jump to state q2 no matter the input.

Related

How can I solve this classical dynamic programming problem?

There are N jewellery shop(s). Each jewellery shop has three kinds of coins - Gold, Platinum, and Diamond having worth value A, B, and C respectively. You decided to go to each of N jewellery shop and take coins from each of the shop. But to do so following conditions must satisfy -
You can take at most 1 coin from an individual shop.
You can take at most X coins of Gold type.
You can take at most Y coins of Platinum type.
You can take at most Z coins of Diamond type.
You want to collect coins from shops in such a way that worth value of coins collected is maximised.
Input Format :
The first line contains an integer N. Where N is the number of jewellery shops.
The second line contains three integers X, Y, Z. Where X, Y, Z denotes the maximum number of coins you can collect of type Gold, Platinum, and diamond respectively.
Then N lines contain three space-separated integers A, B, C. Where A, B, C is the worth value of the Gold, Platinum, and diamond coin respectively.
Output Format :
Print a single integer representing the maximum worth value you can get.
Constraints :
1
<=
N
<=
200
1
<=
X
,
Y
,
Z
<=
N
1
<=
A
,
B
,
C
<
10
9
Example : -
4
2 1 1
5 4 5
4 3 2
10 9 7
8 2 9
Answer:-
27(9+9+5+4)
I tried the obvious greedy approach but it failed :-)

pandas left join where right is null on multiple columns

I have two pandas df x and y, both with the same 3 columns A B C (not nullable). I need to create a new df z, obtained by "subtracting from x the rows which are entirely identical to the rows of y", i.e. a
x left join y on x.A=y.A and x.B=y.B and x.C=y.C
where y.A is null
How would I do that? Got stuck with indexes, concat, merge, join, ...
Example:
dataframe x
A B C
q1 q2 q3
q4 q2 q3
q7 q2 q9
dataframe y
A B C
q4 q2 q3
dataframe z
A B C
q1 q2 q3
q7 q2 q9
I think need merge with indicator and filter only rows from left DataFrame:
df = x.merge(y, indicator='i', how='outer').query('i == "left_only"').drop('i', axis=1)
print (df)
A B C
0 q1 q2 q3
2 q7 q2 q93
In earlier versions of pandas, it may be necessary to replace .drop('i', axis=1) with .drop('i',1). The former is necessary to avoid warnings in later versions of Pandas.
Here are a few other ways to remove certain lines from a dataframe using another dataframe:
pd.concat([dfx,dfy]).drop_duplicates(keep=False)
or
dfx.loc[[i not in dfy.to_records(index = False) for i in dfx.to_records(index = False)]]
or
dfx.loc[~dfx.apply(tuple,axis=1).isin(dfy.to_records(index = False))]
or
pd.MultiIndex.from_frame(dfx).symmetric_difference(pd.MultiIndex.from_frame(dfy)).to_frame().reset_index(drop=True)
pd.DataFrame(set(dfx.apply(tuple,axis=1)).symmetric_difference(dfy.apply(tuple,axis=1)))

Minimum pumping length for a regular language

How to calculate minimum pumping length of a regular language. For example if i have 0001* then minimum pumping length for this should be 4 ,that is 000 could not be pumped . Why it is so?
It will be less than or equal to the number of states in a minimal DFA for the language, minus one. So convert the regular expression into a DFA, minimize it, and count the states. For your example:
0001*
SOURCE SYMBOL DESTINATION
q1 0 q2
q1 1 q5
q2 0 q3
q2 1 q5
q3 0 q4
q3 1 q5
q4 0 q5
q4 1 q4
q5 0 q5
q5 1 q5
Why is it equal to this? Because that's the maximum number of transitions you can take in the DFA without visiting some state twice. Once you visit a state twice, you have looped.
Of course, it's possible for a language's minimal DFA to lack a path of that length. In that case, you can find the longest path (from the start state) that doesn't visit a state twice by using something like depth-first search of the automaton's graph and seeing how long a path you can trace. That would be your real answer.

Verilog dataflow delay model

I am reading about Verilog data-flow programming.
I have learned about delays in data-flow model but now I have some misunderstandings about it. I found that in data-flow model, we have rejection delay model. In other words, for assign #2 c= a | b, changes can be rejected.
My question is when will the input changes be rejected?
I am sure that when a or b change so that c expected value changes, then we should start the 2 unit delay again!
My question is, do we need to start the delay again when a or b change but expected c doesn't change?
For example in a | b "a" is 0 and "b" is 1 and after sometimes we will change "a" to 1. Is there any need to reject previous time and start the 2 unit delay again for seeing the 1 in output? (note that expected c will not change because our operation is |).
The simulator will evaluate the LHS (left hand side) of the expressions first, then apply changes the variable on the RHS (right-hand-side). Rejection (or filtering) is determined by the results of the LHS expression.
To visualize this, add an intermediate step between a | b.
assign ab = a | b;
assign #2 c = ab;
Run it thought the simulate to generate a waveform. An example output:
0 5 10 15 20 25
| | | | | |
_ _ __
a ___/ \__________/ \______/
__ _____
b _______/\___/ \______/
_ __ _ _____
ab ___/ \_/\___/ \/ \___/
_ _____ ___
c xx___/ \______/ \___/
First 2 time steps of c are unknown because there is no data for ab before time 0. The pulse on ab starting at times 7 and 15 are filtered out since they are less then 2 time steps. All other transitions in c is a shift by 2 in time ab.
There is no rejection time when a goes high at time 25 because the intermediate step (ab) does not have a transition. The simulator will do its own intermediate step, evaluating a change on a | b before deciding what action should be performed in c.

How to perform FST (Finite State Transducer) composition

Consider the following FSTs :
T1
0 1 a : b
0 2 b : b
2 3 b : b
0 0 a : a
1 3 b : a
T2
0 1 b : a
1 2 b : a
1 1 a : d
1 2 a : c
How do I perform the composition operation on these two FSTs (i.e. T1 o T2)
I saw some algorithms but couldn't understand much. If anyone could explain it in a easy way it would be a major help.
Please note that this is NOT a homework. The example is taken from the lecture slides where the solution is given but I couldn't figure out how to get to it.
Since you didn't specify the input format, I'm assuming that 0 is the initial state, any integers that appear in the second column but not the first are accepting states (3 for T1 and 2 for T2), and each row is an element of the transition relation, giving the the previous state, the next state, the input letter and the output letter.
Any operation on FSTs needs to produce a new FST, so we need states, an input alphabet, an output alphabet, initial states, final states and a transition relation (the specifications of the FSTs A, B and W below are given in this order). Suppose our FSTs are:
A = (Q, Σ, Γ, Q0, QF, α)
B = (P, Γ, Δ, P0, PF, β)
and we want to find
W = (R, Σ, Δ, R0, RF, ω) = A ∘ B
Note that we don't need to determine the alphabets of W; the definition of composition does that.
Imagine running A and B in series, with A's output tape fed as B's input tape. The state of the combined FST is simply the combined states of A and B. In other words, the states of the composition are in the cross product of the states of the individual FSTs.
R = Q × P
In your example, the states of W would be pairs of integers:
R = {(0,0), (0,1), ... (3, 2)}
though we could renumber these and get (for example):
R = {00, 01, 02, 10, 11, 12, 20, 21, 22, 30, 31, 32}
Similarly, initial and accepting states of the composed FST are the cross products of those in the component FSTs. In particular, R accepts a string iff A and B both accept the string.
R0 = Q0 × P0
RF = QF × PF
In the example, R0 = {00} and RF = {32}.
All that remains is to determine the transition relationship ω. For this, combine each transition rule for A with every transition rule for B that might apply. That is, combine each transition rule of A (qi, σ) → (qj, γ) with every rule of B that has a "γ" as the input character.
ω = { ((qi,ph), σ) → ((qj, pk), δ) : (qi, σ) → (qj, γ) ∈ α,
(ph, γ) → (pk, δ) ∈ β}
In the example, this means combining (e.g.) 0 1 a : b of T1 with 0 1 b : a and 1 2 b : a of T2 to get:
00 11 a : a
01 12 a : a
Similarly, you'd combine 0 2 b : b of T1 with those same 0 1 b : a and 1 2 b : a of T2, 0 0 a : a of T1 with 1 1 a : d and 1 2 a : c of T2 &c.
Note that you might have unreachable states (those that never appear as a "next" state) and transitions that will never occur (those from unreachable states). As an optimization step, you can remove those states and transitions. However, leaving them in will not affect the correctness of the construction; it's simply an optimization.
If you are more amenable to graphical explanations, the following set of slides provides incremental, graphical examples of the composition algorithm in practice, and also includes discussion of epsilon transitions in the component transducers. Epsilon transitions complicate the composition process, and the algorithm described in outis answer may not generate the correct result in this case, depending on the semiring being used.
See slides 10~35 for some graphical examples:
http://www.gavo.t.u-tokyo.ac.jp/~novakj/wfst-algorithms.pdf
T1 and T2
Composition of T1 and T2
The states of the composition T are pairs of a T1 state and a T2 state. T satisfies the following conditions:
its initial state is the pair of the initial state of T1 and the initial state
of T2
Its final states are pairs of a final state of T1 and a final state of T2
There is a transition t from (q1, q2) to (r1, r2) for each pair of transitions T1 from q1 to r1 and T2 from q2 to r2 such that the output label of T1 matches the input label of T2. The transition T takes its input label from T1, its output label from T2, and its weight is the combination of the weights of T1 and T2 done with the same operation
that combines weights along a path.
Since there are no weights we can ignore this. Above was picked up exactly from a following beautiful paper. Link here

Resources