Combinatorics - Summary


Related: Catalan Number - Summary
https://www.mathsisfun.com/combinatorics/combinations-permutations.html
Without repetition our choices get reduced each time.
But when we want to select just 3 we don't want to multiply after 14. How do we do that? There is a neat trick: we divide by 13!
16 × 15 × 14 × 13 × 12 ...13 × 12 ...  =  16 × 15 × 14
That was neat. The 13 × 12 × ... etc gets "cancelled out", leaving only 16 × 15 × 14.
The formula is written:
n!(n − r)!
where n is the number of things to choose from,
and we choose r of them,
no repetitions,
order matters.

Combinations without Repetition

The easiest way to explain it is to:
  • assume that the order does matter (ie permutations),
  • then alter it so the order does not matter.
We already know that 3 out of 16 gave us 3,360 permutations.
But many of those are the same to us now, because we don't care what order!
For example, let us say balls 1, 2 and 3 are chosen. These are the possibilites:
Order does matterOrder doesn't matter
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 3
So, the permutations have 6 times as many possibilites.
In fact there is an easy way to work out how many ways "1 2 3" could be placed in order, and we have already talked about it. The answer is:
3! = 3 × 2 × 1 = 6
(Another example: 4 things can be placed in 4! = 4 × 3 × 2 × 1 = 24 different ways, try it for yourself!)
So we adjust our permutations formula to reduce it by how many ways the objects could be in order (because we aren't interested in their order any more):
combinations no repeat: n!/(n-r)! x (1/r!) = n!/r!(n-r)!

Combinations with Repetition

Let us say there are five flavors of icecream: banana, chocolate, lemon, strawberry and vanilla.
We can have three scoops. How many variations will there be?
Let's use letters for the flavors: {b, c, l, s, v}. Example selections include
  • {c, c, c} (3 scoops of chocolate)
  • {b, l, v} (one each of banana, lemon and vanilla)
  • {b, v, v} (one of banana, two of vanilla)
(And just to be clear: There are n=5 things to choose from, and we choose r=3 of them.
Order does not matter, and we can repeat!)
Now, I can't describe directly to you how to calculate this, but I can show you a special techniquethat lets you work it out.
bclsv
Think about the ice cream being in boxes, we could say "move past the first box, then take 3 scoops, then move along 3 more boxes to the end" and we will have 3 scoops of chocolate!
So it is like we are ordering a robot to get our ice cream, but it doesn't change anything, we still get what we want.
We can write this down as acccaaa (arrow means move, circle means scoop).
In fact the three examples above can be written like this:
{c, c, c} (3 scoops of chocolate):acccaaa
{b, l, v} (one each of banana, lemon and vanilla):caacaac
{b, v, v} (one of banana, two of vanilla):caaaacc
OK, so instead of worrying about different flavors, we have a simpler question: "how many different ways can we arrange arrows and circles?"
Notice that there are always 3 circles (3 scoops of ice cream) and 4 arrows (we need to move 4 times to go from the 1st to 5th container).
So (being general here) there are r + (n−1) positions, and we want to choose r of them to have circles.
This is like saying "we have r + (n−1) pool balls and want to choose r of them". In other words it is now like the pool balls question, but with slightly changed numbers. And we can write it like this:
combinations no repeat: n!/(n-r)! x (1/r!) = n!/r!(n-r)!
combinations repeat: ( r+n-1 r ) = (r+n-1)!/r!(n-r)!
where n is the number of things to choose from,
and we choose r of them
repetition allowed,
order doesn't matter.
Interestingly, we can look at the arrows instead of the circles, and say "we have r + (n−1)positions and want to choose (n−1) of them to have arrows", and the answer is the same:
( r+n-1 r ) = ( r+n-1 n-1 ) = (r+n-1)!/r!(n-r)!

https://en.wikipedia.org/wiki/Combinatorics
https://zh.wikipedia.org/wiki/%E7%BB%84%E5%90%88%E6%95%B0%E5%AD%A6
个元素中取出个元素,个元素的排列数量为:
赛马为例,有8匹马参加比赛,玩家需要在彩票上填入前三胜出的马匹的号码,从8匹马中取出3匹马来排前3名,排列数量为:
因为一共存在336种可能性,因此玩家在一次填入中中奖的概率应该是:
不过,中国大陆的教科书[来源请求]则是把从n取k的情况记作(A代表Arrangement,即排列[1])。
上面的例子是建立在取出元素不重复出现状况。
个元素中取出个元素,个元素可以重复出现,这排列数量为:
[2]
四星彩为例,10个数字取4个数字,因可能重复所以排列数量为:
这时的一次性添入中奖的概率就应该是:

https://zhuanlan.zhihu.com/p/41855459
P 和 C 的本质区别在于:决策的顺序对结果有没有影响。
我们的任务是:将这 3 个奖牌颁发给 8 个人中的 3 个,先颁发金牌,再颁发银牌,再颁发铜牌。问颁发奖牌的不同方式总共有哪些?
第一步:颁发金牌🏅️,可以在8个人中任选一个,有8种选择。A可以被替换为 B C D E F G H中的任何一个。
第二步:颁发银牌🥈,可以在除去已经获得金牌的人之外的7个人中任选一个,有7种选择。
第三步:颁发铜牌🥉,在已经获得金牌、银牌的两个人之外的6个人中任选一个,有6种选择。
那么很明显,总共的颁奖方式有
8 * 7 * 6 种
以此类推,假如我们现在要颁发 8 个奖牌给 8个人,那么我们会按照上述方法,每次颁发一种奖牌,直到奖牌被颁发完为止,这样,颁发奖牌的方式总共有:
8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 种
但是,我们只颁发 3 个奖牌就不颁发了呀,怎么才能在乘到 5 那里停止呢?很明显,摆脱 5 * 4 * 3 * 2 * 1 我们只需要把这个尾巴除掉即可!



也就是:


那么在 8 个人当中选 3 个人颁发一样的可乐瓶,有多少种颁发方法呢?
在上面排列的基础上,也就是给三个人颁发的是不同的奖杯,最终选出的三个人,拿奖是有顺序的,也就是,最后计算出来的所有方法中,把三个奖杯的放置顺序进行了排列。
但是现在,如果颁发的是可乐瓶,那么,获奖的顺序变得不再重要,谁先得,谁后得,结果都是一样的。上面排列的结果已经把不同颁发顺序视作不同颁发方法了,现在,3 个人中,不同的颁发顺序都是同一种!
所以,我们只需要把「上一步排列获得的结果」除以「不同颁发顺序的总数」,得到的就是可乐瓶颁发方法的总数。
不同颁发顺序的总数有 3!种
所以,总共有这么多种:



继续,如果要想在 n 个物品中,选择 k 个物品出来,选择的顺序无所谓,那么选择的方式总共有这么多种



https://en.wikipedia.org/wiki/Binomial_coefficient
the binomial coefficients are the positive integers that occur as coefficients in the binomial theorem. Commonly, a binomial coefficient is indexed by a pair of integers n ≥ k ≥ 0 and is written  It is the coefficient of the xk term in the polynomial expansion of the binomial power (1 + x)n, and it is given by the formula
For example, the fourth power of 1 + x is
and the binomial coefficient  is the coefficient of the x2 term.
Arranging the numbers  in successive rows for  gives a triangular array called Pascal's triangle, satisfying the recurrence relation
The binomial coefficients occur in many areas of mathematics, and especially in combinatorics.
二项式系数二项式定理中各项的系数。一般而言,二项式系数由两个非负整数 n 和 k 为参数决定,写作 ,定义为 的多项式展开式中,项的系数,因此一定是非负整数。如果将二项式系数 写成一行,再依照 顺序由上往下排列,则构成帕斯卡三角形

Program to calculate value of nCr

We know that the formula for (N choose K) is:
    N!
 --------
 (N-K)!K!
Therefore, the formula for (N choose K+1) is:
       N!                N!                   N!               N!      (N-K)
---------------- = --------------- = -------------------- = -------- x -----
(N-(K+1))!(K+1)!   (N-K-1)! (K+1)!   (N-K)!/(N-K) K!(K+1)   (N-K)!K!   (K+1)
That is:
(N choose K+1) = (N choose K) * (N-K)/(K+1)
We also know that (N choose 0) is:
 N!
---- = 1
N!0!
So this gives us an easy starting point, and using the formula above, we can find (N choose K) for any K > 0 with K multiplications and K divisions.

  1. binomial coefficient C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n.
  2. A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.
The value of C(n, k) can be recursively calculated using following standard formula for Binomial Coefficients.
   C(n, k) = C(n-1, k-1) + C(n-1, k)
   C(n, 0) = C(n, n) = 1
    static int binomialCoeff(int n, int k)
    {
    int C[][] = new int[n+1][k+1];
    int i, j;
      
        // Calculate  value of Binomial Coefficient in bottom up manner
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
       
            // Calculate value using previosly stored values
            else
                C[i][j] = C[i-1][j-1] + C[i-1][j];
          }
     }
       
    return C[n][k];
    }


    static int binomialCoeff(int n, int k)
    {
        int C[] = new int[k + 1];
         
        // nC0 is 1
        C[0] = 1;  
       
        for (int i = 1; i <= n; i++)
        {
            // Compute next row of pascal 
            // triangle using the previous row
            for (int j = Math.min(i, k); j > 0; j--)
                C[j] = C[j] + C[j-1];
        }
        return C[k];
    }


Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts