Create a DFA such that L subscript 4 = {0,1}* - {0,01}* and list the first five strings in lexicographic order.
I'm having trouble deducing what L subscript 4 implies, does it be a language of strings with length 4? Also, when we subtract two languages, could we choose the string "1" subtracted from the empty string, meaning can be choose the first {0,1}* to be of length 1 subtracted from {0,01}* of length 0?
The language {0,01}* is the set of strings that before each 1 there should be one 0. For example 00010001 is in this language but 10000 or 0001111 are not. {0,1}* is on the other hand, is all the strings with alphabet {0,1}. Now subtracting these results a language where at least has one 1 and all the 1's come before any 0. For example 1111000 is in L_4 but 000000 or 011111 are not.
The notation {0, 1}* - {0, 01}* is set subtraction - it means "the language of all strings in {0, 1}* that aren't in {0, 01}*." When they write
L4 = {0, 1}* - {0, 01}*
I believe they're defining a new language called L4 that's equal to the above expression.
To solve this problem, you might find it useful to notice that L4 is the complement of the language {0, 01}*. One way to solve this problem would be to build a DFA for {0, 01} * and to then flip all the accepting and rejecting states. That will give you a DFA for L4.
I'll leave the rest of the problem as an exercise to the reader. :-)
Related
How many inversions are there in a binary string of length n ?
For example , n = 3
000->0
001->0
010->1
011->0
100->2
101->1
110->2
111->0
So total inversions are 6
The question looks like a homework, that's why let me omit the details. You can:
Solve the problem as a recurrency (see Толя's answer)
Make up and solve the characteristic equation, get the solution as a close formula with some arbitrary constants (c1, c2, ..., cn); as the matter of fact you'll get just one unknown constant.
Put some known solutions (e.g. f(1) = 0, f(3) = 6) into the formula and find out all the unknown coefficients
The final answer you should get is
f(n) = n*(n-1)*2**(n-3)
where ** means raising into power (2**(n-3) is 2 in n-3 power). In case you don't want to deal with recurrency and the like stuff, you can just prove the formula by induction.
It is easy recurrent function.
Assume that we know answer for n-1.
And after ato all previous sequences we add 0 or 1 as first character.
if we adding 0 as first character that mean that count of inversions will not be changed: hence answer will be same as for n-1.
if we adding 1 as first character that mean count of inversions will be same as before and will be added extra inversion equals to count of 0 into all previous sequences.
Count of zeros ans ones in sequences of length n-1 will be:
(n-1)*2^(n-1)
Half of them is zeros it will give following result
(n-1)*2^(n-2)
It means that we have following formula:
f(1) = 0
f(n) = 2*f(n-1) + (n-1)*2^(n-2)
Given a set A of n positive integers a1, a2,... a3 and another positive integer M, I'm going to find a subset of numbers of A whose sum is closest to M. In other words, I'm trying to find a subset A′ of A such that the absolute value |M - Σ a∈A′| is minimized, where [ Σ a∈A′ a ] is the total sum of the numbers of A′. I only need to return the sum of the elements of the solution subset A′ without reporting the actual subset A′.
For example, if we have A as {1, 4, 7, 12} and M = 15. Then, the solution subset is A′ = {4, 12}, and thus the algorithm only needs to return 4 + 12 = 16 as the answer.
The dynamic programming algorithm for the problem should run in
O(nK) time in the worst case, where K is the sum of all numbers of A.
You construct a Dynamic Programming table of size n*K where
D[i][j] = Can you get sum j using the first i elements ?
The recursive relation you can use is: D[i][j] = D[i-1][j-a[i]] OR D[i-1][j] This relation can be derived if you consider that ith element can be added or left.
Time complexity : O(nK) where K=sum of all elements
Lastly you iterate over entire possible sum you can get, i.e. D[n][j] for j=1..K. Which ever is closest to M will be your answer.
For dynamic algorithm, we
Define the value we would work on
The set of values here is actually a table.
For this problem, we define value DP[i , j] as an indicator for whether we can obtain sum j using first i elements. (1 means yes, 0 means no)
Here 0<=i<=n, 0<=j<=K, where K is the sum of all elements in A
Define the recursive relation
DP[i+1 , j] = 1 , if ( DP[i,j] == 1 || DP[i,j-A[i+1]] ==1)
Else, DP[i+1, j] = 0.
Don't forget to initialize the table to 0 at first place. This solves boundary and trivial case.
Calculate the value you want
Through bottom-up implementation, you can finally fill the whole table.
Now, things become easy. You just need to find out the closest value to M in the table whose value is one.
Here, just work on DP[n][j], since n covers the whole set. Find the closest j to M whose value is 1.
Time complexity is O(kn), since you iterate k*n times in total.
Given a sequence a1a2....a_{m+n} with n +1s and m -1s, if for any 1=< i <=m+n, we have
sum(ai) >=0, i.e.,
a1 >= 0
a1+a2>=0
a1+a2+a3>=0
...
a1+a2+...+a_{m+n}>=0
then the number of sequence that meets the requirement is C(m+n,n) - C(m+n,n-1), where the first item is the total number of sequence, and the second term refers to those sub-sum < 0.
I was wondering whether there is a similar formula for the bi-side sequence number :
a1 >= 0
a1+a2>=0
a1+a2+a3>=0
...
a1+a2+...+a_{m+n}>=0
a_{m+n}>=0
a_{m+n-1}+a_{m+n}>=0
...
a1+a2+...+a_{m+n}>=0
I feel like it can be derived similarly with the single-side subsum problem, but the number C(m+n,n) - 2 * C(m+n,n-1) is definitely incorrect. Any ideas ?
A clue: the first case is a number of paths (with +-1 step) from (0,0) to (n+m, n-m) point, where path never falls below zero line. (Like Catalan numbers for parenthesis pairs, but without balance requirement n=2m)
Desired formula is a number of (+-1) paths which never rise above (n-m) line. It is possible to get recursive formulas. I hope that compact formula exists for it.
If we consider lattice path at nxm grid, where horizontal step for +1 and vertical step for -1, then we need a number of paths restricted by parallelogramm with (n-m) base
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I just started learning about formal lang and automata theory, and recently learned about regex, so I don't know any complicated symbols, so please stick with basic symbols.
The question is: Write a regex for the following language over {0, 1} that is a set of all odd length strings that contain exactly two 0s.
I've got the first part finished (the odd part), it should be:
(0+1)[(0+1)(0+1)]* ( + is the same as | (or) I believe, we learnt it as +)
However, when I think about having exactly two 0s it gets really messed up. I can only see that I can use * with 1 only since # of 0s are limited to 2.
But if I do (11)* , I can't get the permutation of 0s inside the 1s. (e.g. can't get 10101 with (11)*).
What I know:
Only 1s can use *
In the regex only two 0s will be used
The way to make odd length is to add an odd length to an even length (even length needs to have empty string within it's set)
Odd length should not use * since 2 odd = even, so only even length can use *.
For possible hints or answer, please use 0, 1, +/|, *, (, ) only. Some other expressions I will not be able to understand.
Regular Language over {0, 1} that is a set of all odd length strings that contain exactly two 0.
What this language means?
Note language string can be consist of two 0 and any number of 1 such that total length of string is odd. There is no other restriction. 1 and 0 an appear in any order and in any pattern.
As we know even + odd = odd. So in string is consist of at least three length and odd number of 1 because number of 0 in string is two.
So regular expression should be something like: A 0 B 0 C , where A, B, C, are substrings consist of only 1 and total number of 1 in A, B, C is odd, hence all can't be ^ (nul) in a expression.
Now because total number of 1 in A, B, C = odd, so it can be something like: either(1) two even and one odd or (2) all three are odd.
Note: a odd length string can't be null.
Regular Expression:
1(11)*01(11)*01(11)* + 1(11)*0(11)*0(11)* + (11)*01(11)*0(11)* + (11)*0(11)*01(11)*
// all odd A odd, B C even B odd, A C even A B even, C odd
Let's say, I have to model a checkerboard and I want to say that at least 5 squares on the "A" vertical are empty. How do I do that in Alloy? Any other example with numbers different from 0 or 1 would be good. In other words, what do I do when "some" is not precise enough?
Thanks!
You can use the cardinality operator (#) to make assertions about the number of tuples in a relation, e.g.,
#r >= 5
says that the relation r must have at least 5 tuples.
You can also use the cardinality operator with an arbitrary expression, e.g.,
#board.cells >= 5
or
#{c: Cell | c in board.cells and ...} >= 5