Summary: Brute Force
Everything on one page, to review the chapter before moving on.
Key ideas
- Brute force solves a problem in the most direct way, following its statement. Exhaustive search generates and tests every candidate of a combinatorial space (Brute Force and Exhaustive Search).
- It is simple, general and easy to get right, and for small inputs it is often good enough.
- Exhaustive search over subsets costs and over permutations : only very small instances are feasible, and faster hardware barely helps.
- Brute force is the baseline to measure other algorithms and an excellent testing oracle.
- The other techniques of the course are ways of avoiding its work: splitting, choosing greedily, reusing subproblems, pruning and bounding.
At a glance
| Problem | Brute-force cost | Better approach in the course |
|---|---|---|
| Searching an element | Binary search on sorted data, | |
| Maximum pairwise product | Linear scan, | |
| Sorting by definition (selection, bubble) | Quicksort, Mergesort, | |
| Computing | multiplications | Divide and conquer, |
| Matrix multiplication | — | |
| Sorting by trying every permutation until one is sorted | Mergesort, |
When brute force is the right choice
- The input is small and will stay small.
- The algorithm runs once, and writing it quickly matters more than running it quickly.
- You need a trusted reference to test a faster algorithm.
Saved only in your browser
Questions, or something wrong?
Errors, suggestions and public questions open a form on GitHub, visible to everyone.