Given
G = {a, b, c, d}, {S, X, Y}, S, {S->XY, X->aXb, X->ab, Y->cYd, Y->cY, Y->cd}}
Prove that |w|c-|w|d+|w|a≥|w|b
|w|a is how many 'a's there are in the string. This makes sense that there will be more (or the same amount of) 'c's than 'd's as there is no production rule that makes a d without making a c while 'c's can be made without 'd's using Y->cY. I need to formally prove this using induction on the number of derivation steps and have been trying all day. Any help appreciated.
We will use this grammar
S -> XY
X -> aXb
X -> ab
Y -> cYd
Y -> cY
Y -> cd
And show the language of the grammar has |w|c - |w|d + |w|a ≥ |w|b. The proof is by induction on the number of applications of the productions X -> aXb, Y -> cYd and Y -> cY.
Base case: there is only one string derived without invoking these productions, namely, abcd, and it satisfies |w|c - |w|d + |w|a >= |w|b since 1 - 1 + 1 >= 1.
Induction hypothesis: assume the claim holds for all strings derived using up to and including k applications of one of the three productions given above.
Induction step: we show that the claim holds for strings derived by k+1 applications of the productions. Since each of our productions has the same type and number of nonterminals in the LHS and RHS, and since the RHS also contains terminals in all cases, there is a shorter string in the language derived with at least one fewer application of these productions. By the hypothesis, that shorter string has |w|c - |w|d + |w|a >= |w|b. For our string, there are three cases:
X -> aXb was the k+1st production applied. This increases |w|a and |w|b by 1, and we have |w|c - |w|d + |w|a + 1 >= |w|b + 1 iff |w|c - |w|d + |w|a >= |w|b, which we have already determined.
Y -> cYd was the k+1st production applied. This increases |w|c and |w|d by 1, and we have |w|c + 1 - (|w|d + 1) + |w|a >= |w|b which holds iff |w|c - |w|d + |w|a >= |w|b, which we have already determined.
Y -> cY was the k+1st production applied. This increases |w|c by 1, giving |w|c + 1 - |w|d + |w|a >= |w|b which is true since |w|c + 1 - |w|d + |w|a > |w|c - |w|d + |w|a >= |w|b.
In each of the three possible cases of the choice for the k+1st production, the string derived in k+1 productions maintains the property. By induction, the property holds for all numbers n of applications of these productions.
Related
Guys this is the function to implement ceil function Which is working fine,I want to ask what is the logic behind subtracting -1 from denominator ?
I am new to programming
please Help
int checkceil(int numerator,int denominator){
return (numerator+denominator-1)/denominator;
}
Since dividing two integers a and b will always floor (truncate) the result, we need to find some d (delta) such that floor(d + a/b) == ceil (a/b).
How do we find d? Think about it this way:
ceil(a/b) > floor(a/b), except when (a/b) is a whole number. So, we want to bump (a/b) to (or past) the next whole number, unless (a/b) is a whole number, by adding d. This way floor(a/b + d) will be equal to ceil(a/b). We want to find d so that for whole numbers, it won’t quite push them up to the next whole number, but for non-whole numbers it will.
So how much d is just enough?
So assuming (a/b) is not a whole number, the smallest leftover we can have is (1/b). So in order to bump (a/b) to the next whole number, it suffices to add d = 1 - (1/b). This is less than 1, which will not bump (a/b) to the next whole number in case (a/b) is a whole number, but still enough to bump (a/b) to the next whole number in case (a/b) is not a whole number.
Summing it up, we know adding d = 1 - (1/b) to (a/b) will fulfill the equality:
floor(a/b + d) = ceil(a/b). Thus we get:
ceil(a/b) = floor(a/b + d) = floor(a/b + 1 - 1/b) = floor((a + b - 1)/b)
When we write in terms of code it will be:
int myceil = (a + b - 1)/b;
The numerator n is of form n = a * d + b, where b is the remainder of n / d. The remainder is by definition smaller than d.
In C, the division of n/d returns the integral part of a. When b == 0, one can add only d - 1 to n to get the same result (ad + 0)/d == (ad + d-1)/d. For all other remainders 0<b<d, the division returns the next integer a+1, ie the ceiling.
I have a set of data points, which are complex numbers which have, let’s say, 8 rows and 6 columns. If you look at the program, you can see that data sets are arranged very randomly. 1st, 2nd, 3rd columns are okay, but 4th column, is bit anomalous. What I mean to say is that after element at B(5, 4) (i.e. 25.9868674011374) it is coming 2.74257567017122 [B(6, 4)] instead of 26.8410063269595 which has gone to B(6, 6). I want that column arranged in ascending order (every sorting should be done for real part). Also, in the 5th column, even though the real parts ae arranged in ascending order, the imaginary part are kind of swapped. For example, after B(2,5) it should be B(3,6) not B(3,5). I have tried ‘sort’ command, but it not working as I want.
For the people who is wondering wht is the problem with sort, just try to run this command:
A = [1+2i 3+i i 0 -i]; Dreal= sort(real(A)); Dimg = sort(imag(A));D=[Dreal;Dimg];
What I want is -i, 0, i ,3+I,1+2i, what sort gives is something else.
Also, there may be instances where 4th, 5th and 6th column may behave ‘normally’, but 1st, 2nd, 3rd columns behaving erratically. So even if 4th, 5th and 6th columns are behaving ‘normally’ I want it to be sorted down in ascending order. Any way through this? By the by, I am using Matlab- 2015b. The code is:
clear all; clc;
B=[-2.14981736484179 + 0.00000000000000i,-1.38134547606946 + 0.00000000000000i,1.38451324569297 + 0.00000000000000i,22.5759136576435 + 0.00000000000000i,2.28536796878740 + 0.333911501246080i,2.28536796878740 - 0.333911501246080i;-2.22047322414157 + 0.00000000000000i,-1.43596350944258 + 0.00000000000000i,1.43889226552228 + 0.00000000000000i,23.4278498788255 + 0.00000000000000i,2.39484729461819 + 0.303429715954385i,2.39484729461819 - 0.303429715954385i;-2.29148887606605 + 0.00000000000000i,-1.49057388951113 + 0.00000000000000i,1.49328382360683 + 0.00000000000000i,24.2803021611395 + 0.00000000000000i,2.50423839041542 - 0.265225265037282i,2.50423839041542 + 0.265225265037282i;-2.36284412024645 + 0.00000000000000i,-1.54517861014711 + 0.00000000000000i,1.54768832224205 + 0.00000000000000i,25.1333019698605 + 0.00000000000000i,2.61351621914550 + 0.215386193278572i,2.61351621914550 - 0.215386193278572i;-2.43451884781340 + 0.00000000000000i,-1.59977935450756 + 0.00000000000000i,1.60210590260749 + 0.00000000000000i,25.9868674011374 + 0.00000000000000i,2.72266244928806 - 0.142395604889199i,2.72266244928806 + 0.142395604889199i;-2.50649346218904 + 0.00000000000000i,-1.65437753673930 + 0.00000000000000i,1.65653651325774 + 0.00000000000000i,2.74257567017122 + 0.00000000000000i,2.92075248853987 + 0.00000000000000i,26.8410063269595 + 0.00000000000000i;-2.57874914363635 + 0.00000000000000i,-1.70897433922564 + 0.00000000000000i,1.71097996266870 + 0.00000000000000i,2.74509085049068 + 0.00000000000000i,3.13593380433481 + 0.00000000000000i,27.6957188653678 + 0.00000000000000i;-2.65126800477853 + 0.00000000000000i,-1.76357074549849 + 0.00000000000000i,1.76543595943333 + 0.00000000000000i,2.78382099280761 + 0.00000000000000i,3.31458248082037 + 0.00000000000000i,28.5509993172157 + 0.00000000000000i];
k=1:1:4;
B1=sort(real(B));
B2=sort(imag(B));
B3=sort(B);
Thanks in advance.
You seem to be wanting the sort(A,'ComparisonMethod','real') feature available in Matlab r2017a, but for use in r2015b.
You can recreate the feature using the sortrows function:
clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [real(A); imag(A)];
[~,I] = sortrows(AB');
Asort = A(I)
yields Asort = [0 - 1i 0 + 0i 0 + 1i 1 + 2i 3 + 1i]. sortrows sorts each row of an array first according to the first column, then breaks ties with the next column and so on until the whole array is sorted (Documentation here). The idea here is to make the first column the real component, and the second column the imaginary component.
For higher-dimensional arrays of complex numbers, you have a few options, but they'll all use the same principle as above. If you want to sort all elements of your matrix B, you can generate a sorted vector use the following code:
Bsep = [real(B(:)),imag(B(:))];
[~,I] = sortrows(Bsep);
SortedVec = B(I);
To reshape back into an 8x6 array (with sorted elements arranged down columns, from left to right), simply add this line:
Bsort = reshape(SortedVec,8,6);
Or, perhaps you want to sort each column of your array separately. Just use sortrows in a loop:
nCols = size(B,2);
Bsort = NaN(size(B));
for j = 1:nCols
Bsep = [real(B(:,j)),imag(B(:,j))];
[~,I] = sortrows(Bsep);
Bsort(:,j) = B(I,j);
end
If you instead want to sort by the imaginary part first, then break ties with the real part, all you need to do is switch the position of the calls to real and imag in any of the code snippets above. For example,
clear
A = [1+2i 3+1i 1i 0 -1i];
AB = [imag(A); real(A)]; % note that real and imag are switched
[~,I] = sortrows(AB');
Asort = A(I)
yields Asort = [0 - 1i 0 + 0i 0 + 1i 3 + 1i 1 + 2i]
What is the mathematical function that gives the address of an array with more than 3 dimensions?
I already know that for an array a[x][y] with 2 dimensions is (a (x * max_second_dimension + y) * byte)
Given an array a[N1][N2][N3]...[Nk] the address of the element a[i1][i2]...[ik] would be equal to:
a +
+ i1 * (N2 * N3 * ... * Nk) +
+ i2 * (N3 * N4 * ... * Nk) +
+ i3 * (N4 * N5 * ... * Nk) +
...
+ i(k - 1) * Nk +
+ ik
Here what follows i and N are indexes (and so is (k - 1) in i(k - 1).
let dimension of array be lxbxhxg which means array has declared using
data_type array[l][b][h][g];
if you want address of cell at array[x][y][z][a] then it will be
array + (x*b*h*g + y*h*g + z*g + a) * sizeof(array[x][y][z][a])
There is a finite formula for array accessors of arbitrary dimensionality, which I will explain below.
But before, you should know that formal languages usually handle multi dimensional arrays in a grammar rule by accessing the topmost array over an index multiplied by the inner array size - which, in turn, results from a recursion. This may end up in another array, accessed by an index multiplied by the inner array size, with a specific offset and so on, until the accessed element is not an array. You should have a look at (e. g.) ANTLR, if you want to understand how formal languages work.
Of course, you can always find a specific expression, if you know the count of dimensions and the size of each dimension. For example: Let a be the address of the array, s an array of dimension sizes, d the number of dimensions, c an array of indexes (the coordinates in the space described by the array) and e the element size. The address would evaluate in pseudocode to:
a + e * sum(i := 0, d - 1, c[i] * prod(j := i + 1, d - 1, s[j]))
where i, j are loop variables and sum, prod are the big sum/product operator with corresponding lower limit in the first and upper limit in the second parameter. Please note, the empty product operator (last iteration) results to 1. I Haven't tested the above, but the principle should be clear. Sorra, but I don't seem to be able to post formulas in mathematical notation here.
I am looking for the formulas to find the memory location of an element in a 3-D Array for row major and for column major. After using my logic I end up with the following formulas.
say array is A[L][M][N].
row-major:Loc(A[i][j][k])=base+w(M*N(i-x)+N*(j-y)+(k-z))
column-major:Loc(A[i][j][k])=base+w(M*n(i-x)+M*(k-z)+(j-y))
where x, y, z are lower bounds of 1st(L) 2nd(M) and 3rd(N) index.
I tried this formula and got the correct result but when I applied this formula on a Question in the book then the answer did not match. Please can anyone help me out with this.
Formula for 3D Array
Row Major Order:
Address of
A[I, J, K] = B + W * [(D - Do)*RC + (I - Ro)*C + (J - Co)]
Column Major Order:
Address of
A[I, J, K] = B + W * [(D - Do)*RC + (I - Ro) + (J - Co)*R]
Where:
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
R = Row (total number of rows)
C = Column (total number of columns)
D = Width (total number of cells depth-wise)
Ro = Lower Bound of Row
Co = Lower Bound of Column
Do = Lower Bound of Width
Right one is:
row-major:Loc(A[i][j][k])=base+w(N*(i-x)+(j-y)+M*N(k-z))
column-major:Loc(A[i][j][k])=base+w((i-x)+M*N(k-z)+M*(j-y))
Thanks! #Vinay Yadav for your comment. As suggested by Vinay please visit the link to understand this in great detail: https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays.
Keep this in mind and you never get it wrong:
Row Major: Lexicographical Order
Column Major: Co-lexicographical Order
If you don't know what Co-lexicographical and Lexicographical are: Check out this Wikipedia page for more. Let me highlight important part for you, do give it a read:
The words in a lexicon (the set of words used in some language) have a
conventional ordering, used in dictionaries and encyclopedias, that
depends on the underlying ordering of the alphabet of symbols used to
build the words. The lexicographical order is one way of formalizing
word order given the order of the underlying symbols.
The formal notion starts with a finite set A, often called the
alphabet, which is totally ordered. That is, for any two symbols a and
b in A that are not the same symbol, either a < b or b < a.
The words of A are the finite sequences of symbols from A, including
words of length 1 containing a single symbol, words of length 2 with 2
symbols, and so on, even including the empty sequence varepsilon with no symbols
at all. The lexicographical
order on the set of all these finite words orders the words as
follows:
Given two different words of the same length, say a = a1a2...ak and b
= b1b2...bk, the order of the two words depends on the alphabetic order of the symbols in the first place i where the two words differ
(counting from the beginning of the words): a < b if and only if ai <
bi in the underlying order of the alphabet A. If two words have
different lengths, the usual lexicographical order pads the shorter
one with "blanks" (a special symbol that is treated as smaller than
every element of A) at the end until the words are the same length,
and then the words are compared as in the previous case.
After this you can learn about Co-Lexicographical Order from the same Wikipedia page mentioned above. Above quoted part is taken directly from the Motivation and Definition titled part of the Wikipedia page mentioned above. Visit it once and you will have a better understanding of both.
You just need to find the Lexicographical and Co-Lexicographical position of (i, j, k) among all possible (foo1, foo2, foo3) in the array A of yours:
foo1 -> L possibilities: [Lower Bound x, Upper Bound x + L - 1]
foo2 -> M possibilities: [Lower Bound y, Upper Bound y + M - 1]
foo3 -> N possibilities: [Lower Bound z, Upper Bound z + N - 1]
Based on the this knowledge, you will get that:
1). Number of elements A[foo1][foo2][foo3] (foo1, foo2, foo3) present before element A[i][j][k] (i, j, k) in Row Major Order or Lexicographical Order are:
[ (i - x)*M*N + (j - y)*N + (k - z) ]
2). Number of elements A[foo1][foo2][foo3] (foo1, foo2, foo3) present before element A[i][j][k] (i, j, k) in Column Major Order or Co-lexicographical Order are:
[ (i - x) + (j - y)*L + (k - z)*L*M ]
Now, you can do the rest of your calculation where you bring in your base and W thing to get the final answer you need.
Find the number of functional dependencies in a relation having n attributes?
On first thought , I figure out that left hand side can have N+1 possibilities(null can also be there.) and similarly N+1 possibility for right hand side.
Hence total no of FD should be
(n+1)*(n+1) - 1
But ans given is 2^(n+1) .
On analyzing the answer , We can see they are not including trivital ones like ABC -> A etc .
So what should be the correct ans ???
2^n possible attribute sets on the LHS, and again 2^n possible attribute sets on the RHS. Both counts include the empty set.
Number of possible distinct pairs between those is 2^n * 2^n.
While technically correct, this answer also implies that FDs such as {AB} -> {} are also considered. How many of those are there ? For each cardinality l of the LHS, there are 2^l possible subsets, each of those giving a trivial FD if it appears on the RHS. So the number of trivial FDs is 2^0 + 2^1 + 2^2 + ... + 2^n = 2^(n+1) - 1. Leaving in total 2^(2*n) + 1 - 2^(n+1).
But now we have excluded only {AB} -> {A} and the like, and not {AB} -> {AC}. If we want the RHS to mention only attributes that are not mentioned on the LHS, then for each cardinality l of the LHS, there are 2^(n-l)-1 possible subsets on the RHS (that extra minus one is needed because the empty set must be excluded). Summing up to 2^0 - 1 + 2^1 - 1 + 2^2 - 1 + ... + 2^n - 1 = 2^(n+1) - 1 - (n + 1).
Still different from the answer given. And at any rate, the question was formulated hopelessly poorly. The question DID NOT STATE that trivial FDs were to be excluded. The question DID NOT STATE that "partially trivial" FDs were to be excluded.
BTW let's take the answers to the test. Choose a relation of degree 1, {A}. There are four possible FDs :
{} -> {} trivial, RHS subset of LHS
{} -> {A}
{A} -> {} trivial, RHS subset of LHS
{A} -> {A} trivial, RHS subset of LHS.
The correct answer, if trivial FDs are to be excluded, is "one". Your textbook says it's "four".
I think 2^n *2 is the answer because it includes all basic minimal FD’s and the rest can be derived from axioms or are trivial. A -> BC will have A -> B, A -> C, etc. ABC -> A, etc. are trivial.
Although it’s not clear from question whether to include all trivial cases. In that case the answer will be 2^n * 2^n. Because we can have 2^n choices for both LHS and RHS .