Chapter 12 Hash Table.

Slides:



Advertisements
Similar presentations
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Advertisements

Hashing as a Dictionary Implementation
Maps, Dictionaries, Hashtables
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.
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.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hash Table March COP 3502, UCF.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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.
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.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Sets and Maps Chapter 9.
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 12 Hash Table.
Hashing & HashMaps CS-2851 Dr. Mark L. Hornick.
Data Structures Using C++ 2E
Hash table CSC317 We have elements with key and satellite data
CSE373: Data Structures & Algorithms Lecture 6: Hash Tables
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Subject Name: File Structures
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hash functions Open addressing
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Hash Table.
Hash Table.
Computer Science 2 Hashing
Hash Tables.
Data Structures and Algorithms
Chapter 10 Hashing.
Introduction to Algorithms 6.046J/18.401J
Resolving collisions: Open addressing
Searching Tables Table: sequence of (key,information) pairs
CSCE 3110 Data Structures & Algorithm Analysis
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hashing Alexandra Stefan.
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
Introduction to Algorithms
Hash Tables Computer Science and Engineering
Advanced Implementation of Tables
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Sets and Maps Chapter 9.
EE 312 Software Design and Implementation I
Dictionaries 4/5/2019 1:49 AM Hash Tables  
slides created by Marty Stepp
Collision Handling Collisions occur when different elements are mapped to the same cell.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Hashing.
Data Structures and Algorithm Analysis Hashing
EE 312 Software Design and Implementation I
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture-Hashing.
Presentation transcript:

Chapter 12 Hash Table

Hash Table So far, the best worst-case time for searching is O(log n). average search time of O(1). worst case search time of O(n).

Learning Objectives Develop the motivation for hashing. Study hash functions. Understand collision resolution and compare and contrast various collision resolution schemes. Summarize the average running times for hashing under various collision resolution schemes. Explore the java.util.HashMap class.

12.1 Motivation Let's design a data structure using an array for which the indices could be the keys of entries. Suppose we wanted to store the keys 1, 3, 5, 8, 10, with a guaranteed one-step access to any of these.

12.1 Motivation The space consumption does not depend on the actual number of entries stored. It depends on the range of keys. What if we wanted to store strings? For each string, we would first have to compute a numeric key that is equivalent to it. java.lang.String.hashCode() computes the numeric equivalent (or hashcode) of a string by an arithmetic manipulation involving its individual characters.

12.1 Motivation Using numeric keys directly as indices is out of the question for most applications. There isn't enough space

12.1 Motivation

12.2 Hashing A simple hash function table size of 10 h(k) = k mod 10

12.2 Hashing ear collides with cat at position 4. There is empty space in the table, and it is up to the collision resolution scheme to find an appropriate position for this string. A better mapping function For any hash function one could devise, there are always hashcodes that could force the mapping function to be ineffective by generating lots of collisions.

12.2 Hashing

12.3 Collision Resolution There are two ways to resolve collisions. open addressing Find another location for the colliding key within the hash table. closed addressing store all keys that hash to the same location in a data structure that “hangs off” that location.

12.3.1 Linear Probing

12.3.1 Linear Probing As more and more entries are hashed into the table, they tend to form clusters that get bigger and bigger. The number of probes on collisions gradually increases, thus slowing down the hash time to a crawl.

12.3.1 Linear Probing Insert "cat", "ear", "sad", and "aid"

12.3.1 Linear Probing Clustering is the downfall of linear probing, so we need to look to another method of collision resolution that avoids clustering.

12.3.2 Quadratic Probing

12.3.2 Quadratic Probing Avoids Clustering When the probing stops with a failure to find an empty spot, as many as half the locations of the table may still be unoccupied. A hash to 2,3,6,0,7, and 5 are endlessly repeated, and an insertion is not done, even though half the table is empty.

12.3.2 Quadratic Probing For any given prime N, once a location is examined twice, all locations that are examined thereafter are also ones that have been already examined.

12.3.3 Chaining If a collision occurs at location i of the hash table, it simply adds the colliding entry to a linked list that is built at that location.

Example Store the following elements into a hash table with size = 10, using hash function f(k) = k % table size where k is the hash code.: Using linear probing method for resolving collisions. Using Chaining method for resolving collisions. element Hash code “Rami” 93 “Samer” 246 “Majd” 22 “Lina” 103 “Farah” 316

Running times We assume that the hashing process itself (hashcode and mapping) takes O(1). Running time of insertion is determined by the collision resolution scheme.

12.4 The java.util.HashMap Class Consider a university-wide database that stores student records. Every student is assigned a unique id (key), with which is associated several pieces of information such as name, address, credits, gpa, etc. These pieces of information constitute the value.

12.4 The java.util.HashMap Class A StudentInfo dictionary that stores (id, info) pairs for all the students enrolled in the university. The operations corresponding to this relationship can be found in hava.util.Map<K,V> Other Map methods: Clear() isEmpty() Size()

12.4 The java.util.HashMap Class The Map interface also provides operations to enumerate all the keys, enumerate all the values, get the size of the dictionary, check whether the dictionary is empty, and so on. The java.util.HashMap implements the dictionary abstraction as specified by the java.util.Map interface. It resolves collisions using chaining.

12.4.1 Table and Load Factor When the no-arg constructor is used Default initial capacity 16 The table size is defined as the actual number of key-value mappings in the has table.

12.4.1 Table and Load Factor We can choose an initial capacity Only uses capacities that are powers of 2. 101 becomes 128

12.4.1 Table and Load Factor An initial capacity of 128.

12.4.3 Adding an Entry Example Name serves as a key to the phone number value.

Code Example Input file: 201220123 ahmad sami albagdadi 201210345 salem saeed fahmawee 201410556 rana sami malkawi 201410554 leena fahmi alkhateeb

package javaapplication4; import java. io. File; import java. util package javaapplication4; import java.io.File; import java.util.*; public class JavaApplication4 { public static void main(String[] args) { String name=""; String id=""; String sid=""; HashMap<String,String> student = new HashMap<String,String>(); Scanner sin = new Scanner(System.in); try { Scanner s = new Scanner(new File("input.txt")); while(s.hasNext()) id = s.next(); name = s.next()+" "+s.next()+" "+s.next(); student.put(id, name); } catch(Exception e) System.out.println("file not found"); System.out.println("Enter student id"); sid = sin.next(); if(student.containsKey(sid)) System.out.println("Student name is: "+student.get(sid)); else System.out.println(" student id not found");