Here is a DFA from a research project. We created the DFA manually. We are interested in What is Regular Expression that is corresponding to DFA . Certainly, there could be multiple Regular Expressions corresponding to it; we prefer a simpler one.
-
wait what is label for self loop on B ,E – Grijesh Chauhan Mar 29 '13 at 15:23
4 Answers
You have missed the labels in you DFA on self loop at B and E. But because you say for given DFA then only choice for labels is 0
on both loop.
The correct Regular Expression for your DFA is:
(00* 10*1)* (1(0 + 10)* 1 1) ( 0 + 1 (00* 10*1)* 1 ( 0 + 10)* 1 1)*
A brief explanation:
You have only one final state that is
D
. So string can be acceptable if it ends onD
. do you notice incoming edge onD
is labeled1
andD
has a self loop labeled0
.Start state is
A
so string can be start with0
or with1
. Actually there is two loops on A. One starts with0
and travels through upper graph.
RE for upper loop is:00* 10*1
To understand this:
0 0* 1 0* 1 A-E loop on E E-F loop on F F-A
To go from
A
toD
in lower graph. RE is1 (0 + 10)* 1 1
To understand this:1 (0 + 10)* 1 1 A - B loop on B B-C C-D
The complete RE for DFA is: (answer)
(00* 10*1)* (1(0 + 10)* 1 1) ( 0 + 1 (00* 10*1)* 1 ( 0 + 10)* 1 1)*
To understand this:
(00* 10*1)* (1(0 + 10)* 1 1) ( 0 + 1 (00* 10*1)* 1 ( 0 + 10)* 1 1)* ^ ^ ^ upper loop A to D loop on D * for loop on D ( 0 + 1 (00* 10*1)* 1 (0 + 10)* 1 1 )* ^ D-A A-A A-B loop on B, B-c c-D self loop on D
Edit as @RedBaron commented does this Regular expression generate string 01110100110
:
well fist check is it accepted by DFA or not:
A--0--> E--1---> F--1---> A---1---> B--0---> B---1--->C---0--- ->B---0---> B--1-->C---1---> D---0--->D
Yes string is accepted by DFA.
How to generate from RE I given in answer, below I have aligned the RE and string.
(00* 10*1)* (1(0 + 10)* 1 1) ( 0 + 1 (00* 10*1)* 1 ( 0 + 10)* 1 1)*
0^ 1^ 1 1 0100 1 1 0
Only the difficulty you may have to understand: how (0 + 10)*
generates 0100
? for this check below:
(0 + 10)*
be repeat for three times:
(0 + 10)(0 + 10)(0 + 10)
0 10 0

- 57,103
- 20
- 141
- 208
-
`00*` at the beginning can be changed to `0+` and for loop on B I think (0+10)* is wrong because `000` `010` `10` all create the loop – RedBaron Mar 29 '13 at 18:42
-
@RedBaron yes `00*` can be written as `0+` i don't use it because of superscript notion...Second, B has two loops one self loop `0*` and other `10` loop via `C` so combined both in (0 + 10)* read point 3 – Grijesh Chauhan Mar 29 '13 at 19:03
-
-
-
-
@RedBaron Ok We knows, `+` means union. `( A + B)` means either `A` or `B`. similarly `(0 + 10)` means either `0` or `10`. because `*` means any numbers of time. so `(0 + 10)*` means any numbers of time 0 or `10`. – Grijesh Chauhan Mar 29 '13 at 19:25
-
Maybe you'd want to run your regexp and the test strings through a regexp engine to cross-check – RedBaron Mar 29 '13 at 19:25
-
In regexp `+` means 1 or more occurences and **not** union, I presume – RedBaron Mar 29 '13 at 19:26
-
@RedBaron actually + means both its mean dependences on syntax it appears in read this [+ Operator in Regular Expression](http://stackoverflow.com/questions/14122755/what-will-be-the-dfa-for-the-regular-expression-00101011?answertab=votes#tab-top) – Grijesh Chauhan Mar 29 '13 at 19:29
-
Ahh.... different planes of discussions. I was wholly thinking about regular expressions while you had language in mind. Anyways my answer was purely from a regexp point of view. – RedBaron Mar 29 '13 at 19:33
Jack, basically there can be two regex for this DFA. first can be AB*CD*A, second can be AE*F*

- 218
- 2
- 12
10*110*
is for transition from A-B-C-D without the loop in c-B
1(0*(10)*)*110*
I think also covers the loop between C and B
0+10*1
is the loop from A-E-F. So you can prefix it to both the expressions
You get (0+10*1)*10*110*
without the loop and (0+10*1)*1(0*(10)*)*110*
with it
The final expression is thus
(0+10*1)*1(0*(10)*)*110*
for transitioning from A to D
Finally on reaching the state D you could get a 1
, reach A
and repeat the whole thing over again
((0+10*1)*1(0*(10)*)*110*)(1((0+10*1)*1(0*(10)*)*110*))*
See it in action for some valid and invalid strings for this DFA
Clarification - This regexp is based on regular expressions as accepted by PCRE. So +
means 1 or more occurences of a string and *
means 0 or more occurences, while |
means OR
EDIT The (0*(10)*)*
can be written better as (0|(10))*
(Thanks to @grijesh-chauhan for pointing me in that direction). So the RE (PCRE based) would be
((0+10*1)*1(0|(10))*110*)(1((0+10*1)*1(0|(10))*110*))*

- 4,717
- 5
- 41
- 65
-
-
I also assume that E and B self transitions occur at 0 (The digit is missing in the figure) – RedBaron Mar 29 '13 at 08:34
-
your basic regular expression is correct, but review again. Can you find why your expression is wrong? it good question.... (*i am not downvoter*) – Grijesh Chauhan Mar 29 '13 at 18:28
-
No I can't. BTW your Regexp does not accept 01110100110 which is a vlid string for this DFA – RedBaron Mar 29 '13 at 18:41
-
@RedBaron check again my answer as well as you answer. also check both question you asked in comment. I commented to you post because you were just near to answer. And my RE process you string `A --0--E--1---F--1---A---1---B--0---B---1--C---0---B---0---B--1--C---1---D---0---D` – Grijesh Chauhan Mar 29 '13 at 19:00
-
That is for the DFA and is evindent from the figure. But does your _regexp_ allow such a string? – RedBaron Mar 29 '13 at 19:02
-
@grijesh-chauhan Check your regexp on [regexpal](http://www.regexpal.com/?flags=gm®ex=^%2800*10*1%29*%281%280%2B10%29*11%29%280%2B1%2800*10*1%29*1%280%2B10%29*11%29*%24&input=01110100110%0A111%0A1110%0A01011000110%0A1001001) – RedBaron Mar 29 '13 at 19:06
-
@RedBaron read my updated answer, don't try with tool the are not regular language renovators indeed!! – Grijesh Chauhan Mar 29 '13 at 19:16
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27203/discussion-between-redbaron-and-grijesh-chauhan) – RedBaron Mar 29 '13 at 19:19
The algorithm you need to use is described here. I strongly recommend reading Michael Sipser's Introduction to the Theory of Computation if you're more interested in the topic.
For your particular DFA, following the algorithm you get this regex:
[(010*1)*1(10*)110*1]*(010*1)*1(10)*110*

- 2,697
- 20
- 24
-
your regex is very close to the DFA. By drawing your regex to DFA, the transition from D to A is missing. – JackWM Mar 29 '13 at 07:37
-
Sorry, fixed. Obviously this is much better done by computer since it's easy to make mistakes. – Sergiu Toarca Mar 29 '13 at 08:22