How to convert this automata to regular expression via NFA - regex

I need to transform this finite automata to regular expressions via converting the DFA (Deterministic Finite Automata) to a general NFA (Non-deterministic Finite Automata). How one should go about it? Will state diagrams of the NFA and the DFA will be identical?

So there are two DFAs in the picture, so I'll show how to get the RE for each one in turn. For the first, we write down some equations:
(q1) = (q1)a + (q2)b + e
(q2) = (q1)b + (q2)a
Now we can use the rule (q) = (q)x + y <=> (q) = yx* on each:
(q1) = ((q2)b + e)a*
(q2) = (q1)ba*
Now we can substitute in and since we care about (q2) we might as well get that directly:
(q2) = ((q2)b + e)a*ba*
= (q2)ba*ba* + a*ba*
= a*ba*(ba*ba*)*
We get the regular expression a*ba*(ba*ba*)* which, at a glance, appears to be correct. How did we get the equations? For each state, we wrote down the ways of "getting to" the state, and combined them with + (or, union). We include the empty string e in (q1)'s equation since (q1) is the initial state and nothing needs to be consumed to get there (initially).
For the second, the equations look like this:
(q1) = (q3)a + e
(q2) = (q1)(a + b) + (q2)a + (q3)b
(q3) = (q2)b
We can use our rule to eliminate the self-reference for (q2):
(q1) = (q3)a + e
(q2) = ((q1)(a + b) + (q3)b)a*
(q3) = (q2)b
Now we substitute and use the rule again:
(q1) = (q3)a + e
(q2) = ((q1)(a + b) + (q3)b)a*
(q3) = ((q1)(a + b) + (q3)b)a*b
= (q1)(a + b)a*b + (q3)ba*b
= (q1)(a + b)a*b(ba*b)*
Now we substitute again and use the rule again:
(q1) = (q1)(a + b)a*b(ba*b)*a + e
= e((a + b)a*b(ba*b)*a)*
= ((a + b)a*b(ba*b)*a)*
(q2) = ((q1)(a + b) + (q3)b)a*
(q3) = (q1)(a + b)a*b(ba*b)*
We can now substitute back in to get the expression for (q3):
(q1) = ((a + b)a*b(ba*b)*a)*
(q2) = ((q1)(a + b) + (q3)b)a*
(q3) = ((a + b)a*b(ba*b)*a)*(a + b)a*b(ba*b)*
The regular expression will be the union of the expressions for (q1) and (q3) since these are the accepting states:
r = ((a + b)a*b(ba*b)*a)* + ((a + b)a*b(ba*b)*a)*(a + b)a*b(ba*b)*
= ((a + b)a*b(ba*b)*a)*(e + (a + b)a*b(ba*b)*)
The first part of this takes you from the state q1 back to the state q1 in every possible way; the second part says you can stay in q1 or do the other thing, which leads to q3, otherwise.

Wikipedia references this course PDF: Second Part of Regular Expressions Equivalence with Finite Automata, and according to this document, the procedure starts with this initial step:
A DFA is converted to a GNFA of special form by the following procedure:
Add a new start state with an \epsilon arrow to the old start state and a new accept state with an \epsilon arrow from all old accept states.
(emphasis mine)
So the NFA and DFA will not be identical. This also explains how to deal with multiple accepting states.

NO the state diagrams of the NFA and the DFA will not be identical during the conversion process.
For the second FSM regex will be -
ε U (aUb) ab (bUa(aUb)ab)* (εUa)
You can refer to these steps -
Here's an example -
These are screenshots from the PDF version of the book - "Introduction to the theory of computation" by Michael Sipser.

Related

How to generate random math expression trees with sympy?

I am scouring the web but I cannot find how to generate random math expressions with sympy. Is it even possible?
I would like to build an expression tree by randomly selecting functions (product, sum, cosine...) and symbols from a set of predefined functions and symbols.
For instance, given the set [+,.] of sum and product and the symbols [x,y] I'd like to generate expressions such as x+y, (x+y).x, y+(x.x+y)+x etc, controlling parameters as the tree depth, width and the number of nodes.
Any hints?
Something like the following might help you get started:
from random import choice, randint
from sympy import FunctionClass, Add, Mul, cos, sin, binomial, arity, S
def args(n, atoms, funcs):
a = funcs+atoms
g = []
for _ in range(n):
ai = choice(a)
if isinstance(ai, FunctionClass):
g.append(ai(*args(arity(ai), atoms, funcs)))
else:
g.append(ai)
return g
def expr(ops, atoms, funcs=()):
types = [Add, Mul]
atoms = tuple(atoms)
while 1:
e = S.Zero
while e.count_ops() < ops:
_ = choice(types)(*args(randint(1,3), atoms, funcs))
e = choice(types)(e, _)
if e is S.NaN: break
else:
return e
>>> [expr(5, (-1,0,1,x,y)) for do in range(2)]
[(x - 1)*(2*x + y + 2), x + y*(x + 4*y - 2) + y]
>>> expr(5, (-1,0,1,x,y), (cos, binomial))
x*y**2 + x + cos(1)
>>> expr(5, (-1,0,1,x,y), (cos, binomial))
(y + zoo*binomial(y, x) - 2)*(y + cos(1) + 1)
To generate rational expressions you could change make the 2nd _ arg be _**choice((1,-1)).

