














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















I (^) In this lecture we investigate searching methods. I (^) Pseudo-formally we can define the act of searching as follows: I (^) We have some arbitrary structure S which holds tuples (K , D) of key and data items. I (^) We take as input a key K ′^ and want to find the tuple (K , D) in S so that K = K ′. I (^) We briefly look back at hashing and search trees seen before and introduce two new data structures I (^) B+ trees. These are the standard data structures for database system and are designed to be efficient when the data is stored on disk I (^) Skip lists. A randomised data structures that enables us to perform all the operations of a balanced search tree without the complexity needed for rebalancing.
Raphaël Clifford
I (^) Question: What choices do we have to hold our data?
Array O( 1 ) Hash table O( 1 ) expected complexity if the table is not too full Binary tree O(log n) if the tree is balanced Balanced tree O(log n) in the worst case others ...
I (^) We usually make comparisons based on number of tuples examined although other factors are obviously important as well: I (^) Insertion and removal of data items. I (^) Size of structure and access characteristics.
Raphaël Clifford
I (^) A binary tree is the manifestation of binary search: I (^) We have n nodes, each node has at most two children. I (^) To find a node, we start at the root and traverse the tree.
A C E G I K M O
B F J N
D L
H
B
D
F
H
A
C
E
G ...
I (^) In the balanced tree, searching for K traverses H, L, J, K. I (^) In the worst case straggly tree, this same search takes much longer.
Raphaël Clifford
I (^) If we consider binary search trees of height h = log n: I (^) One can implement all sorts of operations on such trees. I (^) For example, search for nodes and insert or delete nodes. I (^) The operations mostly have a run-time related to h. I (^) This is only good if h is small, otherwise the tree is like a list! I (^) Balanced trees address this issue by trying to ensure h stays small even as n grows large: I (^) Red-black/AVL and B+ trees are examples of this sort of structure. I (^) The worst cases are not nearly as bad as unbalanced trees.
Raphaël Clifford
Start from a sorted linked list I (^) Search time is O(n) in the worst case
I (^) Suppose we had two linked lists of subsets of the elements. I (^) Each element can appear in one or both lists I (^) How can we speed up the searches?
Raphaël Clifford
Start from a sorted linked list I (^) Search time is O(n) in the worst case
I (^) Suppose we had two linked lists of subsets of the elements. I (^) Each element can appear in one or both lists I (^) How can we speed up the searches?
Raphaël Clifford
I (^) Walk right in top linked list until going right would go too far I (^) Walk down to bottom linked list I (^) Walk right in bottom list until element found (or not)
Raphaël Clifford
QUESTION: Which nodes should be in each level? I (^) The bottom level must contain all elements I (^) We could try to put “popular” elements at higher levels I (^) Typically we won’t have this information and here we care about worst-case performance I (^) Best approach: Evenly space the nodes in the top level I (^) But how many nodes should we choose?
Raphaël Clifford
Let’s try increasing the number of levels assuming everything is perfectly balanced. I (^) With one level the search cost is n I (^) With two levels the search cost is 2
n. (Bottom n, top
n) I (^) With three levels the search cost is 3 3
n. (Bottom n, then n^2 /^3 , n^1 /^3 ) I (^) With four levels the search cost is 4 4
n. (Bottom n, then n^3 /^4 , n^2 /^4 , n^1 /^4 ) I (^) [.. .] I (^) With log n levels the search cost is log n · log^ n
n = 2 log n
Raphaël Clifford
A skip list with log n levels is like a binary search tree. Search for the element 50 follows four links but 99 only requires one. I (^) We now know what the ideal skip list is like I (^) Need to maintain something roughly like this under insertions and deletions
Raphaël Clifford
Construct a skip list from scratch using the elements 81 , 50 , 17 , 10 , 25 , 99 , 3 using a real coin. See blackboard...
Raphaël Clifford
A skip list supports three types of operations I (^) SEARCH(x), returns TRUE if x occurs in the skip list I (^) INSERT(x), inserts x into the skip list by first searching for x and then inserting it at the appropriate levels using random coin tosses if it is not found I (^) DELETE(x), searches for x and then deletes it from all lists that it occurs in I (^) The running time of each operation is dominated by the time taken to search for x How good are skip lists? (speed/balance) I (^) Intuitively:Pretty good on average I (^) In fact: Really, really good, almost always
Raphaël Clifford
With high probability, an n-element skip list has O(log n) levels
Probability of having more than c log n levels I (^) ≤ n · P(an individual element is promoted more than c log n times) (Union Bound) I (^) = n( 1 / 2 c^ log^ n) I (^) = n( 1 /nc^ ) I (^) = 1 /nc−^1
Raphaël Clifford
With high probability, every search in a skip list takes O(log n) time
The main idea is to analyse backwards, from the bottom level upwards I (^) We want to count the total number of “up” and “left” moves made by the search until it reaches the top left I (^) Number of “up” moves < number of levels I (^) The number of levels ≤ c log n w.h.p by the previous lemma I (^) The remaining technicality is to prove that the number of coin tosses required to get c log n HEADS is Θ(log n) w.h.p. I (^) The Ω(log n) part is obvious. We omit the proof of the upper bound. See end notes for references that contain it.
Raphaël Clifford