2 views

Uploaded by बानि तमिन्

hash

- Arraylist,Hashtable,Dictionary
- Symmetric and Aysmmetric Algorithms
- CSC 201-Design and Analysis of Algorithms-Fall2016
- MBA 758 Database Management System
- iosrjournals.org
- Session 6
- PROD_ora_9358_ALEXANDREBOR_0326_094017
- Real Time Packet Classification and Analysis based on Bloom Filter for Longest Prefix Matching
- SELECT-Q307 Tropashko Symm
- 05 Java Language and OOP Part V
- Computer Notes - Data Structures - 37
- Hashing
- New Plsql Features in Action Doc
- IRJET-Analysis of Perceptual Hashing System for Secure and Robust Image Hashing
- DatastructureC.ppt
- Python Speed Optimization in the Real World
- Bloom
- Collections
- Report + Submission Guideline (TT02 and TT03)
- j Java Streams 5 Brian Goetz PDF

You are on page 1of 37

STIA2024

Data Structures & Algorithm

Analysis

1

Chapter Contents

What is Hashing?

Hash Functions

Computing Hash Codes

Compression a Hash Code into an Index for

the Hash Table

Resolving Collisions

Open Addressing with Linear Probing

Open Addressing with Quadratic Probing

Separate Chaining

2

Learning Objective

To describe the basic idea of hashing,

To describe the purpose of a hash table, and a hash

function,

To describe how a hash function compresses a hash

code into an index to hash table,

To explain what collisions are and why they occur,

To describe open addressing as a method to resolve

collisions,

To describe linear probing, and quadratic probingas

particular open addressing schemes,

To describe separate chaining as method to resolve

collisions, and

To describe the relative efficiencies of various

3

collisions resolution techniques.

Chapter Contents (ctd.)

Efficiency

The Load Factor

The Cost of Open Addressing

The Cost of Separate Chaining

4

What is Hashing?

A technique that determines an index or location

for storage of an item in a data structure

The hash function receives the search key

Returns the index of an element in an array

called the hash table

The index is known as the hash index

Hashing can be excellent choice when searching is

the primary task.

A technique that ideally can result in O(1) search

time.

A perfect hash function maps each search key into

a different integer suitable as an index to the hash

table

5

What is Hashing?

6

What is Hashing?

Two steps of the hash function

Convert the search key into an integer

called the hash code

Compress the hash code into the range

of indices for the hash table

Typical hash functions are not perfect

They can allow more than one search

key to map into a single index

This is known as a collision

7

What is Hashing?

8

Hash Functions

hash function

Minimize collisions

Distribute entries uniformly

throughout the hash table

Be fast to compute

9

Computing Hash Codes

We will override the hashCode method of

Object

Guidelines

If a class overrides the method equals, it should

override hashCode

If the method equals considers two objects equal,

hashCode must return the same value for both

objects

If an object invokes hashCode more than once

during execution of program on the same data, it

must return the same hash code

If an object's hash code during one execution of a

program can differ from its hash code during

10

another execution of the same program

Computing Hash Codes

The hash code for a string, s

int hash = 0;

int n = s.length();

for (int i = 0; i < n; i++)

hash = g * hash + s.charAt(i); // g is a positive constant

Use the primitive typed key itself (e.g.

int)

Manipulate internal binary

representations

Use folding (a bit-wise boolean operation

11

such as exclusive or)

Compressing a Hash Code

Must compress the hash code so it fits into

the index range

Typical method for a code c is to compute

c modulo n (c%n)

n is a prime number (the size of the

table)

Index will then be between 0 and n – 1

private int getHashIndex(Object key)

{ int hashIndex = key.hashCode() % hashTable.length;

if (hashIndex < 0)

hashIndex = hashIndex + hashTable.length;

return hashIndex;

} // end getHashIndex

12

Resolving Collisions

location already used in the table

Use another location in the table

(open addressing)

Change the structure of the hash table

so that each array location can

represent multiple values (separate

chaining)

13

Open Addressing with Linear

Probing

Open addressing scheme locates alternate

location in hash table that is available, or

open.

Locating an open location in a hash table is

called probing.

Linear probing

Resolves a collision by examining

consecutive locations in hash table,

beginning at the original hash index and

locating the next available location.

If collision occurs at hashTable[k], look

successively at location k + 1, k + 2, …

14

Open Addressing with Linear

Probing

entries whose search keys hash to the same index. 15

Open Addressing with Linear

Probing

linear probing resolves collisions; each entry contains a

search key and its associated value 16

Removals

17

to remove entries.

Removals

We need to distinguish among three

kinds of locations in the hash table

1. Occupied

The location references an entry in the

dictionary

2. Empty

The location contains null and always did

3. Available

The location's entry was removed from the

dictionary

18

Open Addressing with Linear

Probing

(b) after removing two entries;

19

Open Addressing with Linear

Probing

during the search while adding an entry; (e) after an

addition to a formerly occupied location. 20

Searches that Dictionary

Operations Require

To retrieve an entry

Search the probe sequence for the key

Examine entries that are present, ignore locations

in available state

Stop search when key is found or null reached

To remove an entry

Search the probe sequence same as for retrieval

If key is found, mark location as available

To add an entry

Search probe sequence same as for retrieval

Note first available slot

Use available slot if the key is not found

21

Open Addressing, Quadratic

Probing

Change the probe sequence

Given search key k

Probe to k + 1, k + 22, k + 32, … k + n2

table if table size is a prime number

For avoiding primary clustering

But can lead to secondary clustering

22

Open Addressing, Quadratic

Probing

using quadratic probing.

23

Separate Chaining

Alter the structure of the hash table

Each location can represent multiple

values

Each location called a bucket

Bucket can be a/an

List

Sorted list

Chain of linked nodes

Array

Vector

Resolving collisions by using buckets

that are linked chains. 24

Separate Chaining

bucket is a chain of linked nodes.

25

Separate Chaining

when integer search keys are (a) duplicate and unsorted;

26

Separate Chaining

when integer search keys are (b) distinct and unsorted;

27

Separate Chaining

when integer search keys are (c) distinct and sorted

28

Separate Chaining

and simple way to resolve collisions.

However, separate chaining requires more

memory than open addressing.

29

Efficiency Observations

Same efficiency as successful search

Unsuccessful retrieval or removal

Same efficiency as unsuccessful search

Successful addition

Same efficiency as unsuccessful search

Unsuccessful addition

Same efficiency as successful search

30

Load Factor

or practical

Thus, collisions likely to occur

Collisions occur more often

(the ratio of the size of the data to the size

of the hash table)

31

Load Factor

For open addressing – the maximum value

of is 1 when the hash table is full.

not measure the number of locations in

available state (especially for separate

chaining: has no maximum value)

32

Cost of Open Addressing

Note: Reasonable

efficiency requires

only < 0.5

a search of the hash table for given values of the load

factor when using linear probing.

33

Cost of Open Addressing

probing or double

hashing, should

have < 0.5

required by a search of the hash table for given

values of the load factor when using either

quadratic probing or double hashing.

34

Cost of Separate Chaining

Note: Reasonable

efficiency requires

only < 1

search of hash table for given values of load factor

when using separate chaining. 35

References

Data Structures and Abstractions with Java . Authors: Frank

M. Carrano & Walter Savitch . Chapter 19.

A. . Chapter 9

36

Conclusion

Q & A Session

37

