Download presentation
Presentation is loading. Please wait.
Published byBrett Wheeler Modified over 6 years ago
1
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
2
Sorted Maps (Ordered Maps)
exact match to the key Sorted Maps Allows inexact match to the key E.g. Flights departing between a time range (MLB, JFK, 3Oct, 1:00) to (MLB, JFK, 3Oct, 3:00) Kayak.com © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
3
Main operations floorEntry(k) ceilingEntry(k) Submap(k1,k2)
Entry with the greatest key less than or equal to k The key “at or before” k ceilingEntry(k) Entry with the least key value greater than or equal to k The key “at or after” k Submap(k1,k2) Entries between k1 and k2 © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
4
Example Index Element Search Key is G F<G<K F is the floor of G
1 2 3 4 5 6 A D F K M O Q Index Element Search Key is G F<G<K F is the floor of G K is the ceiling of G © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
5
Sorted Search Tables Sorted array by the key values Exact match/search
Binary search Inexact match/search Modify binary search To handle “not found” differently © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
6
Binary search (review)
Recall low and high specify the valid range where key is If the mid point is the key Key is found If low > high (no valid range) Key is not found © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
7
Binary Search Binary search can perform nearest neighbor queries on an ordered map that is implemented with an array, sorted by key similar to the high-low children’s game at each step, the number of candidate items is halved terminates after O(log n) steps Example: find(7) 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l=m =h © 2014 Goodrich, Tamassia, Goldwasser Binary Search Trees
8
Binary Search (inexact)
1 2 3 4 5 6 A D F K M O Q Index Element Search Key is G Low=0, high=6, mid=3; G<K, check first half © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
9
Binary Search (inexact)
1 2 3 4 5 6 A D F K M O Q Index Element Search Key is G Low=0, high=6, mid=3; G<K, check first half Low=0, high=2, mid=1; G>D, check second half © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
10
Binary Search (inexact)
1 2 3 4 5 6 A D F K M O Q Index Element Search Key is G Low=0, high=6, mid=3; G<K, check first half Low=0, high=2, mid=1; G>D, check second half Low=2, high=2, mid=2; G>F, check second half © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
11
Binary Search (inexact)
1 2 3 4 5 6 A D F K M O Q Index Element Search Key is G Low=0, high=6, mid=3; G<K, check first half Low=0, high=2, mid=1; G>D, check second half Low=2, high=2, mid=2; G>F, check second half Low=3, high=2; low>high, not found F<G<K Low=3 is the ceiling of G; high=2 is the floor of G © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
12
Disadvantage of sorted array
Fixed maximum size Put/remove could lead to moving a number of entries © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
13
Worst-case Time Complexity
n entries in a sorted array Operation Time Complexity get(k) put(k,v) remove(k) ceilingEntry(k) floorEntry(k) submap(k1,k2) © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
14
Worst-case Time Complexity
n entries in a sorted array Operation Time Complexity get(k) O(log n) put(k,v) O(n) remove(k) ceilingEntry(k) floorEntry(k) submap(k1,k2) O(log n + s), s entries reported O(n) if all entries are reported © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
15
No Fixed Maximum Size Sorted Linked List
Instead of sorted array Can we still use binary search? © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
16
Skip Lists 4/21/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Skip Lists S3 + - S2 + - 15 S1 + - 23 15 S0 + - 10 36 23 15 © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
17
Linked List and Search Sorted array Sorted linked list Binary search
Can’t use binary search No direct access to a node Only sequential access via the next pointer Additional data structure Skip lists for faster search/updates © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
18
What is a Skip List A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , … , Sh such that Each list Si contains the special keys + and - List S0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S0 S1 … Sh List Sh contains only the two special keys S3 + - S2 - 31 + S1 - 23 31 34 64 + S0 56 64 78 + 31 34 44 - 12 23 26 © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
19
Search for Key k start at the first position of the top list At the current position p, y key(next(p)) Compare k with y k = y: we return element(next(p)) k >= y: we “scan forward” k < y: we “drop down” If we try to drop down past the bottom list, we return null Example: search for 78 S3 + - scan forward S2 - 31 + drop down S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 + © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
20
How to find the floor and ceiling of 70?
Search for Key k start at the first position of the top list At the current position p, y key(next(p)) Compare k with y k = y: we return element(next(p)) k >= y: we “scan forward” k < y: we “drop down” If we try to drop down past the bottom list, we return null Example: search for 78 S3 + - scan forward S2 - 31 + drop down S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 + © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
21
Expected Height of Skip Lists
List Si+1 Randomly selected from Si based on a “coin toss” Si+1 has about half of the items in Si For N entries Expected number of skip lists (height) is log N © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
22
Pseudorandom Numbers Library functions Simulate a coin toss
Generate pseudorandom numbers Not truly random Starting with the same “seed” The same sequence of pseudorandom numbers can be generated Simulate a coin toss 0 is tail, 1 is head Even is tail, odd is head © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
23
Randomized Algorithms
Based on pseudorandom numbers Perform an arbitrary operation out of a fixed set of operations For example, operation a if odd operation b if even Insertion to skip lists uses a randomized algorithm © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
24
Insertion: entry (x,o) Repeatedly toss a coin until we get tails i = number of times the coin came up heads If i h, we add to the skip list new lists Sh+1, … , Si +1 each containing only the two special keys For each list Sj, find the position pj that is right before x (floor) Insert entry (x, o) into list Sj after position pj Example: insert key 15, with i = 2 S3 + - p2 S2 S2 - + + - 15 p1 S1 S1 - 23 + + - 23 15 p0 S0 S0 - 10 23 36 + + - 10 36 23 15 © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
25
Deletion: key x search for x in the skip list and find the positions p0, p1 , …, pi of the items with key x, where position pj is in list Sj remove positions p0, p1 , …, pi from the lists S0, S1, … , Si remove all but one list containing only the two special keys Example: remove key 34 S3 - + p2 S2 S2 - 34 + - + p1 S1 S1 - 23 34 + - 23 + p0 S0 S0 - 12 23 34 45 + - 12 23 45 + © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
26
Implementation x quad-node
We can implement a skip list with quad-nodes A quad-node stores: element link to the node prev link to the node next link to the node below link to the node above special keys PLUS_INF and MINUS_INF quad-node x © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
27
Expected (not Worst-case) Complexity
N entries Expected Complexity Height O(log N) Space Complexity O(N) Time Complexity Search/get Insertion/put Deletion/remove Skipping the proof (beyond the scope this course) © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
28
Skipping the rest © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
29
Space Usage Depends on the random “coin toss” used by each invocation of the insertion algorithm Two basic probabilistic facts: Fact 1: The probability of getting i consecutive heads when flipping a coin is (½)i =1/2i Fact 2: If each of n entries is present in a set with probability p the expected size of the set is np © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
30
Space Usage Consider a skip list with n entries
By Fact 1, we insert an entry in list Si with probability 1/2i By Fact 2, the expected size of list Si is n/2i The expected number of nodes used by the skip list is Thus, the expected space usage of a skip list with n items is O(n) © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
31
Height Fact 3: If each of n events has probability p
the probability that at least one event occurs is at most np Consider a skip list with n entries By Fact 1, we insert an entry in list Si with probability 1/2i By Fact 3, the probability that list Si has at least one item is at most n/2i By picking i = 3log n, the probability that S3log n has at least one entry is at most n/23log n = n/n3 = 1/n2 Thus a skip list with n entries has height at most 3log n with probability at least /n2 Highly likely, the height is O(log n) © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
32
Time Complexity of Search
The search time in a skip list is proportional to the number of drop-down steps, plus the number of scan-forward steps The drop-down steps are bounded by the height O(log n) with high probability To analyze the scan-forward steps, we use: Fact 4: The expected number of coin tosses required to get tails is 2 © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
33
Time Complexity of Search
scan forward in a list (and key does not belong to a higher list) A scan-forward step is associated with a former coin toss that gave tails By Fact 4, in each list the expected number of scan-forward steps 2 Expected overall number of scan-forward steps O(log n) © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
34
Time Complexity of Search
Drop-down steps O(log n) Scan-forward steps Overall: O(log n) expected time The analysis of insertion and deletion gives similar results © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
35
Summary A skip list is a data structure for maps that uses a randomized insertion algorithm In a skip list with n entries The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Using a more complex probabilistic analysis, one can show that these performance bounds also hold with high probability Skip lists are fast and simple to implement in practice © 2014 Goodrich, Tamassia, Goldwasser Skip Lists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.