Combinatorics and Counting
π§© Combinatorics and Counting
Combinatorics is the branch of mathematics dealing with the counting, arrangement, and combination of elements within sets. In computer science, this is crucial for Complexity Analysis, Probability, and Algorithm Design.
π’ Part 1: Basic Counting Principles
Before diving into complex formulas, we must understand two fundamental rules.
1. Addition Rule (OR)
If task can be done in ways and task in ways, and and cannot occur simultaneously, then choosing or can be done in ways.
2. Multiplication Rule (AND)
If a task has two independent steps, where step 1 can be done in ways and step 2 in ways, then the total task can be done in ways. Example: A password consists of 3 letters followed by 2 digits. Total combinations: .
π‘ Part 2: Permutations and Combinations
The difference between these two lies in one simple rule: Does order matter?
1. Permutations () - Order Matters
A permutation is an ordered arrangement of objects.
- Formula (No repetition):
- Formula (With repetition):
Example: Arranging 3 people in a row of 3 chairs. Total ways: .
2. Combinations () - Order Does Not Matter
A combination is a selection of objects where the order doesnβt change the outcome.
- Formula:
Example: Selecting a team of 3 people from a group of 10. Total ways: .
π΄ Part 3: Advanced Principles
1. Inclusion-Exclusion Principle
Used to count the union of multiple sets while avoiding double-counting: Application: Counting users who performed action A OR action B in a database.
2. Pigeonhole Principle
If items are put into containers, with , then at least one container must contain more than one item. CS Context: This is why Hash Collisions are mathematically guaranteed when the number of possible keys exceeds the size of the hash table.
π» Programming Example: Calculating Combinations
import math
def combinations(n, r):
"""Calculates nCr (n choose r)"""
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
# Example: Selecting 3 items from a menu of 10
print(f"Combinations: {combinations(10, 3)}") # 120
# Generating all combinations (Python standard library)
from itertools import combinations as c_list
items = ['A', 'B', 'C', 'D']
print(f"All pairs from {items}: {list(c_list(items, 2))}")When to use which?
- Permutation: Creating a playlist (song order matters).
- Combination: Handing out 5 cards from a deck (hand value is the same regardless of deal order).
- Counting Principle: Estimating the size of a search space for an algorithm.