Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Dictionary of ADT and the Hash Tables - Outline | CS 361L, Study notes of Computer Science

Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-qzo
koofers-user-qzo 🇺🇸

5

(1)

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 15
Jared Saia
University of New Mexico
Outline
Dictionary ADT
Hash Tables
1
Dictionary ADT
A dictionary ADT implements the following operations
Insert(x): puts the item x into the dictionary
Delete(x): deletes the item x from the dictionary
IsIn(x): returns true iff the item x is in the dictionary
2
Dictionary ADT
Frequently, we think of the items being stored in the dictio-
nary as keys
The keys typically have records associated with them which
are carried around with the key but not used by the ADT
implementation
Thus we can implement functions like:
Insert(k,r): puts the item (k,r) into the dictionary if the
key k is not already there, otherwise returns an error
Delete(k): deletes the item with key k from the dictionary
Lookup(k): returns the item (k,r) if k is in the dictionary,
otherwise returns null
3
pf3
pf4
pf5

Partial preview of the text

Download Dictionary of ADT and the Hash Tables - Outline | CS 361L and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 15

Jared Saia

University of New Mexico

Outline

Dictionary ADT

Hash Tables

Dictionary ADT

A dictionary ADT implements the following operations

Insert(x)

: puts the item x into the dictionary

Delete(x)

: deletes the item x from the dictionary

IsIn(x)

: returns true iff the item x is in the dictionary

Dictionary ADT

nary asFrequently, we think of the items being stored in the dictio-

keys

The keys typically have

records

associated with them which

implementationare carried around with the key but not used by the ADT

Thus we can implement functions like:

Insert(k,r)

puts the item (k,r) into the dictionary if the

key k is not already there, otherwise returns an error

Delete(k)

: deletes the item with key k from the dictionary

Lookup(k)

: returns the item (k,r) if k is in the dictionary,

otherwise returns null

Implementing Dictionaries

linked listThe simplest way to implement a dictionary ADT is with a

Let

l be a linked list data structure,

assume we have the

following operations defined for

l

head(l): returns a pointer to the head of the list

next(p): given a pointer

p

into the list, returns a pointer

to the next element in the list if such exists, null otherwise

previous(p):

given

a

pointer

p

into

the

list,

returns

a

null otherwisepointer to the previous element in the list if such exists,

of that itemkey(p): given a pointer into the list, returns the key value

value of that itemrecord(p): given a pointer into the list, returns the record

In-Class Exercise

Implement a dictionary with a linked list

to the item with key k if it is in the dictionary or null otherwiseQ1: Write the operation Lookup(k) which returns a pointer

Q2: Write the operation Insert(k,r)

Q3: Write the operation Delete(k)

Q4:

For a dictionary with

n

elements, what is the runtime

of all of these operations for the linked list data structure?

In-Class Exercise

Q5:

Describe how you would use this dictionary ADT to

book.count the number of occurences of each word in an online

Q6:

If

m

is the total number of words in the online book,

and

n

is the number of unique words, what is the runtime of

the algorithm for the previous question?

Dictionaries

This linked list implementation of dictionaries is very slow

Q: Can we do better?

A: Yes, with hash tables, AVL trees, etc

Hash Tables

“Key” Idea:

An element with key

k

is stored in slot

h ( k ),

where

h is a

hash function

mapping

U

into the set

,... , m

Main problem: Two keys can now hash to the same slot

Q: How do we resolve this problem?

making the table large enoughA1: Try to prevent it by hashing keys to “random” slots and

A2: Chaining

A3: Open Addressing

Chained Hash

CH-Delete(T,x){delete x from the list T[h(key(x))];}CH-Search(T,k){search for elem with key k in list T[h(k)];} CH-Insert(T,x){Insert x at the head of list T[h(key(x))];}linked list. In chaining, all elements that hash to the same slot are put in a

Analysis

CH-Insert and CH-Delete take

O

(1) time if the list is doubly

linked and there are no duplicate keys

Q: How long does CH-Search take?

A: It depends.

In particular,

depends on the

load factor

α

=

n/m

(i.e. average number of elems in a list)

CH-Search Analysis

Worst case analysis: everyone hashes to one slot so Θ(

n )

For average case, make the

simple uniform hashing

assump-

mtion: any given elem is equally likely to hash into any of the

slots, indep. of the other elems

Let

n i be a random variable giving the length of the list at

the

i -th slot

Then time to do a search for key

k

is 1 +

(^) n h ( k )

CH-Search Analysis

Q: What is

E

n h ( k ) )?

A: We know that

h ( k ) is uniformly distributed among

, .., m

Thus,

E

n h ( k ) ) =

∑ m − 1

i

/m

n i =

n/m

α

Hash Functions

Want each key to be equally likely to hash to any of the

m

slots, independently of the other keys

terns that might exist in the dataKey idea is to use the hash function to “break up” any pat-

easily convert strings to naturaly numbers)We will always assume a key is a natural number (can e.g.

Division Method

h ( k ) =

k

mod

m

Want

m

to be a

prime number

, which is not too close to a

power of 2

Why?

Multiplication Method

h ( k ) =

b m (^) ∗ (^) ( kA

mod 1)

c

kA

mod 1 means the fractional part of

kA

Advantage: value of

m

is not critical, need not be a prime

A

2 works well in practice