Skip to content
BENIBIX Learn

Backtracking vs Brute Force

Brute force tries every full answer. Backtracking builds an answer step by step and gives up on a path as soon as it cannot work, so it skips most of the tries.

Development🧮 Algorithms & Data Structures

Backtracking

Try a choice, and step back to try another when it leads to a dead end.

Backtracking builds an answer one choice at a time. After each choice, it checks whether the answer can still work. If it cannot, it undoes the last choice and tries the next one. This skips whole groups of choices that are sure to fail. Sudoku solvers, maze solvers and puzzles like eight queens use it.

Example
A Sudoku solver puts a 4 in an empty box. Three moves later nothing fits, so it goes back and tries a 5.
Why it matters
It solves puzzles where trying every full answer would take far too long. Cutting dead ends early saves most of the work.
Full entry
Development🧮 Algorithms & Data Structures

Brute Force

You try each possible answer in turn. You stop when one works.

Brute force solves a problem by trying every possible answer until one works. It is simple to write and always finds the answer, if there is one. The trouble is the number of tries, which can grow huge very fast. A 4-digit code has 10,000 options, but a 12-letter password has far more than anyone could try. It is often the first version, before a smarter one replaces it.

Example
Jack forgot his 3-digit bike lock code. Trying every code from 000 to 999 opens it in at most 1,000 tries.
Why it matters
It gives a right answer to test smart methods against. It is fine when the options are few.
Full entry

In one line

Backtracking
Try a choice, and step back to try another when it leads to a dead end.
Brute Force
You try each possible answer in turn. You stop when one works.