39
CSC212 Data Structure - Section FG Lecture 20 Hashing Instructor: Feng HU Department of Computer Science City College of New York

Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

  • Upload
    lytruc

  • View
    216

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

CSC212 Data Structure

- Section FG

Lecture20Hashing

Instructor:FengHUDepartmentofComputerScience

CityCollegeofNewYork

Page 2: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

HashTables

•Chapter12discussesseveralwaysofstoringinformationinanarray,andlatersearchingfortheinformation.

•Hashtables areacommonapproachtothestoring/searchingproblem.

• Thispresentationintroduceshashtables.Data Structures

and Other ObjectsUsing C++

Page 3: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

WhatisaHashTable?

• Thesimplestkindofhashtableisanarrayofrecords.

• Thisexamplehas701records.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

An array of records

. . .

[ 700]

Page 4: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

WhatisaHashTable?

• Eachrecordhasaspecialfield,calleditskey.

• Inthisexample,thekeyisalongintegerfieldcalledNumber.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

. . .

[ 700]

[ 4 ]

Number 506643548

Page 5: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

WhatisaHashTable?

• Thenumbermightbeaperson'sidentificationnumber,andtherestoftherecordhasinformationabouttheperson.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

. . .

[ 700]

[ 4 ]

Number 506643548

Page 6: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

WhatisaHashTable?

• Whenahashtableisinuse,somespotscontainvalidrecords,andotherspotsare"empty".

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .

Page 7: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

InsertingaNewRecord

• Inordertoinsertanewrecord,thekeymustsomehowbeconvertedto anarrayindex.

• Theindexiscalledthehashvalue ofthekey.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .

Number 580625685

Page 8: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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) ?

Page 9: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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

Page 10: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

InsertingaNewRecord

• Thehashvalueisusedforthelocationofthenewrecord.

Number 580625685

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .

[3]

Page 11: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

InsertingaNewRecord

• Thehashvalueisusedforthelocationofthenewrecord.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .Number 580625685

Page 12: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

Collisions

• Hereisanothernewrecordtoinsert,withahashvalueof2.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .Number 580625685

Number 701466868

My hashvalue is [2].

Page 13: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 14: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 15: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 16: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 17: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 18: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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

. . .

Page 19: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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].

Page 20: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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].

Page 21: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

SearchingforaKey

• Thedatathat'sattachedtoakeycanbefoundfairlyquickly.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .Number 580625685 Number 701466868

Number 701466868

Page 22: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 23: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 24: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 25: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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!

Page 26: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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!

Page 27: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

DeletingaRecord

• Recordsmayalsobedeletedfromahashtable.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 506643548Number 233667136Number 281942902 Number 155778322

. . .Number 580625685 Number 701466868

Pleasedelete me.

Page 28: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

DeletingaRecord

• Recordsmayalsobedeletedfromahashtable.• Butthelocationmustnotbeleftasanordinary"emptyspot"sincethatcouldinterferewithsearches.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902 Number 155778322

. . .Number 580625685 Number 701466868

Page 29: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

DeletingaRecord

• Recordsmayalsobedeletedfromahashtable.• Butthelocationmustnotbeleftasanordinary"emptyspot"sincethatcouldinterferewithsearches.

• Thelocationmustbemarkedinsomespecialwaysothatasearchcantellthatthespotusedtohavesomethinginit.

[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700]Number 233667136Number 281942902 Number 155778322

. . .Number 580625685 Number 701466868

Page 30: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

TimeAnalysis

• Withoutanycollisions• constant

• Withcollisions• O(k)wherekistheaveragecollisions foritems• k<<n, sizeof theproblem

Page 31: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

ImprovingHashing

• Sizeofthehashingtablewhenusingdivisionhashfunction• primenumberintheformof4k+3

•Otherhashingfunctions• mid-square,multiplicative

•Doublehashing(insteadoflinearprobing)• the2nd hashfunctionforsteppingthroughthearray

•Chainedhashing• usingalinkedlistforeachcomponentofthehashtable

Page 32: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

Summary

•Hashtablesstoreacollectionofrecordswithkeys.• Thelocationofarecorddependsonthehashvalueoftherecord'skey.

•Whenacollisionoccurs,thenextavailablelocationisused.

• Searchingforaparticularkeyisgenerallyquick.•Whenanitemisdeleted,thelocationmustbemarkedinaspecialway,sothatthesearchesknowthatthespotusedtobeused.

Page 33: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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).

Page 34: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

Kathy Martin817339024

Took Data Structures in Fall 1993.Grade A.

Hard worker. Always gets things doneon time.

Currently working for ABCin New York City.

Page 35: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 36: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 37: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 38: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.

Page 39: Data Structure - City University of New Yorkvisionlab.engr.ccny.cuny.edu/~fhu/Lecture20-Hashing.pdf ·  · 2016-11-28Data Structure -Section FG Lecture 20 Hashing Instructor: Feng

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.