- Arraylist,Hashtable,DictionaryUploaded byManoj Kavedia
- Symmetric and Aysmmetric AlgorithmsUploaded bystrbrry
- CSC 201-Design and Analysis of Algorithms-Fall2016Uploaded byAhmed Ajmal
- MBA 758 Database Management SystemUploaded byCache Line
- iosrjournals.orgUploaded byInternational Organization of Scientific Research (IOSR)
- Session 6Uploaded by29prabakaran
- PROD_ora_9358_ALEXANDREBOR_0326_094017Uploaded byThierry Vanden Broucke
- Real Time Packet Classification and Analysis based on Bloom Filter for Longest Prefix MatchingUploaded byEditor IJRITCC
- SELECT-Q307 Tropashko SymmUploaded byPrabhakar Sharma
- 05 Java Language and OOP Part VUploaded byHariChristian
- Computer Notes - Data Structures - 37Uploaded byecomputernotes
- HashingUploaded bydeepaksethimca
- New Plsql Features in Action DocUploaded byspikeman42
- IRJET-Analysis of Perceptual Hashing System for Secure and Robust Image HashingUploaded byIRJET Journal
- DatastructureC.pptUploaded bykeshavsaini
- Python Speed Optimization in the Real WorldUploaded bymosqi
- BloomUploaded byRamprasad Banothu
- CollectionsUploaded byJanicealmine Sandeepsharma
- Report + Submission Guideline (TT02 and TT03)Uploaded byPeter Kong
- j Java Streams 5 Brian Goetz PDFUploaded byAk Chowdary
- Collections InterfacesUploaded byshivuhc
- Csci2100 09 FinalUploaded byenergy0124
- Collections_in_CSharpUploaded byapi-3764103
- Efficient Similarity Search Over Encrypted DataUploaded byAnonymous kw8Yrp0R5r
- Unit 4, Applications.pdfUploaded byJava Tech
- unit11Uploaded byBinu Koshy
- TocUploaded byGanesh Singh
- ThreadingUploaded byadeshkumarkhare
- Or GroupworkUploaded byNur Farehan
- classdiagrams-160809153237Uploaded byNOMAN

- UNIT-5 Part-2 coding theory.pdfUploaded bysubramanyam62
- assignment+2+Fall+2018Uploaded bySahin KC
- A Graph Distance Metric Based on the Maximal Common SubgraphUploaded bySaad Manzur
- IT Syllabus Anna University-2008 regulationUploaded byAnanda Padmanaban
- Arithmetic ProgressionUploaded bySiti Ummul Warda
- Cpp_STL_ReferenceManualUploaded byapi-19976667
- ch03_Uploaded byMohammad Bilal Mirza
- 06 Basic Graph AlgorithmsUploaded byPratik Jain
- Galois Groups of Number FieldsUploaded bybayareaking
- Numerical Analysis 2210Uploaded bynidhyn
- MTH 221 Week 1 5 All Assignments and Discussion Questions - Page 2Uploaded byRoderick Thomason
- Floating PointUploaded bySzőnyi István
- acpc2013Uploaded byMoncef Mhasni
- Class XIUploaded byvvek10
- Calculating the Smarandache NumbersUploaded byRyanElias
- Raphael Yuster- Disjoint Color-Avoiding TrianglesUploaded byMnaom
- MathUploaded byHeryVandoro
- Assignment Problem 2Uploaded byGaurav Maru
- PERFORMANCE ANALYSIS OF PARALLEL POLLARD’S RHO FACTORING ALGORITHMUploaded byAnonymous Gl4IRRjzN
- Answers for the Floating Point NumbersUploaded byMinoshini Fonseka
- Channel CodingUploaded byIdris Jeffrey Manguera
- Engineering a Sort FunctionUploaded bynikhil4tp
- DISCRETE OPTIMIZATION PROBLEMSUploaded byZaid Hadi
- Yr 8 NCM Answers-2Uploaded bySasha Cole
- CalculoRaices.pptxUploaded byBrayan Arévalo
- Final s2008 Sol 1Uploaded bysaahil480
- DAA Report - simulatorUploaded byNawanjana Hagarangoda
- Grid Compaction AlgorithmsUploaded bysubbarao76
- Unit 14Uploaded byNarendra Singh
- Number System ConversionUploaded bygeddam06108825