gitbook-way-to-algorithm
  • Introduction
  • Preface 前言
  • Content 目录
  • MathSymbolTable 数学符号表
  • Chapter-1 BasicKnowledge 第1章 基础知识
    • TimeComplexity 时间复杂度
    • Recursion 递归式
  • Chapter-2 Sort 第2章 排序
    • InsertSort 插入排序
    • BubbleSort 冒泡排序
    • QuickSort 快速排序
    • MergeSort 归并排序
  • Chapter-3 Search 第3章 搜索
    • BinarySearch 二分查找法(折半查找法)
    • AdditionMultiplicationPrinciple 加法乘法原理
    • BruteForce 暴力枚举
    • Recursion 递归
    • BreadthFirstSearch 广度优先搜索
    • BidirectionalBreadthSearch 双向广度搜索
    • AStarSearch A*搜索
    • DancingLink 舞蹈链
  • Chapter-4 DataStructure 第4章 数据结构
    • DisjointSet 并查集
    • PrefixTree(TrieTree) 前缀树
    • LeftistTree(LeftistHeap) 左偏树(左偏堆)
    • SegmentTree 线段树
    • FenwickTree(BinaryIndexedTree) 树状数组
    • BinarySearchTree 二叉查找树
    • AVLTree AVL平衡树
    • RedBlackTree 红黑树
    • SkipList 跳跃表
    • BPlusTree B+树
    • BMinusTree B-树
  • Chapter-5 DynamicProgramming 第5章 动态规划
    • Section-1 LinearDP 第1节 线性动规
      • LongestCommonSubsequence 最长公共子序列
      • LongestIncreasingSubsequence 最长递增子序列
      • BidirectionalSubsequence 双向子序列
      • MaximumContinuousSubsequenceSum 最大连续子序列和
      • LongestPalindromicSubsequence 最长回文子序列
    • Section-2 BagDP 第2节 背包问题
      • 01-Bag 01背包
      • CompleteBag 完全背包
      • TwoDimensionBag 二维背包
    • Section-3 RegionalDP 第3节 区域动规
      • MinimumMergeCost - 最小合并代价
      • UniquePath 唯一路径
      • TrianglePath 三角形路径
    • Section-4 TreeDP 第4节 树形动规
      • MaximumBinaryTree 最大二叉树
      • MaxMultipleTree 最大多叉树
      • MaximumBinaryTreeRadiusSum 最大二叉树和
  • Chapter-6 GraphTheory 第6章 图论
    • Section-1 Traverse 第1节 遍历
      • DepthFirstSearch 深度优先搜索
      • BreadthFirstSearch 广度优先搜索
      • TopologicalSort 拓扑排序
      • EulerCycle 欧拉回路
    • Section-2 MinSpanningTree 第2节 最小生成树
      • Kruskal Kruskal算法
      • Prim Prim算法
    • Section-3 ShortestPath 第3节 最短路径
      • BellmanFord BellmanFord算法
      • Dijkstra Dijkstra算法
      • FloydWarshall FloydWarshall算法
      • DifferentConstraints 差分约束
    • Section-4 StronglyConnectedComponents 第4节 强连通分支
      • Kosaraju Kosaraju算法
      • Tarjan Tarjan算法
      • 2-SAT 2-SAT问题
    • Section-5 NetworkFlow 第5节 网络流
      • EdmondsKarp EdmondsKarp算法(最短路径增广算法)
      • PushRelabel 压入与重标记算法
      • Dinic - Dinic算法
      • MinimumCostFlow - 最小费用流
      • MultipleSourceMultipleSinkMaxflow - 多源点多汇点最大流
    • Section-6 BinaryMatch 第6节 二分匹配
      • Hungarian 匈牙利算法
      • HopcroftKarp Hopcroft-Karp算法
      • MatchToMaxflow 二分匹配转化为最大流
      • KuhnMunkres Kuhn-Munkres算法
      • Introduction-Domination,Independent,Covering,Clique 介绍支配集、独立集、覆盖集和团
      • WeightedCoveringAndIndependentSet 最小点权覆盖和最大点权独立集
      • MinimumDisjointPathCovering 最小不相交路径覆盖
      • MinimumJointPathCovering 最小可相交路径覆盖
      • Coloring 染色问题
  • Chapter-7 CombinatorialMathematics 第7章 组合数学
    • FullPermutation 全排列
    • Combination 组合
    • Permutation 排列
    • PermutationGroup 置换群
  • Chapter-8 NumberTheory 第8章 数论
    • PrimeSieve 素数筛法
    • GreatestCommonDivisor 最大公约数
    • Euclid 欧几里得算法
    • ExtendedEuclid 扩展欧几里得算法
    • ChineseRemainerTheorem 中国剩余定理
    • ModularExponentiation 模幂运算
  • Chapter-9 LinearAlgebra 第9章 线性代数
    • Section-1 Matrix 第1节 矩阵
      • Strassen Strassen算法
      • GaussElimination 高斯消元法
      • LUP LUP分解
      • InverseMatrix 矩阵求逆
    • Section-2 LinearProgramming 第2节 线性规划
      • Simplex 单纯形算法
      • Dinkelback Dinkelback算法
  • Chapter-10 AnalyticGeometry 第10章 解析几何
    • Section-1 Polygon 第1节 多边形
      • Cross 向量叉积
      • SegmentIntersection 线段相交
      • Sweeping 扫除算法
      • ConvexPolygonArea 凸多边形面积
      • ConvexPolygonGravityCenter 凸多边形重心
      • NearestNeighbor 最近点对
    • Section-2 ConvexHull 第2节 凸包
      • GrahamScan Graham扫描算法
      • QuickHull 快速凸包算法
      • RotatingCalipers 旋转卡壳
  • Chapter-11 PatternMatch 第11章 文本匹配
    • SimpleMatch 简单匹配
    • AhoCorasickAutomata AC自动机
    • KnuthMorrisPratt KMP匹配算法
    • RabinKarp RabinKarp算法
    • BoyerMoore BoyerMoore算法
  • Chapter-12 GameTheory 第12章 博弈论
    • BashGame 巴什博弈
    • WythoffGame 威佐夫博弈
    • NimGame 尼姆博弈
Powered by GitBook
On this page
  • 问题
  • 解法
  • StackOverflow - algorithm-to-return-all-combinations-of-k-elements-from-n
  • 二项式系数
  • Chase’s Twiddle - Algorithm 382: Combinations of M out of N Objects:
  • Buckles - Algorithm 515: Generation of a Vector from the Lexicographical Index:
  • Remark on algorithm 515: Generation of a vector from the lexicographical index combinations
  • 源码
  • 测试
  1. Chapter-7 CombinatorialMathematics 第7章 组合数学

Combination 组合

PreviousFullPermutation 全排列NextPermutation 排列

Last updated 6 years ago

问题

从包含nnn个元素的集合s=[x0,x1,x2,…,xn−1]s = [x_0, x_1, x_2, \dots ,x_{n-1} ]s=[x0​,x1​,x2​,…,xn−1​]中任意取mmm个元素(m≤nm \leq nm≤n)组成组合,求所有组合。集合sss中任意两元素互不相同。

解法

设置集合c=[c0,c1,c2,…,cm−1]c = [ c_0, c_1, c_2, \dots , c_{m-1} ]c=[c0​,c1​,c2​,…,cm−1​]表示集合sss的选择,ci=1c_i = 1ci​=1表示选择数字xix_ixi​,ci=0c_i = 0ci​=0表示不选择数字xix_ixi​。比如集合s=[x0,x1,x2,…,xn−1]s = [ x_0, x_1, x_2, \dots, x_{n-1} ]s=[x0​,x1​,x2​,…,xn−1​]中取出组合

