The worst case search time for a sorted linked list is O(n). With a Balanced Binary Search Tree, we can skip almost half of the nodes after one comparison with root. For a sorted array, we have random access and we can apply Binary Search on arrays.
One idea to make search faster for Linked Lists is Skip List. Another idea (which is discussed in this post) is to place more frequently accessed items closer to head.. There can be two possibilities. offline (we know the complete search sequence in advance) and online (we don’t know the search sequence).
In case of offline, we can put the nodes according to decreasing frequencies of search (The element having maximum search count is put first). For many practical applications, it may be difficult to obtain search sequence in advance. A Self Organizing list reorders its nodes based on searches which are done. The idea is to use locality of reference (In a typical database, 80% of the access are to 20% of the items). Following are different strategies used by Self Organizing Lists.
1) Move-to-Front Method: Any node searched is moved to the front. This strategy is easy to implement, but it may over-reward infrequently accessed items as it always move the item to front.
2) Count Method: Each node stores count of the number of times it was searched. Nodes are ordered by decreasing count. This strategy requires extra space for storing count.
3) Transpose Method: Any node searched is swapped with the preceding node. Unlike Move-to-front, this method does not adapt quickly to changing access patterns.
The worst case time complexity of all methods is O(n). In worst case, the searched element is always the last element in list. For average case analysis, we need probability distribution of search sequences which is not available many times.
For online strategies and algorithms like above, we have a totally different way of analyzing them called competitive analysis where performance of an online algorithm is compared to the performance of an optimal offline algorithm (that can view the sequence of requests in advance). Competitive analysis is used in many practical algorithms like caching, disk paging, high performance computers. The best thing about competitive analysis is, we don’t need to assume anything about probability distribution of input. The Move-to-front method is 4-competitive, means it never does more than a factor of 4 operations than offline algorithm (See the MIT video lecture for proof).
We will soon be discussing implementation and proof of the analysis given in the video lecture.
MIT Video Lecture
This article is compiled by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Skip List | Set 1 (Introduction)
- Introduction of B-Tree
- Suffix Array | Set 1 (Introduction)
- Red-Black Tree | Set 1 (Introduction)
- Generic Linked List in C
- Fibonacci Heap | Set 1 (Introduction)
- Find shortest unique prefix for every word in a given list | Set 1 (Using Trie)
- Heavy Light Decomposition | Set 1 (Introduction)
- Gomory-Hu Tree | Set 1 (Introduction)
- Find last unique URL from long list of URLs in single traversal
- proto van Emde Boas Trees | Set 1 (Background and Introduction)
- Unrolled Linked List | Set 1 (Introduction)
- Persistent Segment Tree | Set 1 (Introduction)