I have been given a program to write difference combinations of set of number entered by user and when I researched for the same I get examples with terms permutations and derangements.
I am unable to find the clarity between the them. Also adding to that one more term is combinations. Any one please provide a simple one liner for clarity on the question.
Thanks in advance.
http://en.wikipedia.org/wiki/Permutation
The notion of permutation relates to the act of rearranging, or permuting, all the members of a set into some sequence or order (unlike combinations, which are selections of some members of the set where order is disregarded). For example, written as tuples, there are six permutations of the set {1,2,3}, namely: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). As another example, an anagram of a word, all of whose letters are different, is a permutation of its letters.
http://en.wikipedia.org/wiki/Derangement
In combinatorial mathematics, a derangement is a permutation of the elements of a set such that none of the elements appear in their original position.
The number of derangements of a set of size n, usually written Dn, dn, or !n, is called the "derangement number" or "de Montmort number". (These numbers are generalized to rencontres numbers.) The subfactorial function (not to be confused with the factorial n!) maps n to !n.1 No standard notation for subfactorials is agreed upon; n¡ is sometimes used instead of !n.2
Related
While studying about catalan numbers, some of the applications that I came across were:
no of possible binary search trees using n nodes.
no of ways to draw non-intersecting chords using 2*n points on a circle.
no of ways to arrange n pairs of parenthesis.
While I understand the first two problems, how catalan numbers fit in their solution, I am not able to understand how they fit in the third problem.
Couldn't find any other useful resource on the internet which explains the HOW part. Everyone just says that it's the solution.
Can someone please explain.
Since others do not seem to agree with me that this question is off-topic, I now decide that it is on topic and provide and answer.
The Wikipedia is indeed confusing about the "number of ways to arrange n pairs of parentheses" (the second bullet point in this link.) Part of the confusion is that the order of the strings of parentheses does not match the order of the binary tree, which you do understand, or with many of the other examples.
Here is a way to transform a string of n pairs of parentheses which are correctly matched into a binary tree with n internal nodes. Consider the left-most parenthesis, which will be a left-parenthesis, together with its matching right-parenthesis. Turn the string into a node of the binary tree. The sub-string that is inside the currently-considered parentheses becomes the left child of this node, and the sub-string that is after (to the right) of the currently-considered right-parenthesis becomes the right child. Either or both sub-strings may be empty, and the currently-considered parentheses are simply removed. If either sub-string is not empty, continue this procedure recursively until all parentheses have been removed.
Here are two examples. Let's start with the string ((())). We start with
The considered-parentheses are the outermost ones. This becomes
(I did not bother drawing the external leaf nodes) then
then
which is Wikipedia's left-most binary tree with 3 internal nodes.
Now let's do another string, (())(). We start with
Again, the considered-parentheses are the outermost ones. This transforms to
And now the considered-parentheses are the first two, not the outermost ones. This becomes
which finally becomes
which is the second binary tree in Wikipedia's list.
I hope you now understand. Here is a list of all five possible strings of 3 pairs of parentheses that are correctly paired, followed by Wikipedia's list of binary trees. These lists now correspond to each other.
((())) (()()) (())() ()(()) ()()()
I want to create a divide and conquer algorithm (O(nlgn) runtime) to determine if there exists a number in an array that occurs k times. A constraint on this problem is that only a equality/inequality comparison method is defined on the objects of the array (i.e can't use <, >).
So I have tried a number of approaches including splitting the array into k pieces of equal size (approximately). The approach is similar to finding the majority item in an array, however in the majority case when you split the array, you know that one half must have a majority item if such an item exists. Any pointers or tips that one could provide to put me in the right direction ?
EDIT: To clear up a little, I am wondering whether the problem of finding the majority item by splitting the array in half and using a recursive solution can be extended to other situations where k may be n/4 or n/5 etc.
Maybe I should of phrased the question using n/k instead.
This is impossible. As a simple example of why this is impossible, consider an input with a length-n array, all elements distinct, and k=2. The only way to be sure no element appears twice is to compare every element against every other element, which takes O(n^2) time. Until you perform all possible comparisons, you cannot be sure that some pair you didn't compare isn't actually equal.
When inserting n items into a hash table of size m, assuming that the destination of each item is independently uniformly random, what is the probability that no collision occurs?
My working thus far:
We have n items and m locations.
Each item has a 1/m chance of being in any particular location.
There are nC2 possible pairs of items.
The probability of there being no collisions is the probability that for every location, every pair of items does not hash to that location.
For any given location, for any given pair, the probability that the two items do not hash to that location is (m-1)/m.
Then, for any given location, the probability that the above is true for ALL pairs is ((m-1)/m)^(nC2).
Then, the probability that this is true for every location is
[((m-1)/m)^(nC2)]^(m).
You made a few mistakes in that reasoning. The main one is that you assume that the probabilities for pairs not hashing together are independent, so you can multiply them together. You have not shown that is the case, and in fact it is not the case. Consider three elements a, b, and c. If you know that both a and b do not collide with c, then they are limited to m-1 places rather than the initial m places, and they are more likely to collide with each other than if you just ignore c.
Here is a straightforward way to find your desired probability. Looking at the total possibilities ignoring collisions, each of the n items has m places to go. Those placements are independent, so the total possibilities are m^n (or m**n in Python) if we take order into account.
If we know there are no collisions, those n items are a way of choosing out of the m locations without replacement. So if we take order into account, that makes mPn possibilities -- the ways to choose n items out of m choices without replacement and with order (permutations). Therefore your desired probability is
mPn / m^n = (m!) / ((m-n)! * m^n) = m/m * (m-1)/m * (m-2)/m * ... * (m-n+1)/m
There are n factors in that last expression. (This would be so much better in MathJax!) You can choose which of those three equivalent expressions is best for your purpose.
There are other ways to come up with those expressions, of course. That last one can be thought of as the probability of no collision placing 1 item in m slots times the conditional probability of placing a second item given no prior collision times the conditional probability of placing a third item given no prior collision times ....
Those expressions are fairly easy to test. Just choose specific, small values of m and n, generate all possible choices of n items out of those m, and find the empirical probability of no collisions. This should agree with the formula(s) above. I'll leave the choice of programming language and the coding to you. After all, this is a programming site. I just did this in Python, for multiple choices of n and m, and it works out.
I just read this interesting question about a random number generator that never generates the same value three consecutive times. This clearly makes the random number generator different from a standard uniform random number generator, but I'm not sure how to quantitatively describe how this generator differs from a generator that didn't have this property.
Suppose that you handed me two random number generators, R and S, where R is a true random number generator and S is a true random number generator that has been modified to never produce the same value three consecutive times. If you didn't tell me which one was R or S, the only way I can think of to detect this would be to run the generators until one of them produced the same value three consecutive times.
My question is - is there a better algorithm for telling the two generators apart? Does the restriction of not producing the same number three times somehow affect the observable behavior of the generator in a way other than preventing three of the same value from coming up in a row?
As a consequence of Rice's Theorem, there is no way to tell which is which.
Proof: Let L be the output of the normal RNG. Let L' be L, but with all sequences of length >= 3 removed. Some TMs recognize L', but some do not. Therefore, by Rice's theorem, determining if a TM accepts L' is not decidable.
As others have noted, you may be able to make an assertion like "It has run for N steps without repeating three times", but you can never make the leap to "it will never repeat a digit three times." More appropriately, there exists at least one machine for which you can't determine whether or not it meets this criterion.
Caveat: if you had a truly random generator (e.g. nuclear decay), it is possible that Rice's theorem would not apply. My intuition is that the theorem still holds for these machines, but I've never heard it discussed.
EDIT: a secondary proof. Suppose P(X) determines with high probability whether or not X accepts L'. We can construct an (infinite number of) programs F like:
F(x): if x(F), then don't accept L'
else, accept L'
P cannot determine the behavior of F(P). Moreover, say P correctly predicts the behavior of G. We can construct:
F'(x): if x(F'), then don't accept L'
else, run G(x)
So for every good case, there must exist at least one bad case.
If S is defined by rejecting from R, then a sequence produced by S will be a subsequence of the sequence produced by R. For example, taking a simple random variable X with equal probability of being 1 or 0, you would have:
R = 0 1 1 0 0 0 1 0 1
S = 0 1 1 0 0 1 0 1
The only real way to differentiate these two is to look for streaks. If you are generating binary numbers, then streaks are incredibly common (so much so that one can almost always differentiate between a random 100 digit sequence and one that a student writes down trying to be random). If the numbers are taken from [0,1] uniformly, then streaks are far less common.
It's an easy exercise in probability to calculate the chance of three consecutive numbers being equal once you know the distribution, or even better, the expected number of numbers needed until the probability of three consecutive equal numbers is greater than p for your favourite choice of p.
Since you defined that they only differ with respect to that specific property there is no better algorithm to distinguish those two.
If you do triples of randum values of course the generator S will produce all other triples slightly more often than R in order to compensate the missing triples (X,X,X). But to get a significant result you'd need much more data than it will cost you to find any value three consecutive times the first time.
Probably use ENT ( http://fourmilab.ch/random/ )
and we need to put these balls into boxes.
How many states of the states could there be?
This is part of a computer simulation puzzle. I've almost forget all my math knowledges.
I believe you are looking for the Multinomial Coefficient.
I will check myself and expand my answer.
Edit:
If you take a look at the wikipedia article I gave a link to, you can see that the M and N you defined in your question correspond to the m and n defined in the Theorem section.
This means that your question corresponds to: "What is the number of possible coefficient orderings when expanding a polynomial raised to an arbitrary power?", where N is the power, and M is the number of variables in the polynomial.
In other words:
What you are looking for is to sum over the multinomial coefficients of a polynomial of M variables expanded when raised to the power on N.
The exact equations are a bit long, but they are explained very clearly in wikipedia.
Why is this true:
The multinomial coefficient gives you the number of ways to order identical balls between baskets when grouped into a specific grouping (for example, 4 balls grouped into 3, 1, and 1 - in this case M=4 and N=3). When summing over all grouping options you get all possible combinations.
I hope this helped you out.
These notes explain how to solve the "balls in boxes" problem in general: whether the balls are labeled or not, whether the boxes are labeled or not, whether you have to have at least one ball in each box, etc.
this is a basic combinatorial question (distribution of identical objects into non identical slots)
the number of states is [(N+M-1) choose (M-1)]