string recursive description from a definition - recursion

So, I have this description for a string: S consists of all strings of a's, b's and c's where an increasing number of a's come before an increasing number of b's and c's or c's and b's. does this make sense for this recursive definition of the set S over {a,b,c}: 1. a is in S and 2. if x in S then ax, xb, xc are in S?

It makes sense but is incomplete. The string in question follows this regex:
a+[bc]*
So, you need to be clear that there is always at least one a, and the b's and c's can intermix.

Related

Alloy specifications

I'm a beginner learning Alloy, I want to know what means this n.^address (maybe with an example)? because logically if we consider address as a set of pairs like (A0,A1), then how we can join n which is for example N0 with this set?! since the left most element of pairs in address are not the same nature with n, I supposed that it would not possible.
I'd really appreciate if anyone can guide me
It's been a while since I used alloy, but the ^ operator represents the transitive closure of its operand relation. So if address is {(a,b), (b,c)} then ^address is {(a,b), (b,c), (a,c)}.
n.^address is the projection of the new relation on n.
So if n is a, then n.^address is {b,c}
Example:
abstract sig atom{
address: lone atom
}
one sig a,b,c extends atom{}
fact {
address = a->b + b->c
}
check {
a.^address = b+c
}
You ask "what means this n.^address?"
The expression n.^address is a join between the set of tuples denoted by n and the set of tuples denoted by ^address.
The expression ^address, in turn, denotes the transitive closure of the relation address, i.e. the smallest relation containing address which is transitive.
Whether there is in fact, or can be in principle, any tuple in n whose rightmost value is the same as the leftmost value of some tuple in ^address -- or, said another way, whether the expression n.^address is guaranteed to denote the empty set or not -- depends partly on how the variable n and the relation address are defined and partly on how the universe is populated. The same is true for whether the transitive closure of address is the same as address or a larger relation.
If N0, A0, and A1 are all atoms, and if the relation address contains only the pair (A0, A1), and the expression n denotes (the singleton set containing) the atom N0, then indeed the expression n.^address will denote the empty set. If on the other hand address contains a tuple (N0, A0) as well as the tuple (A0, A1), then
the expression address denotes the singleton set containing the tuple (A0, A1),
the expression ^address also denotes the singleton set containing the tuple (A0, A1), and
the expression n.^address denotes the singleton set containing the tuple (N0, A1), because the join of the set {(N0, A0)} with the set {(A0, A1)} is the set {(N0, A1)}.
Since you don't provide any more information about the Alloy model you have in mind, it's not possible to say much more.

DFA + counter with multiplication, addition, brackets

