Presentation is loading. Please wait.

Presentation is loading. Please wait.

SETS AND HASHING. SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S.

Similar presentations


Presentation on theme: "SETS AND HASHING. SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S."— Presentation transcript:

1 SETS AND HASHING

2 SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S and T S – T // The difference of S and T S.contains(e) // True / False Optional S.add(e) S.remove(e) Implementation [discuss the big-O efficiency] (Linked) Lists Trees (preferably self-balancing: e.g. Red-Black Trees) Binary Bit Strings Hash Tables http://www.geneprof.org/GeneProf/help_modules.jsp

3 HASH TABLES A mapping: object => hash-code(integer) Hash tables are arrays Of a fixed size n (n >> expected size) Using a hash code (modulo!) Collisions: Ideally they won't happen They will when: the (n >> expected size) condition isn't met the hashing function is poorly defined. How do we handle them? buckets at each location. My approach: open addressing / probing Other variants: Cuckoo hashing, robin hood hashing, hopscotch hashing, …

4 HASH FUNCTIONS Perfect hashing No collisions Not usually possible (in the general case) Usually requires some a-priori knowledge of the domain. Good hash functions Characteristics: Uniform distribution of hash values (no clustering ) Memory locations?? Every object in Java has a int hashCode() method CRC for strings / binary data

5 OPEN HASHING / CIRCULAR PROBING int findIndex(Object e, boolean adding) { index = hashValue % tableSize if (table[index] == desiredVal): return index; else: temp_pos = (index + 1) % tableSize while (temp_pos != index): if (table[temp_pos] == desiredVal) return temp_pos; temp_pos = (temp_pos + 1) % tableSize; raise Exception?? } # desiredVal: # null / e if adding is true # e if adding is false


Download ppt "SETS AND HASHING. SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S."

Similar presentations


Ads by Google