Skip to content

Commit 9c7cc9b

Browse files
committed
Add solution for DFA problem
1 parent ec02fa3 commit 9c7cc9b

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Deterministic Finite Automaton
2+
[Try the problem](https://www.interviewbit.com/problems/deterministic-finite-automaton/)
3+
4+
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.
5+
6+
- 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).
7+
- The automaton takes a finite sequence of 0s and 1s as input.
8+
- For each state, there is a transition arrow leading out to a next state for both 0 and 1.
9+
- Upon reading a symbol, a DFA jumps deterministically from a state to another by following the transition arrow.
10+
- For example, if the automaton is currently in state S0 and current input symbol is 1 then it deterministically jumps to state S1.
11+
- 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.
12+
13+
![multiples_of_3](https://upload.wikimedia.org/wikipedia/commons/9/94/DFA_example_multiplies_of_3.svg)
14+
15+
These are some strings above DFA accepts,
16+
17+
```
18+
0
19+
00
20+
000
21+
11
22+
110
23+
1001
24+
```
25+
26+
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. Return answer modulo 10<sup>9</sup>+7.
27+
28+
### Notes
29+
30+
1. Assume each state has two outgoing edges(one for 0 and one for 1). Both outgoing edges won’t go to the same state.
31+
2. There could be multiple accept states, but only one start state.
32+
3. A start state could also be an accept state.
33+
34+
### Input Format
35+
36+
1. States are numbered from 0 to K-1, where K is total number of states in DFA.
37+
2. You are given three arrays zeroEdges, oneEdges, accepting and two integers start and N.
38+
3. Array `zeroEdges` denotes a 0 edge from state numbered i to state A[i], for all 0 ≤ i ≤ K-1
39+
4. Array `oneEdges` denotes a 1 edge from state numbered i to state B[i], for all 0 ≤ i ≤ K-1
40+
5. Array `accepting` contains indices of all accept states.
41+
6. Integer `start` denotes the start state.
42+
7. Integer `N` denotes you have to count how many distinct strings of length N the given DFA accepts.
43+
44+
### Example
45+
46+
```
47+
For the DFA shown in image, input is
48+
zeroEdges = [0, 2, 1]
49+
oneEdges = [1, 0, 2]
50+
accepting = [0]
51+
start = 0
52+
53+
Input 1
54+
-------
55+
N = 2
56+
Strings '00' and '11' are only strings on length 2 which are accepted. So, answer is 2.
57+
58+
Input 2
59+
-------
60+
N = 1
61+
String '0' is the only string. Answer is 1.
62+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
const ll MOD = 1e9 + 7;
6+
/*
7+
ll cache[60][4010];
8+
9+
ll solve(int start, int N, vector<int>& zero, vector<int>& one, vector<int>& accepting) {
10+
if (N == 0) {
11+
if (accepting[start]) {
12+
return 1;
13+
}
14+
return 0;
15+
}
16+
if (cache[start][N] != -1) {
17+
return cache[start][N];
18+
}
19+
ll res = 0;
20+
res += solve(zero[start], N - 1, zero, one, accepting);
21+
res %= MOD;
22+
res += solve(one[start], N - 1, zero, one, accepting);
23+
return cache[start][N] = res;
24+
}
25+
*/
26+
int automata(vector<int> &zeroEdge, vector<int> &oneEdge, vector<int> &accepting, int start, int N) {
27+
// memset(cache, -1, sizeof(cache));
28+
29+
int states = zeroEdge.size();
30+
vector<int> acceptingState(states, 0);
31+
for (auto acc: accepting) {
32+
acceptingState[acc] = 1;
33+
}
34+
// return (int)solve(start, N, zeroEdge, oneEdge, acceptingState);
35+
36+
/**
37+
* Time: O(N*K) where N = length, K = no of states
38+
* Space: O(K)
39+
* Iterative Dynamic Programming
40+
*/
41+
vector<ll> acceptingBefore(states, 0); // for length (i - 1)
42+
vector<ll> acceptingCurrent(states, 0); // for length (i)
43+
for (int i = 0; i < states; ++i) {
44+
if (acceptingState[i]) {
45+
acceptingCurrent[i] = 1; // for length 0, it is accepting
46+
}
47+
}
48+
49+
for (int len = 1; len <= N; ++len) {
50+
acceptingBefore = acceptingCurrent;
51+
for (int state = 0; state < states; ++state) {
52+
acceptingCurrent[state] = (acceptingBefore[zeroEdge[state]] + acceptingBefore[oneEdge[state]]) % MOD;
53+
}
54+
}
55+
56+
// only consider strings accepted with start state and having length N
57+
return (int)acceptingCurrent[start];
58+
}
59+
60+
int main () {
61+
vector<int> zeroEdges = {0, 2, 1};
62+
vector<int> oneEdges = {1, 0, 2};
63+
vector<int> accepting = {0};
64+
int start = 0;
65+
int length = 2;
66+
cout << automata(zeroEdges, oneEdges, accepting, start, length) << endl;
67+
return 0;
68+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Solution
2+
3+
<details>
4+
<summary>
5+
Hint 1
6+
</summary>
7+
Being at the start state, you have two options, either to take a zeroEdge from here, or to take a oneEdge from here.
8+
9+
Now, your problem reduces to finding accepting string count for length (N - 1) being at this new state.
10+
So, try to think in terms of recursion.
11+
Make sure you end up writing base cases clearly.
12+
</details>
13+
14+
<details>
15+
<summary>
16+
Hint 2
17+
</summary>
18+
Think about of base case, when the length passed is zero. Then you have two cases to consider, either the current state is accepting or non-accepting!
19+
20+
Now, try to take some examples and you can observe that there will be overlapping subproblems. Thus, you can use dynamic programming over here.
21+
First of all, try to implement the memoized version of the solution, and analyze its time and space complexity.
22+
23+
</details>
24+
25+
<details>
26+
<summary>
27+
Hint 3
28+
</summary>
29+
You will notice that the recursion with memoization takes O(N*K) time where K is the no. of states, as well as `O(N*K)` memory. Thus, the solution is memory intensive.
30+
31+
Try to take an example, and see how the values are getting filled in the memo or cache or table. You can come up with an iterative solution that will take up linear space!
32+
</details>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This repository contains the coding interview problems along with solutions.
2121
| [Diameter of Generic Tree](Diameter%20of%20Generic%20Tree) | text | [CPP](Diameter%20of%20Generic%20Tree/solution.cpp) | Trees, Graphs | Medium |
2222
| [2 Keys Keyboard](2%20Keys%20Keyboard) | [text](2%20Keys%20Keyboard/solution.txt) | [CPP](2%20Keys%20Keyboard/solution.cpp) | DP, Maths | Medium |
2323
| [Very Hard Queries](Very%20Hard%20Queries) | [Solution article](Very%20Hard%20Queries/solution.md) | [CPP](Very%20Hard%20Queries/solution.cpp) | Queries, Maths | Medium |
24+
| [Deterministic Finite Automaton](Deterministic%20Finite%20Automaton/) | [Solution article](Deterministic%20Finite%20Automaton/solution.md) | [CPP](Deterministic%20Finite%20Automaton/solution.cpp) | Recursion, Dynamic Programming | Medium |
2425

2526
# CS Fundamentals
2627

0 commit comments

Comments
 (0)