Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

35
Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming

description

Mission  Write a simple address book  Use hash functions to access a table Insertion Lookup Deletion

Transcript of Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Page 1: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Assignment 2: A Simple Address Book

Andy WangData Structures, Algorithms, and

Generic Programming

Page 2: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Learning Objectives

Gain further experience in C++ Manage project develop using make Apply the concept of hash functions

Page 3: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Mission

Write a simple address book Use hash functions to access a table

Insertion Lookup Deletion

Page 4: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Deliverables (Due 9/26/2003)

makefile hashtable.h hashtable.cpp main.cpp Development logs

Page 5: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

More on Development Log

Due in each class Cumulative

9/17, turn in a log that covers 9/15 – 9/17 9/22, turn in a log that covers 9/15 – 9/22 And so on…

Page 6: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Requirements

Create a proj2 directory makefile hashtable.h hashtable.cpp main.cpp

Page 7: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

hashtable.h

Interface of the class HashTable Required public interface:

bool Insert(const string &name, const string &addr);

bool Lookup(const string &name, string *addr) const;

string Remove(const string &name);

Page 8: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Constructor

Hashtable(unsigned int size); Default size: 5

Page 9: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

bool Insert(const string &name, const string &addr);

• Return value• true on success• false on failure

• Duplicate name• Table is full

Page 10: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Michael JacksonAddress:

0123

Hash Table

Page 11: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Michael JacksonAddress: Never-Never Land

0123

Hash(name)

Hash Table

Page 12: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Michael JacksonAddress: Never-Never Land

01 Michael Jackson Never-Never Land23

Hash(name)

Hash Table

Page 13: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Mickey MouseAddress: Disneyland

01 Michael Jackson Never-Never Land23

Hash Table

Page 14: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Mickey MouseAddress: Disneyland

01 Michael Jackson Never-Never Land23

Hash(name)

Hash Table

Oh, Boy!

Page 15: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

One problem with hashing—collision

Collision handling techniques: Increase the hash table size Chaining Linear probing Quadratic probing Double hashing

Page 16: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Increase the Hash Table Size

If collision, double the size, rehash all entries + Conceptually simple

- Unbounded growth of table size Birthday Paradox

In a group of 60 people, you are very likely to find two people with the same birthday.

The probability of collisions is higher than you think!

Page 17: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Chaining

Requires a data structure from a later lecture Basic idea: each table entry is associated

with multiple (key, value) pairs Sequentially look through those pairs

+ incremental growth of the table- poor performance when an entry is associated

with too many (key, value) pairs

Page 18: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Linear Probing

Sequentially find the next empty table entry (Hash(key) + 1) % size (Hash(key) + 2) % size …+ Simple+ Use all table entries before size increase- Clustering (nonuniform distribution of table

entries)- Can degenerate into sequential searches

Page 19: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Quadratic Probing

Try to avoid clustering (Hash(key) + 12) % size (Hash(key) + 22) % size…+ Simple- Secondary clustering (not as severe)

Page 20: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Double Hashing

Use another hash function to determine the skipping distance If Hash1(key) points to an occupied entry Use (Hash1(key) + Hash2(Hash1(key))) % size (Hash1(key) + 2*Hash2(Hash1(key)) % size…

+ Avoids secondary clustering+ Widely used in compilers

Page 21: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Lookup()

bool Lookup(const string &name, string *address) const;

• Return value• true if found

• address contains the address• false if not found

Page 22: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Lookup()

Name: Michael Jackson

01 Michael Jackson Never-Never Land23

Hash Table

Page 23: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Lookup()

01 Michael Jackson Never-Never Land23

Hash(name)

Hash Table

Name: Michael Jackson

Page 24: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

string Remove(const string &name) const;

• Return value• The address associated with the name

Page 25: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

Name: Michael Jackson

01 Michael Jackson Never-Never Land23

Hash Table

Page 26: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

01 Michael Jackson Never-Never Land23

Hash(name)

Hash Table

Name: Michael Jackson

Page 27: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

0123

Hash(name)

Hash Table

Name: Michael Jackson

Page 28: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Your Hash Function

Hash(name) // may contain spaces Develop/choose your own hash function(s)

Use all characters in the name Use the ordering information of each character Short names should not cluster in the table

Page 29: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

1. Create all files Add #include into .cpp files Add #ifndef into .h files Add files names into the makefile make clean make

Page 30: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

2. Add the class definitions to the header files Try to compile

3. Write the skeleton code in your .cpp files Nothing but empty functions Try to get the interface to compile

4. Write pseudo code (comments) in each skeleton function

Page 31: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

5. Implement the constructor/destructor routines Add private functions as needed Compile and test those routines

6. Implement a dump routine to visualize the hash table

Page 32: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

7. Implement the accessor routines that only read your private data members Compile and test those routines

8. Implement the accessor routines that write to your private data members Compile and test those routines

Page 33: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

9. Implement the Insert routine Implement the associated hash function(s) Test the hash function in isolation Check for clustering Check for duplicate names

10. Implement collision handling

Page 34: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Milestones

11. Implement the Lookup routine Compile and test the routine

12. Implement the Remove routine. Compile and test the routine

Page 35: Assignment 2: A Simple Address Book Andy Wang Data Structures, Algorithms, and Generic Programming.

Pace Yourself

Try to accomplish 1 to 2 milestones per day Start your project early!