Data Abstraction & Problem Solving with C++

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

The ADT Hash Table What is a table?
Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
CS202 - Fundamental Structures of Computer Science II
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Hashing Techniques.
Dictionaries and Their Implementations
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Comp 335 File Structures Hashing.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Hashing as a Dictionary Implementation Chapter 19.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
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.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Appendix I Hashing.
Sets and Maps Chapter 9.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Data Structures Using C++ 2E
Hashing, Hash Function, Collision & Deletion
CSCI 210 Data Structures and Algorithms
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Hash functions Open addressing
Quadratic probing Double hashing Removal and open addressing Chaining
Advanced Associative Structures
Hash Table.
Hash Tables.
Chapter 10 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables and Associative Containers
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Advanced Implementation of Tables
Sets and Maps Chapter 9.
EE 312 Software Design and Implementation I
Ch Hash Tables Array or linked list Binary search trees
Ch. 13 Hash Tables  .
Collision Resolution.
EE 312 Software Design and Implementation I
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture-Hashing.
Presentation transcript:

Data Abstraction & Problem Solving with C++ Chapter 12, Part 2: Hashing Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

Hashing Hashing Hash function Hash table Enables access to table items in time that is relatively constant regardless of their locations Hash function Maps the search key of a table item into a location that will contain the item Hash table An array that contains the table items, as assigned by a hash function

Hashing A perfect hash function Collision Collision-resolution scheme Maps each search key into a unique location Possible if all the search keys are known Collision Occurs when the hash function maps two or more items—all having different search keys— into the same array location Collision-resolution scheme Assigns distinct locations in the hash table to items involved in a collision

Hashing Requirements for a hash function Is easy and fast to compute Places items evenly throughout the hash table Involves the entire search key Uses a prime base, if it uses modulo arithmetic

Hash Functions Simple hash functions Selecting digits Folding Does not distribute items evenly Folding Modulo arithmetic The table size should be prime Converting a character string to an integer Use the integer in the hash function instead of the string search key

Resolving Collisions Approach 1: Open addressing A category of collision resolution schemes that probe for an empty, or open, location in the hash table As the hash table fills, collisions increase The size of the hash table must be increased Need to hash the items again

Resolving Collisions Approach 1: Open addressing Linear probing Searches the hash table sequentially, starting from the original location specified by the hash function Quadratic probing Searches the hash table beginning at the original location specified by the hash function and continuing at increments of 12, 22, 32, and so on Double hashing Uses two hash functions One specifies the first location, the other gives the step size

Resolving Collisions Approach 2: Restructuring the hash table Allows the hash table to accommodate more than one item in the same location Buckets Each location in the hash table is itself an array Separate chaining Each hash table location is a linked list Successfully resolves collisions The size of the hash table is dynamic

Resolving Collisions Figure 12-49 Separate chaining

The Efficiency of Hashing An analysis of the average-case efficiency Load factor  Ratio of the current number of items in the table to the maximum size of the array table Measures how full a hash table is Should not exceed 2/3 Hashing efficiency for a particular search also depends on whether the search is successful Unsuccessful searches generally require more time than successful searches

The Efficiency of Hashing Figure 12-50 The relative efficiency of four collision-resolution methods

Table Traversal: An Inefficient Operation Under Hashing Hashing as an implementation of the ADT table For many applications, hashing provides the most efficient implementation Hashing is a poor choice for certain operations Traversal in sorted order Finding the item that has the smallest or largest search key Range query

Implementing a HashMap Class Using the STL A template class HashMap can be derived from existing STL containers An implementation using separate chaining A vector holds the dynamic hash buckets Each bucket is a map that holds elements having the same hash value The hash function is supplied as a template parameter template <typename Key, typename T, typename Hash> class HashMap : private vector<map<Key, T> >

Data With Multiple Organizations Many applications require a data organization that simultaneously supports several different data-management tasks Several independent data structures Do not support all operations efficiently Waste space Interdependent data structures Provide a better way to support a multiple organization of data

Data With Multiple Organizations Figure 12-51 Independent data structures: (a) a sorted linked list; (b) a pointer-based queue

Data With Multiple Organizations Figure 12-54 A queue pointing into a doubly linked binary search tree

Summary A hash function should be extremely easy to compute and should scatter the search keys evenly throughout the hash table A collision occurs when two different search keys hash into the same array location Probing and chaining are two ways to resolve a collision

Summary Hashing as a table implementation Does not efficiently support operations that require the table items to be ordered Is simpler and faster than balanced search tree implementations when Traversals are not important The maximum number of table items is known Ample storage is available

Summary Some applications require data organized in more than one way Several independent data structures can be inefficient and waste space Several data structures linked together can be a good choice