linear grammar with unequal number of 0s and 1s - grammar

Is it possible to come up with a linear grammar with unequal number of 0s and 1s?
Such as 0100, 01100, 111,1,0, 100101001...
I know there is a context-free grammar for this, but is there a linear grammar?
Thanks.

A grammar is regular if and only if it is either left regular or right regular. The left regular grammars are equivalent to the left linear grammars. The right regular grammars are equivalent to the right linear grammars. Therefore, if a regular grammar exists that generates the indicated language, then it is either right or left regular, and hence equivalent to either a left or right linear grammar.
edit1:
Note that there's no regular grammar generating the indicated language LUNEQ. To see this, consider the fact that LEQ = { w : na(w) = nb(w)} is the complement of LUNEQ. Because the regular languages are closed under complementation and LEQ is not a regular language, LUNEQ is not a regular language.
edit2:
I believe the pumping lemma for linear languages can be used to show that the indicated language LUNEQ is not linear. Here is what I've come up with. I'm fairly confident it's correct. My primary concern is that you were asked - presumably - for a linear language generating the indicated language; however, I came to the conclusion that there is no such grammar.
Assume LUNEQ is linear. By the pumping lemma for linear languages, there exists an n > 0 depending on LUNEQ such that for all z ∈ LUNEQ, z can be written uvwxy where:
|vx| > 0,
|uvxy| ≤ n, and
uviwxiy ∈ LUNEQ for all i ≥ 0.
Let n be the constant guaranteed be the pumping lemma. Consider the string
z = anb(n! + 2n)an
Since z ∈ LUNEQ, it can be decomposed into substrings uvwxy satisfying the constraints of the pumping lemma such that, for all i ≥ 0, the string
uviwxiy = a|u|ai|v|a(n - |u| - |v|)b(n! + 2n)a(n - |x| - |y|)ai|x|a|y|
is a member of LUNEQ. Since 1 ≤ |vx| ≤ n, |vx| divides n!. Hence, (n!|vx|-1 + 1) is a natural number. Setting i to (n!|vx|-1 + 1) gives the string
z' = uv(n!|vx|-1 + 1)wx(n!|vx|-1 + 1)y = a|u|a(n!|vx|-1 + 1)|v|a(n - |u| - |v|)b(n! + 2n)a(n - |x| - |y|)a(n!|vx|-1 + 1)|x|a|y|
Simplifying the pumped string gives us an equal number of a's and b's:
na(z') = 2n - |vx| + (n!|vx|-1 + 1)|vx| = 2n + n!
Since (2n + n!) is equivalent to the number of b's in the pumped string, z' ∉ LUNEQ. But this contradicts the assumption that LUNEQ is a linear language. Hence, LUNEQ is not a linear language.

Related

What are the differences between the unary minus and unary tilde minus operators in ocaml?

I'm learning OCaml, and the docs and books I'm reading aren't very clear on some things.
In simple words, what are the differences between
-10
and
~-10
To me they seem the same. I've encountered other resources trying to explain the differences, but they seem to explain in terms that I'm not yet familiar with, as the only thing I know so far are variables.
In fact, - is a binary operator, so some expression can be ambigous : f 10 -20 is treated as (f 10) - 20. For example, let's imagine this dummy function:
let f x y = (x, y)
If I want produce the tuple (10, -20) I naïvely would write something like that f 10 -20 which leads me to the following error:
# f 10 -20;;
Error: This expression has type 'a -> int * 'a
but an expression was expected of type int
because the expression is evaluated as (f 10) - 20 (so a substract over a function!!) so you can write the expression like this: f 10 (-20), which is valid or f 10 ~-20 since ~- (and ~+ and ~-. ~+. for floats) are unary operators, the predecense is properly respected.
It is easier to start by looking at how user-defined unary (~-) operators work.
type quaternion = { r:float; i:float; j:float; k:float }
let zero = { r = 0.; i = 0.; j = 0.; k = 0. }
let i = { zero with i = 1. }
let (~-) q = { r = -.q.r; i = -.q.i; j = -. q.j; k = -. q.k }
In this situation, the unary operator - (and +) is a shorthand for ~- (and ~+) when the parsing is unambiguous. For example, defining -i with
let mi = -i
works because this - could not be the binary operator -.
Nevertheless, the binary operator has a higher priority than the unary - thus
let wrong = Fun.id -i
is read as
let wrong = (Fun.id) - (i)
In this context, I can either use the full form ~-
let ok' = Fun.id ~-i
or add some parenthesis
let ok'' = Fun.id (-i)
Going back to type with literals (e.g integers, or floats), for those types, the unary + and - symbol can be part of the literal itself (e.g -10) and not an operator. For instance redefining ~- and ~+ does not change the meaning of the integer literals in
let (~+) = ()
let (~-) = ()
let really = -10
let positively_real = +10
This can be "used" to create some quite obfuscated expression:
let (~+) r = { zero with r }
let (+) x y = { r = x.r +. y.r; i = x.i +. y.i; k = x.k +. x.k; j =x.k +. y.j }
let ( *. ) s q = { r = s *. q.r; i = s *. q.i; j = s *. q.j; k = s *. q.k }
let this_is_valid x = +x + +10. *. i
OCaml has two kinds of operators - prefix and infix. The prefix operators precede expressions and infix occur in between the two expressions, e.g., in !foo we have the prefix operator ! coming before the expression foo and in 2 + 3 we have the infix operator + between expressions 2 and 3.
Operators are like normal functions except that they have a different syntax for application (aka calling), whilst functions are applied to an arbitrary number of arguments using a simple syntax of juxtaposition, e.g., f x1 x2 x3 x41, operators can have only one (prefix) or two (infix) arguments. Prefix operators are very close to normal functions, cf., f x and !x, but they have higher precedence (bind tighter) than normal function application. Contrary, the infix operators, since they are put between two expressions, enable a more natural syntax, e.g., x + y vs. (+) x y, but have lower precedence (bind less tight) than normal function application. Moreover, they enable chaining several operators in a row, e.g., x + y + z is interpreted as (x + y) + z, which is much more readable than add (add (x y) z).
Operators in OCaml distinguished purely syntactically. It means that the kind of an operator is fully defined by the first character of that operator, not by a special compiler directive, like in some other languages (i.e., there is not infix + directive in OCaml). If an operator starts with the prefix-symbol sequence, e.g., !, ?#, ~%, it is considered as prefix and if it starts with an infix-symbol then it is, correspondingly, an infix operator.
The - and -. operators are treated specially and can appear both as prefix and infix. E.g., in 1 - -2 we have - occurring both in the infix and prefix positions. However, it is only possible to disambiguate between the infix and the prefix versions of the - (and -.) operators when they occur together with other operators (infix or prefix), but when we have a general expression, the - operator is treated as infix. E.g., max 0 -1 is interpreted as (max 0) - 1 (remember that operator has lower precedence than function application, therefore when they two appear with no parentheses then functions are applied first and operators after that). Another example, Some -1, which is interpreted as Some - 1, not as Some (-1). To disambiguate such code, you can use either the parentheses, e.g., max 0 (-1), or the prefix-only versions, e.g, Some ~-1 or max 0 ~-1.
As a matter of personal style, I actually prefer parentheses as it is usually hard to keep these rules in mind when you read the code.
1) Purists will say that functions in OCaml can have only one argument and f x1 x2 x3 x4 is just ((f x1) x2) x3) x4, which would be a totally correct statement, but a little bit irrelevant to the current discussion.