Is there a way to put two actions as a result of then in the if - else statement?

I want to make two results in if~~then~~.
For example,
fun count (x,[]) = 0
| count (x,y::ys) =
val cnt = 0
if x mod y = 0 then **/ cnt+1 and count(x,y/2) /**
else count (x-y,ys)
If the if statement is true, as in **/ /**, is there a way to make it do two things?
I want to make two results in if~~then~~ [...]
You can make a function that returns two results by using a tuple, e.g.:
(* Calculate the two solutions of a 2nd degree polynomial *)
fun poly (a, b, c) =
let val d = b*b - 4.0*a*c
val sqrt_d = Math.sqrt d
in ( (~b + sqrt_d) / (2.0*a), (~b - sqrt_d) / (2.0*a) )
end
And you can also deliver two different results depending on some criterion, e.g.:
fun poly (a, b, c) =
let val d = b*b - 4.0*a*c
val sqrt_d = Math.sqrt d
val root_1 = (~b + sqrt_d) / (2.0*a)
val root_2 = (~b - sqrt_d) / (2.0*a)
in
if root_1 > root_2
then (root_1, root_2)
else (root_2, root_1)
end
But if you need for a function to return one result in one situation, and two results in another situation, you need to wrap the result in a return type that can hold either one or two values, e.g.:
datatype ('a, 'b) one_or_two = One of 'a | Two of 'a * 'b
datatype item = Apple | Lamp | Knife
val gen = Random.newgen ()
fun loot () =
if Random.random gen > 0.90
then Two (Lamp, Knife)
else One Apple
You may also read the following StackOverflow Q&A: Multiple if statemens in one Function in SML

Are there any languages such that they are proper subsets of each other and satisfy these conditions

Are there languages such that A ⊂ B ⊂ C ⊂ D ⊂ E over the alphabet {a,b,c} where:
A is not context-free
B is context-free and non-regular
C is regular
D is non regular
E is regular and not {a,b,c}*
Start by taking non-context-free language A over {a,b}. For example A = { ww | w \in {a,b}*}, but any other would also work.
You can then build the other languages on top of that:
B = {a,b}* U {a^i c^i | i >= 0}
C = {a,b}* U {a,c}*
D = {a,b}* U {a,c}* U {b^i c^i | i>= 0}
E = {a,b}* U {a,c}* U {b,c}*
You can then verify for each of these that they have the desired properties.
First, let us simplify this and take care of E by just not using c in any language and making E the language (a + b)*. Next, let us deal with D by making it the same as E, but with all strings of prime length greater than two removed. We can choose C to be the set of all even-length strings over {a, b}: (aa + ab + ba + bb)*. For a context-free and non-regular language we can choose the set of even-length palindromes over {a, b}: S -> aSa | bSb | e. Finally, we can choose as A the set of even-length palindromes over {a, b} which begin with a prime number of as.
We might have tried getting rid of D by making it the union of C and some language involving only b, then making C equal to a* and then trying to find A and B using only a... but we might have had trouble finding a context-free non-regular language involving only one symbol.

Generate all expressions from list of numbers equal to a number [PROLOG]

I am given a list of numbers, for example [22,45,2,6,7,...].
Now I have to insert binary operators: +, -, /, * and parentheses (, ) between numbers so that expression is equal to given number k.
List all possible expressions created by insertions of operators and parentheses that will give sum of k.
Position of numbers in resulting expression have to be fixed, i.e. only insertion of operators and parentheses between or around numbers
For example: given number k=9 and list [1,2,3], one solution would be [(,(,1,+,2,),*,3,)].
How would I do that?
[ my current wrong solution ]:
Right now I know how to evaluate expression like [1,+,3,*,5] by going from left to right and eating Operand1,Operator,Operand2 until there is nothing to eat.
But I have to insert parentheses too..
Can anybody sketch a solution or give a hint?
This was an old exam question, and I'm preparing for exam which will be in 3 months, so I'm trying to solve these, but I'm stuck.
EDIT: This is prolog question.
I think trying to directly build the result list with parentheses while traversing the input is a bad idea. It's easier to build up the syntax tree of an expression whose leaves are labeled with the elements of the given list, then process that in a separate step.
For example:
?- leaves_expr([A,B,C,D], Expr).
Expr = leaf(A)+ (leaf(B)+ (leaf(C)+leaf(D))) ;
Expr = leaf(A)+ (leaf(B)+leaf(C)*leaf(D)) ;
Expr = leaf(A)+ (leaf(B)+leaf(C)+leaf(D)) ;
Expr = leaf(A)+ (leaf(B)*leaf(C)+leaf(D)) ;
Expr = leaf(A)+leaf(B)* (leaf(C)+leaf(D)) ;
Expr = leaf(A)+leaf(B)* (leaf(C)*leaf(D)) ;
This can be implemented as follows:
leaves_expr([X], leaf(X)).
leaves_expr(Leaves, X + Y) :-
append([L|Left], [R|Right], Leaves),
leaves_expr([L|Left], X),
leaves_expr([R|Right], Y).
leaves_expr(Leaves, X * Y) :-
append([L|Left], [R|Right], Leaves),
leaves_expr([L|Left], X),
leaves_expr([R|Right], Y).
The append/3 calls are used to decompose the list of leaves into non-empty parts to avoid nontermination problems. I would be interested in an elegant way of doing this with DCGs.
Then, given an expression tree like this, we can "output" it again in a fully parenthesized form:
expr_parenthesized(leaf(X)) -->
[X].
expr_parenthesized(X + Y) -->
['('],
expr_parenthesized(X),
[+],
expr_parenthesized(Y),
[')'].
expr_parenthesized(X * Y) -->
['('],
expr_parenthesized(X),
[*],
expr_parenthesized(Y),
[')'].
Composing these two parts, we get:
?- leaves_expr([A,B,C], Expr), expr_parenthesized(Expr, Parenthesized).
Expr = leaf(A)+ (leaf(B)+leaf(C)),
Parenthesized = ['(', A, +, '(', B, +, C, ')', ')'] ;
Expr = leaf(A)+leaf(B)*leaf(C),
Parenthesized = ['(', A, +, '(', B, *, C, ')', ')'] ;
Expr = leaf(A)+leaf(B)+leaf(C),
Parenthesized = ['(', '(', A, +, B, ')', +, C, ')'] ;
Expr = leaf(A)*leaf(B)+leaf(C),
Parenthesized = ['(', '(', A, *, B, ')', +, C, ')'] ;
Expr = leaf(A)* (leaf(B)+leaf(C)),
Parenthesized = ['(', A, *, '(', B, +, C, ')', ')'] ;
Expr = leaf(A)* (leaf(B)*leaf(C)),
Parenthesized = ['(', A, *, '(', B, *, C, ')', ')'] ;
and so on. If you write the easy predicate expr_value/2 to evaluate such expressions (constructed from numbers at the leaves), you're done.
One way to think about the parenthesis problem without actually putting any parentheses is to use postfix notation. In other words:
(a + b) * c
turns into:
a b + c *
which is the following tree in canonical Prolog notation:
*(+(a, b), c)
Similarly:
a + (b * c) ---> a b c * + ---> +(a, *(b, c))
For a complete example, with three operands, 1, 2, and 3, and only + and * as operators, to keep it short, you get:
1 2 + 3 + ---> (1 + 2) + 3 = 6
1 2 + 3 * ---> (1 + 2) * 3 = 9
1 2 * 3 + ---> (1 * 2) + 3 = 6
1 2 * 3 * ---> (1 * 2) * 3 = 6
1 2 3 + + ---> 1 + (2 + 3) = 6
1 2 3 + * ---> 1 * (2 + 3) = 5
1 2 3 * + ---> 1 + (2 * 3) = 7
1 2 3 * * ---> 1 * (2 * 3) = 6
Looking at the first column, I get the following general idea: you start with n operands and n-1 binary operators. You push the first two operands on the stack, and need to perform 2*n-3 more steps. At each step, you either push an operand or apply an operator. You can always push an operand if you still have any left. You can apply an operator only if you have two or more operands on the stack; you will have to reduce the stack at that point.
Backtracking will take care of enumerating all possibilities (so this is a typical brute-force exhaustive search of the solution space). You will have two sources of choicepoints: picking one of the operators; and either pushing or reducing.
With this in mind, I arrive at the following implementation of a predicate that takes a list of operands, a list of binary operators, and gives you a "parenthesized" expression:
expr(Operands, Operators, E) :-
Operands = [A, B|Rest],
length(Operands, N),
Steps is 2*N - 3,
expr(Steps, Rest, [B, A], Operators, E).
This pushed the first two operands to the stack and calculated the number of steps left.
expr(Steps, Operands, Stack, Operators, E) :-
( succ(Steps0, Steps) ->
next(Steps0, Operands, Stack, Operators, E)
; Stack = [E]
).
Here I used succ/2 to count down to 0 and then stop; at the end, the only element on the stack is your expression.
next(Steps, Operands, Stack, Operators, E) :-
push(Operands, Stack, Operands_next, Stack_next),
expr(Steps, Operands_next, Stack_next, Operators, E).
next(Steps, Operands, Stack, Operators, E) :-
member(Op, Operators),
reduce(Stack, Op, Stack_next),
expr(Steps, Operands, Stack_next, Operators, E).
This is where you either push or reduce. The two separate clauses is the first source of choice points; using member/2 to take one operator from the list is the other.
push([X|Xs], S0, Xs, [X|S0]).
reduce([A,B|Stack], Op, [X|Stack]) :-
X =.. [Op, B, A].
Implementing pushing and reducing is trivial. I used the "univ" operator =.. to make terms like +(1, 2) from a list like [+, 1, 2].
With this, you can already ask "how can I use +, *, and parenthesis to make 7 out of [1,2,3]":
?- expr([1,2,3], [+,*], E), E =:= 7.
E = 1+2*3 ;
false.
This is the most basic "generate and test": you generate arithmetic expressions, then test if they evaluate to a value. If you leave out the test, you can see all expressions:
?- expr([1,2,3], [+,*], E).
E = 1+(2+3) ;
E = 1*(2+3) ;
E = 1+2*3 ;
E = 1*(2*3) ;
E = 1+2+3 ;
E = (1+2)*3 ;
E = 1*2+3 ;
E = 1*2*3 ;
false.
One curious detail is that because + and * are already defined as infix operators, Prolog writes them and even parenthesizes them for you. I don't know if a solution like E = (1+2)*3 is good enough for you or do you really need ['(', 1, +, 2, ')', *, 3]. The other answer seems to have a working solution for this already. Since here the expression is already a valid arithmetic expression, you would have to adjust it slightly. I would probably write it like this:
infix(N) -->
{ number(N)
}, !,
[N].
infix(E) -->
{ compound(E),
E =.. [Op, A, B]
}, !,
['('], infix(A), [Op], infix(B), [')'].
I also don't know if 1+2+3 = 3+3 = 6 is the same as 1+(2+3) = 1+5 = 6: do you need to consider associativity?
Either way, you can wrap expr/3 in a predicate like this:
equals_k(Numbers, K, E) :-
expr(Numbers, [+,-,*,/], E0),
K =:= E0,
phrase(infix(E0), E).
PS: it is quite easy to get a division by zero exception, try for example:
?- expr([1,0], [/], E), R is E.
This my solution proposal which I find to be simple and straight forward,
copy and paste to notepad++ editor for best readability.
* ________________________________________________ *
*|find_expression(NumsList,TargetValue,Expression)| *
**------------------------------------------------* *
* Expression is an arithmetic expression of the numbers in Numslist with *
* possible operators '+','-','*','/' and '(' and ')' between the numbers *
* in such a way that the expression evaluates to the TargetValue argument *
*****************************************************************************/%
/* a single element number list can evaluate only to itself */
find_expression([SingleNumber],SingleNumber,SingleNumber).
/* expression of a multypile number list */
find_expression(NumberList,Target,Expression):-
/* non-deterministically divide the number list
into 2 separate lists which include at least one number each*/
append([X|Xs],[Y|Ys], NumberList),
/* recursively find an expression for east list,
where the expression evaluates to itself */
find_expression([X|Xs],Exp1,Exp1),
find_expression([Y|Ys],Exp2,Exp2),
/* non-deterministically choose an operand from [+,-,*,division]
and compose Expression to be (Exp1 Operand Exp2) */
( member(Expression,[Exp1+Exp2,Exp1-Exp2,Exp1*Exp2])
; /* prevent zero divison */
(Val2 is Exp2, Val2 =\= 0, Expression = (Exp1/Exp2))), %/*
/* assure that final expression evaluates(matches) the targe value
and convert value from integer to float if necessary */
( Target = Expression ; Target is Expression
; FloatTarget is Target*1.0, FloatTarget is Expression)

Regular expression of string of a, b that doesn't contain aa

Using only parenthesis and * symbol, one example that comes in my mind is
((a|b)(bb*))*
but I can have a string for example abba that the last letter is a, which is not included in this... Any ideas?
Here is the DFA:
Use the method described here to derive and solve the equation for R1 (the initial state):
R1 = bR1 + aR2 + λ
R2 = bR1 + λ
Substitute R2 to R1:
R1 = bR1 + abR1 + a + λ
Apply the Arden's theorem:
R1 = (b + ab)*(a + λ)
The rest is change the syntax a bit:
(b|ab)*(a|)
This can be rewritten to regex in Perl-syntax for testing:
^(a?b)*a?$