1 11. Hash Tables Heejin Park College of Information and Communications Hanyang University.

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Slide 1 Insert your own content. Slide 2 Insert your own content.
0 - 0.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Data Structures: A Pseudocode Approach with C
1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)
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.
Hash Table.
1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Analysis of Algorithms CS 477/677
Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hash Tables Introduction to Algorithms Hash Tables CSE 680 Prof. Roger Crawfis.
Hash Tables CIS 606 Spring 2010.
Hashing.
Hash Tables:. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Algorithms
Addition 1’s to 20.
Incremental Rehearsal: This is how the flashcards are to be presented to the student. U1, K1, U1, K1, K2, U1, K1, K2, K3, U1, K1, K2, K3,K4, U1, K1, K2,
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.
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
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.
Hashing for dummies 1 For quick search, insert and delete of data from a table. 1 figures and concepts from MITOPENCOURSEWARE
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Lecture 10: Search Structures and Hashing
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Spring 2015 Lecture 6: Hash Tables
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
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.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
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,
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
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.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
Data Structures Using C++
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hashing TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA Course: Data Structures Lecturer: Haim Kaplan and Uri Zwick.
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
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:
Data Structures Using C++ 2E
Hashing Jeff Chastine.
Hash table CSC317 We have elements with key and satellite data
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Hash Tables – 2 Comp 122, Spring 2004.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Hash Tables – 2 1.
Presentation transcript:

1 11. Hash Tables Heejin Park College of Information and Communications Hanyang University

2 Contents Direct-address tables Hash tables Hash functions

3 Direct address tables U (universe of keys) / / / / / / Key Satellite data Slot Generate a table T with |U| slots. Store k into slot k. Assume that no two elements have the same key. T K (actual keys)

4 Direct-address tables Running time : O(1) DIRECT-ADDRESS-SEARCH(T, k) return T[k] DIRECT-ADDRESS-INSERT(T, x) T[key[x]] x DIRECT-ADDRESS-DELETE(T, x) T[key[x]] NIL

5 Hash tables Space consumption of direct addressing Θ(|U|) If the universe |K|/|U| is small, most of the space allocated for T would be wasted. Is it possible to reduce the space requirement to Θ(|K|) while the running time is still O(1) ?

6 Hash tables Hashing The element with key k is stored in slot h(k). instead of slot k. h is called a hash function. A hash function computes the slot from the key k. h : U {0, 1,..., m - 1}. We say that an element with key k hashes to slot h(k). We also say that h(k) is the hash value of key k.

7 Hash tables U (universe of keys) k1k1 k2k2 k3k3 k4k4 0 h(k 1 ) h(k 3 ) h(k 2 )=h(k 5 ) h(k 4 ) m-1 k5k5 K (actual keys)

8 Hash tables Collision : two keys may hash to the same slot. Avoiding collisions To make h appear to be random. avoiding collisions or at least minimizing collisions. Avoiding collisions is impossible because |U| > m.

9 Hash tables Collision resolution by chaining Put the elements hashed to the same slot in a linked list. U (universe of keys) / / / / / / k5k5 k7k7 k4k4 k8k8 k3k3 k2k2 k1k1 k6k6 k5k5 K (actual keys) k1k1 k4k4 k8k8 k3k3 k6k6 k7k7 k2k2

10 Hash tables CHAINED-HASH-INSERT(T, x) insert x at the head of list T[h(key[x])] CHAINED-HASH-DELETE(T, x) delete x from the list T[h(key[x])] CHAINED-HASH-SEARCH(T, k) search for an element with key k in list T[h(k)]

11 Hash tables The worst-case running time for insertion is O(1). Assuming that the element x being inserted is not already present in the table. Deletion of an element x can be done in O(1) time. Assuming that the lists are doubly linked. For searching, the worst-case running time is proportional to the length of the list.

12 Hash tables Searching time for hashing with chaining Given a hash table T with m slots that stores n elements, we define the load factor α for T. The average number of elements stored in a chain. α = n/m.

13 Hash tables Worst-case All n keys hash to the same slot, creating a list of length n. Searching time : Θ(n) plus the time to compute the hash function. No better than a linked list for all the elements.

14 Hash tables Average case Depends on how well the hash function h distributes the set of keys to be stored among the m slots. Simple uniform hashing An element is equally likely to hash into each slot. An element hashes independently of where any other element has hashed to.

15 Hash tables Let n j denote the length of the list T[j] for j = 0, 1,..., m – 1. n = n 0 + n 1 + … + n m-1 average value of n j E[n j ] = α = n/m. Assume that the hash value h(k) can be computed in O(1) time, the time required to search for an element with key k depends linearly on the length n h(k) of the list T[h(k)].

16 Hash tables Consider two cases of search. When search is unsuccessful. No element in the table has key k. When search is successful. An element with key k is found.

17 Hash tables An unsuccessful search takes Θ(1 + α) expected time. A successful search takes Θ(1 + α) expected time.