[x0,x1,x2][x_0, x_1, x_2][x0​,x1​,x2​]

等价于集合

c=[10,11,12,03,…,0n−1]c = [1_0, 1_1, 1_2, 0_3, \dots, 0_{n-1} ]c=[10​,11​,12​,03​,…,0n−1​]

即元素[c0,c1,c2][ c_0, c_1, c_2 ][c0​,c1​,c2​]为111,元素[c3,…,cn−1][ c_3, \dots, c_{n-1} ][c3​,…,cn−1​]为000。

可以看出,包含nnn个元素的集合s=[x0,x1,x2,…,xn−1]s = [x_0, x_1, x_2, \dots, x_{n-1} ]s=[x0​,x1​,x2​,…,xn−1​]中取出mmm个元素的所有组合,可以转化为求有iii个111,m−im - im−i个000的集合ccc的唯一全排列,其中0≤i≤m0 \leq i \leq m0≤i≤m。

(1)(1)(1) 从集合sss中取出000个元素作为组合,即ccc中有000个111,mmm个000,唯一全排列有C0m=1C_0^m = 1C0m​=1个

[][][]

只要求出所有唯一全排列,即可反向求出对应的组合。

StackOverflow - algorithm-to-return-all-combinations-of-k-elements-from-n

二项式系数

Chase’s Twiddle - Algorithm 382: Combinations of M out of N Objects:

Buckles - Algorithm 515: Generation of a Vector from the Lexicographical Index:

Remark on algorithm 515: Generation of a vector from the lexicographical index combinations

源码

测试

(2)(2)(2) 从集合sss中取出111个元素作为组合。则ccc中有111个111,m−1m-1m−1个000,唯一全排列有C1m=mC_1^m = mC1m​=m个

[10,01,02,…,0n−1][00,11,02,…,0n−1][00,01,12,…,0n−1]⋯[00,01,02,…,1n−1]\begin{array}{lcr} [ 1_0, 0_1, 0_2, \dots , 0_{n-1} ] \\ [ 0_0, 1_1, 0_2, \dots , 0_{n-1} ] \\ [ 0_0, 0_1, 1_2, \dots , 0_{n-1} ] \\ \cdots \\ [ 0_0, 0_1, 0_2, \dots , 1_{n-1} ] \end{array}[10​,01​,02​,…,0n−1​][00​,11​,02​,…,0n−1​][00​,01​,12​,…,0n−1​]⋯[00​,01​,02​,…,1n−1​]​

(3)(3)(3) 从集合sss中取出222个元素作为组合,则ccc中有222个111,m−2m-2m−2个000,唯一全排列有C2m=mC_2^m = mC2m​=m个。

包含nnn个元素中选出mmm个元素的所有组合,需要重复m+1m + 1m+1次,每次求有iii个111,m−im - im−i个000的集合ccc的唯一全排列,其中i∈[0,m]i \in [0, m]i∈[0,m]。根据全排列反向得到组合,算法结束。第iii步求唯一全排列的时间复杂度为O(i!)O(i!)O(i!),该算法的时间复杂度为O(Cmn)=O(n!m!⋅(n−m)!)O(C_m^n) = O(\frac{n!}{m! \cdot (n-m)!})O(Cmn​)=O(m!⋅(n−m)!n!​)。

http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n
https://en.wikipedia.org/wiki/Binomial_coefficient
http://dl.acm.org/citation.cfm?id=362502
http://www.netlib.no/netlib/toms/382
http://dl.acm.org/citation.cfm?id=355739
https://www.researchgate.net/profile/Bill_Buckles/publication/220492658_Algorithm_515_Generation_of_x_Vector_from_the_Lexicographical_Index_G6/links/5716d7ad08ae497c1a5706ec.pdf
http://dl.acm.org/citation.cfm?id=1236470
Combination.h
Combination.cpp
CombinationTest.cpp