Pumping Lemma's Condition 3 concept - computation

I'm following one of the examples from my textbook on the Pumping Lemma:
Let C = {w | w has an equal number of 0s and 1s}
Condition 3 stipulates: |xy| <= p
If |xy| <= p, then y must consist only of 0s, so xyyz is not in C.
Therefore s cannot be pumped
I'm having trouble understanding how condition 3 leads to the conclusion that "y must only consist of 0s, so xyyz is not in C"

I guess the string chosen is 0p1p.
Since |xy| <= p, and xyz = 0p1p, the string xy will be 0k where k <= p since the first p symbols of 0p1p are all 0's. Since xy consists of only 0's, y must also consist of only 0's
And learn to put your question in a proper manner. You cannot expect others to "predict" your question while you put half of the information

Related

Cannot understand the logic of editorial

Question link : https://www.codechef.com/problems/STR
question is :
Little John just had his first class in school. He was taught first 20
letters of English alphabet and was asked to make words from these
alphabets.
Since he doesn't know many dictionary words, he quickly finished this work
by making random strings from these alphabets.
Now while other kids are busy creating their words, John gets curious and
puts all the strings he created in a list and named it X.
He picks two indices 'i' and 'j' ( not necessarily distinct). He assigns A
as X[i] and B as X[j]. He then concatenates both the strings to create a new
string C ( = A + B ). He calls a string "super string" if that string
contains all the 20 letters of English alphabet he has just learnt,atleast
once.
Given the strings of the list, can you tell him how many such unordered
pairs (i,j) he can choose such that string C is a super string.
Editorial : https://discuss.codechef.com/questions/79843/str-editorial
I cannot understand logic of dp here.Can someone help me ?
For the sake of simplicity, assume that we used first 6 characters from 'a' to 'f' instead of 20 characters. We will store each string in 6 bits by putting 1s for the characters they contain (for example, the bitmask of "abc" can be 111000).
A supermask of a string s satisfies the following:
If i-th bit of s is 1, i-th bit of the supermask is 1.
If i-th bit of s is 0, i-th bit of the supermask can be either 0 or 1.
Supermasks of s = 111000 are 111000, 111001 ... 111111. Let's denote x as the integer representation of maximum possible s value, which 63. Notice that for a string s:
s | x - s = x (111000 | 000111 = 56 + 7)
The first solution that author suggests is this: Assume that you have calculated the count of all supermasks for numbers i+1, i+2 ... x where 0 <= i <= x. Let bit(i, k) denote k-th least significant bit of input bitmask i (for i = "111000", bit(i, 2) = 0). Finally, let dp[i] denote the count of supermasks of i. The algorithm suggests that,
An element is a supermask of itself (dp[i] = 1)
From least significant bit to most significant, whenever you encounter a 0 on index k
If you flip bit k to 1, result i' is a supermask of i (dp[i]++)
All supermasks of i' are supermasks of i (dp[i] += dp[i | bit(i, k)])
The problem is this solution counts the same supermasks multiple times. Consider the case when i = 111000, it counts supermask 111111 for both i' = 111001 and i'' = 111010. You need to find a way to eliminate these duplicates.
The final thing that author suggests is as follows: Let dp[i][j] denote the number of supermasks of i, such that rightmost j 0-bits of i are all zeros. For example for i = 111000, dp[i][j] includes 111000 and 111100. Using this approach, iterating i = 111000 gives:
dp[i][0] = 111001, 111011, 111101, 111111
dp[i][1] = 111010, 111110
dp[i][2] = 111100
Unfortunately, the documentation of the author was very bad and I wasn't able to understand the notation used in his final formulation of the problem. Still, I hope that the explanation is useful enough to understand the logic.

Doubts regarding this pseudocode for the perceptron algorithm

I am trying to implement the perceptron algorithm above. But I have two questions:
Why do we just update w (weight) variable once? Shouldn't there be separate w variables for each Xi? Also, not sure what w = 0d means mathematically in the initialization.
What is the mathematical meaning of
yi(< xi,w >+b)
I kinda know what the meaning inside the bracket is but not sure about the yi() part.
(2) You can think of 'yi' as a function that depends on w, xi and b.
let's say for a simple example, y is a line that separates two different classes. In that case, y can be represented as y = wx+b. Now, if you use
w = 0,x = 1 and b = 0 then y = 0.
For your given algorithm, you need to update your weight w, when the output of y is less than or equal to 0.
So, if you look carefully, you are not updating w once, as it is inside an if statement which is inside a for loop.
For your algorithm, you will get n numbers of output y based on n numbers of input x for each iteration of t. Here 'i' is used for indexing both input as xi and output as yi.
So, long story short, out of n numbers of input x, you only need to update the w when the output y for the corresponding input x will be less than or equal to zero (for each iteration of t).
(1) I have already mentioned w is not updated once.
Let's say you know that any output value greater(<) than 0 is the correct answer. So if you get an output which is less than or equal to zero then there is a mistake in your algorithm and you need to fix it. This is what your algorithm is doing by updating the w when the output is not matching the desired one.
Here w is represented as a vector and it is initialized as zero.

Push down automata for a language

I want to design a pushdown automata for the language
L = { a^i b^j c^k | i = j or k <= j <= 2k}
The solution proposed by the instructor is as pictured in the following diagram.
But my concern here is, that it does not handle string of the form when |2c| > |b|. That is when in the q8 state, what if the all the B's are stacked out, but the input C is not finished yet. That transition is not captured here.
Is my concern correct?
Or the proposed solution is a correct PDA.
Remember that j >= k, so that means |b| >= |c|.
If all the "b"s in input were read, then the number of B's stacked is greater than (or equal to) the number of "c"'s to be read in the input.
If j = k, then it will use the transiction from q8 to q8 until the input is finished.
If j = 2k, it will read a "c" (q8 -> q9) and take two B's out of the stack (q9 -> q8), so only strings with |b| = 2|c| can be accepted.
If j < 2k, it will use q8 -> q9 and q9-> q8 until the number of B's stacked is equal to the number of "c"s to be read in the input. Then it will use q 8-> q8 until the input is finished.

algorithm to find a number in which product of number of 4 & 7 is maximum in given range

I am stuck in a question in which lower bound L and Upper bound U is given.
Now suppose in the decimal representation of integer X digit 4 appears A times and digit 7 appears B times.
Problem is to find X which has maximum value of A*B for L<=X<=U.
Is there any efficient algorithm to solve it?
If I understood the problem correctly, the following should work:
Assume all numbers have the same number of digits (if e.g. L has less digits than U, we can just fill in the beginning with 0 s).
Let Z = U - L.
Now we go from the first (/highest/leftmost) digit to the last one. If we are looking at the i th digit, let L(i), U(i), Z(i) and X(i) be the corresponding digit.
for all leading Z(i) which are 0, we set X(i) = L(i) (we don't have a choice).
For the first not 0 Z(i) check: is there a 4 or a 7 in the interval [L(i), U(i)-1]? If yes let X(i) be that 4 or 7 otherwise let X(i) = U(i)-1.
Now fill up the rest of X with 4s and 7s such that you choose a 4 if you have assigned more 7s so far and vice versa.
Maybe an example can help in understanding this:
Given U = 5000 and L = 4900.
Now Z = 0100.
From the algorithm we set
X(1) = L(1) = 4 (we have no choice)
X(2) = U(2)-1 = 9 (the first non 0 digit in Z)
X(3) = 7 (we already had a 4)
X(4) = 4 (can be chosen arbitrarily)
Leading to X = 4974 with an objective of 2*1=2
It seems you have the algorithm thought out already.
Just break it down piece by piece and solve each part. I usually write something like you did there with comments and then break those down until they are at a reasonable bite size to write code for.
When you have it working, if needed, you can optimize it.

Algorithm - Partition two numbers about a power-of-two

Given two floating point numbers, p and q where 0 < p < q I am interested in writing a function partition(p,q) that finds the 'simplest' number r that is between p and q. For example:
partition(3.0, 4.1) = 4.0 (2^2)
partition(4.2, 7.0) = 6.0 (2^2 + 2^1)
partition(2.0, 4.0) = 3.0 (2^1 + 2^0)
partition(0.3, 0.6) = 0.5 (2^-1)
partition(1.0, 10.0) = 8.0 (2^3)
In the last instance I am interested in the largest number (so 8 as opposed to 4 or 2).
Let us assume assume that p and q are both normalized and positive, and p < q.
If p and q have differing exponents, it appears that the number you are looking for is the number obtained by zeroing the mantissa of q after the leading (and often implicit) 1. The corner cases are left as an exercise, especially the case where q's mantissa is already made of zeroes after the leading, possibly implicit, 1.
If p and q have the same exponent, then we have to look at their mantissas. These mantissas have some bits in common (starting from the most significant end). Let us call c1 c2 .. ck pk+1 ... pn the bits of p's mantissa, c1 c2 .. ck qk+1 ... qnthe bits of q's mantissa, where c1 .. ck are common bits and pk+1, qk+1 differ. Then pk+1 is zero and qk+1 is one (because of the hypotheses). The number with the same exponent and mantissa c1 .. ck 1 0 .. 0 is in the interval p .. q and is the number you are looking for (again, corner cases left as an exercise).
Write the numbers in binary (terminating if possible, so 1 is written as 1.0000..., not 0.1111...),
Scan from left to right, "keeping" all digits at which the two numbers are equal
At the first digit where the two numbers differ, p must be 0 and q must be 1 since p < q:
If q has any more 1 digits after this point, then put a 1 at this point and you're done.
If q has no more 1 digits after this point, then doing that would result in r == q, which is forbidden, so instead append a 0 digit. Follow that by a 1 digit unless doing so would result in r == p, in which case append another 0 and then a 1.
Basically, we truncate q down to the first place at which p and q differ, then jigger it a bit if necessary to avoid r == p or r == q. The result is certainly less than q and greater than p. It is "simplest" (has the least possible number of 1 digits) since any number between p and q must share their common initial sequence. We have added only one 1 digit to that sequence, which is necessary since the initial sequence alone is <= p, so no value in range (p,q) has fewer 1 digits. We've chosen the "largest" solution because we always place our extra 1 at the first (biggest) possible place.
It sounds like you just want to convert the binary representation of the largest integer strictly less than your largest argument to the corresponding sum of powers of two.

Resources