Combinations
In mathematics, a combination is a selection of items from a larger set, where the order of the selected items does not matter. Combinations are used in various fields such as combinatorics, probability, and statistics to solve problems involving selecting items from a set without regard to the order.
We can use Binomial Coefficients to solve problems involving combinations. The binomial coefficient C(n, k)
represents the number of ways to choose k
elements from a set of n
elements without regard to the order.
Binomial Coefficient
The binomial coefficient, denoted as C[n][k]
represents the number of ways to choose k elements from a set of n elements without regard to the order
.
It is used in combinatorics to solve problems where you need to select subsets from larger sets.
Combinations vs Permutations
Combinations and permutations are both ways to select items from a collection, but they differ in how they treat the order of the selected items.
Combinations
A combination is a selection of items from a collection where the order does not matter.
For example, for the same 3 letters (A, B, C) and selecting 2 of them, the combinations are: AB, AC, BC (BA and AB are considered the same, because the order doesn’t matter).
Permutations
A permutation is a selection of items from a collection where the order matters.
For example, for the set {A, B, C}
, the permutations of size 2 are: (A, B), (B, A), (A, C), (C, A), (B, C), (C, B)
(A, B) is different from (B, A) in permutations because the order matters.
In general, the number of combinations is less than the number of permutations because combinations do not consider the order of the items.
Python Implementation using built-in itertools
module
from itertools import combinations
arr = [1, 2, 3, 4]
comb_length = 2
comb = combinations(arr, comb_length)
for ch in comb:
print(ch)
Output is:
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
Custom Python Implementation
def combinations_backtracking(arr, k):
result = []
combination = []
def backtrack(start):
if len(combination) == k:
result.append(combination.copy())
return
for i in range(start, len(arr)):
combination.append(arr[i])
backtrack(i + 1)
combination.pop()
backtrack(0)
return result
# Example usage:
arr = [1, 2, 3, 4]
k = 2
combinations = combinations_backtracking(arr, k)
print(combinations)
Problems
- Counting Combinations. A class has 12 students, and the teacher needs to form a committee of 5 students. How many different ways can the committee be selected? This is a simple combination problem where order does not matter. The answer is the number of ways to choose 5 students from 12, which is
C(12,5)
. - Handshakes in a Group. In a group of 10 people, each person shakes hands with every other person exactly once. How many handshakes occur? Each handshake involves 2 people, so this is equivalent to choosing 2 people from a group of 10. The number of handshakes is
C(10,2)
. - Paths in a Grid. You are at the top-left corner of a 5x5 grid, and you want to reach the bottom-right corner. You can only move right or down. How many distinct paths can you take?
- Lottery Problem. In a lottery, you pick 6 numbers from a set of 49 numbers. How many different sets of 6 numbers can you choose?
- Forming Teams. There are 8 girls and 7 boys in a club. You need to form a team of 4 people, where at least 2 members are girls. How many different teams can be formed?
- Binary Strings with a Fixed Number of 1’s. How many binary strings of length 8 have exactly 3 ones?