Time complexity of Simpson's rule for simple intergral calculus

I am looking for a reference and a proof for the time complexity of Simpson's rule for integral calculus.
I am not sure if the class complexity of that rule belongs to O(N).
Could you point me out to the right direction ?
Thanks
First of all, the Simpson's Rule requires three inputs:
The function f(x), assume it takes O(1) time.
The bounds of integration (a, b)
The number of subdivisions, n. Then the width of the "bar" d = (b - a) / n Note n must be an even positive integer.
Simpson's Rule states that
∫ab f(x) ≈ (d/3)([f(x0) + f(xn)] + [2f(x1) + 4f(x2)] + [2f(x3) + 4f(x4)] + ... [2f(xn-2) + 4f(xn-1)])
∫ab f(x) ≈ (d/3)([f(x0) + f(xn)] + ∑k=2(n-1)/2 f(xk)
where xk is equal to a + kd. Note x0 = a, xn = a + nd = b.
From the summation term ∑k=2(n-1)/2, we can easily state that there are [(n-1)/2 - 2 + 1] terms, and there are also two more terms for f(x0), f(xn). The number of terms used for the Simpson rule for a given n is linear to n.
Assuming multiplication is constant and the function complexity is constant, we note the summation formula to determine that the time complexity of the Simpson rule is O(n), it runs in linear time.

How can I prove this language is regular?

I'm trying to prove if this language:
L = { w={0,1}* | #0(w) % 3 = 0 } (number of 0's is divisble by 3)
is regular using the pumping lemma, but I can't find a way to do it. All other examples I got, have a simple form or let's say a more defined form such as w = axbycz etc.
I don't think you can use pumping lemma to prove that a language is regular. To prove a language is regular, you just need to give a regular expression or a DFA. In this case the regular expression is quite easy:
1*(01*01*01*)*
(proof: the regular expression clearly does not accept any string which has the number of 0's not divisible by 3, so we just need to prove that all possible strings which has the number of 0's divisible by 3 is accepted by this regular expression, which can be done by confirming that for strings that contain 3n 0's, the regular expression matches it since 1n001n101n201n3...01n3n-201n3n-101n3n has the same number of 0's and the nk's can be substituted so that it matches the string, and that this format is clearly accepted by the regular expression)
Pumping lemma cannot be used to prove that a language is regular because we cannot set the y as in Daniel Martin's answer. Here is a counter-example, in a similar format as his answer (please correct me if I'm doing something fundamentally different from his answer):
We prove that the language L = {w=0n1p | n ∈ N, n>0, p is prime} is regular using pumping lemma as follows: note that there is at least one occurrence of 0, so we take y as 0, and we have xykz = 0n+k-11p, which still satisfy the language definition. Therefore L is regular.
But this is false, since we know that a sequence with prime-numbered length is not regular. The problem here is we cannot just set y to any character.
Any string in this language with at least three characters in it has this property: either the string has a "1" in it, or there are three "0"s in a row.
If the string contains a 1, then you can split it as in the pumping lemma and set y equal to some 1 in the string. Then obviously the strings xyz, xyyz, xyyyz, etc. are all in the language because all those strings have the same number of zeros.
If the string does not contain a 1, it contains three 0s in a row. Setting y to those three 0s, it should be obvious that xyz, xyyz, xyyyz, etc. are all in the language because you're adding three 0 characters each time, so you always have a number of 0s divisible by 3.
#justhalf in the comments is perfectly correct; the pumping lemma can be used to prove that a regular language can be pumped or that a language that cannot be pumped is not regular, but you cannot use the pumping lemma to prove that a language is regular in the first place. Mea Culpa.
Instead, here's a proof that the given language is regular based on the Myhill-Nerode Theorem:
Consider the set of all strings of 0s and 1s. Divide these strings into three sets:
E0, all strings such that the number of 0s is a multiple of three,
E1, all strings such that the number of 0s is one more than a multiple of three,
E2, all strings such that the number of 0s is two more than a multiple of three.
Obviously, every string of 0s and 1s is in one of these three sets.
Furthermore, if x and z are both strings of 0s and 1s, then consider what it means if the concatenation xz is in L:
If x is in E0, then xz is in L if and only if z is in E0
If x is in E1, then xz is in L if and only if z is in E2
If x is in E2, then xz is in L if and only if z is in E1
Therefore, in the language of the theorem, there is no distinguishing extension for any two strings in the same one of our three Ei sets, and therefore there are at most three equivalence classes. A finite number of equivalence classes means the language is regular.
(in fact, there are exactly three equivalence classes, but that isn't needed)
A language is regular if and only if some nondeterministic finite automaton recognizes it.
Automaton is a finite state machine.
We have to build an automaton that regonizes L.
For each state, thinking like:
"Where am I?"
"Where can I go to, with some given entry?"
So, for L = { w={0,1}* | #0(w) % 3 = 0 }
The possibilites (states) are:
The remainder (rest of division) is 0, 1 or 2. Which means we need three states.
Let q0,q1 and q2 be the states that represent the remainderes 0,1 and 2, respectively.
q0 is the start and final state.
Now, for "0" entries, do the math #0(w)%3 and go to the aproppriated state.
Transion functions:
f(q0, 0) = q1
f(q1, 0) = q2
f(q2, 0) = q0
For "1" entries, it just loops wherever it is, 'cause it doesn't change the machine state.
f(qx, 1) = qx
The pumping lemma proves if some language is not regular.
Here is a good book for theory of computation: Introduction to the Theory of Computation 3rd Edition
by Michael Sipser.

Example of Non-Linear, UnAmbiguous and Non-Deterministic CFL?

In the Chomsky classification of formal languages, I need some examples of Non-Linear, Unambiguous and also Non-Deterministic Context-Free-Language(N-CFL)?
Linear Language: For which Linear grammar is possible( ⊆ CFG) e.g.
L1 = {anbn | n ≥ 0 }
Deterministic Context Free Language(D-CFG): For which Deterministic Push-Down-Automata(D-PDA) is possible e.g.
L2 = {anbncm | n ≥ 0, m ≥ 0 }
L2 is unambiguous.
A CF grammar that is not linear is nonlinear.
Lnl = {w: na(w) = nb(w)} is also a Non-Linear CFG.
--
3.
Non-Deterministic Context Free Language(N-CFG): For which only Non-Deterministic Push-Down-Automata(N-PDA) is possible e.g.
L3 = {wwR | w ∈ {a, b}* }
L3 is also Linear CFG.
--4. Ambiguous CFL: CFL for which only ambiguous CFG is possible
L4 = {anbncm | n ≥ 0, m ≥ 0 } U {anbmcm | n ≥ 0, m ≥ 0 }
L4 is both non-linear and Ambiguous CFG And Every Ambigous CFL \subseteq N-CFL.
My Question is:
Whether all non-linear, Non-Deterministic CFL are Ambiguous? If not then
I need a example that is non-linear, non-deterministic CFL and also unambiguous?
Given Venn-diagram below:
Also asked here
"IN CONTEXT OF CHOMSHY CLASSIFICATION OF FORMAL LANGUAGE"
(1) L3 = {wwR | w ∈ {a, b}* }
Language L3 is a Non-Deterministic Context Free Language, its also Unambiguous and Liner language.
(2) Lp is language of parenthesis matching. There are two terminal symbols "(" and ")".
Grammar for Lp is:
S → SS
S → (S)
S → ()
This language is nonlinear, deterministic and unambiguous too.
Language L that is union of Lp and L3 is unambiguous, nonlinear (due to Lp), and non-deterministic (due to L3) (Assuming language symbols for both languages are different).
This Language is an example of language in Venn-diagram for which I marked ??.
Also the correct diagram is below:
An ambiguous context free language also be a liner context free

Modular arithmetic

I'm new to cryptography and modular arithmetic. So, I'm sure it's a silly question, but I can't help it.
How do I calculate a from
pow(a,q) = 1 (mod p),
where p and q are known? I don't get the "1 (mod p)" part, it equals to 1, doesn't it? If so, than what is "mod p" about?
Is this the same as
pow(a,-q) (mod p) = 1?
The (mod p) part refers not to the right hand side, but to the equality sign: it says that modulo p, pow(a,q) and 1 are equal. For instance, "modulo 10, 246126 and 7868726 are equal" (and they are also both equal to 6 modulo 10): two numbers x and y are equal modulo p if they have the same remainder on dividing by p, or equivalently, if p divides x-y.
Since you seem to be coming from a programming perspective, another way of saying it is that pow(a,q)%p=1, where "%" is the "remainder" operator as implemented in several languages (assuming that p>1).
You should read the Wikipedia article on Modular arithmetic, or any elementary number theory book (or even a cryptography book, since it is likely to introduce modular arithmetic).
To answer your other question: there is no general formula for finding such an a (to the best of my knowledge) in general. Assuming that p is prime, and using Fermat's little theorem to reduce q modulo p-1, and assuming that q divides p-1 (or else no such a exists), you can produce such an a by taking a primitive root of p and raising it to the power (p-1)/q. [And more generally, when p is not prime, you can reduce q modulo φ(p), then assuming it divides φ(p) and you know a primitive root (say r) mod p, you can take r to the power of φ(p)/q, where φ is the totient function -- this comes from Euler's theorem.]
Not silly at all, as this is the basis for public-key encryption. You can find an excellent discussion on this at http://home.scarlet.be/~ping1339/congr.htm#The-equation-a%3Csup%3Ex.
PKI works by choosing p and q that are large and relatively prime. One (say p) becomes your private key and the other (q) is your public key. The encryption is "broken" if an attacker guesses p, given aq (the encrypted message) and q (your public key).
So, to answer your question:
aq = 1 mod p
This means aq is a number that leaves a remainder of 1 when divided by p. We don't care about the integer portion of the quotient, so we can write:
aq / p = n + 1/p
for any integer value of n. If we multiply both sides of the equation by p, we have:
aq = np + 1
Solving for a we have:
a = (np+1)1/q
The final step is to find a value of n that generates the original value of a. I don't know of any way to do this other than trial and error -- which equates to a "brute force" attempt to break the encryption.