Kleene Closure for infinite subset - regex

Let L = {an | n >= 0}, where and for all n >= 1.
Thus L consists of sequences of a of all lengths, including a sequence
of length 0. Let L2 be any infinite subset of L. I need to show there always exists a DFA to recognize (L2)*.
If L2 is a finite subset it is very obvious as L2 would be a DFA and hence by kleene closure L2* would be recognized by a DFA. But I am unable to get it for infinite subset as L2 may not be expressed as DFA for eg length of strings is prime.

Approach
While there exists a DFA to describe a set L of all strings an, n >= 0, there is no guarantee that a DFA exists for all subsets of L. The subset of L which contains all strings whose length is prime, as you have mentioned, is one example where a DFA the describes the language does not exist.
The correct approach would be to directly prove that (L')* is a regular language for any subset L' of L.
Definition
Let us define GCD(K) = GCDw ∈ K, |w| > 0 (|w|), where K is any non-empty subset of L. We can now refer to the greatest common divisor of all the lengths of all non-empty words in a language K as GCD(K). This definition applies for both finite and infinite subset of L.
Similarly, we can define LCM(K) = LCMw ∈ K, |w| > 0 (|w|), where K is any non-empty and finite subset of L.
Proof
We will try to prove that when GCD(L') = 1, there exists a number M such that all string an, n >= M belongs to the language (L')*. This leads to (L')* being a regular language, since we can construct a regular expression of the form:
All strings of length less than M and belongs to (L')*
OR
All strings of length more than or equal to M
The regular expression above has a corresponding DFA which has M + 1 states.
When GCD(L') > 1, we can reduce the problem to the case of GCD = 1 by "dividing" all words in the subset L' by GCD(L').
If GCD(L') = 1 (set-wise coprime), there exists a finite subset S of L' where GCD of the length of all strings in S is also 1.
We can prove the claim above by construction.
Pick any element w1 from L', |w1| > 0 and construct set S1 = {w1}
If GCD(Sn) = 1, Sn is the set we want to find.
If GCD(Sn) > 1, pick an element wn+1 from L' and construct set Sn+1 = {wn+1} ∪ Sn, so that
GCD(Sn+1) < GCD(Sn)
If GCD(Sn) > 1, there always exists an element from set L' that decreases the GCD when we add it to the set; otherwise, the GCD of the set L' cannot be 1. And since the length of the first element w1 has finite number of factors, the size of the final set S is finite.
Back to the problem, for any subset L' of L, we can find a finite subset S of L', which satisfies GCD(L') = GCD(S). From the set S, we can construct a generalized linear Diophantine equation with |S| unknowns ai:
a1|w1| + a2|w2| + ... + a|S||w|S|| = c where c is a non-negative integer
Since GCD(S) = 1, the equation above is always solvable, by recursively applying the solution to the simplest form of linear Diophantine equation ax + by = c.
Solve the generalized Diophantine equations above for c = 0 to (LCM(S) - 1). The solutions (a1, a2, ..., a|S|) can contain negative numbers. However, we can keep adding multiples of LCM(S) to both sides of the equations until all the solutions contain only non-negative numbers.
Let k be the smallest multiple of LCM(S) so that all Diophantine equations for c = k * LCM(S) + q, q = 0 to (LCM(S) - 1) has non-negative solution. Then we can define M as k * LCM(S), since any strings whose length larger than M can be decomposed as concatenation of words in S (thus in L').
Example calculation based on the proof
Suppose L' is set of all strings in L whose length is prime.
Let us construct set S = {a2, a5}. S can be {a2, a19} or {a5, a23}, doesn't really matter. The final value of M might be different, but it doesn't affect the fact that (L')* is regular language.
We need to solve 10 equations (separately):
2a1 + 5a2 = 0 => (a1, a2) = (0, 0)
2a1 + 5a2 = 1 => (a1, a2) = (3, -1)
2a1 + 5a2 = 2 => (a1, a2) = (1, 0)
2a1 + 5a2 = 3 => (a1, a2) = (-1, 1)
2a1 + 5a2 = 4 => (a1, a2) = (2, 0)
2a1 + 5a2 = 5 => (a1, a2) = (0, 1)
2a1 + 5a2 = 6 => (a1, a2) = (3, 0)
2a1 + 5a2 = 7 => (a1, a2) = (1, 1)
2a1 + 5a2 = 8 => (a1, a2) = (4, 0)
2a1 + 5a2 = 9 => (a1, a2) = (2, 1)
Add one LCM(2,5) = 10. Note that we can modify the solution directly without solving again, due to the property of LCM:
2a1 + (5a2 + 10) = 1 + 10 => (a1, a2) = (3, 1)
(2a1 + 10) + 5a2 = 3 + 10 => (a1, a2) = (4, 1)
Since all the solutions are non-negative, and we only add one LCM(2,5), M = 10.
The regular expression for (L')* can be constructed:
a2+a4+a5+a6+a7+a8+a9+a10a*
The regular expression is not very compact, but it is not our concern here. For the sake of the proof, we only need to know there exists a number M so that an belongs to (L')* for all n >= M, which implies that there are finite number of states and a DFA can be constructed.

Related

if condition in ampl

I am new in ampl and I want to use if condition in ampl with the following information:
I have a binary variable X[p,r], where {p in P, r in R}.
Now I want to make a new constraint such that the variable R[p,r] is used where X[p,r]=0.
I do not know how I can write it or even if the ampl can handle it or not, I tried the following constraint but they did not work:
s.t. a1{r in R, p in P and X[p,r]=0}:
s.t. a2{r in R p in P and X[p,r]=0};
s.t. a2{r in R ,p in P, and X[p,r]=0};
s.t. a2{r in R, p in P: and X[p,r]=0};
You cannot include a decision variable in the "for all" part of the constraint (in AMPL, the part inside the {...}). Instead, you need build into the constraint itself the logic that says the constraint is only active if X[p,r] = 0. The way to do that depends on the type of constraint: >=, =, or <=. I'll write each case separately, and I'll do it in a generic way instead of specific to your problem.
In the explanation below, I assume that the constraint is written as
a[1]y[1] + ... + a[n]y[n] >=/=/<= b,
where a[i] and b are constants and y[i] are decision variables. I also assume we want the constraint to hold if x = 0, where x is a binary decision variable, and we don't care whether the constraint holds if x = 1.
Let M be a new parameter (constant) that equals a large number.
Greater-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] >= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the right-hand side is very negative.
(If all of the a[i] are nonnegative, you can instead use
a[1]y[1] + ... + a[n]y[n] >= bx,
which is tighter.)
Less-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] <= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the RHS is very large.
Equality constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] = b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the equality constraint holds, and if x = 1, the constraints have no effect.
Note: If your model is relatively large, i.e., it takes a non-negligible amount of time to solve, then you need to be careful with big-M-type formulations. In particular, you want M to be as small as possible while still enforcing the logic of the constraints above.

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].

elem function of no limit list

list comprehension haskell
paar = [(a,b) | a<-[a | a<-[1..], mod a 3 == 0], b<-[b*b | b<-[1..]]]
a = divisor 3
b = square
The Elements must be constructed by equitable order.
the test >elem (9, 9801) must be True
my Error
Main> elem (9, 9801) test
ERROR - Garbage collection fails to reclaim sufficient space
How can I implement this with Cantor's diagonal argument?
thx
Not quite sure what your goal is here, but here's the reason why your code blows up.
Prelude> let paar = [(a,b) | a<-[a | a<-[1..], mod a 3 == 0], b<-[b*b | b<-[1..]]]
Prelude> take 10 paar
[(3,1),(3,4),(3,9),(3,16),(3,25),(3,36),(3,49),(3,64),(3,81),(3,100)]
Notice you're generating all the (3, ?) pairs before any other. The elem function works by searching this list linearly from the beginning. As there are an infinite number of (3, ?) pairs, you will never reach the (9, ?) ones.
In addition, your code is probably holding on to paar somewhere, preventing it from being garbage collected. This results in elem (9, 9801) paar taking not only infinite time but also infinite space, leading to the crash you described.
Ultimately, you probably need to take another approach to solving your problem. For example, something like this:
elemPaar :: (Integer, Integer) -> Bool
elemPaar (a, b) = mod a 3 == 0 && isSquare b
where isSquare = ...
Or alternatively figure out some other search strategy than straight up linear search through an infinite list.
Here's an alternate ordering of the same list (by hammar's suggestion):
-- the integer points along the diagonals of slope -1 on the cartesian plane,
-- organized by x-intercept
-- diagonals = [ (0,0), (1,0), (0,1), (2,0), (1,1), (0,2), ...
diagonals = [ (n-i, i) | n <- [0..], i <- [0..n] ]
-- the multiples of three paired with the squares
paar = [ (3*x, y^2) | (x,y) <- diagonals ]
and in action:
ghci> take 10 diagonals
[(0,0),(1,0),(0,1),(2,0),(1,1),(0,2),(3,0),(2,1),(1,2),(0,3)]
ghci> take 10 paar
[(0,0),(3,0),(0,1),(6,0),(3,1),(0,4),(9,0),(6,1),(3,4),(0,9)]
ghci> elem (9, 9801) paar
True
By using a diagonal path to iterate through all the possible values, we guarantee that we reach each finite point in finite time (though some points are still outside the bounds of memory).
As hammar points out in his comment, though, this isn't sufficient, as it will still take
an infinite amount of time to get a False answer.
However, we have an order on the elements of paar, namely (3*a,b^2) comes before (3*c,d^2) when
a + b < c + d. So to determine whether a given pair (x,y) is in paar, we only have to check
pairs (p,q) while p/3 + sqrt q <= x/3 + sqrt y.
To avoid using Floating numbers, we can use a slightly looser condition, that p <= x || q <= y.
Certainly p > x && q > y implies p/3 + sqrt q > x/3 + sqrt y, so this will still include any possible solutions, and it's guaranteed to terminate.
So we can build this check in
-- check only a finite number of elements so we can get a False result as well
isElem (p, q) = elem (p,q) $ takeWhile (\(a,b) -> a <= p || b <= q) paar
And use it:
ghci> isElem (9,9801)
True
ghci> isElem (9,9802)
False
ghci> isElem (10,9801)
False

Expression transformation problem

Let's say we have the following statement: s = 3 * a * b - 2 * c, where s, a, b and c are variables. Also, we used Shunting Yard algorithm to build RPN expression, so now we can assign values to variables a, b and c and calculate s value by using simple RPN evaluator.
But, the problem is that I should be able to calculate a value of any variable a, b or c when values of all other variables are set.
So, I need to transform existing expression somehow to get a set of expressions:
a = (s + 2 * c) / (3 * b)
b = (s + 2 * c) / (3 * a)
c = (3 * a * b - s) / 2
How can I generate such expressions on basis of one original statement? Is there any standard approaches for solving such problems?
Constraints:
A set of available operators: +, -, *, /, including unary + and -
operators *, / and = can't have the same variable on both sides (e.g. s = a * a, or s = a + s are not acceptable)
Thanks
See this first: Postfix notation to expression tree to convert your RPN into a tree.
Once you have the equation left expression = right expression change this to left expression - right expression = 0 and create a tree of left expression - right expression via Shunting Yard and the above answer. Thus when you evaluate the tree, you must get the answer as 0.
Now based on your restrictions, observe that if a variable (say x) is unknown, the resulting expression will always be of the form
(ax + b)/(cx + d) where a,b,c,d will depend on the other variables.
You can now recursively compute the expression as a tuple (a,b,c,d).
In the end, you will end up solving the linear equation
(ax + b)/(cx + d) = 0 giving x = -b/a
This way you don't have to compute separate expressions for each variable. One expression tree is enough. And given the other variables, you just recursively compute the tuple (a,b,c,d) and solve the linear equation in the end.
The (incomplete) pseudocode will be
TupleOrValue Eval (Tree t) {
if (!t.ContainsVariable) {
blah;
return value;
}
Tuple result;
if (t.Left.ContainsVariable) {
result = Eval(t.Left);
value = Eval(t.Right);
return Compose(t.Operator, result, value);
} else {
result = Eval(t.Right);
value = Eval(t.Left);
return Compose(t.Operator, result, value);
}
}
Tuple Compose(Operator op, Tuple t, Value v) {
switch (op) {
case 'PLUS': return new Tuple(t.a + v*t.c, t.b + v*t.d, t.c, t.d);
// (ax+b)/(cx+d) + v = ( (a + vc)x + b + dv)/(cx + d)
// blah
}
}
For an example, if the expression is x+y-z = 0. The tree will be
+
/ \
x -
/ \
y z
For y=5 and z=2.
Eval (t.Right) will return y-z = 3 as that subtree does not contain x.
Eval(t.Left) will return (1,0,0,1) which corresponds to (1x + 0)/(0x + 1). Note: the above pseudo-code is incomplete.
Now Compose of (1,0,0,1) with the value 3 will give (1 + 3*0, 0 + 3*1, 0, 1) = (1,3,0,1) which corresponds to (x + 3)/(0x + 1).
Now if you want to solve this you take x to be -b/a = -3/1 = -3
I will leave the original answer:
In general it will be impossible.
For instance consider the expression
x*x*x*x*x + a*x*x*x*x + b*x*x*x + c*x*x + d*x = e
Getting an expression for x basically corresponds to find the roots of the polynomial
x5 + ax4 + bx3 + cx2 + dx -e
which has been proven to be impossible in general, if you want to use +,-,/,* and nth roots. See Abel Ruffini Theorem.
Are there are some restrictions you forgot to mention, which might simplify the problem?
The basic answer is you have to apply algebra to the set of equations you have, to produce equations that you want.
In general, if you start with this symbolic equation:
s = 3 * a * b - 2 * c
and you add constraints for s, a, and c:
s = ...
a = ...
c = ...
you need to apply standard laws of algebra to rearrange the set of equations to produce what you want, in this case, a formula for b:
b = ...
If you add different constraints, you need the same laws of algebra, applied in different ways.
If your equations are all of the form (as your example is not) of
left_hand_side_variable_n = combination_of_variables
then you can use rules for solving simultaneous equations. For linear combinations, this is pretty straightforward (you learned how to do this high school). And you can even set up a standard matrix and solve using a standard solver package without doing algebra.
If the equations are not linear, then you may not be able to find a solution no matter how good your math is (see other answers for examples). To the extent it is possible to do so, you can use a computer algebra system (CAS) to manipulate formulas. They do so by representing the formulas essentially as [math] abstract syntax trees, not as RPN, and apply source-to-source transformation rules (you would call these "algebra rules" from high school). They usually have a pretty big set of built-in rules ("knowledge") already. Some CAS will attempt to solve such systems of equations for you using the built-in rules; others, you have to tell what sequence of algebraic laws to apply in what order.
You may also use a constraint solver, which is a special kind of computer algebra system focused only on answering the kind of question you've posed, e.g., "with this set of constraints, and specific values for variables, what is the value of other variables?". These are pretty good if your equations follows the forms they are designed to solve; otherwise no gaurantee but that's not surprising.
One can also use a program transformation system, which manipulate arbitrary "syntax trees", because algrebra trees are just a special case of syntax trees. To use such a system, you define the langauge to be manipulated (e.g., conventional algebra) and the rules by which it can be manipulated, and the order in which to apply the rules. [The teacher did exactly this for you in your intro algebra class, but not in such a formal way] Here's an example of a my program transformation system manipulating algebra. For equation solving, you want much the same rules, but a different order of application.
Finally, if you want to do this in a C++ program, either you have to simulate one of the above more general mechanisms (which is a huge amount of work), or you have narrow what you are willing to solve significantly (e.g., "linear combinations") so that you can take advantage of a much simpler algorithm.
There is a quite straight-forward one for very basic expressions (like in your example) where each variable occurs mostly once and every operator is binary. The algorithm is mostly what you would do by hand.
The variable we are looking for will be x in the following lines. Transform your expression into the form f(...,x,...) == g(...). Either variable x is already on the left hand side or you just switch both sides.
You now have two functions consisting of applications of binary operators to sub-expressions, i.e. f = o_1(e_1,e_2) where each e_i is either a variable, a number or another function e_i = o_i(e_j, e_k). Think of this as the binary tree representation where nodes are operators and leafs are variables or numbers. Same applies for g.
Yyou can apply the following algorithm (our goal is to transform the tree into one representing the expression x == h(...):
while f != x:
// note: f is not a variable but x is a subexpression of f
// and thus f has to be of the form binop(e_1, e_2)
if x is within e_1:
case f = e_1 + e_2: // i.e. e_1 + e_2 = g
g <- g - e_2
case f = e_1 - e_2: // i.e. e_1 - e_2 = g
g <- g + e_2
case f = e_1 * e_2: // i.e. e_1 * e_2 = g
g <- g / e_2
case f = e_1 / e_2: // i.e. e_1 / e_2 = g
g <- g * e_2
f <- e_1
else if x is within e_2:
case f = e_1 + e_2: // i.e. e_1 + e_2 = g
g <- g - e_2
case f = e_1 - e_2: // i.e. e_1 - e_2 = g
g <- g + e_2
case f = e_1 * e_2: // i.e. e_1 * e_2 = g
g <- g / e_2
case f = e_1 / e_2: // i.e. e_1 / e_2 = g
g <- g * e_2
f <- e_1
Now that f = x and f = g was saved during all steps we have x = g as solution.
In each step you ensure that x remains on the lhs and at the same time you reduce the depth of the lhs by one. Thus this algorithm will terminate after a finite amount of steps.
In your example (solve for b):
f = 3a*b*2c*- and g = s
f = 3a*b* and g = s2c*+
f = b and g = s2c*+3a*/
and thus b = (s + 2*c)/(3*a).
If you have more operators you can extend the algorithm but you might run into problems if they are not invertible.