Mathwords logoMathwords

Brute Force Algorithm — Definition & Example

A brute force algorithm is a problem-solving method that lists every possible candidate answer, checks each one, and keeps the best or correct one. It trades speed for certainty: if you check every case, you cannot miss the answer.

An exhaustive search algorithm solves a problem over a finite candidate set SS. It enumerates every element of SS, tests each element against the problem's constraints, and evaluates the objective when one applies. It then returns a valid or optimal element. Its correctness follows from the completeness of the enumeration. Its running time is proportional to ∣S∣|S| times the cost of checking one candidate.

Worked Example

Problem: A hiker can carry at most 6 lb of extra gear. A tent weighs 3 lb and is worth 4 points of usefulness, a stove weighs 4 lb and is worth 5 points, and a lantern weighs 2 lb and is worth 3 points. Use brute force to find the most useful load that fits.
Step 1: List every possible candidate. Each item is either packed or left behind, so 3 items give 232^3 subsets, including packing nothing.
23=8 subsets2^3 = 8 \text{ subsets}
Step 2: Check each subset's weight against the 6 lb limit, and total the points for every subset that fits. Do not skip any.
∅:0 lb, 0 ptsT:3 lb, 4 ptsS:4 lb, 5 ptsL:2 lb, 3 ptsT+S:7 lb (too heavy)T+L:5 lb, 7 ptsS+L:6 lb, 8 ptsT+S+L:9 lb (too heavy)\begin{aligned} \varnothing &: 0 \text{ lb},\ 0 \text{ pts} & T &: 3 \text{ lb},\ 4 \text{ pts} \\ S &: 4 \text{ lb},\ 5 \text{ pts} & L &: 2 \text{ lb},\ 3 \text{ pts} \\ T+S &: 7 \text{ lb (too heavy)} & T+L &: 5 \text{ lb},\ 7 \text{ pts} \\ S+L &: 6 \text{ lb},\ 8 \text{ pts} & T+S+L &: 9 \text{ lb (too heavy)} \end{aligned}
Step 3: Compare the totals of the loads that fit and keep the largest. You checked every subset, so no better load can exist.
max⁡(0, 4, 5, 3, 7, 8)=8\max(0,\ 4,\ 5,\ 3,\ 7,\ 8) = 8
Answer: Pack the stove and the lantern: 6 lb and 8 points. Brute force guarantees this is the best load because all 8 subsets were checked.

Why It Matters

In algorithms and discrete math courses, brute force is the baseline. You write it first because it is easy to prove correct, then use it to test faster methods on small inputs. Its cost grows quickly. With 50 items to pack, there are 250≈1.13×10152^{50} \approx 1.13 \times 10^{15} subsets. A round trip through 20 cities has about 6.08×10166.08 \times 10^{16} possible tours. In cryptography, designers choose key sizes such as 128 bits so that trying all 21282^{128} keys is far beyond any computer.

Common Mistakes

Mistake: Stopping as soon as you find a candidate that looks good, such as the first load worth 7 points.
Correction: A brute force search guarantees the optimum only after it checks every case. If you stop early, you have a heuristic, not an exhaustive search. Finish the list before you declare a winner, unless you only need any valid answer and not the best one.

Related Terms

  • Greedy Algorithm — Faster strategy that may miss the optimum brute force finds
  • Algorithm — General step-by-step procedure that brute force is one type of
  • Discrete Mathematics — Field where you count and enumerate finite candidate sets
  • Bin Packing — Hard packing problem solved exactly by brute force when small
  • Enumerate — Lists every candidate the method then checks
  • Traveling Salesman Problem — Classic problem solved exactly by checking every tour