Explain the Brute Force Method of Algorithm Design with Suitable Examples
Brute-force search (also called exhaustive search) is a general algorithm design paradigm that solves a problem by enumerating all candidates and verifying which one(s) satisfy the goal condition. It is “generate and test” in spirit: systematically generate possibilities, test each, and return the first valid (satisficing) or the best among them (optimization).
Because it tries everything, brute force is typically easy to implement and always correct, but it often becomes impractical due to rapid growth in the number of candidates (e.g., exponential or factorial). For instance, brute-force substring matching has a worst-case time of for a pattern of length in a text of length . Similarly, the eight queens brute-force idea is to examine all arrangements and check validity.
Key term set: candidate space, verification predicate, generate-and-test, worst-case complexity.
Footnotes
-
Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩ ↩2
-
Medium/Brute Force and Exhaustive Search - Explains brute-force string matching and states the worst-case is with pseudocode. ↩
Brute-Force & Exhaustive Search
Core idea and “algorithm skeleton”
A brute-force algorithm usually follows this structure:
- Identify what a candidate is (a permutation, subset, route, substring alignment, etc.).
- Enumerate all candidates in the candidate space.
- For each candidate, use a verification predicate to decide if it meets the requirement.
- Return the correct answer (first valid) or the optimum (best valid).
This “blind iteration” characterization is a standard view of brute force as a technique that “blindly iterates an entire domain of possible solutions” without shortcuts to improve performance.
Key term set: verification predicate, candidate generation, satisficing, optimization.
Footnotes
-
ALGORITHMS DESIGN STRATEGIES (Chapter 5 PDF) - Provides a classification of algorithm design strategies; describes brute force as iterating through a domain of solutions without shortcuts. ↩
Designing a Brute Force Solution (Generate → Test → Return)
- 1Step 1
Decide the unit you will enumerate (e.g., an entire path, a subset, a permutation, a substring alignment).
- 2Step 2
Determine how many candidates exist (e.g., subsets, permutations, substring alignments). Candidate counts drive runtime growth.
Footnotes
-
Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩
-
- 3Step 3
Implement a function that returns true/false (or a score) for a candidate. Example: check if two queens attack each other; check if sums equal target.
- 4Step 4
Use nested loops/recursive generation to traverse the full candidate space without skipping possibilities.
- 5Step 5
If the problem asks for any valid solution, stop at the first that passes. If it asks for the best, keep the best score seen so far.
Example 1: Linear search (brute force on an array)
Although linear search is often taught separately, it is still brute-force search over the candidate set “each index.” You check elements one-by-one until you find the key. A brute-force/linear approach has worst-case comparisons because you may scan the entire list when the key is absent.
Key term set: linear scan, sequential search, key (search target).
Footnotes
-
DEV Community: Brute Force Algorithms: The Power of Exhaustive Search - Includes linear/sequential search as a brute-force example and notes worst-case Θ(). ↩
Example 2: Brute-force substring pattern matching ( worst case)
Given text of length and pattern of length , the brute-force method aligns at every position from to , compares characters, and succeeds if all characters match. In the worst case, each alignment compares up to characters, for about comparisons.
Key term set: substring alignment, character comparisons, worst-case bound.
Footnotes
-
Medium/Brute Force and Exhaustive Search - Explains brute-force string matching and states the worst-case is with pseudocode. ↩
Example 3: Subset Sum via exhaustive enumeration ( subsets)
For a set of numbers and a target , brute force tests every subset and checks whether the subset sum equals . Because a set of elements has subsets, this runs in exponential time.
A worked micro-example conceptually:
Key term set: [power set]{def="Set of all subsets of a given set; size is ."}, subset enumeration, [exponential time]{def="Runtime that grows as for some constant c."}.
Footnotes
-
Subset Sum Problem — Definition, Formula & Examples (Mathwords) - States brute-force checks all subsets; since there are subsets, it runs in exponential time. ↩
-
Subset Sum Problem (SlideShare example) - Provides an example of subset sum brute force (e.g., {1,9} sums to 10) and describes trying all subsets. ↩
Example 4: Traveling Salesman Problem (TSP) with permutations
For TSP, brute force enumerates all possible routes (permutations of cities), computes total route length, and returns the minimum. This approach is correct but becomes infeasible as grows because the number of permutations grows very fast (factorially). Brute force for TSP is commonly described as “calculate the total distance for every possible route and then select the shortest one,” but it is not efficient for large instances.
Key term set: route permutation, total distance objective, [factorial growth]{def="Candidate count that scales like ."}.
Footnotes
-
FreeCodeCamp: Brute Force Algorithms Explained - Explains brute-force solution for TSP by trying every possible route; notes inefficiency and gives context for brute-force complexity. ↩
How candidate counts explode for common brute-force patterns
Candidate counts are the main driver of brute-force runtime; verification per candidate may add a factor.
Where brute force fits in the algorithm design landscape
Common algorithm design paradigms include divide and conquer, dynamic programming, greedy, backtracking/branch-and-bound, etc. Brute force is a baseline that tests all candidates without structural pruning. Algorithm strategy material often lists brute force as one of the core design techniques alongside others.
A practical guideline: use brute force
- as a correctness baseline (verify results of faster algorithms),
- on small inputs, or
- when no better structure is available. A general discussion of choosing techniques notes brute force is correct but slow due to testing all possible solutions.
Key term set: algorithm paradigm, correctness baseline, structural pruning.
Footnotes
-
ALGORITHMS DESIGN STRATEGIES (Chapter 5 PDF) - Provides a classification of algorithm design strategies; describes brute force as iterating through a domain of solutions without shortcuts. ↩
-
CloudThat Resources: Brute Force, Greedy, and Dynamic Programming (Key Differences) - Discusses when to use brute force (small inputs/baselines) and why it is correct but slow (tries all solutions). ↩
Pro Tip
When teaching or learning, always write (1) the candidate definition and (2) the verification predicate first. Those two choices determine whether your brute force is correct and how fast it will be.
Warning: Runtime blow-up
Brute force can become infeasible because candidate counts grow extremely fast (e.g., subsets or permutations). Even if each test is simple, the total work can explode, which is why brute force is often used on small inputs or for benchmarking.2
Footnotes
-
Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩
-
FreeCodeCamp: Brute Force Algorithms Explained - Explains brute-force solution for TSP by trying every possible route; notes inefficiency and gives context for brute-force complexity. ↩
Common FAQs about brute force
Knowledge Check
Brute-force search (exhaustive search) is best described as: