3

I am using the following brute force algorithm for searching a string inside another string.

As I know, the number of comparisons is (n-m+1)*m in the worst case, but the right answer for time complexity is supposed to be O(n*m).

To get this answer, I do the following transformations:

(n-m+1)*m = (n+1) * m - m^2 = O(n*m) - m^2

How do you get O(n*m) from here?

Where did -m^2 go?

Brute force algorithm:

NAIVE-STRING-MATCHER

n = T.length
m = P.length
for s = 0 to n - m
    if P[1...m] == T[s+1...s+m]
        print s
anatolyg
  • 26,506
  • 9
  • 60
  • 134
Sword
  • 159
  • 1
  • 3
  • 10
  • I am sorry for my pool English and I don't know how to express it. Then you just answer how I get the NAIVE-STRING-MATCHER algorithm's time complexity O(n*m) when I know comparisions times is (n-m+1)*m. – Sword Feb 28 '17 at 11:23
  • O-Notation describes the upper bound. Since O(n*m) > O(n*m)-m^2 the upper bound is also O(n*m). Some will also argue that m is very small compared to n so it could be omitted. – CoronA Apr 21 '17 at 14:33

1 Answers1

2

The running time indeed belongs to O(m(n-m)). But as the Big-O notation is an upper bound, this is also O(mn), as mn ≥ m(n-m).

In practice, no harm is done by this simplification, as you usually expect the length of the search string to be proportional to that of the pattern. Then m = αn yields m(n-m) = mn(1-α).

  • Does ' the Big-O notation is an upper bound' mean I can ignore any negative term(here is m^2 - m) ? And 'm = αn yields m(n-m) = mn(1-α)', why it is not m(n-m) = αn*n(1-α): O(n^2) ? – Sword Feb 28 '17 at 12:19
  • @Sword: you can write that as well. –  Feb 28 '17 at 12:34