CSC212 Data Structure
- Section FG
Lecture20Hashing
Instructor:FengHUDepartmentofComputerScience
CityCollegeofNewYork
HashTables
•Chapter12discussesseveralwaysofstoringinformationinanarray,andlatersearchingfortheinformation.
•Hashtables areacommonapproachtothestoring/searchingproblem.
• Thispresentationintroduceshashtables.Data Structures
and Other ObjectsUsing C++
WhatisaHashTable?
• Thesimplestkindofhashtableisanarrayofrecords.
• Thisexamplehas701records.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
An array of records
. . .
[ 700]
WhatisaHashTable?
• Eachrecordhasaspecialfield,calleditskey.
• Inthisexample,thekeyisalongintegerfieldcalledNumber.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
. . .
[ 700]
[ 4 ]
Number 506643548
WhatisaHashTable?
• Thenumbermightbeaperson'sidentificationnumber,andtherestoftherecordhasinformationabouttheperson.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
. . .
[ 700]
[ 4 ]
Number 506643548
WhatisaHashTable?
• Whenahashtableisinuse,somespotscontainvalidrecords,andotherspotsare"empty".
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .
InsertingaNewRecord
• Inordertoinsertanewrecord,thekeymustsomehowbeconvertedto anarrayindex.
• Theindexiscalledthehashvalue ofthekey.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .
Number 580625685
InsertingaNewRecord
• Typicalwaytocreateahashvalue:
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .
Number 580625685
(Number mod 701)
What is (580625685 mod 701) ?
InsertingaNewRecord
• Typicalwaytocreateahashvalue:
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .
Number 580625685
(Number mod 701)
What is (580625685 mod 701) ?3
InsertingaNewRecord
• Thehashvalueisusedforthelocationofthenewrecord.
Number 580625685
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .
[3]
InsertingaNewRecord
• Thehashvalueisusedforthelocationofthenewrecord.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Collisions
• Hereisanothernewrecordtoinsert,withahashvalueof2.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Number 701466868
My hashvalue is [2].
Collisions
• Hereisanothernewrecordtoinsert,withahashvalueof2.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Number 701466868
My hashvalue is [2].When a collision occurs,
move forward until youfind an empty spot.
Collisions
• Thisiscalledacollision,becausethereisalreadyanothervalidrecordat[2].
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Number 701466868
When a collision occurs,move forward until you
find an empty spot.
Collisions
• Thisiscalledacollision,becausethereisalreadyanothervalidrecordat[2].
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Number 701466868
When a collision occurs,move forward until you
find an empty spot.
Collisions
• Thisiscalledacollision,becausethereisalreadyanothervalidrecordat[2].
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685
Number 701466868
When a collision occurs,move forward until you
find an empty spot.
Collisions
• Thisiscalledacollision,becausethereisalreadyanothervalidrecordat[2].
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
The new record goesin the empty spot.
AQuiz
Where would you be placed in this table, if there is no collision? Use your social security number or some other favorite number.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322Number 580625685 Number 701466868
. . .
AnotherKindofCollision
Where would you be placed in this table, if there is no collision? Use your social security number or some other favorite number.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322Number 580625685 Number 701466868
. . .
Number 155779023
My hashvalue is [700].
AnotherKindofCollision
Where would you be placed in this table, if there is no collision? Use your social security number or some other favorite number.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322Number 580625685 Number 701466868
. . .
Number 155779023
My hashvalue is [700].
SearchingforaKey
• Thedatathat'sattachedtoakeycanbefoundfairlyquickly.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
SearchingforaKey
• Calculatethehashvalue.• Checkthatlocationofthearrayforthekey.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
My hashvalue is [2].
Not me.
SearchingforaKey
• Keepmovingforwarduntilyoufindthekey,oryoureachanemptyspot.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
My hashvalue is [2].
Not me.
SearchingforaKey
• Keepmovingforwarduntilyoufindthekey,oryoureachanemptyspot.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
My hashvalue is [2].
Not me.
SearchingforaKey
• Keepmovingforwarduntilyoufindthekey,oryoureachanemptyspot.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
My hashvalue is [2].
Yes!
SearchingforaKey
• Whentheitemisfound,theinformationcanbecopiedtothenecessarylocation.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Number 701466868
My hashvalue is [2].
Yes!
DeletingaRecord
• Recordsmayalsobedeletedfromahashtable.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
Pleasedelete me.
DeletingaRecord
• Recordsmayalsobedeletedfromahashtable.• Butthelocationmustnotbeleftasanordinary"emptyspot"sincethatcouldinterferewithsearches.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
DeletingaRecord
• Recordsmayalsobedeletedfromahashtable.• Butthelocationmustnotbeleftasanordinary"emptyspot"sincethatcouldinterferewithsearches.
• Thelocationmustbemarkedinsomespecialwaysothatasearchcantellthatthespotusedtohavesomethinginit.
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902 Number 155778322
. . .Number 580625685 Number 701466868
TimeAnalysis
• Withoutanycollisions• constant
• Withcollisions• O(k)wherekistheaveragecollisions foritems• k<<n, sizeof theproblem
ImprovingHashing
• Sizeofthehashingtablewhenusingdivisionhashfunction• primenumberintheformof4k+3
•Otherhashingfunctions• mid-square,multiplicative
•Doublehashing(insteadoflinearprobing)• the2nd hashfunctionforsteppingthroughthearray
•Chainedhashing• usingalinkedlistforeachcomponentofthehashtable
Summary
•Hashtablesstoreacollectionofrecordswithkeys.• Thelocationofarecorddependsonthehashvalueoftherecord'skey.
•Whenacollisionoccurs,thenextavailablelocationisused.
• Searchingforaparticularkeyisgenerallyquick.•Whenanitemisdeleted,thelocationmustbemarkedinaspecialway,sothatthesearchesknowthatthespotusedtobeused.
Hash Table Exercise
Five records of past students
- Create a small hash table with size 5 (indexes 0 to 4).
- Insert the five items
- Remove Bill Clinton
- Do three searches (for Will Smith, Bill Clinton, and Elizabeth).
Kathy Martin817339024
Took Data Structures in Fall 1993.Grade A.
Hard worker. Always gets things doneon time.
Currently working for ABCin New York City.
Will Smith506643973
Took Data Structures in Fall 1995.Grade A.
A bit of a goof-off, but he comes throughin a pinch.
Currently saving the world from alieninvasion.
William “Bill” Clinton330220393
Took Data Structures in Fall 1995.Grade B-.
Gets along with mostpeople well.
Been laid off even before the slowdown of the economy.
Elizabeth Windsor092223340
Took Data Structures in Fall 1995.Grade B-.
Prefers to be called “Elizabeth II” or “HerMajesty.” Has some family problems.
Currently working in public relationsnear London.
Al Einstein699200102
Took CSCI 2270 in Fall 1995.Grade F.
In spite of poor grade, I think there isgood academic ability in Al.
Currently a well-known advocate forpeace.
THE END
Presentation copyright 1997 Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.