Skip List
The motivation of skip lists to imitate binary search in an ordered Linked List. To do binary search on a linked list!
In my opinion, SkipList was introduced as it is one possible implementation of dictionary ADT and it has interesting runtime as well.
With an ordered array, we can directly do binary search, but we cannot do binary search directly on a linked list
A hierarchy of ordered linked lists (levels) :
- Each list contains the special keys and (sentinels)
- List contains the KVPs of in non-decreasing order. (The other lists store only keys, or links to nodes in )
- Each list is a subsequence of the previous one
- List contains only the sentinels; the left sentinel is the root
- Each KVP belongs to a tower of nodes
- There are (usually) more nodes than keys
- The skip list consists of a reference to the topmost left node
- Each node has references and
Search
- We will need a function called which will return a stack with all the predecessor of on each level.
- This function will be useful for insert/delete.
Returns a stack of the predecessor of on each level.
Pseudocode for search: Top of the stack is the bottom row of the skip list.
We check what is right after the top of the stack, if we find then we’ve found it. If not, then is not in the list.
Search is logarithmic since we don’t do much steps in each level. (Expected time that is)
Insert
- We randomize the construction of the skip list by randomly repeatedly tossing a coin until we get tails.
- Let the number of times the coin came up heads, is the height of the key to be inserted. If there is one element, the height is 0, if two elements, the height is 1, and so on.
- Increase height of skip list, if needed to have levels.
- Use to get stack .
Deletion
It is easy to remove a key since we can find all predecessors.
Then eliminate layers if there are multiple ones with only sentinels.
Analysis of Skip List
Expected space usage: Expected height:
In big
- Search: average time
- number of drop-downs = height
- expected # forward-steps is in each level
- expected total # forward-steps is in
- Insert: expected time (and average runtime?? I assume)
- Delete: expected time
What is the average-runtime? Is it ? Yes. Whats the worst-case runtime for every operations? ?
worst-case Skip List
If the height is bounded by , then the worst case is , however if it is unbounded, then the worst case is .
Summary: expected space, all operations take expected time!
Reordering
Recall: Unordered list/array implementation of ADT Dictionary search: Θ(n), insert: Θ(1), delete: Θ(1) (after a search)
Questions
- 9, since we remove all five 24 nodes, and since we have two extra towers, we will also need to remove 4 sentinels? nodes. It is 9.
- So in delete, we adjust the height of the whole skip list. Yes. The height of the skip list the the max height of the towers, so from left to right we have tower height: 2, 1, 3, 2 after deletion. So, our skip list need to adjust its height to 3. (Start counting from bottom by 0).
- We use the H before the T, so that is , a tower of height for the 17 node.
- My question is, do we count from the bottom starting from 0 or 1? In my understanding, looking at the picture, 24 is a tower of .
- So our skip list will have a height of . It would be 7 in the same convention used in @928, because you would unconditionally insert in S0 before the first flip, so you should end up with a node on S6 after insertion.
In a skip list with items where the tower has been bounded by a constant , the worst-case runtime for search is .
- The skip list guarantees a runtime of for all operations? So false?
- NO, the answer is True. But why? (ask on piazza)
Running in a skip list may not take the shortest path to reach the node in with key .
- node in means the last row in the skip list, and node with key , is the only node on that row that we are looking for.
- True, it may take longer paths through higher levels, bypassing some nodes in the base level?
In a skip list with elements and of expected height, what is the best-case runtime of search?
What is the probability that a tower in a ski plist has exactly 3 nodes?
What is the probability that a tower in a skip list has at least 3 nodes?
- , why??
Suppose we query in that order in the array below.
What is the final array after these queries, supposing that we use the Transpose Heuristic and how many comparisons are made in total.
- 14 comparisons, Array:
Suppose we have the same array and queries as in the previous question. What is the final array and the number of comparisons made after using the MTF heuristic?
- 16 comparisons, Array:
You are given a dictionary implemented using the Transpose heuristic, with keys, stored as an unordered linked list . Suppose you want to perform a sequence of searches on keys present in where the number of different keys searched for is a constant and . The worst-case runtime to perform all searches is:
- ? no, it’s
- Since it’s items we are looking for and accessing in dictionary is , then a place in the dictionary can have a linked list that is long. How does Transpose heuristic play a role in this?
- In the case with Transpose, you can search for nth key and n-1th key alternating (i.e. nth, n-1th, nth, n-1th…).
- However, in the worst case, the desired item might always be at the end of the list, resulting in a linear search time of O(n) for each of the k searches. Therefore, the worst-case runtime for performing all k searches is .
You are given a dictionary implemented using the MTF heuristic, with keys, stored as an unordered linked list . Suppose you want to perform a sequence of searches on keys present in where the number of different keys searched for is a constant and . The worst-case runtime to perform all searches is:
- How does MTF heuristic play a role in this??
- , in the case with MTF, worst-case would be when you search in reverse order and repeat it if .
Give a linked list of items implemented with the MTF heuristic, what is the fewest number of searches to cause the first item to be relocated to the end of the list?
- Need searches to cause the first item to be at the back
Given a linked list of items implemented with the Transpose heuristic, what is the fewest number of searches to cause the first item to be relocated to the end of the list?
- n-1