DOC PREVIEW
UMD CMSC 132 - Hashing

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

HashingSlide 2Slide 3Hash FunctionSlide 5Slide 6Types of Hash TablesOpen Addressing HashingSlide 9Open Addressing ExampleSlide 11Slide 12Slide 13Efficiency of Open HashingChaining (Bucket Hashing)Chaining ExampleSlide 17Slide 18Efficiency of ChainingHashing in JavaJava ContractHashingNelson Padua-PerezChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College ParkHashingApproachTransform key into number (hash value)Use hash value to index object in hash tableUse hash function to convert key to numberHashingHash TableArray indexed using hash valuesHash Table A with size NIndices of A range from 0 to N-1Store in A[ hashValue % N]Hash FunctionGoalScatter values uniformly across rangeHash( <everything> ) = 0Satisfies definition of hash functionBut not very usefulMultiplicative congruency methodProduces good hash valuesHash value = (a  int(key)) % NWhere N is table sizea, N are large primesHash FunctionExamplehashCode("apple") = 5hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2Perfect hash functionUnique values for each key kiwibananawatermelonapplemangograpesstrawberry0123456789Hash FunctionSuppose nowhashCode("apple") = 5hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2hashCode(“orange") = 3CollisionSame hash value for multiple keyskiwibananawatermelonapplemangograpesstrawberry0123456789Types of Hash TablesOpen addressingStore objects in each table entryChaining (bucket hashing)Store lists of objects in each table entryOpen Addressing HashingApproachHash table contains objectsProbe  examine table entryCollisionMove K entries past current locationWrap around table if necessaryFind location for X1. Examine entry at A[ key(X) ]2. If entry = X, found3. If entry = empty, X not in hash table4. Else increment location by K, repeatOpen Addressing HashingApproachLinear probingK = 1May form clusters of contiguous entriesDeletionsFind location for XIf X inside cluster, leave non-empty markerInsertionFind location for XInsert if X not in hash tableCan insert X at first non-empty markerOpen Addressing ExampleHash codesH(A) = 6 H(C) = 6H(B) = 7 H(D) = 7Hash tableSize = 8 elements = empty entry* = non-empty markerLinear probingCollision  move 1 entry past current location12345678Open Addressing ExampleOperationsInsert A, Insert B, Insert C, Insert D12345678A12345678AB12345678ABC12345678DABCOpen Addressing ExampleOperationsFind A, Find B, Find C, Find D12345678123456781234567812345678DABCDABCDABCDABCOpen Addressing ExampleOperationsDelete A, Delete C, Find D, Insert C12345678123456781234567812345678DCB*D*BCD*B*D*B*Efficiency of Open HashingLoad factor = entries / table sizeHashing is efficient for load factor < 90%Chaining (Bucket Hashing)ApproachHash table contains lists of objectsFind location for X Find hash code key for XExamine list at table entry A[ key ]CollisionMultiple entries in list for entryChaining ExampleHash codesH(A) = 6 H(C) = 6H(B) = 7 H(D) = 7Hash tableSize = 8 elements = empty entry12345678Chaining ExampleOperationsInsert A, Insert B, Insert C12345678 A12345678AB12345678CBAChaining ExampleOperationsFind B, Find A12345678CBA12345678CBAEfficiency of ChainingLoad factor = entries / table sizeAverage caseEvenly scattered entriesOperations = O( load factor )Worse case Entries mostly have same hash valueOperations = O( entries )Hashing in JavaCollectionshashMap & hashSet implement hashingObjectsBuilt-in support for hashingboolean equals(object o)int hashCode()Can override with own definitionsMust be careful to support Java contractJava ContracthashCode() Must return same value for object in each execution, provided no information used in equals comparisons on the object is modifiedequals()if a.equals(b), then a.hashCode() must be the same as b.hashCode()if a.hashCode() != b.hashCode(), then !a.equals(b)a.hashCode() == b.hashCode()Does not imply a.equals(b)Though Java libraries will be more efficient if it is


View Full Document

UMD CMSC 132 - Hashing

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Load more
Download Hashing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Hashing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Hashing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?