CS210- Lecture 17 July 12, 2005 Agenda Collision Handling

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Skip Lists1 S0S0 S1S1 S2S2 S3S
Advertisements

Skip Lists. Outline and Reading What is a skip list (§9.4) – Operations (§9.4.1) – Search – Insertion – Deletion Implementation Analysis (§9.4.2) – Space.
SKIP LISTS Amihood Amir Incorporationg the slides of Goodrich and Tamassia (2004)
© 2004 Goodrich, Tamassia Hash Tables1  
© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
9.3 The Dictionary ADT    … 01/25/98, Taipei, … 03/04/98, Yunlin, … 02/15/98, Douliou, … 01/20/98,
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 12 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
© 2004 Goodrich, Tamassia Dictionaries   
CSC311: Data Structures 1 Chapter 9: Maps and Dictionaries Objectives: Map ADT Hash tables –Hash functions and hash code –Compression functions and collisions.
Dictionaries and Hash Tables1  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
Maps, Hash Tables and Dictionaries Chapter 10.1, 10.2, 10.3, 10.5.
Maps, Dictionaries, Hashing
Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Hash Tables1   © 2010 Goodrich, Tamassia.
Maps, Hash Tables, Skip Lists, Sets last Update: Nov 4, 2014 EECS2011: Maps, Hash Tables, Skip Lists, Sets1.
© 2004 Goodrich, Tamassia Hash Tables1  
Skip Lists 二○一七年四月二十五日
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
1 COMP9024: Data Structures and Algorithms Week Eight: Maps and Dictionaries Hui Wu Session 1, 2014
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Maps 1/28/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,
Skip Lists S3   S2   S1   S0  
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
COMP9024: Data Structures and Algorithms
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Hashing CSE 2011 Winter July 2018.
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Skip Lists S3 + - S2 + - S1 + - S0 + -
COMP9024: Data Structures and Algorithms
Skip Lists.
Dictionaries < > = Dictionaries Dictionaries
Data Structures Maps and Hash.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Dictionaries 11/23/2018 5:34 PM Hash Tables   Hash Tables.
Dictionaries and Hash Tables
Skip Lists S3 + - S2 + - S1 + - S0 + -
Maps.
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Hash Tables   Maps Dictionaries 12/7/2018 5:58 AM Hash Tables  
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Ordered Maps & Dictionaries
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Dictionaries and Hash Tables
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Parasol Lab, Dept. CSE, Texas A&M University
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Skip List: formally 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.
Maps 4/25/2019 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Dictionaries < > = Dictionaries Dictionaries
Dictionaries and Hash Tables
Presentation transcript:

CS210- Lecture 17 July 12, 2005 Agenda Collision Handling Dictionary ADT Linked list implementation of Dictionaries Hash Table implementation Skip Lists 5/12/2019 CS210-Summer 2005, Lecture 17

Collision Handling Collisions occur when different elements are mapped to the same cell Separate Chaining: let each cell in the table point to a linked list of entries that map there  1 2 3 4 451-229-0004 981-101-0004 025-612-0001 Separate chaining is simple, but requires additional memory outside the table 5/12/2019 CS210-Summer 2005, Lecture 17

Map Methods with Separate Chaining used for Collisions Delegate operations to a list-based map at each cell: Algorithm get(k): Output: The value associated with the key k in the map, or null if there is no entry with key equal to k in the map return A[h(k)].get(k) {delegate the get to the list-based map at A[h(k)]} Algorithm put(k,v): Output: If there is an existing entry in our map with key equal to k, then we return its value (replacing it with v); otherwise, we return null t = A[h(k)].put(k,v) {delegate the put to the list-based map at A[h(k)]} if t = null then {k is a new key} n = n + 1 return t Algorithm remove(k): Output: The (removed) value associated with key k in the map, or null if there is no entry with key equal to k in the map t = A[h(k)].remove(k) {delegate the remove to the list-based map at A[h(k)]} if t ≠ null then {k was found} n = n - 1 5/12/2019 CS210-Summer 2005, Lecture 17

Linear Probing Example: h(x) = x mod 13 Open addressing: the colliding item is placed in a different cell of the table Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a “probe” Colliding items lump together, causing future collisions to cause a longer sequence of probes Example: h(x) = x mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73 1 2 3 4 5 6 7 8 9 10 11 12 5/12/2019 CS210-Summer 2005, Lecture 17

Search with Linear Probing Consider a hash table A that uses linear probing get(k) We start at cell h(k) We probe consecutive locations until one of the following occurs An item with key k is found, or An empty cell is found, or N cells have been unsuccessfully probed Algorithm get(k) i  h(k) p  0 repeat c  A[i] if c =  return null else if c.key () = k return c.element() else i  (i + 1) mod N p  p + 1 until p = N 5/12/2019 CS210-Summer 2005, Lecture 17

