Brute Force and Algorithm Types

Is Brute Force a Greedy Algorithm?

Brute force is not a greedy algorithm: it checks every candidate, while a greedy method commits to the best-looking choice at each step and never reconsiders.

On this page
  1. No, they are different approaches
  2. What brute force does
  3. What a greedy method does
  4. A coin change example
  5. Where each one fits
  6. Using brute force to check a greedy answer

No, they are different approaches

Brute force and greedy are not the same kind of algorithm. Brute force tries every candidate and keeps the one that works, or the best one. A greedy algorithm builds an answer one step at a time. At each step it takes the option that looks best right then, and it never goes back to change that choice.

The two can give the same answer on some problems, but they work in very different ways. Brute force is thorough, and greedy is quick, which is why people often confuse them.

What brute force does

Brute force lists all the candidate answers and checks each one. For a small problem, that is a simple way to be sure of the best result. The downside is that the number of candidates usually grows very fast as the problem gets bigger.

What a greedy method does

A greedy method makes one decision at a time and commits to it. It does not compare the full set of options before choosing. That makes it fast and simple to write, but it can miss the best overall answer if an early choice blocks a better one later.

A coin change example

Suppose you need to make 6 cents using coins worth 1, 3 and 4 cents, and you want the fewest coins.

Brute force tries every combination of coins that adds up to 6. It finds that two 3-cent coins work, which is the fewest possible.

A greedy method takes the largest coin that fits each time. It takes a 4-cent coin first, leaving 2 cents. Then it takes a 1-cent coin, then another. That is three coins, which is not the best answer here. The greedy method is quick, but it has made a choice that does not lead to the best result.

Where each one fits

Greedy methods work well for some problems. With standard US coins (1, 5, 10 and 25 cents), the greedy method gives the fewest coins for any amount. Greedy methods are also correct for finding minimum spanning trees, using algorithms such as Kruskal’s and Prim’s.

Brute force is correct whenever the answer is among the candidates it checks. Its weakness is speed. For large problems, checking every candidate is often impractical.

Using brute force to check a greedy answer

On small inputs, brute force is a useful way to test a greedy method. Run both on the same inputs, and compare the answers. If they disagree, the greedy method has missed the best answer for that input. It is a simple way to find the cases where a fast method goes wrong.

The complete guide to brute force and algorithm types

Brute Force Algorithm Examples
Read the guide

Keep exploring

Guides from other topics

All topics →