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

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?$

Related

How to convert this automata to regular expression via NFA

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.

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)

Prolog generating all possible operation for given list

Hello I am facing a problem. Let's say I have 3 operations(+,-,*).
I would like to generate variable that contains all possible expressions for given list of arguments using those 3 operators.
my_problem([1,2],X) would return
X=1-2
X=1*2
X=1+2
my_problem([1,2,3],X) would return
X=1+2+3 X=1-2-3
X=1+2-3 X=1+2*3
X=1-2+3 ...
and so on.
I know that i can build expression using this predicates.
args_expr(Arg1,Arg2,Arg1 + Arg2).
args_expr(Arg1,Arg2,Arg1 - Arg2).
args_expr(Arg1,Arg2,Arg1 * Arg2).
Is there any clever way to generete this variable? I would be grateful for any help or advice.
You can use the so called "univ" operator, =.., to build expressions:
?- Expr =.. [+, A, B].
Expr = A+B.
And you can enumerate the operations you have:
op(+). op(-). op(*).
Then:
?- op(Op), Expr =.. [Op, A, B].
Op = (+),
Expr = A+B ;
Op = (-),
Expr = A-B ;
Op = (*),
Expr = A*B.
Or maybe:
?- op(Op1), op(Op2), E =.. [Op1, A, E0], E0 =.. [Op2, B, C].
From here to what you need is just figuring out how to do this for lists of arbitrary number of elements (not just two or three).
Hint: If you figure out how to define expr/3, you can do:
?- foldl(expr, [B,C], A, E).
E = C+ (B+A) ;
E = C- (B+A) ;
E = C* (B+A) ;
E = C+ (B-A) ;
E = C- (B-A) ;
E = C* (B-A) ;
E = C+B*A ;
E = C-B*A ;
E = C* (B*A).
Of course, the second argument to foldl can be a list of arbitrary length.

How to solve a simple quadratic equation with Sympy?

solve(-14.4*(x**2)+71.8*x+5.083, x)
result is None. how come? My calculation by hand gives two roots, 5.0559 and -0.063
Perhaps you are not using the most current version. I get
>>> from sympy import *
>>> var('x')
x
>>> solve(-14.4*(x**2)+71.8*x+5.083, x)
[-0.0698162934055920, 5.05592740451670]
More generally:
import sympy as sp
y = 'a * x ** 2 + b * x + c' # for example a quadratic polynomial
s = sp.var('x a b c') # define four symbols as variables
print(sp.solve(y, s )) # sympy solves y(a,b,c,x) for each of a, b, c, x
print(sp.solve(y, x )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, 'x')) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
Yields:
[(x, -(b*x + c)/x**2, b, c)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
While:
s = sp.var('x') # define one symbol as a varible
print(sp.solve(y, s )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, x )) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
print(sp.solve(y, 'x')) # sympy solves Y(a,b,c,x) for x treating a, b, c as constants
Returns:
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]

Sympy rewrite expression to template

If I have an expression such as c1 / (c2*s + c3) I would like sympy to transform the expression to a template looking like C1 / (s + C2) such that C1 = c1/c2 and C2 = c3/c2.
Is there an easy way to do that?
So you are asking "What must C1 and C2 be to make this true?" solve can answer that question:
>>> v = var('c1:4 C1:3 s')
>>> expr = c1 / (c2*s + c3)
>>> tmpl = C1 / (s + C2)
>>> t = tmpl.free_symbols - expr.free_symbols
>>> solve(expr - tmpl, t, dict=True)
[{C1: c1/c2, C2: c3/c2}]