3.7.1 · D1Algorithm Paradigms

Foundations — Brute force — exhaustive search, when acceptable

2,699 words12 min readBack to topic

This page assumes you have seen nothing. Every symbol below is earned before it is used. It is the prerequisite floor for the parent topic, which sits inside Algorithm Paradigms.


0. The absolute-zero starting point: what is a "candidate"?

Before any symbol, one word: candidate. A candidate is a single possible answer you might hand in — one guess. Brute force is the act of writing down the complete list of all guesses and ticking each one.

Figure — Brute force — exhaustive search, when acceptable

Look at the figure: each amber dot is one candidate; the cyan box drawn around all of them is the search space. Brute force = visit every dot. The whole rest of this page is just counting how many dots there are for the common shapes of problem — because that count is the running time.


1. The symbol — "how big is the input?"

Picture as the number of boxes in a row:

 box 1   box 2   box 3   ...   box n
[     ] [     ] [     ]  ...  [     ]

Why the topic needs it: every cost formula in the parent is written in terms of . If we could not name "the size of the input", we could not say how the work grows. is the dial we turn to ask "what if the input gets bigger?"


2. The symbol — "at most"

Picture a number line: means sits at or anywhere to its left.

 <----|---------|---------|--------->
      x         y
      x is here or anywhere left of y

Why the topic needs it: constraints are always phrased as "" (an upper bound on input size) and feasibility checks as "weight " (must not exceed a cap). The whole "when is brute force acceptable?" table is a list of bounds.


3. Multiplication as independent choices — the counting engine

Everything explosive in brute force comes from ONE idea: when you make several choices one after another and each choice is free of the others, you multiply the number of options.

Figure — Brute force — exhaustive search, when acceptable

Why the topic needs it: , , and (later) are all just this rule applied over and over. Master this one picture and the scary formulas become obvious. We now derive each.


4. — the number of subsets

First, two sub-symbols.

Now the derivation, WHAT/WHY/PICTURE:

  • WHAT we do: walk the items one by one, each time choosing in or out.
  • WHY multiply: the choices are independent (choosing item 1 doesn't restrict item 2), so by the rule of product we multiply. There are items, each with options: .
  • WHAT IT LOOKS LIKE: a branching tree that doubles at every level.
Figure — Brute force — exhaustive search, when acceptable

Count the leaves at the bottom of the figure: for items there are leaves — that is subsets, from the empty selection to the full one.


5. — the number of orderings (permutations)

  • WHAT we do: fill ordered slots left to right.
  • WHY the numbers shrink: slot 1 can be any of the items. Once placed, only items remain for slot 2, then , and so on. The choices are still independent in the multiply sense (any first choice pairs with any valid second choice), so we multiply: .
  • WHAT IT LOOKS LIKE: the same branching tree, but each level has one fewer branch than the last.

6. — the number of unordered pairs

  • WHAT we do: pick a first item ( ways) and a second, different item ( ways): that gives ordered pairs.
  • WHY divide by 2: each unordered pair got counted twice (once as , once as ). Undo the double-count by dividing by :
  • WHAT IT LOOKS LIKE: a grid of all cells; the diagonal (self-pairs ) is thrown out and the two triangles are mirror images — keep just one.
Figure — Brute force — exhaustive search, when acceptable

In the figure the cyan cells above the diagonal are the pairs we keep; the greyed-out diagonal is self-pairs, and the lower triangle is the duplicate we divide away. This is exactly why Worked Example 1 in the parent starts its inner loop at i+1.


7. — the number of length- strings over symbols

The parent's Example 3 cracks a PIN and quotes "". Let us earn and from zero.

  • WHAT we do: fill ordered slots; each slot can independently hold any of the symbols.
  • WHY it is : slot 1 has options, slot 2 has options, …, slots in all. Repeated independent choices → rule of product → . (Unlike permutations, symbols may repeat, so the count does not shrink to — every slot still sees all options.)
  • WHAT IT LOOKS LIKE: the same branching tree as subsets, but each level now splits into branches instead of .

8. Big- and Big- — "how fast does the count grow?"

Why we throw away constants: we care about how the work scales when input grows, not the exact tick-count, which depends on your machine. Doubling makes an algorithm do the work — that ratio is the useful truth.

Why the topic needs it: the parent's whole acceptability table (, , …) is written in this language. Without / we couldn't compare " at " against " at ".


9. Bits, the AND operator, and bit-masks — turning a subset into a number

The parent's Example 2 loops for mask in range(1 << n) and tests mask & (1 << i). Decode it now.

The test mask & (1 << i) builds the number (a single in slot ) and ANDs it against mask. The result is non-zero only if bit of mask is a — i.e. it asks "is item in this subset?". The & isolates that one bit and ignores all the others. This machinery is explored fully in Bitmasking.


Prerequisite map

counting number n

multiplication rule independent choices

at most sign le

feasibility and constraints

2 to the n subsets

n factorial permutations

n choose 2 pairs

b to the k strings

bitmask enumeration

Big Theta and Big O growth

Brute force feasibility

Brute force topic

Everything funnels into the parent: the counts tell you the search-space size, Big-/Big- tells you how fast it grows, and constraints tell you whether it fits the time budget.


Where these foundations lead next

Once you can count the search space, you can decide whether to accept brute force or reach for a smarter paradigm — Dynamic Programming (reuse overlapping sub-answers), Greedy Algorithms (commit to locally-best moves), or Backtracking (prune dead branches early). For problems where no shortcut is known — NP-Hard Problems — brute force with pruning is often all we have.


Equipment checklist

Self-test: cover the right side, answer, then reveal.

What does stand for?
The size of the input — how many things the problem gives you.
Read in plain words.
" is at most " — is smaller than or equal to it.
State the multiplication (product) rule.
If step 1 has independent options and step 2 has , the combined count is .
Why does an -element set have subsets?
Each item is independently IN or OUT — choices, times, multiplied: .
What is and why?
— with zero items there is still exactly one subset, the empty selection.
Why do permutations give and not ?
Slots are filled with shrinking options — then then … — because used items can't repeat.
What is and why?
— there is exactly one way to order nothing (the empty ordering).
Why is , with the divide-by-2?
counts ordered pairs; each unordered pair is counted twice, so divide by .
What do and mean, and how many length- strings are there?
= number of symbols per slot, = number of slots; count is .
What is the difference between and ?
= grows exactly like ; = grows no faster than (upper bound) .
What does mask & (1 << i) test, and what does & do?
& ANDs two numbers bit by bit; the test is non-zero only if bit is set — i.e. item is in the subset.
What is equal to?
— a followed by zero-bits.
Recall Quick numeric readiness
  • ::: about (a million).
  • ::: about .
  • ::: .
  • (a 4-digit PIN space) ::: .