liveBook · Manning

Chapter 6: Combinatorial algorithms

What you will learn in chapter 6 of Fabulous Adventures in Data Structures and Algorithms

This chapter introduces algorithms for generating Cartesian products, permutations, and combinations of sequences, and relates their ordered outputs to representations of integers.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces algorithms for generating Cartesian products, permutations, and combinations of sequences, and relates their ordered outputs to representations of integers.

Key terms: Cartesian product, permutations, combinations, sequences, pth sequence, decomposed into sums, factorial base representation, combinatorial base representation, .NET, base-class library

6.1 The Cartesian product

Introduces the Cartesian product as the meaning behind iterating over every character–weapon pairing and previews ways to compute it.

Key terms: Cartesian product, A×B, ordered pair, nested loop, foreach, query expression, from clause, CROSS JOIN

6.1.1 The Cartesian product of a few sets or sequences

This section develops Cartesian-product extension methods for fixed numbers of sets or sequences, progressing from pair tuples to lazy sequence products and projected results.

Key terms: Cartesian product, set, tuple, extension method, nested pairs, two-item tuple, three-valued tuple, CartesianProduct, sequence, HashSet<T>

6.1.2 The Cartesian product of arbitrarily many sequences

This section derives a lazy Cartesian-product algorithm for any number of same-typed sequences by progressively extending immutable deque prefixes.

Key terms: sequence of sequences, tuples, fixed size at compile time, extension method, Cartesian product, zero-element sequence, PushRight(), immutable deque, query expression, fully lazy

6.1.3 The connection to the integers

This section connects repeated Cartesian products to positional integer representations and introduces permutations as the operation for reordering a sequence.

Key terms: Cartesian product, lexicographic order, n-digit strings, decimal notation, base, place value, powers of 10, hexadecimal, octal, permutation

6.2 Permutations

This section introduces permutations as sequence reorderings, explains their factorial growth and uses, and shows how integer permutations can reorder lists of any type.

Key terms: permutation, reordering, shuffling, n factorial, n!, O(n!), distinct elements, indices, items, original list

6.2.1 Lexicographic permutations with repetitions

This section presents Pandita’s in-place next-lexicographic-permutation algorithm and an algorithm for directly generating a numbered lexicographic permutation of distinct integers.

Key terms: lexicographic ordering, Pandita’s algorithm, NextLexiPermutation, mutable array, duplicates, repetitions, lexicographic permutations, O(1) space, O(n) worst case, amortized O(1)

6.2.2 The factorial base representation of permutation numbers

This section shows how a permutation number can be written in factorial base and decoded into its corresponding lexicographic permutation.

Key terms: factorial base notation, permutation number, factorial base digit, remaining list, result list, lexicographic permutation, GetLexiPermutation, r

6.2.3 The Fisher–Yates shuffling algorithm

This section presents the Fisher–Yates method for randomly shuffling a mutable list and relates its random choices to a cheaper, nonlexicographic permutation numbering.

Key terms: Fisher–Yates algorithm, shuffle, mutable list, swap, factorial base, permutation number, Fisher–Yates permutation, lexicographic ordering, nonlexicographic ordering, O(1)

6.2.4 A recursive change-ringing algorithm

This section develops a recursive change-ringing order for permutations, shows how to generate either all changes or one indexed change, and analyzes the cost of the lazy implementation.

Key terms: change-ringing, permutations, swap of consecutive bells, lexicographic ordering, recursive change-ringing algorithm, n rows, (n−1)! columns, column direction, insert, from the right

6.2.5 Even’s change-ringing algorithm

This section presents Even’s iterative algorithm for generating every permutation in change-ringing order, including its mutable implementation safeguards and linear per-permutation cost.

Key terms: Even’s change-ringing algorithm, change-ringing order, mobile number, largest mobile number, direction, mobile, left, right, mutable arrays, permutation

6.3 Combinations

This section introduces combinations as order-preserving k-item subsequences, shows how index combinations apply to arbitrary lists, and previews ways to enumerate or directly generate them.

Key terms: combinations, subsequences, n, k, indices, k-item subsequence, 0, …, n−1, combination, lexicographic algorithm, Pandita’s algorithm

6.3.1 Lexicographic combinations with a twist

This section shows how to enumerate combinations in right-to-left lexicographic order using either an in-place successor algorithm or an immutable recursive algorithm.

Key terms: lexicographic order, right-to-left ordering, combination, bit strings, increasing integers, NextLexiCombination, Increasable, Combinations1, lexicographic combination, ToList()

6.3.2 Counting combinations

This section defines and efficiently computes binomial coefficients, then uses them to map lexicographic combination numbers back to combinations recursively.

Key terms: Choose operator, n C k, n choose k, empty combination, lexicographic order, BigInteger, Choose, n C k = n C (n-k), n C k = (n C (k-1))(n-k+1)/k, compound assignment operator

6.3.3 The combinatorial base representation of combinations

This section derives a direct combinatorial-base formula for finding a combination’s position in the ordered list of combinations.

Key terms: combination number, lexicographic order, Choose, combinatorial base, rightmost number, combination, BigInteger, memoize, recursion, deque

6.4 Summary

This summary recaps algorithms for Cartesian products, permutations, and combinations, along with their connections to integer representations.

Where this is used later

Concepts explained in this chapter

← Chapter 5: What’s up with you, Directed Acyclic Word Graph? Chapter 7: First abstract nonsense interlude: Category theory →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.