Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Similar presentations


Presentation on theme: "Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"— Presentation transcript:

1 Lecture14: Hashing Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Maps A map models a searchable collection of key‐value entries Characteristics  The main operations: search, inserting, and deleting items  Multiple entries with the same key are not allowed. Applications  Address book  Student‐record database 2 Customer IDNameAddress 010‐5323‐3221Kim, XXXSeoul 011‐4221‐1949Lee, YYYPohang 010‐9492‐0988Park, ZZZDaegu 010‐8383‐3221Kim, XXXSeoul entry key value

3 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 The Map ADT Methods for map M  get(key) : if M has an entry with key, return its associated value; otherwise, return null  put(key,value) : insert entry (key,value) into the map M; if key is not already in M, then return null; else, return old value associated with the key  remove(key) : if the map M has an entry with key, remove it from M and return its associated value; else, return null  size()  isEmpty()  entrySet() : return an iterable collection of the entries in M  keySet() : return an iterable collection of the keys in M  values() : return an iterator of the values in M 3

4 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 4 OperationOutputMap isEmpty()trueØ put(5,A)null(5,A) put(7,B)null(5,A),(7,B) put(2,C)null(5,A),(7,B),(2,C) put(8,D)null(5,A),(7,B),(2,C),(8,D) put(2,E)C(5,A),(7,B),(2,E),(8,D) get(7)B(5,A),(7,B),(2,E),(8,D) get(4)null(5,A),(7,B),(2,E),(8,D) get(2)E(5,A),(7,B),(2,E),(8,D) size()4(5,A),(7,B),(2,E),(8,D) remove(5)A(7,B),(2,E),(8,D) remove(2)E(7,B),(8,D) get(2)null(7,B),(8,D) isEmpty()false(7,B),(8,D)

5 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Simple List-Based Map We can efficiently implement a map using an unsorted list We store the items of the map in a list S (based on a doubly-linked list), in arbitrary order 5 trailer header nodes/positions entries 9 c 6 c 5 c 8 c

6 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map get(k) algorithm 6

7 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map put(k,v) algorithm 7

8 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map remove(v) algorithm 8

9 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Performance of a List-Based Map 9

10 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Problems and Solution 10

11 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hashing 11

12 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Functions and Hash Tables 12

13 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Tables Use keys to store and access entries (in constant time) 13

14 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 14     0 1 2 3 4 9997 9998 9999 … 451-229-0004 981-101-0002 200-751-9998 025-612-0001

15 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Functions 15

16 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Codes Memory address  We reinterpret the memory address of the key object as an integer (default hash code of all Java objects)  Good in general, except for numeric and string keys Integer cast  We reinterpret the bits of the key as an integer  Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in Java) Component sum  We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows).  Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in Java) 16

17 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Codes (cont.) 17

18 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Compression Functions 18

19 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 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.  Simple, but requires additional memory outside the table 19    0 1 2 3 4 451-229-0004981-101-0004 025-612-0001

20 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Map with Separate Chaining 20 Algorithm get(k) return A[h(k)].get(k) Algorithm put(k,v): t = A[h(k)].put(k,v) if t = null then // k is a new key n = n + 1 return t Algorithm remove(k) t = A[h(k)].remove(k) if t ≠ null then // k was found n = n - 1 return t

21 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Linear Probing 21 0123456789101112 41 18445932223173 0123456789101112

22 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Search with Linear Probing 22 Algorithm get(k) i  h(k) p  0 repeat c  A[i] if c =  return null else if c.getKey () = k return c.getValue() else i  (i + 1) mod N p  p + 1 until p = N return null

23 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Updates with Linear Probing If collided item is removed  The items with the same key value may not be accessible.  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 (key,value) is found, we replace it with the special item AVAILABLE and we return element value  Otherwise, we return null. 23

24 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Updates with Linear Probing 24

25 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Double Hashing 25

26 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example of Double Hashing 26 0123456789101112 31 41 183259732244 0123456789101112

27 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Performance of Hashing 27

28 28


Download ppt "Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)"

Similar presentations


Ads by Google