Download - ΜΑΘΗΜΑ 10 ο

Transcript
Page 1: ΜΑΘΗΜΑ  10 ο

8-1

ΜΑΘΗΜΑ 10ο

Πίνακες Κατακερματισμού

Υλικό εκτός εξετάσιμης ύλης για την χρονιά 2007-2008.

Page 2: ΜΑΘΗΜΑ  10 ο

8-2

Hashing Lecture 10

• hashing methods and their implementation

• hash tables

• scatter tables

Το υλικό για αυτή τη διάλεξη είναι από το βιβλίο «Data Structures and Algorithms with Object Oriented Design Patterns in Java”, Bruno R. Preiss, John Willey and Sons.

Page 3: ΜΑΘΗΜΑ  10 ο

8-3

Rationale for Hashing

• many applications: information store & retrieval

• consider containers storing {key, value} pairs

• hashing-based containers suitable for applications with

– frequently executed basic operations [find(key), insert]

– items are not required to be ordered

• main advantage: find, insert are O(1) (average case)

Page 4: ΜΑΘΗΜΑ  10 ο

8-4

Hashing Methods Section 8.1-3

• basic idea

• hash functions

– characteristics

– common methods

• dealing with arbitrary keys

Page 5: ΜΑΘΗΜΑ  10 ο

8-5

Array-Based Implementation of the Container

• for clarity of presentation, consider the case when the container

contains keys only

• an array will hold some number of items of a given set of keys K

• the position of a key in the array is given by a hash function h(·)

• in general |K| is large or even unbounded

• the actual number of items stored in the container is typically

much less than |K| use an array of size M

Page 6: ΜΑΘΗΜΑ  10 ο

8-6

Hash Functions

• we need a function1

0: MKh

Keys

0

M-1

h()

Page 7: ΜΑΘΗΜΑ  10 ο

8-7

Characteristics of a Good Hash Function

• good hash function

– avoids collisions

– spreads keys evenly in the array

– its computation is fast

Page 8: ΜΑΘΗΜΑ  10 ο

8-8

Hashing Methods

• division method: h(x) = x mod M

• middle-square method W=2word length

• multiplication method

• Fibonacci hashing

W

WxMxh

mod )(

2

Wa 1

W

WaxMxh

mod )(

Page 9: ΜΑΘΗΜΑ  10 ο

8-9

Implementing the Division Method

public class DivisionMethod {

static final int M = 1031; // a prime

public static int h (int x) {

return Math.abs (x) % M;

}

}

Page 10: ΜΑΘΗΜΑ  10 ο

8-10

Implementing the Multiplication & Fibonacci Methods

public class MultiplicationMethod {

static final int k = 10; // M==1024

static final int w = 32;

static final int a = (int) 2654435769L;

public static int h(int x)

{ return (x * a) >>> (w - k); }

}

Page 11: ΜΑΘΗΜΑ  10 ο

8-11

Page 12: ΜΑΘΗΜΑ  10 ο

8-12

Dealing With Arbitrary Keys

• What if keys are not integers?

• function f maps keys into non-negative integers:

• function g maps non-negative integers into

• hash function h is simply h = f g

Kf :

10

M

10: Mg

illustration

Page 13: ΜΑΘΗΜΑ  10 ο

8-13

Hashing of Strings

• view a character string, s, as a sequence of n characters,

{s0, s1, . . . , sn-1}

• simple (but very poor) hash function is

• e.g., for all possible a ASCII strings of length five 0 ≤ f(s) ≤ 640

1

0

)(n

iissf

Page 14: ΜΑΘΗΜΑ  10 ο

8-14

Better String Hash Function

• suppose we known a priori that all strings have length four

• let B = 256

• we can compute the 32-bit quantity like this

f(s) = s0B3 +s1B2 +s2B +s0

• this function spreads out the values better

Page 15: ΜΑΘΗΜΑ  10 ο

8-15

Dealing with Arbitrary Length Strings

• generalize the preceding approach:

• computes a unique integer for every possible string

• unfortunately the range of f(s) is unbounded

• a simple modification bounds the range:

1

0

1)(n

ii

in sBsf

WsBsfn

ii

in mod)(1

0

1

Page 16: ΜΑΘΗΜΑ  10 ο

8-16

More Elaborate Hash Function

Page 17: ΜΑΘΗΜΑ  10 ο

8-17

Hash Tables Section 8.4

• hash tables

– main difference: dealing with collisions

• separate chaining hash tables [illustration]

– running time analysis• worst case, average case

• scatter tables [illustration]

– chained, open addressing

– running time analysis

Page 18: ΜΑΘΗΜΑ  10 ο

8-18

Page 19: ΜΑΘΗΜΑ  10 ο

8-19

Hash Tables

• a hash table is a searchable container that implements the

HashTable interface

• the AbstractHashTable class is an abstract class from

which various implementations are derived

– method getLength (returns M)

– methods f, g, and h (h = f g)

• the implementation shown uses the division method of

hashing

Page 20: ΜΑΘΗΜΑ  10 ο

8-20

Page 21: ΜΑΘΗΜΑ  10 ο

8-21

Page 22: ΜΑΘΗΜΑ  10 ο

8-22

Separate Chaining Hash Table

• uses separate chaining to resolve collisions

• hash table is implemented as an array of linked lists

• linked list into which an item is to b inserted is determined by

hashing that item

• item is inserted into the linked list by appending it

Page 23: ΜΑΘΗΜΑ  10 ο

8-23

Example: Keys and their Hash Values

key k f(k) in octal h(k)8 = f%208 (octal)

“ett” 01446564 04

“tva” 01656545 05

“tre” 01656345 05

“fyra” 0147706341 01

“fem” 01474455 15

“sex” 01624470 10

“sju” 01625365 05

“atta” 0344656541 01

“nio” 01575057 17

“tio” 01655057 17

“elva” 044557741 01

“tolv” 065565566 06

Prog 8.3(octal)

Page 24: ΜΑΘΗΜΑ  10 ο

8-24

key k h(k)8

“ett” 04

“tva” 05

“tre” 05

“fyra” 01

“fem” 15

“sex” 10

“sju” 05

“atta” 01

“nio” 17

“tio” 17

“elva” 01

“tolv” 06

Page 25: ΜΑΘΗΜΑ  10 ο

8-25

Page 26: ΜΑΘΗΜΑ  10 ο

8-26

Page 27: ΜΑΘΗΜΑ  10 ο

8-27

ChainedHashTable Running Time (Worst Case)

constructor O(M)

purge O(M)

insert T <hashCode> + O(1)

withdraw T <hashCode> + O(n)

find O(1) +T<hashCode> +nT <isEQ> +O(n)

Page 28: ΜΑΘΗΜΑ  10 ο

8-28

Average Case Analysis

• consider we have a hash table of size M

• let there be exactly n items in the hash table

• the quantity λ = n/M is called the load factor

• the load factor is the average length of a linked list!

Page 29: ΜΑΘΗΜΑ  10 ο

8-29

Average Running Times

• unsuccessful search

O(1)+T<hashCode>+λT <isEQ>+O(λ)

• successful search

O(1)+T<hashCode>+((λ+1)/2)T<isEQ>+O(λ)

• if one could guarantee λ ≤ 1, then

T <hashCode> = O(1) and T <isEQ> = O(1),

consequently all operations would be O(1) [average]

• to guarantee this, must resize