1

Let's say we're comparing the time complexity of search function in hashmap vs trie.

On a lot of resources I can find, the time complexities are described as Hashmap get: O(1) vs Trie search: O(k) where k is the length of chars in the string you want to search.

However, I find this a bit confusing. To me, this looks like the sample size "n" is defined differently in the two scenarios.

If we define n as the number of characters, and thus are interested in what's the complexity of this algorithm as the number of characters grow to infinity, wouldn't hashmap get also have a time complexity of O(k) due to its hash function?

On the other hand, if we define n as the number of words in the data structure, wouldn't the time complexity of Trie search also be O(1) since the search of the word doesn't depend on the number of words already stored in the Trie?

In the end, if we're doing an apple to apple comparison of time complexity, it looks to me like the time complexity of Hashmap get and Trie search would be the same.

What am I missing here?

EdH
  • 75
  • 1
  • 5

1 Answers1

1

Yes, you are absolutely correct.

What you are missing is that statements about an algorithm's complexity can be based on whatever input terms you like. Outside of school, such statements are made to communicate, and you can make them to communicate whatever you want.

It's important to make sure that you are understood, though, so if there is a chance for confusion about how the n in O(n) is measured, or any assumed constraints on the input (like bounded string size), then you should just specify that explicitly.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Thank you for confirming and clearing up my confusion. Yeah, I guess it's always good to specify this explicitly to avoid any miscommunication. Maybe the reason for the different "n" for describing complexity of hashmap vs trie in many articles I read is to highlight their functionality. Usually, w/ hashmap, you are interested in the performance of get() as number of items grows. With trie, the author of such articles wanted to emphasize that you need a node to represent each character and thus the O(n) or O(k) which is proportional to the number of characters in the string. – EdH May 23 '21 at 21:18
  • i want to upvote your answer but was told "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." Haha.. let me know how I can help give credits to your response ========================================================= Edited: Found the green check mark.. – EdH May 23 '21 at 21:21