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

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hashing General idea Hash function Separate Chaining Open Addressing
CSCE 3400 Data Structures & Algorithm Analysis
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing as a Dictionary Implementation
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
CpSc 3220 File and Database Processing Hashing. Exercise – Build a B + - Tree Construct an order-4 B + -tree for the following set of key values: (2,
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
1 Joe Meehean 1.  BST easy to implement average-case times O(LogN) worst-case times O(N)  AVL Trees harder to implement worst case times O(LogN)  Can.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Comp 335 File Structures Hashing.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing as a Dictionary Implementation Chapter 19.
CSC 427: Data Structures and Algorithm Analysis
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Hash Tables CSIT 402 Data Structures II. Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
Hashing. Search Given: Distinct keys k 1, k 2, …, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ), …, (k n, I n ) where I j is.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Building Java Programs Generics, hashing reading: 18.1.
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Fundamental Structures of Computer Science II
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hash Tables.
Chapter 28 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Searching Tables Table: sequence of (key,information) pairs
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE190D: Topics in Database System Implementation
Presentation transcript:

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

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

Deliverables (Due 9/26/2003)  makefile  hashtable.h  hashtable.cpp  main.cpp  Development logs

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…

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

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

Constructor  Hashtable(unsigned int size);  Default size: 5

Insert() bool Insert(const string &name, const string &addr); Return value true on success false on failure Duplicate name Table is full

Insert() Name: Michael Jackson Address: Hash Table

Insert() Name: Michael Jackson Address: Never-Never Land Hash(name) Hash Table

Insert() Name: Michael Jackson Address: Never-Never Land 0 1Michael JacksonNever-Never Land 2 3 Hash(name) Hash Table

Insert() Name: Mickey Mouse Address: Disneyland 0 1Michael JacksonNever-Never Land 2 3 Hash Table

Insert() Name: Mickey Mouse Address: Disneyland 0 1Michael JacksonNever-Never Land 2 3 Hash(name) Hash Table Oh, Boy!

One problem with hashing—collision  Collision handling techniques: Increase the hash table size Chaining Linear probing Quadratic probing Double hashing

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!

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

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

Quadratic Probing  Try to avoid clustering (Hash(key) ) % size (Hash(key) ) % size… + Simple - Secondary clustering (not as severe)

Double Hashing  Use another hash function to determine the skipping distance If Hash 1 (key) points to an occupied entry Use (Hash 1 (key) + Hash 2 (Hash 1 (key))) % size (Hash 1 (key) + 2*Hash 2 (Hash 1 (key)) % size… + Avoids secondary clustering + Widely used in compilers

Lookup() bool Lookup(const string &name, string *address) const; Return value true if found address contains the address false if not found

Lookup() Name: Michael Jackson 0 1Michael JacksonNever-Never Land 2 3 Hash Table

Lookup() 0 1Michael JacksonNever-Never Land 2 3 Hash(name) Hash Table Name: Michael Jackson

Remove() string Remove(const string &name) const; Return value The address associated with the name

Remove() Name: Michael Jackson 0 1Michael JacksonNever-Never Land 2 3 Hash Table

Remove() 0 1Michael JacksonNever-Never Land 2 3 Hash(name) Hash Table Name: Michael Jackson

Remove() Hash(name) Hash Table Name: Michael Jackson

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

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

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

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

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

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

Milestones 11. Implement the Lookup routine Compile and test the routine 12. Implement the Remove routine. Compile and test the routine

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