8 Skip Lists Hash Tables Skip Lists Definition
8 תרגול Skip Lists Hash Tables
Skip Lists • Definition: – A skip list is a probabilistic data structure where elements are kept sorted by key. – It allows quick search, insertions and deletions of elements with simple algorithms. – It is basically a linked list with additional pointers such that intermediate nodes can be skipped. – It uses a random number generator to make some decisions.
Skip Lists • Skip Levels – Doubly Linked lists S 1. . Sh, each start at -∞ and end at ∞. – Level S 1 - Doubly linked list containing all the elements in the set S. – Level Si is a subset of level Si-1. – Each element in Level i has the probability 1/2 to be in level i+1, thus if there are n elements in level S 1, the expected number of elements in level Si is (n/2)i-1. – The expected number of levels required is O(log n).
Skip Lists •
Skip Lists : דוגמה
Question 2 • Write an algorithm that builds a skip list S from the given BST T with n elements (T can be unbalanced ), such that – the worst query time in S will be O(log n). – The time complexity of the algorithm should be O(n).
Question 2 Solution: Time Complexity: The inorder traversal is O(n). The running time of the rest of the algorithm is linear in the number of elements in the skip list, that is O(n). The worst query time in such a skip list is O(log n). This question demonstrates how to construct a deterministic skip-list from an ordered set of n keys in O(n) time.
Hash Tables Hash Function A hash function h maps keys of a given type into integers in a fixed interval [0, m-1] Uniform Hash Table A hash table for a given key type consists of: Hash function h: keys-set →[0, m-1] Array (called table) of size m
Hash Tables Direct Addressing Chaining K is a set whose elements' keys are in the range [0, m-1]. Use a table of size m and store each element x in index x. key. Disadvantage: when |K| << m → waste of space h(k) = k mod m (This is an example of a common hash function) If h(k) is occupied, add the new element in the head of the chain at index h(k)
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(22)=0 0 1 2 3 4 5 6 7 8 9 10 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(22)=0 0 1 2 3 4 5 6 7 8 9 10 22 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(1)=1 0 1 2 3 4 5 6 7 8 9 10 22 / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(1)=1 0 1 2 3 4 5 6 7 8 9 10 22 / 1 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 / 1 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(13)=2 0 1 2 3 4 5 6 7 8 9 10 22 / 1 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(13)=2 0 1 2 3 4 5 6 7 8 9 10 22 / 13 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 / 13 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(11)=0 0 1 2 3 4 5 6 7 8 9 10 22 / 13 / / / / /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(11)=0 0 1 2 3 4 5 6 7 8 9 10 11 1 / 13 / / / / / 22 /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(24)=2 0 1 2 3 4 5 6 7 8 9 10 11 1 / 24 / / / / 22 / 13 /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(33)=0 0 1 2 3 4 5 6 7 8 9 10 33 1 / 24 / / / / 11 13 / 22 /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(18)=7 0 1 2 3 4 5 6 7 8 9 10 33 1 / 24 / / 18 / / 11 13 / 22 /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(42)=9 0 1 2 3 4 5 6 7 8 9 10 33 1 / 24 / / 18 / / 42 / / 11 13 / 22 /
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Chaining h(k)=k mod 11 h(31)=9 0 1 2 3 4 5 6 7 8 9 10 33 1 / 24 11 13 / / / 18 / / 31 / 42 / 22 /
Hash Tables Open Addressing Linear Probing: h(k, i) = (h'(k) + i)mod m 0≤ i ≤ m-1 h'(k) - common hash function First try h(k, 0) = h'(k) , if it is occupied, try h(k, 1) etc. . Advantage: simplicity Disadvantage: clusters, uses Θ(m) permutations of index addressing sequences Double Hashing: h(k, i) = (h 1(k) + i·h 2(k))mod m 0≤ i ≤ m-1 h 1 – hash function h 2 – step function First try h(k, 0) = h 1(k), if it is occupied, try h(k, 1) etc. Advantage: less clusters , uses Θ(m*m) permutations of index addressing sequences
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(22)=0 0 1 2 3 4 5 6 7 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(22)=0 0 1 2 3 4 5 6 7 8 9 10 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(22)=0 0 1 2 3 4 5 6 7 8 9 10 22 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(1)=1 0 1 2 3 4 5 6 7 8 9 10 22 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(1)=1 0 1 2 3 4 5 6 7 8 9 10 22 1 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 1
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(13)=2 0 1 2 3 4 5 6 7 8 9 10 22 1 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(13)=2 0 1 2 3 4 5 6 7 8 9 10 22 1 13 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 0 1 2 3 4 5 6 7 8 9 10 22 1 13
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(11)=0 0 1 2 3 4 5 6 7 8 9 10 22 1 13 תפוס פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(11)=0 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(24)=2 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 תפוס פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(24)=2 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 פנוי
שאלה 3 22, 1, 13, 11, 24, 33, 18, 42, 31 תפוס תפוס פנוי 22 1 13 11 24 0 1 2 3 4 5 6 7 8 9 10 Linear Probing h(k)=k mod 11 h(33)=0
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(33)=0 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(18)=7 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 18 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(42)=9 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 18 42 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(31)=9 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 18 42 תפוס פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Linear Probing h(k)=k mod 11 h(31)=9 0 1 2 3 4 5 6 7 8 9 10 22 1 13 11 24 33 18 42 31 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(22)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(22)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(22)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(1)=1 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(1)=1 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(13)=2 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(13)=2 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1 13 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1 13
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(11)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(11)=2 10 22 1 13 תפוס פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(11)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(11)=2 10 22 1 13 11 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(24)=2 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(24)=5 10 22 1 13 תפוס 11 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(24)=2 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(24)=5 10 22 1 13 11 24 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(33)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(33)=4 10 22 1 13 תפוס 11 תפוס 24 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(33)=0 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(33)=4 10 22 1 13 11 24 33 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(18)=7 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(18)=9 10 22 1 13 11 18 24 33 פנוי תפוס
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(42)=9 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 10 22 1 13 11 18 24 33 42 פנוי
3 שאלה 22, 1, 13, 11, 24, 33, 18, 42, 31 Double Hashing 0 1 2 3 h 1(k)=k mod 11 4 h 1(31)=9 5 6 7 Step Function h 2(k)=1+ (k mod 10) 8 9 h 2(31)=2 10 22 1 13 תפוס 11 18 31 24 33 42 תפוס פנוי תפוס
Hash Tables Average (expected) Search Time ! לוח
Hash Tables •
Hash Tables Bloom Filter
Question 7 • Suppose we have n elements and a very good hash function. We have a hash table with m=n(1. 5) double slots, that is, each hash table slot can hold two elements. • We perform insert and search in the obvious manner, checking both slots if necessary, but we do not implement any collision resolution as in open addressing. • Instead, we have an overflow linked list for those elements that do not fit. – This list accepts all overflow elements, regardless of where they come from. – Clearly, one must search the overflow list after having inspected a full slot. • Show that the expected unsuccessful search time is O(1). In other words, show that the expected number of elements in the overflow list is O(1) – (Hint: look at the total number of triples of n elements, and what is the chance that the hash function takes a triple to the same slot).
Question 7 Solution: • Number of triplets among n indices = Θ(n 3) • The probability for two keys to fall into the same slot 1/m • The probability of a triplet to fall into the same slot 1/m 2 • m = n(1. 5) so the average number of collisions which will cause a key to go into the overflow list is: • Θ(n 3)/m 2 = Θ(n 3)/n 3 = O(1)
9 שאלה • In Moshe's grocery store, an automatic ordering system that uses a Queue (FIFO) is installed. Whenever a client places an order, the order's data (product, quantity, client's name) are being inserted to the back of the Queue. • Moshe is extracting orders from the front of the queue, handling them one by one. In order to avoid a scenario where the store runs out of some product, every time Moshe extracts an order from the front of the Queue, he would like to know the total quantity of the currently extracted product, summed over all of this product's orders in the Queue. • Find a data structure that supports the following operations in the given time:
9 שאלה 1. Enqueue(r)-Inserting an order to the back of the queue, r = (product, quantity, client's name). Running time - O(1) on average. 2. Dequeue()-Extracting an order from the front of the queue. Running time - O(1) on average. 3. Query(p)-Returns the total quantity of the product p in the Queue - O(1) on average. • It is known that there are n products in the grocery. The Queue may hold at most m orders at any given time. We know that m<n. The data structure may use O(m) memory.
פתרון - 9 שאלה • Solution: • We will use a Queue and a hash table with chaining of size O(m). Each element (in the linked list) contains a key – the product's name and another field – its quantity. • Enqueue(r) – Insert the order to the back of the queue. Search for r. product in the hash table. If r. product is in the table, add r. quantity to the quantity field of the appropriate element. If not, insert r. product to the hash table and update its quantity. • Dequeue() – Extract r from the front of the queue. Search for r. product in the hash table (it must be in it). Decrement the quantity field of the element by r. quantity. If the quantity is 0, remove the element from the hash table.
פתרון - 9 שאלה • Solution: • Query(p) – Look for p in the hash table. If p is in the table, return p. quantity else return 0. Notice that ; therefore, the running time of the three operations is O(1) in average.
- Slides: 110