I'm looking at an exam question which says
'Explain how a correctly formed arithmetic expression over variables a, b, c that contains additions, multiplication and brackets can be recognised by a DFA with a counter. (Such a DFA can increment and decrement the counter on each transition as well as test it for zero).'
I'm not quite sure I understand what it's actually asking. If someone could provide a hint that would be greatly appreciated. (i.e. what the multiplication of a and b would be for example)
If all you want is just a hint, then I think this should be enough:
Some strings represent valid arithmetic expressions such as
a + b
(a + b)*(c + d)
and some are invalid such as
a + - b
a b
)
()
(a + b)*)c+d(
"can be recognised by a DFA with a counter" here means that you can build a Deterministic finite automaton such that it stops at one of the "accept states" after processing a sting if and only if that string represents a valid arithmetic expression
P.S. Additional hint: the counting part is important here because a simple DFA can't even recognize whether an arbitrary string of brackets is properly matched.

Recursive definition of set strings over {a,b} that contains one b and even number of a's before the first b

I'm working on a problem from the Languages and Machines: An Introduction to the Theory of Computer Science (3rd Edition) in Chapter 2 Example 6.
I need help finding the answer of:
Recursive definition of set strings over {a,b} that contains one b and even number of a's before the first b?
When looking for a recursive definition, start by identifying the base cases and then look for the recursive steps - like you're doing induction. What are the smallest strings in this language? Well, any string must have a b. Is b alone a string in the language? Why yes it is, since there are zero as that come before it and zero is an even number.
Rule 1: b is in L.
Now, given any string in the language, how can we get more strings? Well, we can apparently add any number of as to the end of the string and get another string in the language. In fact, we can get all such strings from b if we simply allow you to add one more a to the end of a string in the language. From x in L, we therefore recover xa, xaa, ..., xa^n, ... = xa*.
Rule 2: if x is in L then xa is in L.
Finally, what can we do to the beginning of strings in our language? The number of as must be even. So far, rules 1 and 2 only allow us to construct strings that have zero as before the b. We should be able to get two, four, six, etc., all the even numbers, of as. A rule that lets us add two as to any string in our language will let us add ever more as to the beginning while maintaining the evenness property we require. Starting with x in L, we recover aax, aaaax, ..., (aa)^(2n)x, ... = (aa)*x.
Rule 3: if x is in L, then aax is in L.
Optionally, you may add the sometimes implicitly understood rule that only those things allowed by the aforementioned rules are allowed. Otherwise, technically anything is allowed since we haven't explicitly disallowed anything yet.
Rule 4: Nothing is in L unless by virtue of some combination of the rules 1, 2 and/or 3 above.

derangements and permutations in cryptography

i have a problem that i am having a bit of trouble with;
we are given a partial key (missing 11 letters) for a mono-alphabetic substitution cipher and asked to calculate the number of possible keys given that no plaintext letter can be mapped to itself.
ordinarily, the number of possible keys would be the number of derangements of the missing letters (!11), however 5 of the plaintext letters that are missing mappings already exist as mappings in the partial key, so logically it shouldnt matter what the mapping of those plaintext letters is, because they can never map to themselves.
so shouldnt the number of possible keys be 5! * !6, ie. (the number of permutations of the 5 already mapped free letters) * (the number of derangements of the remaining 6)?
the problem is that 5! * !6 = 31800 which is much less than !11 = 14684570
intuitively the set of derangements should be a smaller subset of !11, shouldnt it?
am i just getting something wrong in my arithmetic? or am i completely missing the concepts? any help would be greatly appreciated
thanks gus
ps. i know this isn't strictly a programming question, but it is a computing question and related to a programming project, so i thought it might be pertinent. also, i posted it on math.stackexchange.com yesterday but havent had any responses yet..
EDIT: corrected the value of !11
I think your problem can be rephrased as the following:
How many permutation has a list with elements a_0, a_1, ... a_n-1, b_0, b_1, ..., b_m-1, in which no a_k element is at position k? (Let us denote this number with p_{n,m} - your specific question is the value of p_{6,5}.)
Please note that your suggested formula 5!*!6 is not correct because of the following:
it only counts the cases, where the a_ks are in the first 6 positions (without any of them being in the position of its own index), and the b_ks on the last 5.
You do not count any other configurations like: a_3, b_4, b_1, a_0, a_5, b_0, a_2, b_2, b_3, a_1, a_4, where the order is totally mixed.
Your other idea about the result being a subset of the !11-element derangement on all the elements is also not correct, as any of the b_ks can be at any position.
However, we can easily add a recursive formula for p_{n,m} by separating it into two cases based on the position of a_0.
If a_0 gets in one of the positions 1, 2, ..., n-1. (n-1 different possibilities.)
This means that neither a_0 is at position 0, and it also prevents another a_k from being at position k by occupying that position. Thus this a_k becomes 'free', it can go to any other positions. If a_0 gets fixed this way, the other elements can be permutated in p_{n-2,m+1} different ways.
If a_0 gets in one of the positions n, n+1, ..., n+m-1. (m different possibilities.)
This way no other a_k gets prevented to be at the position corresponding to it's index. The other elements can be permutated in p_{n-1,m} different ways.
Adding this together gives the recursion: p_{n,m} = (n-1)*p_{n-2,m+1} + m*p_{n-1,m}. The halting conditions are p_{0,m}=m! for every m, as it means, that each element can be at any location.
I also coded it in python:
import math
def derange(n,m):
if n<0:
return 0
elif n==0:
return math.factorial(m)
else:
return (n-1)*derange(n-2, m+1) + m*derange(n-1, m)
print derange(6,5)
gives 22852200.
If you are interested in the general case, you can find some related sequences on OEIS.
The search term 'differences of factorial numbers' can be interesting, e.g. in triangular form: http://oeis.org/A047920.
There is also an article mentioned there: http://www.pmfbl.org/janjic/enumfun.pdf, maybe it can help if you are interested in a generic closed formula for n and m.
Suddenly I didn't have any good idea to come up with, but I think this can be a good starting point.

regular languages with concatenations

the regular languages are closed under the operation:
init(L) = the set of the strings w such that for some x, wx is in L.
EDIT :
x can be any string, a character or empty string
How can I prove that ?
OK, misread the quesion on the first time, now I get it. It still trivial. Looking at the automate what you searching is a partion of the automate into two state sets S1 and S2, so that just one transition is between them (and if its from S1->S2 S1 contains of course the start node, and S2 the end node). Such exist always (exception empty language), in case there is no such node you can add one, so w is just a set containing the empty word, which is of course also regular (as well as the empty language case).
Unless I'm misunderstanding, the answer is that you can't. Because it's not true.
First, let's consider the language L = {aa, bb, cc} and alphabet {a, b, c}
So, init(L) = {a, b, c}. However, each of the elemnts in init(L) are not in L.
Edit: If we are concatenating empty characters, then init(L) = {a, b, c, aa, bb, cc}. Which is still not equal to L.
A language is regular iff there's a finite-state automaton that recognizes it. So, suppose L is a regular language and let A be an automaton that recognizes it. Now, say that a state of A is "good" if there's some set of possible transitions starting there and ending in the "accept" state. Define a new automaton A' in which all transitions to "good" states are replaced by direct transitions to the accept state. Then the language recognized by A' is exactly init(L).
I think it's a new DFA B, that makes all the state of A(the original DFA) that can reach to the final states of A the final state of B.

Resources