Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Hash Tables,

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Lecture 22, Revision 1 Lecture notes Java code – CodeFromLectures folder* Example class sheets – 4 of these plus solutions Extra examples (quicksort) Lab.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Data Structures: A Pseudocode Approach with C
Chapter 24 Lists, Stacks, and Queues
© 2004 Goodrich, Tamassia Hash Tables
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.
Searching: Binary Trees and Hash Tables
CSCI 2720 Hashing   Spring 2005.
The ADT Hash Table What is a table?
Briana B. Morrison Adapted from William Collins
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Hash Tables.
Chapter 10: Data Structures II
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing as a Dictionary Implementation
© 2004 Goodrich, Tamassia Hash Tables1  
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing Techniques.
Dictionaries and Their Implementations
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
© 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.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
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.
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.
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.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Hash Table March COP 3502, UCF.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Hashing Dr. Yingwu Zhu.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hash Tables1   © 2010 Goodrich, Tamassia.
Comp 335 File Structures Hashing.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
CSC 427: Data Structures and Algorithm Analysis
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.
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.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Hashing.
Slides by Steve Armstrong LeTourneau University Longview, TX
Week 8 - Wednesday CS221.
Chapter 21 Hashing: Implementing Dictionaries and Sets
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 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
Sets and Maps Chapter 9.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables, Maps and Sets Chapter 15 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary tree search O(log 2 n) –Unbalanced binary tree can degrade to O(n)

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables In some situations faster search is needed –Solution is to use a hash function –Value of key field given to hash function –Location in a hash table is calculated

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Functions Simple function could be to mod the value of the key by some arbitrary integer int h(int i) {return i % someInt; } Note the max number of locations in the table will be same as someInt Note that we have traded speed for wasted space –Table must be considerably larger than number of items anticipated

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Functions Observe the problem with same value returned by h(i) for different values of i –Called collisions A simple solution is linear probing –Linear search begins at collision location –Continues until empty slot found for insertion

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Functions When retrieving a value linear probe until found –If empty slot encountered then value is not in table If deletions permitted –Slot can be marked so it will not be empty and cause an invalid linear probe

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Functions Strategies for improved performance –Increase table capacity (less collisions) –Use different collision resolution technique –Devise different hash function Hash table capacity –Size of table must be 1.5 to 2 times the size of the number of items to be stored –Otherwise probability of collisions is too high

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Collision Strategies Linear probing can result in primary clustering Consider quadratic probing –Probe sequence from location i is i + 1, i – 1, i + 4, i – 4, i + 9, i – 9, … –Secondary clusters can still form Double hashing –Use a second hash function to determine probe sequence

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Collision Strategies Chaining –Table is a list or vector of head nodes to linked lists –When item hashes to location, it is added to that linked list

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Improving the Hash Function Ideal hash function –Simple to evaluate –Scatters items uniformly throughout table Modulo arithmetic not so good for strings –Possible to manipulate numeric (ASCII) value of first and last characters of a name

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Implementations Hash Table Resizable Array Balanced Tree Linked List Hash Table + Linked List Interfaces Set HashSet TreeSet LinkedHashSet List ArrayList LinkedList Map HashMap TreeMap LinkedHashMap

HashMap -- an associative data structureHashMap HashSet -- a container for holding valuesHashSet TreeMap – just like HashMap only items are ordered by key (internal red-black tree)TreeMap Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Hash Tables 13 the Map Interface Map ADT methods: –get(k): if the map M has an entry with key k, return its associated value; else, return null –put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k –remove(k): if the map M has an entry with key k, remove it from M and return its associated value; else, return null –size(), isEmpty() –keys(): return an iterator of the keys in M –values(): return an iterator of the values in M

Hash Tables 14 the Set Interface Set ADT methods: –add(o): Adds the specified element to this set if it is not already present. Returns true if the set does not already contain element (new item added) –contains(o): returnts true if the set contains the specified element –remove(o): Removes the specified element from this set if it is present. Returns true if the set contained the specified element –size(), isEmpty()

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Using Chaining to resolve collisions A linked list in every cell of array No rehashing required Unlimited collisions can be handled Lousy hash codes will still reduce performance Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved