Download presentation
Presentation is loading. Please wait.
Published byDominic Preston Modified over 6 years ago
1
Dictionaries < > = 6 2 9 1 4 8 12/3/2018 8:58 AM Dictionaries
2
Outline and Reading Dictionary ADT (§8.1) Log file (§8.2)
Binary search Lookup table (§8.5) Binary search tree (§9.1) Search Insertion Deletion Performance 12/3/2018 8:58 AM Dictionaries
3
Dictionary ADT The dictionary ADT models a searchable collection of key-element items The main operations of a dictionary are searching, inserting, and deleting items Multiple items with the same key are allowed Applications: address book credit card authorization mapping host names (e.g., cs16.net) to internet addresses (e.g., ) Dictionary ADT methods: findElement(k): if the dictionary has an item with key k, returns its element, else, returns the special element NO_SUCH_KEY insertItem(k, o): inserts item (k, o) into the dictionary removeElement(k): if the dictionary has an item with key k, removes it from the dictionary and returns its element, else returns the special element NO_SUCH_KEY size(), isEmpty() keys(), Elements() 12/3/2018 8:58 AM Dictionaries
4
Log File A log file is a dictionary implemented by means of an unsorted sequence We store the items of the dictionary in a sequence (based on a doubly-linked lists or a circular array), in arbitrary order Performance: insertItem takes O(1) time since we can insert the new item at the beginning or at the end of the sequence findElement and removeElement take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key The log file is effective only for dictionaries of small size or for dictionaries on which insertions are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation) 12/3/2018 8:58 AM Dictionaries
5
Binary Search Binary search performs operation findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key similar to the high-low game at each step, the number of candidate items is halved terminates after a logarithmic number of steps Example: findElement(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 12/3/2018 8:58 AM Dictionaries
6
Lookup Table A lookup table is a dictionary implemented by means of a sorted sequence We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys Performance: findElement takes O(log n) time, using binary search insertItem takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item removeElement take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations) 12/3/2018 8:58 AM Dictionaries
7
Binary Search Tree A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8 12/3/2018 8:58 AM Dictionaries
8
Dictionaries 12/3/2018 8:58 AM Search To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return NO_SUCH_KEY Example: findElement(4) Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k < key(v) return findElement(k, T.leftChild(v)) else if k = key(v) return element(v) else { k > key(v) } return findElement(k, T.rightChild(v)) < 6 2 > 9 1 4 = 8 12/3/2018 8:58 AM Dictionaries
9
Insertion < 6 To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5 12/3/2018 8:58 AM Dictionaries
10
Deletion To perform operation removeElement(k), we search for key k
6 To perform operation removeElement(k), we search for key k Assume key k is in the tree, and let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) Example: remove 4 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8 12/3/2018 8:58 AM Dictionaries
11
Deletion (cont.) 1 v We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z) Example: remove 3 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 12/3/2018 8:58 AM Dictionaries
12
Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods findElement , insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case 12/3/2018 8:58 AM Dictionaries
13
Skip Lists S3 + - S2 + - S1 + - S0 + - 15 23 15 10 36 23 15
Dictionaries 12/3/2018 8:58 AM Skip Lists S3 + - S2 + - 15 S1 + - 23 15 S0 + - 10 36 23 15 12/3/2018 8:58 AM Dictionaries
14
Outline and Reading What is a skip list (§8.6) Operations
Search Insertion Deletion Implementation Analysis Space usage Search and update times 12/3/2018 8:58 AM Dictionaries
15
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 We show how to use a skip list to implement the dictionary ADT S3 + - S2 - 31 + S1 - 23 31 34 64 + S0 56 64 78 + 31 34 44 - 12 23 26 12/3/2018 8:58 AM Dictionaries
16
Search S3 S2 S1 S0 We search for a key x in a a skip list as follows:
We start at the first position of the top list At the current position p, we compare x with y key(after(p)) x = y: we return element(after(p)) x > y: we “scan forward” x < y: we “drop down” If we try to drop down past the bottom list, we return NO_SUCH_KEY Example: search for 78 S3 + - S2 - 31 + S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 + 12/3/2018 8:58 AM Dictionaries
17
Search: Algorithm SkipSearch(k):
Input: A search key k and the skip-list S Output: Position p in S such that the item at p has the largest key less than or equal to k Let p be the top-most, - of S (S has at least two levels) while below(p) != NULL do p below(p) // drop-down while key(after(p)) k do let p after(p) //scan-forward return p; 12/3/2018 8:58 AM Dictionaries
18
Randomized Algorithms
Dictionaries 12/3/2018 8:58 AM Randomized Algorithms A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b random() if b = 0 do A … else { b = 1} do B … Its running time depends on the outcomes of the coin tosses We analyze the expected running time of a randomized algorithm under the following assumptions the coins are unbiased, and the coin tosses are independent The worst-case running time of a randomized algorithm is often large but has very low probability (e.g., it occurs when all the coin tosses give “heads”) We use a randomized algorithm to insert items into a skip list 12/3/2018 8:58 AM Dictionaries
19
Insertion To insert an item (x, o) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails, and we denote with i the 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 We search for x in the skip list and find the positions p0, p1 , …, pi of the items with largest key less than x in each list S0, S1, … , Si For j 0, …, i, we insert item (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 12/3/2018 8:58 AM Dictionaries
20
Deletion To remove an item with key x from a skip list, we proceed as follows: We 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 We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si We 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 + 12/3/2018 8:58 AM Dictionaries
21
Implementation x quad-node
We can implement a skip list with quad-nodes A quad-node stores: item link to the node before link to the node after link to the node below link to the node above Also, we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them quad-node x 12/3/2018 8:58 AM Dictionaries
22
insertAfterAbove(p, q, new)
new.after = p.after; new.before = p; new.below = q; new.up = NULL; p.after.before = new; p.after = new; If (q != NULL) q.above = new; q P new q 12/3/2018 8:58 AM Dictionaries
23
SkipInsert: Algorithm SkipInsert(k, e): Input: item(k, e) Output: none
p SkipSearch(k); // go to the bottom q insertAfterAbove(p, NULL, item(k, e)); while random() < 0.5 do while above(p) = NULL do p before(p) // scan backward p above(p) // jump up to higher level insertAfterAbove(p, q, item(k, e)) 12/3/2018 8:58 AM Dictionaries
24
Space Usage Consider a skip list with n items
By Fact 1, we insert an item 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 The space used by a skip list depends on the random bits used by each invocation of the insertion algorithm We use the following two basic probabilistic facts: Fact 1: The probability of getting i consecutive heads when flipping a coin is 1/2i Fact 2: If each of n items is present in a set with probability p, the expected size of the set is np Thus, the expected space usage of a skip list with n items is O(n) 12/3/2018 8:58 AM Dictionaries
25
Height The running time of the search an insertion algorithms is affected by the height h of the skip list We show that with high probability, a skip list with n items has height O(log n) We use the following additional probabilistic fact: 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 items By Fact 1, we insert an item 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, we have that the probability that S3log n has at least one item is at most n/23log n = n/n3 = 1/n2 Thus a skip list with n items has height at most 3log n with probability at least /n2 12/3/2018 8:58 AM Dictionaries
26
Search and Update Times
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 of the skip list and thus are O(log n) with high probability To analyze the scan-forward steps, we use yet another probabilistic fact: Fact 4: The expected number of coin tosses required in order to get tails is 2 When we scan forward in a list, the destination 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 is 2 Thus, the expected number of scan-forward steps is O(log n) We conclude that a search in a skip list takes O(log n) expected time The analysis of insertion and deletion gives similar results 12/3/2018 8:58 AM Dictionaries
27
Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm In a skip list with n items 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 12/3/2018 8:58 AM Dictionaries
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.