2

I have an algorithm that:

1. takes in a book's text as a string.

2. splits that string into words and stores it in a list.

3. creates a hash table from this list by iterating over the elements of the list

4. It then iterates over a separate list of words to see if each of these words (in this separate list) is contained in the hashmap.

Three linear traversals occur here. One to split the input string into a word list, one linear traversal of the word list to create the hash table and one final traversal over the separate list to check each word is in the newly created hash table.

Time complexity is O(n + k + m), where n is the number of characters in the input string, k is the number of words and m is the number of words in the other list.

My question is, since k is derived from n and we can guarantee that k will always <= n can we omit n and write the complexity as O(n+m)?

screeb
  • 155
  • 5
  • FWIW https://cs.stackexchange.com/a/3150/23870 points to this paper http://people.cs.ksu.edu/~rhowell/asymptotic.pdf which claims, "We show that it is impossible to define big-O notation for functions on more than one variable in a way that implies the properties commonly used in algorithm analysis. " – JimLohse Dec 13 '17 at 19:00

1 Answers1

1

Yes, you can. Since $k \le n$, you have $n+k+m \le 2n+m = O(n+m)$.

D.W.
  • 159,275
  • 20
  • 227
  • 470