I want to make regular expression having even number of b's and odd number of a's also DFA AND NFA of it
for this i made below DFA
I got these two Regular Expression
Regular Expression For Even no of b's (a*a*a*bb)*
Regular Expression For Odd no of a's (a b*b*)(a b*b*a)*
QUESTION: Did I make the right DFA ?
How to merge above two Regular Expressions into one if both are correct??
How to Convert DFA into NFA?
Edits: I got DFA From Grijesh Chauhan Answer
still unable to make regular expression which will allow only even number of b's and odd nubmer of a's .
I also tried this Regular Expression
(a(bb)*(aa)*)*
Note: From above RE only those strings are generated which start from a but i want that RE which generate string of even number of b's and odd number of a's regardles of starting from a or b
The regexes are incorrect. They should be
a*(ba*ba*)* for an even number of b
b*ab*(ab*ab*)* for an odd number of a
There is a systematic way to perform a merge of these two, because every regular expression can be represented by a state machine and vice versa and there is definitely a way to merge state machines such that the resulting state machine accepts if either of the two state machines accept, but I cannot remember how this is done directly on regular expressions.
Your DFA is incorrect. One can see this because you have cycles of odd length. Following those cycles changes the even/odd parity. So I can start with "babb" which your DFA accepts, having odd number of b's and odd number of a's. q0->q1->q2 is a cycle of 3 a's so adding 3 a's when I am in one of those states does not change wether the automata accepts, so your automata accepts "aaababb" despite neither having an odd number of a's or an even number of b's. (Also your machine fails for "bab", despite this having both odd number of a's and even number of b's)
Your DFA should at minimum keep track of the parity of the number of a's and b's. So you should start with 4 states. Q_{even,even},Q_{even,odd},Q_{odd,even} and Q_{odd,odd}. Having labeled the states in this way it should be straightforward to set up the transitions and selecting what should be the intial and accepting states.
Your regular expressions also has some issues. I would note that a* means 0 or more a's, so a*a* means 0 or more a's followed by 0 or more a's. This means that a*a*=a*. Other than that see Georg's answer.
Conventional definitions are such that every DFA is also a NFA. Converting can be a problem when going from NFA to DFA.
See Need Regular Expression for Finite Automata: Even number of 1s and Even number of 0s for a discussion on what algebra can be done on regular expressions.
use this DFA....may be help you....i made in paint so,not looking pretty...
Related
Suppose I have a deterministic finite state automaton that recognizes the language L, consisting of all strings in {a, b}* which contain an odd number of b’s
such that there is at least one a between every two b’s in the string.
I managed to derive the finite state machine for this, as seen in this example (checkboxes mean an accepted state):
But now I'm stuck; how can I convert this into a regular expression? I'm really struggling with this since it's a somewhat complicated example. I tried:
a*b[a*ba*b]*a*
But this still matches strings with even numbers of b's. What is the correct regular expression?
You can use
^a*b(?:(?:a+b){2})*a*$
https://regex101.com/r/bSpZ40/2
In normal language:
zero or more as, followed by:
a b, followed by any number of repititions of the following group:
(at least one a, followed by b) repeated twice (ensures total number of bs is odd)
followed by zero or more as
So there can be one b, or three bs, or five bs, etc, every b must have some number of as between them, and there can be leading and/or trailing as.
I have been trying different ways to solve this problem for over an hour and am getting very frustrated.
The problem is: Give regular expressions and DFAs for each of the following languages over Sigma = {0,1}.
a). {w ∈ Σ* | w contains an even number of 0s or an odd number of 1s}
If anyone could provide hints or get me started on figuring this one out, it would be very appreciated!
I know it is something along the lines of this DFA but this one is for
{w ∈ Σ* | w contains an even number of 0s or exactly two 1's}
so it's a bit different but I can't figure it out.
You can see it as follows: you always have to remember two things:
whether the number of 0s is even or odd; and
whether the number of 1s is even or odd.
Now if we denote even with e and odd with o, we consider four states: ee (both even), eo (even number of 0s and odd number of 1s), oe and oo.
Now when we read a zero (0), we simply swap the first state token, so it means we introduce transitions from:
ee - 0 -> oe;
eo - 0 -> oo;
oe - 0 -> ee; and
oo - 0 -> eo.
The same for ones (1):
ee - 1 -> eo;
eo - 1 -> ee;
oe - 1 -> oo; and
oo - 1 -> oe.
Now we only need to determine the initial state and the accepting state(s). The intial state is ee, since at that moment we have considered no zeros and no ones.
Furthermore the accepting state can by determined by the condition:
w contains an even number of 0s or an odd number of 1s
So that means the accepting states are ee, eo and oo. A drawing of this DFA is shown below:
There exists an algorithmic way to convert a DFA into an equivalent regular expression as is stated here.
You can construct a regular expression by splitting the problem into two easier problems:
a regex that checks if the number of 0s is even; and
a regex that checks if the number of 1s is odd.
For the first, you can use the regex:
(1*01*0)*1*
Indeed: you first have a group (1*01*0). This group ensures that there are two zeros, and 1s can appear everywhere in between. We allow an arbitrary number of repetitions, since the number always remains even. The regex ends with 1* since it is still possible that there are additional ones in the string.
The second problem can be solved with the regex:
0*1(0*10*1)*0*
The solution is more or less the same. The expression between the brackets: (0*10*1) ensures that the ones occur evenly. By adding a 1 in front, we ensure the number of 1s is odd.
A regular expression that then solves the problem is:
(1*01*0)*1*|0*1(0*10*1)*0*
Since the "pipe" (|) means "or".
Think about what possible states you can ever be in.
A number contains either an even number of 0's or an odd number of 0's. (2 possible states)
A number contains either an even number of 1's or an odd number of 1's. (2 possible states)
Now let's look at what combinations are accepted by your language:
even 0's, even 1's: accept
even 0's, odd 1's: accept
odd 0's, even 1's: reject
odd 0's, odd 1's: accept
As a result, your DFA will need 4 states, of which 3 are accept states and 1 is a reject state. Every state will have 2 transitions leading to a different state. Since the empty string has an even number of 0's and an even number of 1's, the first state will be the initial state.
For making this into a regular expression: think about how you'd match an even number of 0's, then how you'd match an odd number of 1's. The language is just the union of these two.
Alternatively, as suggested by Willem, you can use an algorithm to convert any NFA to a regular expression. It has the advantage of being very general, but it's also more technical. Either way, it should lead to an equivalent regular expression.
What does a number with an even number of 0's look like? It might start with any number of 1's, but when we do find a 0 we better find another one! There can be any number of 1's in between, but we only care about the 0's. Thus, we come up with the following regular expression:
1*(01*01*)*
You should be able to apply a similar logic to match an odd number of 1's. Finally, OR the two expressions to get the requested regular expression.
How to find regular expression with equal number of 1 and 0.
I am also interested in how you think such solution ?
example:
should match : 1100, 00100111 , 01 .
shouldn't match: 110 , 0, 11001.
I need regular expression which gives set of all such string .
If length of string in set given by regular expression in 2n then number of 0s should be equal to number 1s = n.
It is not possible to generate a regular expression for the language L = (0,1) (same number of 1s and 0s).
This is not a regular language, so cannot be described with a regular expression. It's not regular because an automaton which accepts it would need differing amounts of memory depending on the length of the input. A regular language is one which uses constant memory, regardless of the length of the input.
The language you describe can be generated by a Context Free Grammar, but not a regular expression.
The following CFG generates strings where the numbers of 0s and the number of 1s are equal. If S is any word in the language:
S -> SS
S -> 0S1
S -> 1S0
S -> ε (the empty word)
For this language you need a stack, and a pushdown automaton could be designed to accept it, or a Turing machine.
Not possible with regular grammar (finite state automaton) : http://en.wikipedia.org/wiki/Regular_language
Here is a regex pattern for the .NET engine that does satisfy your needs. See it in action at ideone.com.
^((?(D)(?!))(?<C>1)|(?(D)(?!))(?<-C>0)|(?(C)(?!))(?<D>0)|(?(C)(?!))(?<-D>1))*(?(C)(?!))(?(D)(?!))$
It works by using two stacks, using one (C) if there are curretly more 1s than 0s and the other one (D) if there are more zeroes than ones.
Not pretty, definitely not usable, but it works. (Ha!)
While this is not possible with a regular grammar as stated in another answer, it should be relatively easy to scan the string, increment a counter for each 1 and decrement it for each 0. If the final count is 0, then the number of 0s and 1s is equal (modulo 2^wordsize - watching out for overflow would make it a little trickier, but depending on whether there are other assumptions that can be made regarding the input, that may not be necessary).
Is there any solution that can compare two regular expression for Subsumption, Partially overlapping, disjoint i.e. i want to know how to compare two regular expression. Secondly can i combine two regular expression if regex 1 is subsumpted by regex 2.
Say you have two expressions A and B and want to see if A matches a subset of what B does.
You need to compute the minimized DFA of B and then combine the two expressions to make a union of A and B and then compute the minimized DFA of that new expression. If those two DFAs are equal then A matches a subset of B.
In essence, you can't properly check this without going through the process of constructing a minimized automata. It will however, give a verifiable true answer to the question.
Combining the two expressions can be done by making a new expression like (A)|(B), perhaps substituting the paranthesis for non-capturing varieties if your engine supports that.
If you decide to go the whole way to do the algorithms, I've written a series of articles on the process:
http://binarysculpting.com/2012/02/11/regular-expressions-how-do-they-really-work-automata-theory-for-programmers-part-1/
http://binarysculpting.com/2012/02/15/converting-dfa-to-nfa-by-subset-construction-regular-expressions-part-2/
http://binarysculpting.com/2012/03/21/dfa-state-minimization/
To compare two automatas you could just check that the states and transitions are the same. They should be exactly equal.
I have the following question from a past exam paper:
I am struggling to formalise their definitions within the necessary 15 word limit. So far I have:
i) The empty string or set of strings that contain zero or many a's OR b's OR both
ii) The set of strings that start with one or many a's, unless preceded by b's, followed by one or many a's with zero or many possible preceding b's.
My definitions seem rather cumbersome...I just don;t want to lose any info by oversimplifying the definition.
Try to simplify the regular expressions before describing them.
i is equivalent to (a | b)* which means any number of a's and b's in any order.
ii is equivalent to (a|b)*a(a|b)*a which is hard to describe in only 15 words, my best attempt is a's and b's in any order, at least two a's, the final letter is a
I have written a tool that attempts to do this for arbitrary regular expressions. You can find it here. Enter your regular expression and change the mode to "Explain."