Algorithms And Data Structures Codexery

Brute-force search

A problem-solving method checking all possible candidates.

Brute-force search

Brute-force search, also called exhaustive search or generate and test, is a foundational algorithmic paradigm in computer science. It operates by enumerating every possible candidate solution to a problem and testing each one against the problem’s requirements. For instance, to find the divisors of a natural number, a brute-force algorithm would check every integer from one up to that number, testing whether each divides it evenly. For the eight queens puzzle, a brute-force approach would examine every possible arrangement of eight queens on a sixty-four-square chessboard and verify whether any queen attacks another. The method is straightforward to implement and guarantees finding a solution if one exists, but its cost scales directly with the number of candidates. This number often grows explosively as the problem size increases, a phenomenon known as combinatorial explosion. For example, checking all divisors of a sixteen-digit number requires roughly ten quadrillion instructions, taking days on a typical computer. Similarly, rearranging twenty letters yields about two quintillion candidates, a search that can last years. Consequently, brute-force search is practical mainly for small problem instances, when problem-specific heuristics can shrink the candidate set, or when simplicity of implementation outweighs processing speed. It is also used in critical applications where algorithm errors are unacceptable, or when proving mathematical theorems. The technique serves as a baseline for benchmarking other algorithms and can be viewed as the simplest metaheuristic. It differs from backtracking, which discards large groups of solutions without explicit enumeration. The core algorithm requires four procedures: one to generate the first candidate, one to produce the next candidate (returning a null value when none remain), one to validate a candidate, and one to output a valid solution. The search then iterates through candidates, testing each.

field
Computer science
known_for
Systematic enumeration of all candidate solutions
type
Algorithmic paradigm
also_known_as
Exhaustive search, generate and test
related_concepts
Combinatorial explosion, linear search, backtracking

Lore & Background

The brute-force search method is simple to implement and will always find a solution if one exists. Its implementation costs are proportional to the number of candidate solutions, which in many practical problems grows very quickly as problem size increases—a phenomenon called combinatorial explosion. For example, finding divisors of a number n requires testing all integers from 1 to n; for a 64-bit natural number, this could take about 10 years on a typical PC. The method is typically used when problem size is limited, when heuristics can reduce the candidate set, or when simplicity of implementation outweighs processing speed.

Reader's Guide

Brute-force search serves as a baseline method for benchmarking other algorithms or metaheuristics and can be viewed as the simplest metaheuristic. It is used in critical applications where algorithm errors would have serious consequences, or when using a computer to prove a mathematical theorem. The method should not be confused with backtracking, which can discard large solution sets without explicit enumeration. Reordering the search space can also improve expected running time when only one solution is needed. The technique remains fundamental in computer science for its generality and reliability.

Did You Know?

Frequently Asked Questions

Who is Brute-force search?

Brute-force search is a foundational algorithmic paradigm in computer science that solves problems by methodically testing every single candidate until the correct one is identified. It is one of the most straightforward and universally applicable strategies in the field.

What are Brute-force search's powers/role?

Its core ability is systematic enumeration: it generates every possible candidate and checks each one against the problem's requirements. This makes it applicable to virtually any problem where a finite set of candidates exists, regardless of domain.

How does Brute-force search's story end?

Brute-force search typically gets outpaced by more efficient algorithms once the candidate space grows large, a phenomenon known as combinatorial explosion. In practice it often serves as a correctness baseline or last-resort fallback rather than the final solution for large-scale problems.

Why is Brute-force search important?

It provides a guaranteed-correct reference point that proves a problem is solvable and sets the performance bar against which smarter techniques like backtracking or pruning are measured. Without it, there would be no simple benchmark to demonstrate that an optimization actually helps.

What are Brute-force search's aliases?

It is also widely called "exhaustive search" or "generate and test" in the literature. All three names describe the same core idea of leaving no candidate unchecked before declaring a result.

More in Algorithms And Data Structures 1-24

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →