Hey i have been stuck on this problem for a few days now and even going through sample problems in my textbook as well as sample solutions i cant figure out how to make this grammar work.
Give a Grammar for this language L:
L = { a^n^2 : n ≥ 0 }
I know it may be a vague question but i really could use some help figuring this one out.
Thanks in advance!
My answer might come late. I hope it is yet useful for you (or other readers).
S -> a
S -> aIIIE
Finish counting:
aII -> aaFI
aFII -> aaFI
aFIE -> aa
Going on counting:
Produce "a"s and "J"s
aII -> aCaLPI
PII -> aLPI
PIE -> aRRRE
Move ALL "a"s to the left:
La -> aL
LR -> RR
Ca -> aC
Convert "R"s to "I"s:
CR -> IC
CE -> E
Names for states:
S : Start
E : "End"
I : "1"
F : "Finish"
P : "Produce"
L : "Left"
R : "Right"
C : "Convert"
Explanation:
Let me also sketch the idea of the solution.
Square numbers are always the sum of a sequence of odd numbers.
E.g. 2^2=1+3=4, 3^2=1+3+5=9, and so on.
Mathematically: "sum over 2*k-1 of k between 1 and n" = n^2
What you actually have to do is only counting over the odd numbers.
That is easier said than to be done.
My grammar works like this:
On the left hand side, you have the previous result.
The next odd number is indicated by the odd number of the same Non-Terminal ("I" in my case).
So I count like aIIIE, aaaaIIIIIE, and so on.
Whenever you achieved that state you always decide whether to go on or finish counting.
When you go on counting you need to produce I-times an "a" AND AT THE SAME TIME (I+2)-times "I"s again.
However "I"s and "a"s will be mixed in their order.
Therefore you must introduce some mechanism that moves all "a"s to the left
(and all "I"s to the right by that).
Moreover you must always limit the freedom of producing words
so that your "current path" cannot be left anyhow.
Otherwise you can run into a deadend with your production.
("F", "P", "L", "R", "C" , "E" serve for that.)
I want to demonstrate it with n=2 and n=3.
This should be enough.
"*->" : "produces"
n=2:
aIIIE
(aII)IE *-> (aaFI)IE
a(aFII)E *-> a(aaFI)E
aa(aFIE) *-> aa(aa)
aaaa
n=3:
aIIIE
(aII)IE *-> (aCaLPI)IE
aCaL(PII)E *-> aCaL(aLPI)E
aCaLaL(PIE) *-> aCaLaL(aRRRE)
a(Ca)LaLaRRRE *-> aaCLaLaRRRE
aaCLa(La)RRRE *-> aaCLa(aL)RRRE
aaCLaa(LR)RRE *-> aaCLaa(RR)RRE
aaC(La)aRRRRE *-> aaC(aL)aRRRRE
aaCa(La)RRRRE *-> aaCa(aL)RRRRE
aa(Ca)aLRRRRE *-> aa(aC)aLRRRRE
aaa(Ca)LRRRRE *-> aaa(aC)LRRRRE
aaaaC(LR)RRRE *-> aaaaC(RR)RRRE
aaaa(CR)RRRRE *-> aaaa(IC)RRRRE
aaaaI(CR)RRRE *-> aaaaI(IC)RRRE
aaaaII(CR)RRE *-> aaaaII(IC)RRE
aaaaIII(CR)RE *-> aaaaIII(IC)RE
aaaaIIII(CR)E *-> aaaaIIII(IC)E
aaaaIIIII(CE) *-> aaaaIIIII(E)
aaaaIIIIIE
Related
I have some multivariate integral manipulation that I'd like to verify with Maple, but I can't seem to get it to invoke Divergence theorem (a.k.a. Gauss's theorem; special cases of Stokes' theorem; generalizations of integration by parts, Green's theorem).
For example, consider the Dirichlet energy:
E(v) = ∫ ∇v.∇v dA = -∫ v ∆v dA + ∮v ∇v.n ds
Suppose I'd like to verify the application of Divergence theorem, I could try defining both sides of the equality (Dirichlet and Dirichlet2) over the unit square and checking whether their difference is 0:
with(LinearAlgebra):
with(VectorCalculus):
Lap := (v) -> Divergence(Gradient(v,[x y])):
boundary := (v,w) -> \
eval(int(DotProduct(Multiply(w,Gradient(v,[x,y])),< 0,-1>),x=0..1),y=0) + \
eval(int(DotProduct(Multiply(w,Gradient(v,[x,y])),< 0, 1>),x=0..1),y=1) + \
eval(int(DotProduct(Multiply(w,Gradient(v,[x,y])),<-1, 0>),y=0..1),x=0)+ \
eval(int(DotProduct(Multiply(w,Gradient(v,[x,y])),< 1, 0>),y=0..1),x=1):
Dirichlet2 := (v,w) -> \
-int(Lap(v)*w,x=0..1,y=0..1) + boundary(v,w);
Dirichlet := (v,w) -> \
int(DotProduct(Gradient(v,[x,y]),Gradient(w,[x,y])),x=0..1,y=0..1):
simplify(Dirichlet(v(x,y),w(x,y)) - Dirichlet2(v(x,y),w(x,y)),size);
Unfortunately, this doesn't return the desired:
0
instead, just a long sequence of unevaluated interior and boundary integrals.
I can of course verify this for explicit functions:
Dirichlet(x^2+y^4,x^2+y^4) - Dirichlet2(x^2+y^4,x^2+y^4);
does return
0
(any of course will for any other function). But is there anyway to tell Maple to invoke Divergence theorem internally? Then I can verify the above (and my actual, more complicated expressions) fully symbolically. Or, if not, is there some way I can rearrange my input to help maple figure this out?
Regular languages are closed under concatenation - this is demonstrable by having the accepting state(s) of one language with an epsilon transition to the start state of the next language.
If we consider the language L = {a^n | n >=0}, this language is regular (it is simply a*). If we concatenate it with another language L = {b^n | n >=0}, which is also regular, we end up with a^nb^n, but we obviously know this isn't regular.
Where am I going wrong with my logic here?
The definition of the concatenation of two languages L1 and L2 is the set of all strings wx where w ∈ L1 and x ∈ L2. This means that L1L2 consists of all possible strings formed by pairing one string from L1 and one string from L2, which isn't necessarily the same as pairing up matching strings from each language.
As a result, as #Oli Charlesworth pointed out, the language you get back here isn't actually { anbn | n in N }. Instead, it's the language { anbm | n in N and m in N }, which is the language a*b*. This language is regular, since it's given by the regular languages.
Hope this helps!
I want to implement A* and I looked to Wikipedia for a reference.
It looks like it can fail in the following case. Consider three nodes, A, B, and C.
START -> A -> C -> GOAL
| ^
\-> B
The path costs are:
START -> A : 10
START -> B : 1
B -> A : 1
A -> C : 100
C -> GOAL : 100
Clearly the solution is START -> B -> A -> C -> GOAL but what happens if the heuristic lets us expand A before expanding B?
Our heuristic costs are as follows (note these are all underestimates)
A -> GOAL : 10
B -> GOAL : 50
When A is expanded, the true cost to C will turn out out to be higher than B's heuristic cost, and so B will be expanded before C.
Fine, right?
The problem I see is that when we expand B and replace the datum "A comes from START with cost 10" to "A comes from B with cost 2" we aren't also updating "C comes from A with cost 110" to "C comes from A with cost 102". There is nothing in Wikipedia's pseudocode that looks like it will forward-propagate the cheaper path. Now imagine another node D which can reach C with cost 105, it will erroneously override "C comes from A with cost 110".
Am I reading this wrong or does Wikipedia need to be fixed?
If you are using graph search, i.e. you remember which nodes you visit and you don't allow revisiting the nodes, then your heuristic is not consistent. It says in the article, that for a heuristic to be consistent, following needs to hold:
h(x) <= d(x, y) + h(y) for all adjacent nodes x, y
In your case the assumption h(B) = 50 is inconsistent as d(B -> A) + h(A) = 1 + 10 = 11. Hence your heuristic is inconsistent and A* wouldn't work in this case, as you rightly noticed and as is also mentioned in the wikipedia article: http://en.wikipedia.org/wiki/A%2a_search_algorithm#Properties.
If you are using tree search, i.e. you allow the algorithm to revisit the nodes, the following will happen:
Add A and B to the queue, score(A) = 10 + 10 = 20, score(B) = 1 + 50 = 51.
Pick A from queue as it has smallest score. Add C to the queue with score(C) = 10 + 100 + h(C).
Pick B from the queue as it is now the smallest. Add A to the queue with score(A) = 2 + 10 = 12.
Pick A from the queue as it is now again smallest. Notice that we are using tree search algorithm, so we can revisit nodes. Add C to the queue with score(C) = 1 + 1 + 100 + h(C).
Now we have 2 elements in the queue, C via A with score 110 + h(C) and C via B and A with score 102 + h(C), so we pick the correct path to C via B and A.
The wikipedia pseudocode is the first case, i.e. graph search. And they indeed state right under the pseudocode that:
Remark: the above pseudocode assumes that the heuristic function is monotonic (or consistent, see below), which is a frequent case in many practical problems, such as the Shortest Distance Path in road networks. However, if the assumption is not true, nodes in the closed set may be rediscovered and their cost improved. In other words, the closed set can be omitted (yielding a tree search algorithm) if a solution is guaranteed to exist, or if the algorithm is adapted so that new nodes are added to the open set only if they have a lower f value than at any previous iteration.
I am learning Coq at school, and I have an assignment to do for home. I have a lemma to proove: If a list contains a zero among its elements, then the product of its elements is 0. I started my code, and I am stuck in a point where I do not know how to go on. I do not know all the commands from Coq, I did a lot of research, but I cannot manage to find my way to the end of the Proof. Here is my code:
Require Import List ZArith.
Open Scope Z_scope.
Fixpoint contains_zero (l : list Z) :=
match l with
nil => false
| a::tl => if Zeq_bool a 0 then true else contains_zero tl
end.
Fixpoint product (l : list Z) :=
match l with
nil => 1
| a::tl => a * product tl
end.
Lemma Zmult_integral_r :
forall n m, m <> 0 -> m * n = 0 -> n = 0.
Proof.
intros n m m0; rewrite Zmult_comm; apply Zmult_integral_l.
assumption.
Qed.
Lemma product_contains_zero :
forall l, contains_zero l = true -> product l = 0.
intros l H.
So, I thought that it would be a good idea to create a function that checks if the list contains a zero, and another one to calculate the product of its elements. I have also found (luckily) a lemma online that prooves that if I have 2 numbers , and I know that one of them is not 0, and their product is 0, then necessarily the other number is 0 (I thought that might help). I thought about doing a proof by induction, but that didn't work out. Any ideas? I know that this is not the place to ask someone to do my work , I AM NOT ASKING THAT, I just need an idea.
1/ You do not need the theorem that you mention, " if I have 2 numbers , and I know that one of them is not 0, and their product is 0, then necessarily the other number is 0". You don't need it because you want to prove that the product is 0, you are not in a situation where you want to use the fact that the product is zero.
So the theorem Zmult_integral_r is not useful for the lemma in this question. It would be useful if you had to prove forall l, product l = 0 -> contains_zero l = true.
Here, for your problem, the two functions that you consider are recursive, so the usual hint is to do a proof by induction, and then to use the tactic simpl to make the functions look simpler.
Represent product of two numbers as one number while you will stand with that lemma.
Is the following language context free?
L = {a^i b^k c^r d^s | i+s = k+r, i,k,r,s >= 0}
I've tried to come up with a context free grammar to generate this but I can not, so I'm assuming its not context free. As for my proof through contradiction:
Assume that L is context free,
Let p be the constant given by the pumping lemma,
Choose string S = a^p b^p c^p d^p where S = uvwxy
As |vwx| <= p, then at most vwx can contain two distinct symbols:
case a) vwx contains only a single type of symbol, therefore uv^2wx^2y will result in i+s != k+r
case b) vwx contains two types of symbols:
i) vwx is composed of b's and c's, therefore uv^2wx^2y will result in i+s != k+r
Now my problem is that if vwx is composed of either a's and b's, or c's and d's, then pumping them won't necessary break the language as i and k or s and r could increase in unison resulting in i+s == k+r.
Am I doing something wrong or is this a context free language?
I can't come up with a CFG to generate that particular language at the top of my head either, but we know that a language is context free iff some pushdown automata recognizes it.
Designing such a PDA won't be too difficult. Some ideas to get you started:
we know i+s=k+r. Equivalently, i-k-r+s = 0 (I wrote it in that order since that is the order in they appear). The crux of the problem is deciding what to do with the stack if (k+r)>i.
If you aren't familiar with PDA's or cannot use them to answer the problem, at least you know now that it is Context Free.
Good luck!
Here is a grammar that accepts this language:
A -> aAd
A -> B
A -> C
B -> aBc
B -> D
C -> bCd
C -> D
D -> bDc
D -> ε