Data Structures Hash Table (aka Dictionary) i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, Andreas Veneris, Glenn Brookshear,

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

Peer to Peer and Distributed Hash Tables
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:
CHORD – peer to peer lookup protocol Shankar Karthik Vaithianathan & Aravind Sivaraman University of Central Florida.
Chord: A scalable peer-to- peer lookup service for Internet applications Ion Stoica, Robert Morris, David Karger, M. Frans Kaashock, Hari Balakrishnan.
© 2004 Goodrich, Tamassia Hash Tables1  
September 26, Algorithms and Data Structures Lecture VI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 12 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
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.
Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Topics in Reliable Distributed Systems Lecture 2, Fall Dr. Idit Keidar.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Review for Test 2 i206 Fall 2010 John Chuang. 2 Topics  Operating System and Memory Hierarchy  Algorithm analysis and Big-O Notation  Data structures.
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.
Topics in Reliable Distributed Systems Fall Dr. Idit Keidar.
Hashing General idea: Get a large array
File Sharing : Hash/Lookup Yossi Shasho (HW in last slide) Based on Chord: A Scalable Peer-to-peer Lookup Service for Internet ApplicationsChord: A Scalable.
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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
Hash Tables1   © 2010 Goodrich, Tamassia.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
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.
© 2004 Goodrich, Tamassia Hash Tables1  
CS201: Data Structures and Discrete Mathematics I Hash Table.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
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 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
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:
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Hashing CSE 2011 Winter July 2018.
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Advanced Associative Structures
Hash Table.
Maps.
CSE 373 Data Structures and Algorithms
"He's off the map!" - Eternal Sunshine of the Spotless Mind
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

Data Structures Hash Table (aka Dictionary) i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, Andreas Veneris, Glenn Brookshear, Nicolas Christin, or Ion Stoica.

John Chuang2 Outline  What is a data structure  Basic building blocks: arrays and linked lists  Data structures (uses, methods, performance): -List, stack, queue -Dictionary -Tree -Graph

John Chuang3 Dictionary  Also known as hash table, lookup table, associative array, or map  A searchable collection of key-value pairs -each key is associated with one value  Many possible applications, e.g., -Address book -Student record database -Routing tables -…

John Chuang4 Dictionary Methods  get(k): if the dictionary has an entry with key k, return its associated value; else return null  put(k,v): insert entry (k,v) into the dictionary; if key is not already in dictionary, return null; else return old value associated with k  remove(k): if dictionary has an entry with key k, remove it from dictionary and return its associated value; else return null  size()  isEmpty()

John Chuang5 Dictionary in Python  Example: >>> agents = {‘006’:’Jack Giddings’, ‘007’:’James Bond’} >>> agents[‘001’] = ’Edward Donne’ >>> agents[‘007’] ‘James Bond’

John Chuang6 Desirable Properties  Fast search, insert and delete (get, put, and remove)  Efficient space usage  Arrays: O(1) operations; not space efficient  Linked lists: O(n) operations; space efficient  Binary trees: O(log n) operations; space efficient  Can we do better?

John Chuang7 Hash Table  A generalization of an array that, if properly designed, can realize fast insert/delete/search operations with good space efficiency -average run-time of O(1) -worst case run-time of O(n)  A hash table is: -Good for storing and retrieving key-value pairs -Not good for iterating through a list of items

John Chuang8 Hash Table  A hash table consists of two major components: -Bucket array (for storing entries) -Hash function (for mapping keys to buckets) Obj5 key=1 obj1 key=15 Obj4 key=2 Obj2 key= table Obj3 key=4 buckets hash value/index

John Chuang9 Hash Table Design  Bucket array is an array A of size N, where each cell of A is considered a “bucket”  Hash function h maps each key k to an integer in the range [0, N -1]  Store entry (k,v) in the bucket A[h(k)]  Search for entry (k,v) in the bucket A[h(k)]  Choose hash function such that -Can take arbitrary objects (keys) as input -Hash computation is fast -Key mappings evenly distributed across [0, N -1]

John Chuang10 Example Hash Function  h(k) = k mod N  mod stands for modulo, the remainder of the division of two numbers. For example: -8 mod 5 = 3 -9 mod 5 = mod 5 = mod 5 = 0  Observe that collisions are possible: -two different keys hash to the same value

John Chuang11 Example  h(k) = k mod key value Insert (2,x) 2 x key value Insert (21,y) 2 x 21 y key value Insert (34,z) 2 x 21 y 34 z Insert (54,w) There is a collision at array entry #4 ???

John Chuang12 Dealing with Collisions  Hashing with Chaining: every hash table entry contains a pointer to a linked list of keys that hash in the same entry key value Insert (54,w) 2 x 21 y 54 w34 z CHAIN Insert (101,x) 21 y 2 x 101 x 54 w34 z

John Chuang13 Dictionary Performance  What is the run time for insert/search/delete? -Insert: It takes O(1) time to compute the hash function and insert at head of linked list -Search: It is proportional to the length of the linked list -Delete: Same as search key value Insert (54,w) 2 x 21 y 54 w34 z CHAIN Insert (101,x) 21 y 2 x 101 x 54 w34 z

John Chuang14 Load Factor  Average length of the linked lists is a function of the load factor -Load factor = number of items in hash table / array size  In Python, the implementation details are transparent to the programmer (load factor kept under 2/3)  In Java, the programmer can set the initial table size (default=16) and/or the load factor (default = 0.75)

John Chuang15 Distributed Hash Table (DHT)  Similar to traditional hash table data structure, except data is stored in distributed peer nodes -Each node is analogous to a bucket in a hash table.  Applications: distributed search, e.g., peer-to-peer networks, content distribution networks (CDNs), etc.  DHTs are typically designed to scale to large numbers of nodes and to handle continual node arrivals and departures/failures.  Put(), Get() interface like a regular hash table: -put(id, item); -item = get(id);

John Chuang16 DHT Example: Chord  Nodes: n1(id=1), n2(id=2), n3(id=0), n4(id=6)  Items inserted: f1(id=7), f2(id=1) i id+2 i succ Finger Table i id+2 i succ Finger Table i id+2 i succ Finger Table 7 Items 1 i id+2 i succ Finger Table Slide adapted from Ion Stoica, Nicolas Christin

John Chuang17 DHT Example: Chord  Upon receiving a query for item id, a node:  Check if the item is stored locally  If not, forwards the query to the largest node in its successor table that does not exceed id i id+2 i succ Finger Table i id+2 i succ Finger Table i id+2 i succ Finger Table 7 Items 1 i id+2 i succ Finger Table query(7) Slide adapted from Ion Stoica, Nicolas Christin