Counting principleThe counting principle is a fundamental rule of counting; it is usually taken under the head of the permutation rule and the combination rule. It states that if a work X can be done in m ways, and work Y can be done in n ways, then provided X and Y are mutually exclusive, the number of ways of doing both X and Y is m x n. For example, suppose a person (T) can go to the market from their home by bus, by foot, and by bicycle, i.e., T = 3 if he is returning home (B) by foot and by car, i.e., B = 2. So, TB can be done in 3*2 = 6 ways. The Rules of Sum and ProductThe rule of sum or addition principle and the rule of product or multiplication principle are given below. The Rule of Sum (Addition Principle) If several tasks P1, P2, P3………, Pm can be done in K1, K2, K3…. Km Number of ways respectively in which no tasks can be performed simultaneously, the number of ways to perform one of these tasks is given by K1 + K2 + ………… + Km. Suppose two different tasks, P and Q, which are disjoint it means P ∩Q = Ø Mathematically, |P∪Q|=|P|+|Q| The Rule of Product (Multiplication rule) If several tasks P1, P2, P3,…………, Pm can be done in K1, K2, K3,………..Km Number of ways respectively and every task comes after the occurrence of the previous task, the number of ways to perform one of these tasks is given by K1 x K2 x ………… x Km. Mathematically, If a task Q comes after a task P, then |P x Q|=|P| x |Q| Example 1: An employee lives at location K and wants to go to the office at location G. From his home, K, he has first to reach location B and then B to G. He may go to K to B by either four metro routes or three buses routes. From there, he can either select six metro routes or four bus routes to reach location G. Find the no of ways to go from K to G. Solution: He can go in 4 + 3 = 7 ways (Addition rule). After that, he can go B to G in 6 + 4 (10) ways (Addition rule). Hence from K to G, he can go in 7 * 10 = 70 ways (Multiplication rule) PermutationsPermutations refer to the arrangement of elements in a definite manner. For example We have to form a permutation of two-digit numbers from a set of numbers S = (5, 7). Different two digits numbers will be formed when we arrange the given numbers. The permutations will be = (5,7) and (7,5). Example 1: From a set S = (P, Q, R) by taking two at a time, all permutations are pq, qp, pr,rp,qr,rq Example 2: We have to form a permutation of two coins from a set of S = (H, T). Different two sides will be formed when we arrange the two sides. The permutations will be (H, T) and (T, H) Number of PermutationsThe number of permutations of n different objects taken r at a time is given by P(n,r) = n(n-1) (n-2)………(n-r-1) (n≥r) = n! / (n-r)! Where, n! = 1.2.….(n-1).n Note: n is always greater than or equal to r, which is also equivalence to n factorial divided by n minus r.Proof of permutations formulaLet us suppose there are three vacant boxes, and you need to fill them in three different colors. There is n number of ways to fill up the first box. After filling the first box (n-1), the number of boxes is left. Hence, there are (n-1) ways to fill up the color in the second place. After filling the first and second place, (n-2) number of boxes is left. Hence, there are (n-2) ways to fill up the third place with different colors. Now, we can generalize the total number of ways to fill up the rth box as [n - (r-1)] = n-r+1 So, the total number of ways to fill up the box with color from the first place up to rth place is given by npr=n(n-1) (n-2) .... (n-r+1) npr= [n(n-1) (n-2) ...(n-r+1)] [(n-r) (n-r-1) …3.2 .1]/[(n-r) (n-r-1) …3.2.1] Hence, =n! /(n-r)! Points to remember in permutations
Let's understands this concept with the help of examples Example 1: In how many ways can the letters of the word "SCISSORS" be arranged? Solution: There are eight letters word (4 S, 1 C, 1 I, 1 O 1 R) in the word SCISSORS The permutation will be = 8! / [(4!) (1!) (1!) (1!)] = 1680 Example 2: In how many ways can the letters of the word "GOOD" be arranged? Solution: There are 4 letters word (1 G, 2 O, 1 D) in the word GOOD The permutation will be = 4! / [(1!) (2!) (1!)] = 12 Example 3: From a bunch of 4 different cards, how many ways can we permute it? Solution: As we are taking 4 cards at a time from a deck of 4 cards. The permutation will be 4p4= 4! = 24 Example 4: How can the letters of the word 'READER' be arranged so that the consonants occupy only the even positions? Solution: There are 3 vowels and 3 consonants in the word 'READER'. Several ways of arranging the consonants among themselves 3p3= 3! = 6. 3 vowels will fill up the remaining 3 vacant places in 3p3= 3! = 6 ways. Hence, the total number of permutations is 6×6=36 CombinationsA combination refers to selecting elements from a collection such that the order of selection does not matter, unlike permutation. In the combination generally, we prefer only selection. The ordering of the selected elements is not Important. Generally, the number of permutations exceeds the number of combinations. Each combination corresponds to many permutations. The combination of n different things taken r at a time is given by nCr=n! /r! (n-r)! Example 1 Find the number of subsets of the set {1,2,3,4,5,6,7} having 4 elements. Solution: The cardinality of the set is 7, and we have to select 4 elements from the set. Here, the ordering of the number does not matter. Hence the number of subsets will be nCr =n! / r! (n-r)! 7C4 =7! / 4! (7-4)! = 7 * 6 * 5/ 3 *2 = 35 Example 2 There is 8 Boy and 6 Girls who participated in a contest. In how many ways can we select 4 boys and 3 girls from the contest? Solution: The number of ways to select 4 boys from 8 boys is 8C4, and the number of ways to select 3 girls from 6 girls is 6Cs Hence, the total number of ways is 8C4 6Cs = 8! / 4! (8-4)! 6! / 3! (6-3)! = 8 × 7 ×6 ×5/ 4 ×3 ×2 × 6×5×4 /3 ×2 = 70 × 20 = 1400 Example 4 How many ways can you select 4 different groups of 2 students from a total of 8 students? Solution: Suppose the number of groups is 1, 2, 3 and 4 For selecting 2 students for 1st group, the number of ways will be =8C2 The number of ways for selecting 2 students for the second group after selecting 1st group =6C2 The number of ways for selecting 2 students for the third group after selecting 1st and 2nd group =4C2 The number of ways for selecting 2 students for the fourth group after selecting 1st,2nd and 3rd group =2C2 Hence, the total number of ways 8C2 ×6C2 ×4C2× 2C2 = 28 × 15 ×6 ×1 = 2520 Pascal IdentityPascal's identity was first derived by Blaise Pascal in the 17th century. It states that the total number of ways to select k elements from n different elements is equal to the summation of the number of ways to select (k-1) elements from (n-1) elements and the number of ways to select elements from n-1 elements Mathematically, for any positive integers k and n, we can write the equation: nCk = n-1Ck-1 + n-1Ck Inclusion-Exclusion Principle The Inclusion-Exclusion principle refers to a very basic theorem of counting, and various problems in various programming contests are based on it; a basic example of the inclusion-exclusion principle is given below. Consider A as a collection of elements and |A| as the number of elements in A and the same as for B. The cardinality of the collection of elements of both sets A and B (when both A and B are disjoint) can be stated as (for 2 finite sets): |AUB|=|A|+|B| Consider that case if the sets are disjoint. We have to subtract the common elements counted twice while calculating the cardinality of both A and B, and a new form will become: |AUB|=|A|+|B|-|A∩B| Generalized formula: Example based on Inclusion-Exclusion PrincipleExample 1 How many integers from 1 to 100 are multiples of 5,6 but not both? Solution: From 1 to 100, there are 100/5 = 20 numbers which are multiples of 5 And, there are 100/6 = 16 numbers which are multiples of 6. And, there are 100/30 = 3 numbers which are multiples of both 5 and 6 Therefore, |A| = 20 , |B| = 16 and |A∩B| = 3 We know that, |AUB| =|A|+|B|-|A∩B| = 20 + 16 -3 = 33 Example 2 In a group of 60 employees, 32 like pizza and 38 like burgers, and each employee likes at least one of the two snacks. Find how many employees like both pizza and burgers? Solution: Let P be the set of employees who like pizza and Q be employees who like a burger. Therefore, |P| = 32, |Q| = 38 and |PUQ| = 60 We know that, |AUB| =|A|+|B|-|A∩B| So, |P∩Q|=|P|+|Q|-|PUQ| = 32 + 38 - 60 = 70 - 60 = 10 |