Docsity

CS61B Lecture #25: Hashing and Data Structures, Slides of Data Structures and Algorithms

Partial preview of the text

Download CS61B Lecture #25: Hashing and Data Structures and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture

Today: Hashing ( Data Structures Chapter 7).

Next topic: Sorting ( Data Structures Chapter 8).

Back to Simple Search: Hashing

  • Linear search is OK for small data sets, bad for large.
  • So linear search would be OK if we could rapidly narrow the search to a few items.
  • Suppose that in constant time could put any item in our data set into a numbered bucket, where # buckets stays within a constant factor of # keys.
  • Suppose also that buckets contain roughly equal numbers of keys.
  • Then search would be constant time.

External chaining

  • Array of M buckets.
  • Each bucket is a list of data items.

...

300 100 1500

201 1

1199

  • Not all buckets have same length, but average is N/M = L, the load factor.
  • To work well, hash function must avoid collisions: keys that “hash” to equal values.

Open Addressing

  • Idea: Put one data item in each bucket.
  • When there is a collision, and bucket is full, just use another.
  • Various ways to do this:
    • Linear probes: If there is a collision at h(K), try h(K)+m, h(K)+ 2 m, etc. (wrap around at end).
    • Quadratic probes: h(K) + m, h(K) + m^2 ,...
    • Double hashing: h(K) + h′(K), h(K) + 2h′(K), etc.
  • Example: h(K) = K%M , with M = 10, linear probes with m = 1.
    • Add 1, 2, 11, 3, 102, 9, 18, 108, 309 to empty table.

108 1 2 11 3 102 309 18 9

  • Things can get slow, even when table is far from full.
  • Lots of literature on this technique, but
  • Personally, I just settle for external chaining.

Hash Functions: Strings

  • For String, "s 0 s 1 · · · sn− 1 " want function that takes all characters and their positions into account.
  • What’s wrong with s 0 + s 1 +... + sn− 1?
  • For strings, Java uses

h(s) = s 0 · 31 n−^1 + s 1 · 31 n−^2 +... + sn− 1 computed modulo 232 as in Java int arithmetic.

  • To convert to a table index in 0 ..N − 1 , compute h(s)%N (but don’t use table size that is multiple of 31!)
  • Not as hard to compute as you might think; don’t even need multipli- cation!

int r; r = 0; for (int i = 0; i < s.length (); i += 1) r = (r << 5) - r + s.charAt (i);

Hash Functions: Other Data Structures I

  • Lists (ArrayList, LinkedList, etc.) are analagous to strings: e.g., Java uses hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null? 0 : obj.hashCode()); }
  • Can limit time spent computing hash function by not looking at entire list. For example: look only at first few items (if dealing with a List or SortedSet).
  • Causes more collisions, but does not cause equal things to go to dif- ferent buckets.

What Java Provides

  • In class Object, is function hashCode().
  • By default, returns address of this, or something similar.
  • Can override it for your particular type.
  • For reasons given on last slide, is overridden for type String, as well as many types in the Java library, like all kinds of List.
  • The types Hashtable, HashSet, and HashMap use hashCode to give you fast look-up of objects. HashMap<KeyType,ValueType> map = new HashMap<KeyType,ValueType> ( approximate size, load fac- tor);

map.put (key, value); // Map KEY -> VALUE. // VALUE last mapped to by SOMEKEY. ... map.get (someKey) // VALUE last mapped to by SOMEKEY. ... map.containsKey (someKey) // Is SOMEKEY mapped? ... map.keySet () // All keys in MAP (a Set)

Characteristics

  • Assuming good hash function, add, lookup, deletion take Θ(1) time, amortized.
  • Good for cases where one looks up equal keys.
  • Usually bad for range queries: “Give me every name between Martin and Napoli.” [Why?]
  • But sometimes OK, if hash function is monotonic (i.e., when key k 1 > k 2 , then h(k 1 ) ≥ h(k 2 ). For example, - Items are time-stamped records; key is the time. - Hashing function is to have one bucket for every hour.
  • Hashing is probably not a good idea for small sets that you rapidly create and discard [why?]