Skip to content

Brute-force search

words

Level 1 of 3

Also called Naive string search.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Brute force looks for coa in cocoa. How many comparisons does it make?

the answer, and why

the answer is B

Place 1: c, o match, c is not a, 3. Place 2: o is not c, 1. Place 3: c, o, a match, 3. That is 7.

if you chose another

  • That counts only the letters that matched, 2 + 0 + 3, not the ones that differed.
  • That is the whole pattern at every place, 3 times 3.
  • That counts the places, not the comparisons.

question 2

Each text has 8 letters and each pattern 4. Which makes brute force compare the most?

the answer, and why

the answer is C

At each of the 5 places, a, a and a match, then the b differs. That is 4 comparisons at each, 20 in all.

if you chose another

  • abab matches at places 1, 3 and 5, 4 each, but places 2 and 4 cost 1 each, so 14.
  • Only place 1 gets past the first letter, so 4 + 1 + 1 + 1 + 1, which is 8.
  • No first letter ever matches, 1 at each place, so 5.

question 3

A log has 800 letters, all a. It is searched for a pattern of 400 letters, all a but the last, which is b. How many comparisons?

the answer, and why

the answer is B

The pattern is tried at 800 - 400 + 1, which is 401 places, and compares all 400 letters at each. 400 times 401 is 160,400.

this tests where the formula comes from, on the learn page

if you chose another

  • That compares 400 letters at each of 800 places, but the pattern is tried at only 401.
  • That is the places, one comparison each, the best.
  • That is n2n^2, the text's length squared, the ceiling in question 2 of the learn page's section 4, not the count.

question 4

The text doubles from 1,000 letters to 2,000, the pattern still half as long. About how many times as many comparisons does the worst make?

the answer, and why

the answer is C

The worst is nearly n24\frac{n^2}{4}, so twice the letters is 2 times 2 the comparisons. 250,500 becomes 1,001,000.

this tests how it scales, on the learn page

if you chose another

  • That is the best, which grows as nn.
  • That is how a count growing as nn times nn times nn grows.
  • The places and the pattern both grow, so the count does too.

pen and paper

question 5about 6 minutes

A tape holds the letters baabaab. Search it for aab by brute force, numbering places from 1.

  1. For each place, how many comparisons?
  2. Where does aab stand?
  3. How many comparisons in all?
a hint

At each place, compare from the left until a letter differs or all 3 match.

a worked answer

5 places. Place 1, b is not a: 1. Place 2, a, a and b match: 3. Place 3, a matches, then b is not a: 2. Place 4, b is not a: 1. Place 5, a, a and b match: 3. aab stands at places 2 and 5, and the comparisons are 1 + 3 + 2 + 1 + 3, which is 10.

placeletters abovecomparisons
1baa1
2aab3, a match
3aba2
4baa1
5aab3, a match
aab under each place of baabaab. It matches at places 2 and 5.

this tests how it works, on the learn page

what a full answer includes
  • 1, 3, 2, 1 and 3.
  • Places 2 and 5.
  • 10.

question 6about 8 minutes

A text holds 1,001 letters, all a. The pattern has half as many, rounded up.

  1. How many letters has the pattern, and at how many places is it tried?
  2. How many comparisons if it is all b?
  3. How many if it is a's ending in b?
a hint

Questions 1 and 2 of the learn page's section 4 count each.

a worked answer

Half of 1,001 rounded up is 501 letters. Places: 1,001 - 501 + 1, which is 501. All b: one comparison at each place, 501. a's ending in b: all 501 letters at each of the 501 places, 501 times 501, which is 251,001.

Question 2: how many comparisons, MM, on a text of nn a's and a pattern of a's ending in b, half as long, rounded up?
  1. the move: a place's most

    the line:
    a place comparesat most m letters\begin{gathered} \text{a place compares} \\ \text{at most } m \text{ letters} \end{gathered}

    why it is allowed: At a place the pattern's letters are compared from the left, one each, until one differs. It has mm letters, so at most mm.

  2. the move: every letter but the last matches

    the line:
    a’s ending in b:m at every place\begin{gathered} \text{a's ending in b:} \\ m \text{ at every place} \end{gathered}
    placeletters aboveagainstcomparisons
    1aaaaab3
    2aaaaab3
    3aaaaab3
    4aaaaab3
    all12
    A text of 6 a's and the pattern aab. At each of the 4 places, a and a match, then b differs. 4 times 3 is 12.

    why it is allowed: The text is all a, and so is every letter of the pattern but the last. Those match at every place, and the last, b, differs. So all mm letters are compared at every place.

  3. the move: multiply by the places

    the line:
    M=⌈n2⌉(⌊n2⌋+1)M = \lceil \frac{n}{2} \rceil (\lfloor \frac{n}{2} \rfloor + 1)

    why it is allowed: By line 2, mm comparisons at each place, with mm as ⌈n2⌉\lceil \frac{n}{2} \rceil by section 4's question 1, line 2. Question 1 made one comparison at each place, ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 in all, by its lines 3 and 4, so there are ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 places. Multiply the two. For 6, 3 times 4, which is 12; for 7, 4 times 4, which is 16.

  4. the move: a ceiling

    the line:
    M≤n2M \le n^2

    why it is allowed: Half of nn is at most nn, a whole number, so rounding it up stays at most nn. ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 is at most nn by section 4's question 1, line 5. Multiply the two. For 6, 12 is at most 36.

  5. the move: read the bound

    the line:
    M∈O(n2)M \in O(n^2)

    why it is allowed: By line 4, with the fixed multiple 1.

  6. the move: a floor

    the line:
    M≥n24M \ge \frac{n^2}{4}

    why it is allowed: Rounding up never makes a number smaller, so ⌈n2⌉\lceil \frac{n}{2} \rceil is at least n2\frac{n}{2}. By section 4's question 1, line 7, ⌊n2⌋+1\lfloor \frac{n}{2} \rfloor + 1 is more than n2\frac{n}{2}. Multiply the two, as n2\frac{n}{2} times n2\frac{n}{2} is n24\frac{n^2}{4}. For 6, 12 against 9.

  7. the move: read the bound

    the line:
    M∈Ω(n2)M \in \Omega(n^2)

    why it is allowed: By line 6, with the fixed multiple a quarter.

  8. the move: both together

    the line:
    M∈Θ(n2)M \in \Theta(n^2)

    why it is allowed: Lines 5 and 7 bound MM by the same growth.

try it with a small nn: at n=6n = 6, line 3 gives M=12M = 12, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 501 letters, 501 places.
  • 501.
  • 251,001.