Here is the question I came across
Deterministic finite automaton(DFA) is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automation for each input string.
DFAs can be represented using state diagrams. For example, in the automaton shown below, there are three states: S0, S1, and S2 (denoted graphically by circles). The automaton takes a finite sequence of 0s and 1s as input. For each state, there is a transition arrow leading out to a next state for both 0 and 1. Upon reading a symbol, a DFA jumps deterministically from a state to another by following the transition arrow. For example, if the automaton is currently in state S0 and current input symbol is 1 then it deterministically jumps to state S1. A DFA has a start state (denoted graphically by an arrow coming in from nowhere) where computations begin, and a set of accept states (denoted graphically by a double circle) which help define when a computation is successful.
These are some strings above DFA accepts,
0
00
000
11
110
1001
You are given a DFA in input and an integer N. You have to tell how many distinct strings of length N the given DFA accepts.
Notes
Input format
Constraints
1 ≤ K ≤ 50
1 ≤ N ≤ 10^4
Example :
For the DFA shown in image, input is
A = [0, 2, 1]
B = [1, 0, 2]
C = [0]
D = 0
N = 2
Strings '00' and '11' are only strings on length 2 which are accepted. So, answer is 2.
N = 1
String '0' is the only string. Answer is 1.
I have a brute force recursive solution in Mind which works like this:
curr
N==0
and curr
is accept state then increment 1
to the total states and return;0 and 1
as input let the curr
state goes to curr0
and curr1
. call the recursive function twice with curr
state as curr0
and curr1
and also with N-1
; But the problem is that this solution will check all possible strings of length N
containing {0,1}
. So the Time complexity of this would be 2^N
and since 1 <= N <= 10^4
this is exponential and not feasible.
Is there an efficient solution to this problem that this that someone could suggest? May be this problem is NP-Complete and this is the only solution. Any help would be appreciated.
This language has the strings that have some number of 0's followed by the same number of 1's. Let's start by showing that a DFA accepting L must have at least 3 states.
That is a string is accepted by a DFA if and only if the DFA starting at the initial state ends in an accepting state after reading the string. A language L is accepted by a DFA < Q , , q0 , , A > , if and only if L = { w | *( q0 , w ) A } .
DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the computation. The finite automata are called deterministic finite automata if the machine is read an input string one symbol at a time. In DFA, there is only one path for specific input from the current state to the next state.
Strings that are accepted by DFA: ababba, aabba, aa, a. Strings that are not accepted by DFA: ab, b, aabab.
The ideas in your solution are fine. The problem is that it can make MANY recursive calls on exactly the same state and N. You can see a simple example of this if both 0 and 1 take the start state to the same new state, where it will make an identical call on the new state twice.
This property is the hallmark of an algorithm that can be improved using dynamic programming and/or memoization. Depending on who you talk to, the two techniques are either the same thing or close cousins. Either way, they both make sure that the work for any given call is done only once, even if an identical call comes up later. (The identical call can just produce the same answer the original call did.)
To do this, we need to track which calls have been make, that is which combinations of (State, Length) have been computed. We can keep these answers in a table.
First initialize all the Length=0 spots in the table. If the state is an accept state, fill the spot with 1; if the state is not an accept state, fill the spot with 0. Now loop for K from 1 to N. For each K, loop over all states S. Fill in Table[S,K] with Table[S0,K-1] + Table[S1,K-1], where S0 is the state S transitions to on an input of 0 and S1 on an input of 1.
Finally, read off the answer from Table[StartState,N].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With