Podcast Ch21a Title: Hash Functions

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Advertisements

An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Theory I Algorithm Design and Analysis (5 Hashing) Prof. Th. Ottmann.
Hashing as a Dictionary Implementation
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
Hashing Part One Reaching for the Perfect Search Most of this material stolen from "File Structures" by Folk, Zoellick and Riccardi.
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.
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.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
SETS AND HASHING. SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S.
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.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
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.
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
CS203 Lecture 14. Hashing An object may contain an arbitrary amount of data, and searching a data structure that contains many large objects is expensive.
Appendix I Hashing.
Sets and Maps Chapter 9.
COMP 53 – Week Eleven Hashtables.
LEARNING OBJECTIVES O(1), O(N) and O(LogN) access times. Hashing:
Hashing CSE 2011 Winter July 2018.
Data Abstraction & Problem Solving with C++
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing - Hash Maps and Hash Functions
Review Graph Directed Graph Undirected Graph Sub-Graph
Introduction to Hashing - Hash Functions
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
Searching.
Hash functions Open addressing
Hash Functions Sections 5.1 and 5.2
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
Hash Table.
Chapter 10 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries Collection of pairs. Operations. (key, element)
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
CSE 373 Data Structures and Algorithms
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.
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
What we learn with pleasure we never forget. Alfred Mercier
Podcast Ch21b Title: Collision Resolution
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 5: Hashing Hash Tables
Dictionaries and Hash Tables
Presentation transcript:

Podcast Ch21a Title: Hash Functions Description: Introduction to hashing; designing hash functions Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Introduction to Hashing A hash table distributes elements in a series of linked lists, referred to as buckets. A hash function maps a value to an index in the table. The function provides access to an element much like an index provides access to an array element. Like a binary search tree, a hash table provides an implementation of the Set and Map interfaces.

Introduction to Hashing (continued) A binary search tree can access data stored by value with O(log2n) average search time. We would like to design a storage structure that yields O(1) average retrieval time. In this way, access to an item is independent of the number of other items in the collection.

Introduction to Hashing (continued) A hash table is an array of references. Associated with the table is a hash function that takes a key as an argument and returns an integer value. By using the remainder after dividing the hash value by the table size, we have a mapping of the key to an index in the table.

Introduction to Hashing (concluded) Hash Value: hf(key) = hashValue HashTable index: hashValue % n

Hashing is an ______ algorithm. (a) O(log2n) (b) O(n2) (c) O(n) (d) O(1)

Using a Hash Function Consider the hash function hf(x) = x, where x is a nonnegative integer (the identity function). Assume the table is the array tableEntry with n = 7 elements.

Using a Hash Function (concluded) With hash function hf() and table size n, the table index for a key is i = hf(key)%n. Collisions occur for any two keys that differ by a multiple of n.

Designing Hash Functions Some general design principles guide the creation of all hash functions. Evaluating a hash function should be efficient. A hash function should produce uniformly distributed hash values. This spreads the hash table indices around the table, which helps minimize collisions. The Java programming language provides a general hashing function with the hashCode() method in the Object superclass.

Given a set of keys k0, k1, …, kn-1, a __________ is a hash function that produces no collisions. (a) closed probe hash (b) sparse distribution (c) non-link hash (d) perfect hashing function

Designing Hash Functions (continued) Object's hashCode() converts the internal address of the object into an integer value, which has limited application since two different objects will normally have different values for hashCode(), even if they store the same data. // strings one and two are the same; not so for // integer values one.hashCode() and two.hashCode() String one = "java", two = "java";

Consider the following hashing function Consider the following hashing function. Assume that s is a string variable in the class that defines hashCode(). public int hashCode() { int i; int hashval = 0; for(i=0; i < s.length(); i++) hashval += s[i]; return hashval; } Give two words of length four that hash to the same value. How could you improve this hash function?

Designing Hash Functions (continued) In the majority of hash-table applications, the key is a string. To create an efficient hash function, we must combine the sequence of characters in the string to form an integer. public int hashCode() { int hash = 0; for (int i = 0; i < n; i++) hash = 31*hash + s[i]; return hash; }

Designing Hash Functions (concluded) The following are hash code values for three different strings. The value for string strB is a negative number due to integer overflow. String strA = "and", strC = "algorithm"; strB = "uncharacteristically", hashValue=strA.hashCode();// hashValue = 96727 hashValue=strB.hashCode();// hashValue = -2112884372 hashValue=strC.hashCode();// hashValue = 225490031 In general, a hash function may result in integer overflow and return a negative number. The following calculation insures that the table index is nonnegative. tableIndex = (hashValue & Integer.MAX_VALUE) % tableSize

User-Defined Hash Functions To create a custom hash function, a class overrides the method hashCode(). For the Time24 class, the hash value for an object is its time converted to minutes. Since hour and minute are normalized to fall within the ranges 0 to 23 and 0 to 59 respectively, each time is unique. public int hashCode() { // hash value is time in minutes; // as normalized time, value is positive return hour*60 + minute; }

User-Defined Hash Functions (continued) The custom hash function for Product objects must mix the bits for the serial number to create a random value. public class Product { // last 4 digits record year in which the product // was made. // identity hash function is not sufficient private int serialNum; ... }

User-Defined Hash Functions public class Product { private int serialNum; ... public int hashCode() { // assign serialNum to a long variable long hashValue = serialNum; // square to obtain a nonnegative long integer hashValue *= hashValue; // return the remainder after dividing // by the largest int value; its bits // are "jumbled up" return (int)(hashValue % Integer.MAX_VALUE); }