Updates with Linear Probing To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements remove(k) We search for an entry with key k If such an entry (k, o) is found, we replace it with the special item AVAILABLE and we return element o Else, we return null put(k, o) We throw an exception if the table is full We start at cell h(k) We probe consecutive cells until a cell i is found that is either empty or stores AVAILABLE We store entry (k, o) in cell i 5/12/2019 CS210-Summer 2005, Lecture 17

Dictionary ADT Dictionary ADT methods: find(k): if the dictionary has an entry with key k, returns it, else, returns null findAll(k): returns an iterator of all entries with key k insert(k, o): inserts and returns the entry (k, o) remove(e): remove the entry e from the dictionary entries(): returns an iterator of the entries in the dictionary size(), isEmpty() The dictionary ADT models a searchable collection of key-element entries The main operations of a dictionary are searching, inserting, and deleting items Multiple items with the same key are allowed Applications: word-definition pairs 5/12/2019 CS210-Summer 2005, Lecture 17

Example Operation Output Dictionary insert(5,A) (5,A) (5,A) insert(7,B) (7,B) (5,A),(7,B) insert(2,C) (2,C) (5,A),(7,B),(2,C) insert(8,D) (8,D) (5,A),(7,B),(2,C),(8,D) insert(2,E) (2,E) (5,A),(7,B),(2,C),(8,D),(2,E) find(7) (7,B) (5,A),(7,B),(2,C),(8,D),(2,E) find(4) null (5,A),(7,B),(2,C),(8,D),(2,E) find(2) (2,C) (5,A),(7,B),(2,C),(8,D),(2,E) findAll(2) (2,C),(2,E) (5,A),(7,B),(2,C),(8,D),(2,E) size() 5 (5,A),(7,B),(2,C),(8,D),(2,E) remove(find(5)) (5,A) (7,B),(2,C),(8,D),(2,E) find(5) null (7,B),(2,C),(8,D),(2,E) 5/12/2019 CS210-Summer 2005, Lecture 17

A List-Based Dictionary A log file or audit trail 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 list or array), in arbitrary order Performance: insert takes O(1) time since we can insert the new item at the beginning or at the end of the sequence find and remove 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) 5/12/2019 CS210-Summer 2005, Lecture 17

The findAll(k) Algorithm Algorithm findAll(k): Input: A key k Output: An iterator of entries with key equal to k Create an initially-empty list L B = D.entries() while B.hasNext() do e = B.next() if e.key() = k then L.insertLast(e) return L.elements() 5/12/2019 CS210-Summer 2005, Lecture 17

The insert and remove Methods Algorithm insert(k,v): Input: A key k and value v Output: The entry (k,v) added to D Create a new entry e = (k,v) S.insertLast(e) {S is unordered} return e Algorithm remove(e): Input: An entry e Output: The removed entry e or null if e was not in D {We don’t assume here that e stores its location in S} B = S.positions() while B.hasNext() do p = B.next() if p.element() = e then S.remove(p) return null {there is no entry e in D} 5/12/2019 CS210-Summer 2005, Lecture 17

Hash Table Implementation We can also create a hash-table dictionary implementation. If we use separate chaining to handle collisions, then each operation can be delegated to a list-based dictionary stored at each hash table cell. 5/12/2019 CS210-Summer 2005, Lecture 17

Performance of Hashing In the worst case, searches, insertions and removals on a hash table take O(n) time The worst case occurs when all the keys inserted into the map collide The load factor a = n/N affects the performance of a hash table The expected running time of all Dictionary ADT operations in a hash table is O(1). In the worst case it could be O(n). 5/12/2019 CS210-Summer 2005, Lecture 17

Binary Search Binary search performs operation find(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: 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 5/12/2019 CS210-Summer 2005, Lecture 17

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 5/12/2019 CS210-Summer 2005, Lecture 17

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(next(p)) x = y: we return element(next(p)) x > y: we “scan forward” x < y: we “drop down” If we try to drop down past the bottom list, we return null Example: search for 78 S3 + - S2 - 31 + S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 + 5/12/2019 CS210-Summer 2005, Lecture 17

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 5/12/2019 CS210-Summer 2005, Lecture 17

Insertion To insert an entry (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 5/12/2019 CS210-Summer 2005, Lecture 17

Deletion To remove an entry 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 + 5/12/2019 CS210-Summer 2005, Lecture 17

Implementation x quad-node We can implement a skip list with quad-nodes A quad-node stores: entry link to the node prev link to the node next 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 5/12/2019 CS210-Summer 2005, Lecture 17

Summary A skip list is a data structure for dictionaries 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 5/12/2019 CS210-Summer 2005, Lecture 17