CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Advertisements

Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
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.
CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Log Files. O(n) Data Structure Exercises 16.1.
Hashing CS 3358 Data Structures.
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
REPRESENTING SETS CSC 172 SPRING 2002 LECTURE 21.
Hashing (Ch. 14) Goal: to implement a symbol table or dictionary (insert, delete, search)  What if you don’t need ordered keys--pred, succ, sort, select?
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Hash Table March COP 3502, UCF.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Spring 2015 Lecture 6: Hash Tables
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Intro to Computer Algorithms Lecture 16 Phillip G. Bradford Computer Science University of Alabama.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
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.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
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,
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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.
Ihab Mohammed and Safaa Alwajidi. Introduction Hash tables are dictionary structure that store objects with keys and provide very fast access. Hash table.
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
David Evans CS150: Computer Science University of Virginia Computer Science Class 38: Googling.
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.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
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.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
1 the hash table. hash table A hash table consists of two major components …
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
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.
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.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Hashing (part 2) CSE 2011 Winter March 2018.
CSCI 210 Data Structures and Algorithms
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
CS216: Program and Data Representation
Searching Tables Table: sequence of (key,information) pairs
Hashing Alexandra Stefan.
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review / Fast Dictionaries

2 UVa CS216 Spring Lecture 23: Fast Dictionaries Announcements PS7 Comments will be posted later today Exam 2 will be posted Thursday after 5pm Office hours: Today: 2-3pm; Tomorrow: am After Thursday, I will start charging storage fees on uncollected graded assignments: –Exam 1: 1 point per page per day –Problem Sets: 1 star color per week

3 UVa CS216 Spring Lecture 23: Fast Dictionaries Exam 2 Review Questions JVML: Do istore_1 and astore_1 share the same memory location? Memory management: –Explain memory leaks –Garbage collection Calling convention Complexity classes: What is NP- Complete?

4 UVa CS216 Spring Lecture 23: Fast Dictionaries CS216 Roadmap Data RepresentationProgram Representation Bits Addresses, Numbers, Characters 0x42381a, 3.14, ‘x’ Objects “Hello” Arrays [‘H’,’i’,\0] Python code High-level language C code Low-level language Virtual Machine language JVML Assemblyx86 Real World Problems Real World Physics Rest of CS216 Note: depending on your answers to the topic interest exam question, we might also look at another VM (CLR) or another assembly language (RISC)

5 UVa CS216 Spring Lecture 23: Fast Dictionaries Fast Dictionaries Problem set 2, question 5... Class 6: fastest possible search using binary comparator is O(log n) “You may assume Python’s dictionary type provides lookup and insert operations that have running times in O(1).” Can Python really have an O(1) lookup?

6 UVa CS216 Spring Lecture 23: Fast Dictionaries Fast Dictionaries If the keys can be anything? No – best one comparison can do is eliminate ½ the elements Data Representation Bits x42381a, 3.14, ‘x’ Objects “Hello” Arrays [‘H’,’i’,\0] The keys must be bits, so we can do better!

7 UVa CS216 Spring Lecture 23: Fast Dictionaries Lookup Table KeyValue “red” “orange” “blue” null “green” “white”... Works great...unless the key space is sparse.

8 UVa CS216 Spring Lecture 23: Fast Dictionaries Sparse Lookup Table Keys: names (words of up to bit ASCII characters) How big a table do we need? 40 * 7 = = ~1.9*10 84 entries We need lookup tables where many keys can map to the same entry

9 UVa CS216 Spring Lecture 23: Fast Dictionaries Hash Table Hash Function: h: Key  [0, m-1] Location KeyValue 0“Alice”“red” 1“Bob”“orange” 2“Coleen”“blue” 3null 4“Eve”“green” 5“Fred”“white”... m-1“Zeus”“purple” Here: h = firstLetter(Key)

10 UVa CS216 Spring Lecture 23: Fast Dictionaries Collisions What if we need both “Colleen” and “Cathy” keys?

11 UVa CS216 Spring Lecture 23: Fast Dictionaries “Alice”, “red” Separate Chaining Each element in hash table is not a pair, but a list of pairs LocationEntry “Coleen”, “blue” “Cathy”, “green”

12 UVa CS216 Spring Lecture 23: Fast Dictionaries Hash Table Analysis Lookup Running Time? Worst Case:  (N) N entries, all in same bucket Hopeful Case: O(1) Most buckets with < c entries

13 UVa CS216 Spring Lecture 23: Fast Dictionaries Requirements for “Hopeful” Case Function h is well distributed for key space Size of table ( m ) scales linearly with N –Expected bucket size is ( N / m ) for a randomly selected k  K, probability (h(k) = i) = 1/m Finding a good h can be tough (more next class)

14 UVa CS216 Spring Lecture 23: Fast Dictionaries Saving Memory “Alice”, “red” LocationEntry “Coleen”, “blue” “Cathy”, “green” Can we avoid the overhead of all those linked lists?

15 UVa CS216 Spring Lecture 23: Fast Dictionaries Linear Open Addressing Location KeyValue 0“Alice”“red” 1“Bob”“orange” 2“Coleen”“blue” 3“Cathy”“yellow” 4“Eve”“green” 5“Fred”“white” 6“Dave”“red”...

16 UVa CS216 Spring Lecture 23: Fast Dictionaries Sequential Open Addressing def lookup (T, k): i = hash (k) while (not looped all the way around): if T[i] == null: return null else if T[i].key == k: return T[i].value else i = i + 1 mod T.length

17 UVa CS216 Spring Lecture 23: Fast Dictionaries Problems with Sequential “Primary Clustering” –Once there is a full chunk of the table, anything hash in that chunk makes it grow –Note that this happens even if h is well distributed Improved strategy? Don’t look for slots sequentially i = i + s mod T.length Doesn’t help – just makes clusters appear scattered

18 UVa CS216 Spring Lecture 23: Fast Dictionaries Double Hashing Use a second hash function to look for slots i = i + hash2 (K) mod T.length Desirable properties of hash2: –Should eventually try all slots –Should be independent from hash result of hash2(K) should be relatively prime to m (Easiest to make m prime)

19 UVa CS216 Spring Lecture 23: Fast Dictionaries Charge (Announcements) PS7 Comments will be posted later today Exam 2 will be posted Thursday after 5pm Office hours: Today: 2-3pm; Tomorrow: 10-11am After Thursday, I will start charging storage fees on uncollected graded assignments: –Exam 1: 1 point per page per day –Problem Sets: 1 star color per week