Collection 07
Algorithms
State the precondition, then follow the invariant.
Algorithm entries keep the input constraint, the invariant, and the cost together. The goal is to know when a familiar method still applies, not just to repeat its name.
4 entries
Download the collection index (JSON)Index
On this shelf
| Method | Problem family | Precondition and cost |
|---|---|---|
| The Queue in Breadth-First Search Concept | Graphs | Why does BFS need a queue rather than a stack? |
| Average and Worst-Case Hash Table Lookups Concept | Hashing | When is a hash lookup O(1), and what breaks that bound? |
| Finding the Left Boundary with Binary Search Guide | Search | How do I find the first element that satisfies a monotone predicate? |
| Stable Sorting with Equal Keys Concept | Sorting | What does stability preserve, and when does it matter? |
Reading path
A way in
Read the left-boundary search first for the invariant pattern. Then compare stable sorting and BFS queues to see how an ordering guarantee travels through a data structure.
- 01 Finding the Left Boundary with Binary SearchBinary search finds the first element that satisfies a monotone predicate by keeping a half-open range and moving the lower bound past every false midpoint. The predicate must be false on a prefix and true on the suffix, and the final lower bound is the answer.
- 02 Stable Sorting with Equal KeysA stable sort preserves the original relative order of records that compare equal, so only ties are affected. Stability matters when an earlier ordering carries meaning, such as a secondary key or an arrival sequence, and it is a property of the algorithm rather than the comparator.
- 03 The Queue in Breadth-First SearchBFS uses a FIFO queue so that vertices are expanded in discovery order, which is nondecreasing distance from the source; that ordering is what makes the first visit to a vertex its shortest path. A stack expands the newest vertex first, which produces depth-first search instead.
All entries
Full catalog
- Average and Worst-Case Hash Table Lookups A hash lookup is O(1) on average when the hash spreads keys evenly and the load factor stays bounded, but the bound is expected rather than worst-case. Collisions concentrated in one bucket, from a weak hash or adversarial keys, degrade a lookup to O(n).
- Finding the Left Boundary with Binary Search Binary search finds the first element that satisfies a monotone predicate by keeping a half-open range and moving the lower bound past every false midpoint. The predicate must be false on a prefix and true on the suffix, and the final lower bound is the answer.
- Stable Sorting with Equal Keys A stable sort preserves the original relative order of records that compare equal, so only ties are affected. Stability matters when an earlier ordering carries meaning, such as a secondary key or an arrival sequence, and it is a property of the algorithm rather than the comparator.
- The Queue in Breadth-First Search BFS uses a FIFO queue so that vertices are expanded in discovery order, which is nondecreasing distance from the source; that ordering is what makes the first visit to a vertex its shortest path. A stack expands the newest vertex first, which produces depth-first search instead.