## A.7 Permutations and Combinations
Suppose we have four balls marked A, B, C, and D in an urn or container, and two balls will he drawn from the urn. To win a lottery, we must pick the balls in the order in which they are drawn. To gain insight into the likelihood of our winning, we should determine how many outcomes are possible. The possible outcomes are:
AB
AC
AD
BA
BC
BD
CA
CB
CD
DA
DB
DC
The outcomes AB and BA, for example, are different because we must pick the balls in the correct order. We have listed 12 different outcomes. However, can we be sure that these are all the outcomes? Notice that we arranged the outcomes in four rows and three columns. Each row corresponds to a distinct choice for the first ball; there are four such choices. Once we have made that choice, the second letters in the entries in a row correspond to the remaining distinct choices for the second ball; there are three such choices. The total number of outcomes is therefore
![](https://box.kancloud.cn/fa1a6d429505d9afb9084c847bb955b1_96x22.jpg)
This result can be generalized. For example, if we have four balls and three are drawn, the first ball can be any one of four; once the first ball is drawn, the second ball can be any of three; and once the second ball is drawn, the third ball can be any of two. The number of outcomes is therefore
![](https://box.kancloud.cn/fd4e11ba354bc2ca8528ebc8b5174234_123x21.jpg)
In general, if we have *n* balls, and we are picking *k* of them, the number of possible outcomes is
![](https://box.kancloud.cn/09423b8014f9ed4de5e9e7fe0de1c661_174x21.jpg)
This is called the number of permutations of *n* objects taken *k* at a time. If *n* = 4 and *k* = 3, this formula yields
![](https://box.kancloud.cn/07b9d8a89f31e7ff930957d226345106_298x23.jpg)
which is the result already obtained. If *n* = 10 and *k* = 5, the formula yields
![](https://box.kancloud.cn/67cd06912578d629a2d1fbeaa88d15c5_400x21.jpg)
If *k* = *n*, we are picking all the balls. This is simply called the number of ***permutations*** of *n* objects. The previous formula shows that it is given by
![](https://box.kancloud.cn/7853d5a02112d4a90f21f60eeea68aa3_203x22.jpg)
Recall that for a positive integer *n, n*! is defined as the integer times all the positive integers less than it, the value of 0! is defined to be 1, and *n*! is not defined for negative integers.
Next consider a lottery that we can win by merely picking the correct balls. That is, we do not have to get the order right. Suppose again that there are four balls marked A, B, C, and D, and two are drawn. Each outcome in this lottery corresponds to two outcomes in the previous lottery. For example, the outcomes AB and BA in the other lottery are both the same for the purposes of this lottery. We will call this outcome
A and B.
Because two outcomes in the previous lottery correspond to one outcome in this lottery, we can determine how many outcomes there are in this lottery by dividing the number of outcomes in the previous lottery by 2. This means that there are
![](https://box.kancloud.cn/d1621eb1ab78c32430395cc9ac8832c2_85x41.jpg)
outcomes in this lottery. The six distinct outcomes are
A and B A and C A and D B and C B and D C and D.
Suppose now that three balls are drawn from the urn containing the four balls marked A, B, C, and D, and we do not have to get the order right. Then the following outcomes are all the same for the purposes of this lottery:
ABC ACB BAC BCA CAB CBA.
These outcomes are simply the permutations of three objects. Recall that the number of such permutations is given by 3! = 6. To determine how many distinct outcomes there are in this lottery, we need to divide by 3! the number of distinct outcomes in the lottery, where the order does matter. That is, there are
![](https://box.kancloud.cn/d3f604f4e7cc66f4f3f0121f66306809_107x42.jpg)
outcomes in this lottery. They are
A and B and C A and B and D A and C and D A and C and D.
In general, if there are *n* balls and *k* balls are drawn and the order does not matter, the number of distinct outcomes is given by
![](https://box.kancloud.cn/ee4f026c9580232c2faee0e1cdacf7e4_206x42.jpg)
This is called the number of ***combinations*** of *n* objects taken *k* at a time. Because
![](https://box.kancloud.cn/86ba7ad3cf8bd464b558001c7ade9c2c_400x75.jpg)
the formula for the number of combinations of *n* objects taken *k* at a time is usually shown as
![](https://box.kancloud.cn/72fad8e110ad4a448fa18cb2514b290f_94x45.jpg)
Using this formula, the number of combinations of eight objects taken three at a time is given by
![](https://box.kancloud.cn/2ed986f661652ec13ad382ae6b1f2890_127x44.jpg)
The ***Binomial theorem***, which is proven in algebra texts, states that for any nonnegative integer *n* and real numbers *a* and *b*,
![](https://box.kancloud.cn/3427b366c7837a1090e51ea8172279e1_257x53.jpg)
Because the number of combinations of *n* objects taken *k* at a time is the coefficient of *akbn-k* in this expression, that number is called the ***binomial coefficient***. We will denote it ![](https://box.kancloud.cn/24ee194554b8a360d973549b31db2c47_39x47.jpg).
Example A.10**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**We show that the number of subsets, including the empty set, of a set containing *n* items is 2*n*. For 0 ≤ *k* ≤ *n*, the number of subsets of size *k* is the number of combinations of *n* objects taken *k* at a time, which is ![](https://box.kancloud.cn/792dff136da8a8bc107f5f3bb603c0fb_29x26.jpg). This means that the total number of subsets is
![](https://box.kancloud.cn/c9dc0b691e01e63e352cae88c0b76507_343x54.jpg)
The second-to-last equality is by the Binomial theorem.
**![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**
- Table of Contents
- BackCover
- Foundations of Algorithms Using C++ Pseudocode, Third Edition
- Preface
- Chapter Contents
- Pedagogy
- Course Outlines
- Acknowledgments
- Errors
- Chapter 1: Algorithms - Efficiency, Analysis, and Order
- 1.2 The Importance of Developing Efficient Algorithms
- 1.3 Analysis of Algorithms
- 1.4 Order
- 1.5 Outline of This Book
- Exercises
- Chapter 2: Divide-and-Conquer
- 2.1 Binary Search
- 2.2 Mergesort
- 2.3 The Divide-and-Conquer Approach
- 2.4 Quicksort (Partition Exchange Sort)
- 2.5 Strassen's Matrix Multiplication Algorithm
- 2.6 Arithmetic with Large Integers
- 2.7 Determining Thresholds
- 2.8 When Not to Use Divide-and-Conquer
- Exercises
- Chapter 3: Dynamic Programming
- 3.1 The Binomial Coefficient
- 3.2 Floyd's Algorithm for Shortest Paths
- 3.3 Dynamic Programming and Optimization Problems
- 3.4 Chained Matrix Multiplication
- 3.5 Optimal Binary Search Trees
- 3.6 The Traveling Salesperson Problem
- Exercises
- Chapter 4: The Greedy Approach
- 4.1 Minimum Spanning Trees
- 4.2 Dijkstra's Algorithm for Single-Source Shortest Paths
- 4.3 Scheduling
- 4.4 Huffman Code
- 4.5 The Greedy Approach versus Dynamic Programming: The Knapsack Problem
- Exercises
- Chapter 5: Backtracking
- 5.2 The n-Queens Problem
- 5.3 Using a Monte Carlo Algorithm to Estimate the Efficiency of a Backtracking Algorithm
- 5.4 The Sum-of-Subsets Problem
- 5.5 Graph Coloring
- 5.6 The Hamiltonian Circuits Problem
- 5.7 The 0-1 Knapsack Problem
- Exercises
- Chapter 6: Branch-and-Bound
- 6.1 Illustrating Branch-and-Bound with the 0 - 1 Knapsack problem
- 6.2 The Traveling Salesperson Problem
- 6.3 Abductive Inference (Diagnosis)
- Exercises
- Chapter 7: Introduction to Computational Complexity - The Sorting Problem
- 7.2 Insertion Sort and Selection Sort
- 7.3 Lower Bounds for Algorithms that Remove at Most One Inversion per Comparison
- 7.4 Mergesort Revisited
- 7.5 Quicksort Revisited
- 7.6 Heapsort
- 7.6.1 Heaps and Basic Heap Routines
- 7.6.2 An Implementation of Heapsort
- 7.7 Comparison of Mergesort, Quicksort, and Heapsort
- 7.8 Lower Bounds for Sorting Only by Comparison of Keys
- 7.8.1 Decision Trees for Sorting Algorithms
- 7.8.2 Lower Bounds for Worst-Case Behavior
- 7.8.3 Lower Bounds for Average-Case Behavior
- 7.9 Sorting by Distribution (Radix Sort)
- Exercises
- Chapter 8: More Computational Complexity - The Searching Problem
- 8.1 Lower Bounds for Searching Only by Comparisons of Keys
- 8.2 Interpolation Search
- 8.3 Searching in Trees
- 8.4 Hashing
- 8.5 The Selection Problem: Introduction to Adversary Arguments
- Exercises
- Chapter 9: Computational Complexity and Interactability - An Introduction to the Theory of NP
- 9.2 Input Size Revisited
- 9.3 The Three General Problem Categories
- 9.4 The Theory of NP
- 9.5 Handling NP-Hard Problems
- Exercises
- Chapter 10: Number-Theoretic Algorithms
- 10.1 Number Theory Review
- 10.2 Computing the Greatest Common Divisor
- 10.3 Modular Arithmetic Review
- 10.4 Solving Modular Linear Equations
- 10.5 Computing Modular Powers
- 10.6 Finding Large Prime Numbers
- 10.7 The RSA Public-Key Cryptosystem
- Exercises
- Chapter 11: Introduction to Parallel Algorithms
- 11.1 Parallel Architectures
- 11.2 The PRAM Model
- Exercises
- Appendix A: Review of Necessary Mathematics
- A.2 Functions
- A.3 Mathematical Induction
- A.4 Theorems and Lemmas
- A.5 Logarithms
- A.6 Sets
- A.7 Permutations and Combinations
- A.8 Probability
- Exercises
- Appendix B: Solving Recurrence Equations - With Applications to Analysis of Recursive Algorithms
- B.2 Solving Recurrences Using the Characteristic Equation
- B.3 Solving Recurrences by Substitution
- B.4 Extending Results for n, a Power of a Positive Constant b, to n in General
- B.5 Proofs of Theorems
- Exercises
- Appendix C: Data Structures for Disjoint Sets
- References
- Index
- Index_B
- Index_C
- Index_D
- Index_E
- Index_F
- Index_G
- Index_H
- Index_I
- Index_J
- Index_K
- Index_L
- Index_M
- Index_N
- Index_O
- Index_P
- Index_Q
- Index_R
- Index_S
- Index_T
- Index_U
- Index_V
- Index_W-X
- Index_Y
- Index_Z
- List of Figures
- List of Tables
- List of Algorithms, Examples, and Theorems
- List of Sidebars