CS 171: Introduction to Computer Science II Hashing and Priority Queues.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Hashing.
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
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.
Skip List & Hashing CSE, POSTECH.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing CS 3358 Data Structures.
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.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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.
Hashing General idea: Get a large array
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Hash Table March COP 3502, UCF.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
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.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Search  We’ve got all the students here at this university and we want to find information about one of the students.  How do we do it?  Linked List?
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.
CS201: Data Structures and Discrete Mathematics I Hash Table.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Data Structures and Algorithms Lecture (Searching) Instructor: Quratulain Date: 4 and 8 December, 2009 Faculty of Computer Science, IBA.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
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 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 Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Copyright © Curt Hill Hashing A quick lookup strategy.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
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,
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing CSE 2011 Winter July 2018.
Hashing Exercises.
Advanced Associative Structures
Hash Table.
Lecture-Hashing.
Presentation transcript:

CS 171: Introduction to Computer Science II Hashing and Priority Queues

 Prepare to be aMAZEd!  Find a path through a maze  Open path  Wall path, just use the walls  Hand-in on Gimle  Tuesday March 31 at 11:59pm.

 A bag is a collection where removing items is not supported (a.k.a. multi-set)  Useful if you don’t need removal  Provides a client the ability to collect items and then to iterate through the collected items.

 An abstract data type like a regular queue, but each element is associated with a priority value  The element with highest priority will be removed first  Queue: first in first out  Stack: last in first out  Priority queue: highest priority first out  Priority queue applications  Task scheduling  Search and optimization

Implementing Priority Queue Assumes an element with a smaller value has higher priority Implementation using ordered array  Insert – insert an element to the correct position  Remove – delete the front element Implementation using unordered array  Insert?  Remove?

Priority Queue vs. Ordered Array So priority queue looks quite the same with an orderedArray. What are the differences? – You can only remove elements at the front one by one. You can’t remove arbitrary elements. – Priority queue only needs the capability of returning the highest priority element. There are efficient implementations (such as heaps) which do not require all elements to be sorted at all time.

Using Priority Queues Example: PriorityQueue s = new PriorityQueue(10); s.insert(25); s.insert(35); System.out.println(s.remove()); s.insert(45); s.insert(15); System.out.println(s.remove());

 Set membership: Have I seen “x” before?  One of the most common problems in practice  Same interface as BST:  search(“x”) ▪ Either returns the data or an error  insert(“y”) ▪ Inserts to data structure if not there ▪ Up to you what to do with duplicates…  We don’t need to preserve order

 BST takes on average O(log(N)) time to search for.  But sometimes we have ample space, much more than N, and really want to answer searches in nearly O(1) time.  It’s okay if insertions take longer time  Also, we sometimes have long and crazy keys  addresses  Full names of Icelanders  URLs  UUID (universal unique identifiers)  How do we do that?

 Suppose we have an array arr of length M  For now, suppose all of our N keys are tiny  In fact, they’re distinct integers between [0,M-1]  What could we do to quickly look up if we have a given key?

 Aha! We can use the key as an index into arr  When somebody searches for key i  We look in arr[i]  If there is nothing there, we return Not Found error  Otherwise, we return the value in arr[i]  So that’s easy. But what if keys are long?

 Can we “convert” each key into short, distinct numbers?  Needs to be fast „Barbie“ Magic! „Turtles“

 Can we “convert” each key into short numbers?  Needs to be fast „Barbie“ „Turtles“

 A hash function converts any given key into a number between [0,M-1].  It should be consistent  Same key should always return the same number  Moreover, we want it to distribute keys evenly across the possible numbers!  That helps them be distinct

 Suppose all the keys were large integers  What would be a good hash function?  Caveat: Ideally M should be prime num % M num

 What if keys are general strings?  “Flugeldufel”, “Ausgeschnitzel”, …  We could loop through the string, and add them up to a large integer, then use the modulo operation

 But what if different keys produce the same hash value?

18  23 people in a room  How likely that two people share the same birthday? Roughly: Answer: 50.7%!

19  Birthday paradox:  Can’t avoid collisions unless you have ridiculous amount of memory  How many collisions do we expect to see?

 Need to deal with these hash collisions  Several ways to deal with them  Hashing with separate chaining:

 Put keys that collide into a list associated with an index

 Hashing with linear probing

 Average case lookup (without resize):  O(1 + M/N)  Worst case lookup:  O(N)  If the hash table is close to filling up, we could resize it and rehash every element  Usually one would double the its, or grow by x%

 Hashing  In practice, most often you’ll want to use hashing ▪ Best to allocate about 150% of the space you’ll need to the table  Fast search, O(1) average case (unless close to full)  Dead simple to use for standard data types  Binary search trees  Doesn’t require building efficient uniform hash functions for your data  Don’t need to worry about fullness of the table or resizing  Guaranteed worst-case performance (“red-black” BSTs)