Chapter 5: Hashing Hash Tables

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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.
Theory I Algorithm Design and Analysis (5 Hashing) Prof. Th. Ottmann.
Hashing as a Dictionary Implementation
CST203-2 Database Management Systems Lecture 7. Disadvantages on index structure: We must access an index structure to locate data, or must use binary.
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.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Hashing Techniques.
Dictionaries and Their Implementations
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Maps, Dictionaries, Hashtables
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.
Dictionaries and Hash Tables1  
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.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed 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.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hash Table March COP 3502, UCF.
Spring 2015 Lecture 6: Hash Tables
CS261 Data Structures Hash Tables Concepts. Goals Hash Functions Dealing with Collisions.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
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.
Chapter 5: Hashing Collision Resolution: Separate Chaining Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Hash Tables1   © 2010 Goodrich, Tamassia.
Comp 335 File Structures Hashing.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
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.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
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.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
Chapter 5: Hashing Collision Resolution: Open Addressing Extendible Hashing Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova,
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
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.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
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.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
© 2013 Goodrich, Tamassia, Goldwasser
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.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Collision Resolution: Open Addressing Extendible Hashing
Chapter 5: Hashing Hash Tables
Presentation transcript:

Chapter 5: Hashing Hash Tables Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 5: Hashing Hash Tables Lydia Sinapova, Simpson College

Hashing What is Hashing? Direct Access Tables Hash Tables

Hashing - basic idea A mapping between the search keys and indices - efficient searching into an array. Each element is found with one operation only.

Hashing example Example: 1000 students, identification number between 0 and 999, use an array of 1000 elements. SSN of each student a 9-digit number. much more elements than the number of the students, - a great waste of space.

The Approach Directly referencing records in a table using arithmetic operations on keys to map them onto table addresses. Hash function: function that transforms the search key into a table address.

Direct-address tables The most elementary form of hashing. Assumption – direct one-to-one correspondence between the keys and numbers 0, 1, …, m-1., m – not very large. Array A[m]. Each position (slot) in the array contains a pointer to a record, or NIL.

Direct-Address Tables The most elementary form of hashing Assumption – direct one-to-one correspondence between the keys and numbers 0, 1, …, m-1., m – not very large. Array A[m]. Each position (slot) in the array contains either a reference to a record, or NULL. Cost – the size of the array we need is determined the largest key. Not very useful if there are only a few keys

Hash Functions Transform the keys into numbers within a predetermined interval to be used as indices in an array (table, hash table) to store the records

Hash Functions – Numerical Keys Keys – numbers If M is the size of the array, then h(key) = key % M. This will map all the keys into numbers within the interval [0 .. (M-1)].

Hash Functions – Character Keys Keys – strings of characters Treat the binary representation of a key as a number, and then apply the hash function

How keys are treated as numbers If each character is represented with m bits, then the string can be treated as base-m number.

Example A K E Y : 00001 01011 00101 11001 = 1 . 323 + 11 . 322 + 5 . 321 + 25 . 320 = 44271 Each letter is represented by its position in the alphabet. E.G, K is the 11-th letter, and its representation is 01011 ( 11 in decimal)

Long Keys If the keys are very long, an overflow may occur. A solution to this is to apply the Horner’s method in computing the hash function.

Horner’s Method 4x5 + 2x4 + 3x3 + x2 + 7x1 + 9x0 = anxn + an-1.xn-1 + an-2.xn-2 + … + a1x1 + a0x0 = x(x(…x(x (an.x +an-1) + an-2 ) + …. ) + a1) + a0 4x5 + 2x4 + 3x3 + x2 + 7x1 + 9x0 = x( x( x( x ( 4.x +2) + 3) + 1 ) + 7) + 9 The polynomial can be computed by alternating the multiplication and addition operations

Example V E R Y L O N G K E Y 10110 00101 10010 11001 01100 01111 01110 00111 01011 00101 11001 V E R Y L O N G K E Y 22 5 18 25 12 15 14 7 11 5 25 22 . 3210 + 5 . 329 + 18 . 328 + 25 . 327 + 12 . 326 + 15 . 325 + 14 . 324 + 7 . 323 + 11 . 322 + 5 . 321 + 25 . 320

Example (continued) V E R Y L O N G K E Y h0 = (22.32 + 5) % M 22 . 3210 + 5 . 329 + 18 . 328 + 25 . 327 + 12 . 326 + 15 . 325 + 14 . 324 + 7 . 323 + 11 . 322 + 5 . 321 + 25 . 320 (((((((((22.32 + 5)32 + 18)32 + 25)32 + 12)32 + 15)32 + 14)32 + 7)32 +11)32 +5)32 + 25 compute the hash function by applying the mod operation at each step, thus avoiding overflowing. h0 = (22.32 + 5) % M h1 = (32.h0 + 18) % M h2 = (32.h1 +25) % M . . . .

Code int hash32 (char[] name, int tbl_size) { key_length = name.length; int h = 0; for (int i=0; i < key_length ; i++) h = (32 * h + name[i]) % tbl_size; return h; }

Hash Tables sentinel value, or a special field in each slot. Index - integer, generated by a hash function between 0 and M-1 Initially - blank slots. sentinel value, or a special field in each slot.

Hash Tables Insert - hash function to generate an address Search for a key in the table - the same hash function is used.

Size of the Table Table size M - different from the number of records N Load factor: N/M M must be prime to ensure even distribution of keys