Counting Techniques II
Permutations of indistinguishable objects, Binomial Theorem,
Permutations with Indistinguishable Objects
• Some elements may be indistinguishable in
counting problems. When this is the case, care must be taken to avoid counting things more than once.
• EXAMPLE: How many different strings can be made by reordering the letters of the word
• Solution: Because some of the letters of SUCCESS are the same,
the answer is not given by the number of permutations of seven letters. This word contains three Ss, two Cs, one U, and one E.
• To determine the number of different strings that can be made
by reordering the letters, first note that the three Ss can be placed among the seven positions in C(7, 3) different ways, leaving four positions free. Then the two Cs can be placed in C(4, 2) ways, leaving two free positions. The U can be placed in C(2, 1) ways, leaving just one position free. Hence E can be
• C(7, 3)C(4, 2)C(2, 1)C(1, 1)
=
=
• In general, for counting in which some elements
are indistinguishable, the following theorem holds:
• The number of different permutations of n
objects, where there are n1 indistinguishable
objects of type 1, n2 indistinguishable objects of type 2, . . . , and nk indistinguishable objects of type k, is
• Proof: To determine the number of permutations, first note
that the n1 objects of type one can be
• placed among the n positions in C(n, n1) ways, leaving n − n1 positions free. Then the objects
• of type two can be placed in C(n − n1, n2) ways, leaving n − n1 − n2 positions free. Continue
• placing the objects of type three, . . . , type k − 1, until at the
last stage, nk objects of type k
• C(n, n1)C(n − n1, n2) · · · C(n − n1 −· · ·− nk −1, nk)
=
Examples
• How many different strings can be made from the letters in MISSISSIPPI, using all the letters? • How many different strings can be made from
Binomial coefficients and identities
• We denote the number of r-combination from
The Binomial Theorem
• THE BINOMIAL THEOREM Let x and a be
variables, and let n be a nonnegative integer. • Then
• EXAMPLE: What is the expansion of (x + y)⁴? • Solution: From the binomial theorem it
follows that • (x + y)⁴
• = x⁴ + x³y + x²y² + xy³ + y⁴
• EXAMPLE 2: What is the coefficient of x¹²y¹³ in the expansion of (x + y)²⁵?
• Solution: From the binomial theorem it follows that this coefficient is
• EXAMPLE 3: What is the coefficient of x¹²y¹³ in the expansion of (2x − 3y)²⁵?
• Solution: First, note that this expression
equals (2x + (−3y))²⁵. By the binomial theorem, we have
• (2x + (−3y))²⁵
=-• Find the coefficient of x100y101 in the expansion
of (2x + 5y)201 .
• What is the coefficient of x101y99 in the
Generating permutations and combinations
• Generating permutations
• Any set with n elements can be placed in one-to-one correspondence with the set {1, 2, 3, . . . , n}. We can list the permutations of any set of n elements by generating the permutations of the n smallest positive integers and then replacing these integers with the corresponding elements. Many different algorithms have been developed to generate the n! permutations of this set. We will describe one of these that is based on the lexicographic (or
dictionary) ordering of the set
• of permutations of {1, 2, 3, . . . , n}. In this ordering, the permutation a1 a2 · · ·
an precedes the permutation of b1b2 · · · bn, if for some k, with 1 ≤ k ≤ n, a1 =
Algorithm for generating permutations
• An algorithm for generating the permutations of {1,
2, . . . , n} can be based on a procedure that constructs the next permutation in lexicographic order following a given permutation a1 a2 · · · an. First, suppose that an-1 < an. Interchange an-1 and an to obtain a larger
permutation. No other permutation is both larger than the original permutation and smaller than the
• On the other hand, if an-1 > an, then a larger permutation cannot be obtained by interchanging these last two
terms in the permutation. Look at the last three integers in the permutation. If an-2 < an-1, then the last three
• On the other hand, if an-2 > an-1 (and an-1 > an),
then a larger permutation cannot be obtained by permuting the last three terms in the
• that is, the last pair of adjacent integers in the permutation where the first integer in the pair is smaller than the second. Then, the next larger
permutation in lexicographic order is obtained by
putting in the j th position the least integer among aj+1, aj+2, . . . , and an that is greater than aj and listing in
increasing order the rest of the integers aj, aj+1, . . . , an
• Examples:
For simplicity of implementation, we input number as a string.
• Input: n = "218765" Output: "251678" • Input: n = "1234" Output: "1243”
• Following are few observations about the next
greater number.
1) If all digits are sorted in descending order, then output is always “Not Possible”. For example, 4321. 2) If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.
• You can now try developing an algorithm yourself.
• Following is the algorithm for finding the next greater number.
I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”.
• II) Now search the right side of above found digit ‘d’ for the smallest digit
greater than ‘d’. For “534976″, the right side of 4 contains “976”. The smallest digit greater than 4 is 6.
• III) Swap the above found two digits, we get 536974 in above example.
• Following is a C++ implementation of the algorithm above
• // C++ program to find the smallest number which greater than a given number • // and has same set of digits as given number
• #include <iostream> • #include <cstring> • #include <algorithm>
• using namespace std; •
• // Utility function to swap two digits • void swap(char *a, char *b)
• {
• char temp = *a; • *a = *b;
• EXAMPLE 1 The permutation 23415 of the set
{1, 2, 3, 4, 5} precedes the permutation 23514, because these permutations agree in the first two positions, but the number in the third
position in the first permutation, 4, is smaller than the number in the third position in the second permutation, 5.
• Similarly, the permutation 41532 precedes
• EXAMPLE 2 What is the next permutation in
lexicographic order after 362541?
• Solution: The last pair of integers aj and aj+1 where aj < aj+1 is a3 = 2 and a4 = 5. The least integer to the right of 2 that is greater than 2 in the permutation is a5 = 4. Hence, 4 is placed in the third position. Then the integers 2, 5, and 1 are placed in order in the last
• EXAMPLE 3 Generate the permutations of the integers 1, 2, 3 in
lexicographic order.
• Solution: Begin with 123. The next permutation is obtained by
interchanging 3 and 2 to obtain 132. Next, because 3 > 2 and 1 < 3, permute the three integers in 132. Put the smaller of 3 and 2 in the first position, and then put 1 and 3 in increasing order in positions 2 and 3 to obtain 213. This is followed by 231, obtained by
interchanging 1 and 3, because 1 < 3. The next larger permutation
has 3 in the first position, followed by 1 and 2 in increasing order, namely, 312. Finally, interchange 1 and 2 to obtain the last
• EXAMPLE 4 Generate the permutations of the
letters a, b, c in lexicographic order.
• Solution:
• By a similar analysis as example 3, the