COMP 53 – Week Eleven Hashtables.

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Hashing Techniques.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Hashing is another method for sorting and searching data.
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.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Introduction toData structures and Algorithms
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Hashing (part 2) CSE 2011 Winter March 2018.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
COMP 53 – Week Fourteen Trees.
Course Developer/Writer: A. J. Ikuomola
Copy Constructor / Destructors Stacks and Queues
Hashing Jeff Chastine.
Chapter 15 Lists Objectives
CSCI 210 Data Structures and Algorithms
School of Computer Science and Engineering
Cse 373 April 24th – Hashing.
Review Graph Directed Graph Undirected Graph Sub-Graph
Hashing Exercises.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Searching.
Hash functions Open addressing
Data Structures and Database Applications Hashing and Hashtables in C#
Hash table another data structure for implementing a map or a set
Hashing CS2110 Spring 2018.
Advanced Associative Structures
Hash Table.
Hash Table.
Chapter 15 Lists Objectives
Data Structures and Database Applications Hashing and Hashtables in C#
Hashing CS2110.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Dictionaries and Their Implementations
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.
CSE 373: Data Structures and Algorithms
Hash Tables and Associative Containers
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
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
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Sets and Maps Chapter 9.
Pseudorandom number, Universal Hashing, Chaining and Linear-Probing
EE 312 Software Design and Implementation I
Podcast Ch21a Title: Hash Functions
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
What we learn with pleasure we never forget. Alfred Mercier
Data Structures and Algorithm Analysis Hashing
EE 312 Software Design and Implementation I
Skip List: Implementation
Lecture-Hashing.
Presentation transcript:

COMP 53 – Week Eleven Hashtables

What We’ve Covered So Far Data Structures Linear Direct Access Array Hashtable Sequential Access List Stack Queue Nonlinear Hierarchical Tree Heap

Topics Hash Table Concepts CRUD Operations Hash Algorithms

Why Do We Care More efficiency to access data elements Attempts to maximize space-time continuum Related to security and data encryption

Hash Tables A hash table or hash map is a data structure that efficiently stores and retrieves data from memory Typically implemented as a hash table that uses an array in combination with singly linked lists Uses a hash function Maps an object to a key For example, a string to an integer

Hash Table Concept Storage Model Create (add item to table), Update Make an array of fixed size, say 10 Each array element is a linked list Create (add item to table), Update Map (i.e. hash) it to one of the 10 array elements Add or Update the linked list at that location Read (Retrieve) Determine its hash code then search the linked list at the corresponding array slot for the item

Simple Hash Function for Strings int computeHash(string s) { int hash = 0; for (int i = 0; i < s.length( ); i++) hash = hash + s[i]; } return hash % SIZE; // SIZE = 10 in example Example: "dog" = ASCII 100, 111, 103 Hash = (100 + 111 + 103) % 10 = 4 Sum the ASCII value of every character in the string and then compute the modulus of the sum using the size of the fixed array Other models include basic division or midsquare (square of the key and select middle bits)

Hash Table Practice 1 of 3 Open up your existing linked list practice project Create a new class for the hash table Private Properties Node<T>* table[SIZE]; int hash(string s); Methods Private : hash(string s)

But sum of “Steve” = sum of “Notes” Hash Table Collisions These values map to different array locations But sum of “Steve” = sum of “Notes”

Adding Hash Table Elements Insert node with collision after the head Insert new element as head of list

Constructing a Hash Table

Hash Table Practice 2 of 3 Add methods to insert and print out the hash table Methods Public : insert(string key, T item) Public : print()

Hash Table Efficiency Worst Case – O(n) Best Case – O(1) Every item inserted into the table has the same hash key Find operation may have to search through all items every time Best Case – O(1) Every item inserted into the table has a different hash key Find operation will only have to search a list of size 1 Can decrease the chance of collisions with a better hash function Tradeoff: Lower chance of collision with bigger hash table, but more wasted memory space

Collisions Chaining Option [0] [1] [2] [3] [4] [5] [6] [7] Bob And Car

Collision Linear Probe Option [0] [1] [2] [3] [4] [5] [6] [7] Bob And Car

Hash Table Practice 3 of 3 Add methods to search and remove items Public : search(string key, T item) Public : remove(string key, T item)

Other Hash Table Algorithms Random number generation to identify the key Double hash – Similar to chaining, but second algorithm determine the “jump” distance. 2-Choice Hash – Two algorithms. If hash One collides, use hash Two. If both collide, choose the hash with lowest number of items. + many others

Key Takeaways Unique blend of data structures – arrays and linked lists Balanced combination of search and insertion operations