How to convert NFA to Regular Expression - regex

I knew that converting a regular expression to a NFA, there is a algorithm.
But I was wondering if there is a algorithm to convert a NFA to regular expression.
If there is, what is it?
And if there isn't, I am also wondering if all NFA can convert to a regular expression.
Is there a NFA that a regular expression that cannot represent?
Thank you! :D

Here is an algorithm where each transition is incrementally replaced with a regex, until there is only an initial and final state: https://courses.engr.illinois.edu/cs373/sp2009/lectures/lect_08.pdf [PDF]

An NFA can be written as a system of inequalities (over a Kleene algebra), with one variable for each state, one inequality q ≥ 1 for each final state q and one inequality q ≥ x r for each transition on x from state q to state r. This is a right-affine fixed point system, over a Kleene algebra, whose least fixed point solution gives you, for any q, the regular expression recognized by the NFA that has q as the start state. The system can be collated, so that all the inequalities q ≥ A, q ≥ B, ..., q ≥ Z, for each given q, are combined into q ≥ A + B + ... Z. The result is a matrix system 𝐪 ≥ 𝐚 + H 𝐪, where 𝐪 is the vector of all the variables, 𝐚 the vector of the affine coefficients - 0's for non-final states, 1's for final states, but those details are not important for what follows; and H is the matrix of all the linear coefficients.
To solve a right-affine system, do so one variable at a time. In Kleene algebra, the least fixed point solution to x ≥ a + bx is x = b* a. This applies both singly and matrix-wise, so that the least fixed point solutuion to 𝐪 ≥ 𝐚 + H 𝐪, in matrix form is 𝐪 = H* 𝐚.
Matrices over Kleene algebras form a Kleene algebras, with matrix addition and matrix multiplication, respectively, for the sum and product operators and the matrix star for the Kleene star. Finding the matrix star is one and the same process as solving the corresponding system 𝐪 ≥ 𝐚 + H 𝐪.
A Generic Example:
Consider the NFA with states q, r, s, with q the start state and s the only final state, and with transitions:
a: q → q, b: q → r, c: q → s,
d: r → q, e: r → r, f: r → s,
g: s → q, h: s → r, i: s → s.
Let (x, y, z) = (0, 0, 1) denote the corresponding affine coefficients. Then, the corresponding right-affine system is:
q ≥ x + a q + b r + c s,
r ≥ y + d q + e r + f s,
s ≥ z + g q + h r + i s.
Solve for s, first, to obtain
s = i* (z + g q + h r) = i* z + i* g q + i* h r.
Substitute in the other inequalities to get:
q ≥ x + c i* z + (a + c i* g) q + (b + c i* h) r,
r ≥ y + f i* z + (d + f i* g) q + (e + f i* h) r.
Rewrite this as
q ≥ x' + a' q + b' r,
r ≥ y' + d' q + e' r,
where
x' = x + c i* z, a' = a + c i* g, b' = b + c i* h,
y' = y + f i* z, d' = d + f i* g, e' = e + f i* h.
Solve for r to get
r = e'* (y' + d' q) = e'* y' + e'* d' q.
Substitute into the inequality for q to get
q ≥ (x' + b' e'* y') + (a' + b e'* d') q
and rewrite this as
q ≥ x" + a" q
where
x" = x' + b' e'* y', a" = a' + b e'* d'.
Finally, solve this for q to get
q = a"* x".
This is also the general form for that embodies the generic fail-safe solution for NFA's with 3 states.
Since q is the start state, then a"* x" is the regular expression sought for, with a", x", a', b', d', e', x', y', x, y and z as indicated above. If you try to in-line substitute them all, the expression will blow up to a size that is exponential in the number of states and will be large in size even for three states.
An Optimized Example:
Consider the system for the NFA whose states are q, r, s, with q the start state, s the final state, and the transitions
a: q → r, a: q → q, b: q → q, b: q → s, a: s → s, b: s → s
The corresponding right-affine system is
q ≥ a r + a q + b q
r ≥ b s
s ≥ 1 + a s + b s
Solve for s first:
s ≥ 1 + a s + b s = 1 + (a + b) s ⇒ s = (a + b)*.
Substitute into the inequality for r and solve to find the least fixed point:
r ≥ b (a + b)* ⇒ r = b (a + b)*.
Finally, substitute into the inequality for q and solve to find the least fixed point:
q ≥ a b (a + b)* + (a + b) q ⇒ q = (a + b)* a b (a + b)*.
The resulting regular expression is (a + b)* a b (a + b)*. So, with chess-playing strategizing, simpler and optimal forms for the solution can be found.

Related

Integrating without `polar_lift`

from sympy import *
x = Symbol('x')
c, k = symbols('c, k', real=True, nonnegative=True)
integrate(exp(-c * k * x**2), (x, -oo, oo))
Produces
(sqrt(pi)*sqrt(polar_lift(c))*sqrt(polar_lift(k))/(c*k)
I don't want polar_lift in the output, is there an assumption I can add, or something else I can do to get a cleaner result?
You've set the symbols to be nonnegative which implies the possibility that they are both zero in which case the integral does not converge. If the symbols are both positive (i.e. not zero) then you get a simple result:
In [85]: x = Symbol('x')
In [86]: c, k = symbols('c, k', positive=True)
In [87]: integrate(exp(-c * k * x**2), (x, -oo, oo))
Out[87]:
√π
─────
√c⋅√k

get coefficient of a monomial in a sympy expression

I have a sympy expression like so:
exp_str = '3 * x**2*y + 4*a**2 * x*y + 9*b * x'
my_expr = sp.parsing.sympy_parser.parse_expr(exp_str)
and I want to get the coefficient of x*y, which should be 4*a**2.
Is there a function that I can pass my_expr to along with a list of variables I want my polynomial to be over? For example, I would need to pass this function x and y so that it knows x and y are variables and that a and b are coefficients.
If there is no such function, and recommendations on how to write code to do this would be appreciated. Thanks
There is a coeff method of sympy expressions:
In [28]: x, y, a, b = symbols('x, y, a, b')
In [29]: expr = 3 * x**2*y + 4*a**2 * x*y + 9*b * x
In [30]: expr.coeff(x*y)
Out[30]:
2
4⋅a
https://docs.sympy.org/latest/modules/core.html?highlight=coeff#sympy.core.expr.Expr.coeff
You might find it useful though to work with expressions as structured polynomials e.g.:
In [31]: p = Poly(expr, [x, y])
In [32]: p
Out[32]: Poly(3*x**2*y + 4*a**2*x*y + 9*b*x, x, y, domain='ZZ[a,b]')
In [33]: p.coeff_monomial(x**2 * y)
Out[33]: 3
In [34]: p.coeff_monomial(x * y)
Out[34]:
2
4⋅a
https://docs.sympy.org/latest/modules/polys/basics.html

Polynomial Long Division in OCaml

I'm trying to implement polynomial long division based on polynomials of type int array. Here the highest degree coefficients are at the end. I'm basing my code off of the pseudo code available on Wikipedia:
function n / d is
require d ≠ 0
q ← 0
r ← n // At each step n = d × q + r
while r ≠ 0 and degree(r) ≥ degree(d) do
t ← lead(r) / lead(d) // Divide the leading terms
q ← q + t
r ← r − t × d
return (q, r)
H
This line
q = poly_add q t
is testing for the equality between q and poly_add q t, it is not an assignment and the compiler is warning you that you are ignoring the result of this test. You are also misunderstanding your pseudo-code: t is supposed to be a polynomial of degree degree r - degree d.
You need to use references, or transform your while loop into a recursive function.
Similarly, since r is an array, whose length cannot be changed:
while ((Array.length r) >= n2) && (Array.length r != 0)do
This test does not change during the loop. Structural inequality is <>.
Another issue is that this line
t.(0) <- r.((Array.length r)-1)/p2.(n2-1)
is mutating your zero polynomial which is a bad idea.
Overall, it is not clear if your polynomial are supposed to be mutable or not.
If they are not supposed to be mutable, you should avoid a.( ) <- altogether.

Use '->' in prolog

I want to generate a list that is made up by sublists which contains 2 intergers and their sum and product.
The expected result is
A = [[2,3,5,6],[2,4,6,8],[2,5,7,10],[2,6,8,12],
[2,7,9,14],[2,8,10,16],[3,4,7,12],[3,5,8,15],
[3,6,9,18],[3,7,10,21],[4,5,9,20]].
But I kept have some sublists that does not have the sum and product:
?- get(A).
A = [[2,3,5,6],[2,4,6,8],[2,5,7,10],[2,6,8,12],
[2,7,9,14],[2,8,10,16],[2,_G419,_G422,_G425],
[3,4,7,12],[3,5,8,15],[3,6,9,18],[3,7,10,21],
[3,_G530,_G533,_G536],[4,5,9,20]].
Here is my code :
get(4,5,[]):-!.
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10 ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
get(List):-get(2,2,List).
I think the problem is about using -> in my code, but I don't know how to fix it, I think the logic about -> is right: if S=<10, then calculate the product and the other value with N and Q; else calculate N+1 and N+2 and ...
The operator precedence of (->)/2 is higher than the
operator precedence of (,)/2. As a result the (,)/2
are grouped together as an argument for (->)/2.
So your rule:
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10 ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
Is basically read by the interpreter as:
get(N,M,[[N,Q,S,P]|List]):-
(Q is M + 1,
S is N + Q,
S =< 10) ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
With the result that Q ans S are unbound when
the if condition is not satisfied and the else
part is executed. You can fix your code by introducing
additional parenthesis:
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
(S =< 10 ->
P is N * Q,
get(N,Q,List)
; X is N + 1,
get(X,X,List)).
But even with this fix, there is an issue with P
being unbound in the else branch.
For clarity, you might even try a solution without
if-then-else, see for example here
Prolog removing IF THEN ELSE
I concur with Jan, but I want to inteject a point about formatting. Because ; and -> so strongly affect the meaning of a program, it's poor form (meaning makes it harder to understand) to put those operators at the end of a line.
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10
->
P is N * Q,
get(N,Q,List)
; X is N + 1,
get(X,X,List).
When you arrange code like this it becomes immediately apparent that the entire clause is an if-then-else, with the first 3 lines being the 'if' portion. (Though I wouldn't object to Jan's layout for the final version.)

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].