Teaching Kids Programming – Introduction to Permutation and Combination


Teaching Kids Programming: Videos on Data Structures and Algorithms

Permutation and Combination are the math concepts to count. Given N items, if we want to choose M items (M no bigger than N) from it, there are C(N, M) ways if the order does not matter, or P(N, M) is the order matters.

Permutation

If there are N items, we have N choices for the first item, and N-1 for the second item and so on. Thus:

tex_e5c3955b12eec3ddc25eea99a99667ea Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_c3e2cbe5cf890e5192f49adf1c7b415e Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

In particular:
tex_250a3061f5e21c67bb6c677449c32979 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_d511a6648f66ec6338bbf377e8878ed8 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_1ac889a552d7e7e09c0d7261f1cf60b7 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

Combination

For Combination, the order does not matter. So we can first count the P(N, M) and then divide by P(M, N).

tex_bb979bba6ad7fe95cb3aee4bc660f5c9 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_2ecaccf3738f6543269e6dd645c34231 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

In particular:
tex_4e71b9eba27b0d6d48e1974f1c246e90 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_5235ee5f587ac28100f5e3675e952c20 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video
tex_ba87f5ff7a9f4c050ebe1c9a5dc60cfe Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

Choosing M items from N is the same as choosing N-M items from N:

tex_65a992197203242dd1b0197d6ab3b611 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

Dynamic Programming to Compute the Combination

If we want to calculate C(N, M), for the last item, we can choose to pick or not pick. If we pick, we have C(N-1, M-1), if we don’t pick the last item, then we have C(N-1, M). Therefore:

tex_2c26edf2b163b8221e9c8f78243a34a3 Teaching Kids Programming - Introduction to Permutation and Combination algorithms Combination Combinatoric Mathematics math Permutation programming languages python recursive teaching kids programming youtube video

To implement this in Python via Recursive Algorithm with Memoization aka Dynamic Programming Algorithm:

1
2
3
4
5
6
7
8
# caching the intermediate results
@lru_cache(None)
def C(N, M):
  if M == N or M == 0:
    return 1
  if M == 1:
    return N
  return C(N - 1, M - 1) + C(N - 1, M) 
# caching the intermediate results
@lru_cache(None)
def C(N, M):
  if M == N or M == 0:
    return 1
  if M == 1:
    return N
  return C(N - 1, M - 1) + C(N - 1, M) 

Pascal Triangle and Combination

Revisit the Pascal Triangle:

1 
1 1
1 2 1
1 3 3 1
1 4 6 4 1

We can see for the numbers in Pascal Triangle, we can see it equals to those two numbers above it – which perfectly maps to the Combination Formula.

Permutations and Combinations

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
1109 words
Last Post: Improved Depth First Search Algorithm to Generate Combinations of Parentheses
Next Post: Design a Rate Limiter in Python

The Permanent URL is: Teaching Kids Programming – Introduction to Permutation and Combination

Leave a Reply