Saturday, April 10, 2010

TRIE data structure Part 5 : Complexity Analysis



Now that we've seen the basic operations on how to work with a TRIE, we shall now see the space and time complexities in order to get a real feel of how good a TRIE data structure is. Lets take the two important operations INSERT and SEARCH to measure the complexity.

INSERT operation first. Lets always take into account the worst case timing first and later convince ourselves of the practical timings. For every Node in the TRIE we had something called as Collection where the Collection can be either a Set or a List. If we choose Set, the order of whatever operation we perform over that will be in O(1) time, whereas if we use a LinkedList the number of comparisons at worst will be 26 (the number of alphabets). So for moving from one node to another, there will be at least 26 comparisons will be required at each step. 

Having these in mind, for inserting a word of length 'k' we need (k * 26) comparisons. By Applying the Big O notation it becomes O(k) which will be again O(1). Thus insert operations are performed in constant time irrespective of the length of the input string (this might look lik an understatement, but if we make the length of the input string a worst case maximum, this sentence holds true).

Same holds true for the search operation as well. The search operation exactly performs the way the insert does and its order is O(k*26) = O(1).

10 comments:

chiranjeevi said...

Hey, How come O(k) became O(1) in the above explanation? Could you pls clarify me?

b said...

@chiranjeevi:
To understand this, you will have to have a deep knowledge on what "Order" means. The Big O notation is used to tell how the complexity increases according to the increase in the input space. For TRIE ADT, the input space is the number of words that we use. Assume we have 10 words, the insert operation will take a total of its word length multiplied by the 26 letters in the linked list. Which will be equal to k*26. Applying Big O we have O(k*26) = O(1). The reason is this does not depend upon the increase in the number of words that the TRIE is going to have.
Even if the TRIE has a billion words, the insert operation will take a constant time of k*26 which is irrespective of 'n'. So, if you plot a curve you could see that its constant and in Big O terms, it is called as O(1) or constant time insertion.
Hope this helps.

Chiranjeevi said...

Thankyou very much for the clarification. Now, I got it.

b said...

@chiranjeevi: Happy to help!

Balaji, said...

is ther c code for this..

badalchowdhary said...

Very good explanation. To the point and in Java :)

Ramya Priyadarshini said...

Very well explained!! great post.

BragBoy said...

Thanks Ramya for stopping by!

Darko Darko said...

Can you give me any idea how can i compare 2 tries?

Jujhar Khurana said...

Excellent job. You should write a book on all data structures. Another thing is that you wrote the algo..after giving the code for insert and search. If algo could have been discussed first, it would have been a bit easier to understand for new bees like me.
Anyways, it was very good.
Thanks.