CIS 190: C/C++ Programming. Lecture 12 Student Choice

Similar documents
CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

Hash Tables. Gunnar Gotshalks. Maps 1

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2010

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

CS301 - Data Structures Glossary By

Open Addressing: Linear Probing (cont.)

COMP171. Hashing.

Linked Lists and Abstract Data Structures A brief comparison

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Standard ADTs. Lecture 19 CS2110 Summer 2009

COEN244: Class & function templates

CSCE 2014 Final Exam Spring Version A

Hash Table and Hashing

III Data Structures. Dynamic sets

TEMPLATES AND ITERATORS

Algorithms and Data Structures

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CSE 100: C++ TEMPLATES AND ITERATORS

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Maps, Hash Tables and Dictionaries. Chapter 10.1, 10.2, 10.3, 10.5

ECE250: Algorithms and Data Structures Midterm Review

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Priority Queue Sorting

Measuring Input size. Last lecture recap.

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018

Priority Queue. 03/09/04 Lecture 17 1

CS11 Advanced C++ Fall Lecture 1

HASH TABLES. Goal is to store elements k,v at index i = h k

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Hash Tables. Hash Tables

DATA STRUCTURES AND ALGORITHMS

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Understand how to deal with collisions

CSCI-1200 Data Structures Fall 2015 Lecture 24 Hash Tables

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

Depth-wise Hashing with Deep Hashing Structures. A two dimensional representation of a Deep Table

Lecture 17: Hash Tables, Maps, Finish Linked Lists

EECE.2160: ECE Application Programming

Chapter 9: Maps, Dictionaries, Hashing

C++ Standard Template Library

About this exam review

Lecture 12 Notes Hash Tables

Lecture 18. Collision Resolution

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1

Cpt S 223. School of EECS, WSU

Hashing. Hashing Procedures

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech

Hash[ string key ] ==> integer value

Data Structure. Measuring Input size. Composite Data Structures. Linear data structures. Data Structure is: Abstract Data Type 1/9/2014

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Hashing Techniques. Material based on slides by George Bebis

1. Attempt any three of the following: 15

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture Notes on Hash Tables

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

CS 350 : Data Structures Hash Tables

CSCD 326 Data Structures I Hashing

EECS 560 Lab 8: Leftist Heap as Priority Queue

CSE 100: C++ TEMPLATES AND ITERATORS

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSE 100: C++ TEMPLATES AND ITERATORS

Hash table basics mod 83 ate. ate. hashcode()

Dictionaries-Hashing. Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2)

Advanced C++ STL. Tony Wong

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

Dynamic Data Structures

Hashing. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Lecture 12 Hash Tables

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing

CSED233: Data Structures (2017F) Lecture10:Hash Tables, Maps, and Skip Lists

CS 202, Fall 2017 Homework #4 Balanced Search Trees and Hashing Due Date: December 18, 2017

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

6.7 b. Show that a heap of eight elements can be constructed in eight comparisons between heap elements. Tournament of pairwise comparisons

STANDARD ADTS Lecture 17 CS2110 Spring 2013

Topic HashTable and Table ADT

CISC 3130 Data Structures Spring 2018

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Course Review for Finals. Cpt S 223 Fall 2008

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University

Summer Final Exam Review Session August 5, 2009

CPSC 331 Term Test #2 March 26, 2007

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

Module 3: Hashing Lecture 9: Static and Dynamic Hashing. The Lecture Contains: Static hashing. Hashing. Dynamic hashing. Extendible hashing.

MIDTERM EXAM THURSDAY MARCH

Templates and the STL. Bryce Boe 2012/09/10 CS32, Summer 2012 B

Transcription:

CIS 190: C/C++ Programming Lecture 12 Student Choice

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Hash Maps (AKA Hash Tables) data structure that maps keys to values a hash function takes a given key and outputs an index into an array, where the value will be stored providing the same key will produce the same index, where the value is stored

Hash Map Example key: name value: phone number 0 867-5309 1 555-2368 2 3 the Hash() function gives an integer; then use modulus to get a valid index for the table Hash( Jenny ) = 68; 68 % 4 = 0; Hash( Egon ) = 41; 41 % 4 = 1;

Choosing a Hash Function a good hash function is easy to compute has a uniform distribution of keys hash function uniformity is dependent on the size of the hash map powers of two prime numbers

Why Use Hash Maps? speed in best-case scenario, the lookup time is O(1); the requested value is found immediately in worst-case scenario (collisions), lookup time can be O(n) this scenario is incredibly rare

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Collisions occur when two keys map to the same index many ways of handling, dependent on situation chaining open addressing etc.

Hash Map Example Setup key: class number value: class name examples: key: 190 value: C++ Prog. key: 191 value: Python have less than a dozen entries, so our hash map size will be 11 (prime number)

Hash Map Example Function the Hash() function we re going to use is very naïve and not very good the function is very simple: the digits of the key are multiplied together, excepting zeroes so Hash(123) = 1 * 2 * 3 = 6

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Hash Map Example Using 0 1 2 3 4 5 6 7 8 9 10 let s add 190 - C++ Prog.

Hash Map Example Using 0 1 2 3 4 5 6 7 8 9 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9

Hash Map Example Using 0 1 2 3 4 5 6 7 8 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9

Hash Map Example Using 0 1 2 3 4 5 6 7 8 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9 and also 240 - Comp Arch. Hash(240) = 2 * 4 = 8

Hash Map Example Using 0 1 2 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9 and also 240 - Comp Arch. Hash(240) = 2 * 4 = 8

Hash Map Example Using 0 1 2 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9 and also 240 - Comp Arch. Hash(240) = 2 * 4 = 8 and then 262 - Automata Hash(262) = 2 * 6 * 2 = 24

Hash Map Example Using 0 1 2 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9 and also 240 - Comp Arch. Hash(240) = 2 * 4 = 8 and then 262 - Automata Hash(262) = 2 * 6 * 2 = 24 24 is too large, so we ll use mod: 24 % 11 = 2

Hash Map Example Using 0 1 2 Automata 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 let s add 190 - C++ Prog. Hash(190) = 1 * 9 = 9 and also 240 - Comp Arch. Hash(240) = 2 * 4 = 8 and then 262 - Automata Hash(262) = 2 * 6 * 2 = 24 24 is too large, so we ll use mod: 24 % 11 = 2

Hash Map Example Using 0 1 2 Automata 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 so far so good! let s add 191 - Python Hash(191) = 1 * 9 * 1 = 9

Hash Map Example Using 0 1 2 Automata 3 4 5 6 7 8 Comp Arch. 9 C++ Prog. 10 so far so good! let s add 191 - Python Hash(191) = 1 * 9 * 1 = 9 Python is colliding with C++ Prog.

Hash Map Example Decisions 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 require that we have been storing the key with the value this is a decision you need to make when you first set up your hash map

Hash Map Example Decisions 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 now we can use open addressing or chaining

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Collisions Open Addressing key is stored with the value in the given index when a collision occurs, the hash table is probed until an empty index has been found different probe sequences can be used using that same probe sequence, you can then find the given value when you use the same key

Hash Map Example Open Addressing 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 for open addressing, we need to choose a probe procedure

Hash Map Example Open Addressing 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 for open addressing, we need to choose a probe procedure for simplicity s sake, we ll use linear probing interval of probes is fixed we ll use an interval of 1

Hash Map Example Open Addressing 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python for open addressing, we need to choose a probe procedure for simplicity s sake, we ll use linear probing interval of probes is fixed we ll use an interval of 1

Hash Map Example Open Addressing 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python 120 Prog. I Hash(120) = 1 * 2 = 2

Hash Map Example Open Addressing 0 1 2 262 Automata 3 120 Prog. I 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python 120 Prog. I Hash(120) = 1 * 2 = 2

Hash Map Example Open Addressing 0 1 2 262 Automata 3 120 Prog. I 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python 120 Prog. I Hash(120) = 1 * 2 = 2 121 Prog. II Hash(121) = 1 * 2 * 1 = 2

Hash Map Example Open Addressing 0 1 2 262 Automata 3 120 Prog. I 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python 120 Prog. I Hash(120) = 1 * 2 = 2 121 Prog. II Hash(121) = 1 * 2 * 1 = 2

Hash Map Example Open Addressing 0 1 2 262 Automata 3 120 Prog. I 4 121 Prog. II 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 191 Python 120 Prog. I Hash(120) = 1 * 2 = 2 121 Prog. II Hash(121) = 1 * 2 * 1 = 2

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Collisions Chaining key is stored with the value in the index s list each index has a list of entries when a collision occurs, the new value is added to the list sorted at end at beginning

Hash Map Example Chaining 0 1 2 262 Automata 3 4 5 6 7 8 240 Comp Arch. 9 190 C++ Prog. 10 chaining is even more different than open addressing in addition to storing keys with the values, the indexes instead contain pointers to a list of key/value pairs

Hash Map Example Chaining 0 Ø 1 Ø 2 3 Ø 4 Ø 5 Ø 6 Ø 7 Ø 8 9 10 Ø 262 Automata Ø 240 Comp. Arch Ø 190 C++ Prog. Ø chaining is even more different than open addressing in addition to storing keys with the values, the indexes instead contain pointers to a list of key/value pairs

Hash Map Example Chaining 0 Ø 1 Ø 2 3 Ø 4 Ø 5 Ø 6 Ø 7 Ø 8 9 10 Ø 262 Automata Ø 240 Comp. Arch Ø so when we go to add 191 - Python at index 9 190 C++ Prog. 191 Python Ø

Hash Map Example Chaining 0 Ø 1 Ø 2 3 Ø 4 Ø 5 Ø 6 Ø 7 Ø 8 9 10 Ø 262 Automata 240 Comp. Arch Ø 120 Prog. I Ø and as we add 120 - Prog. I to index 2 190 C++ Prog. 191 Python Ø

Hash Map Example Chaining 0 Ø 1 Ø 2 3 Ø 4 Ø 5 Ø 6 Ø 7 Ø 8 9 10 Ø 262 Automata 120 Prog. I 121 Prog. II Ø and 121 - Prog. II 240 Comp. Arch Ø 190 C++ Prog. 191 Python Ø

Hash Map Variations array of elements of needed size alone with elements allowing pointers to next in that index array of pointers to (list of) elements better in terms of space needed, if entries are larger than the size of a pointer

Drawbacks of Hash Maps collisions pseudo-random order can t find next closest entry hash function may take a long time not very good for small numbers of things resizing can be a very slow operation necessary to rehash all stored entries means you need to store key with value as well

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

Hash Maps in C++ there is technically a hash_map library but it never made it into the STL probably shouldn t rely on it (or use it) there are two standard STL containers you can use to help implement a hash map: map unordered_map

Map Containers map and unordered_map take a variable type for both key and value map <string, int> mapinstance; maps are ordered by key unordered_maps are not take a Hash function as an argument unordered_maps require C++11

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

C++ STL Standard Template Library set of ready-made common classes Iterators Algorithms Functors Containers

C++ STL Iterators iterators are used to traverse containers five types: input, output, forward, bidirectional, random access iterators allow the STL to be flexible can write a function using iterators that will work for both lists and vectors not always good for associative containers have their own member functions for most things

C++ STL Algorithms utility functions searches sorts merges partitioning etc.

C++ STL Functors also called function objects classes that overload the function call operator operator() allows you to declare an object that can be work as and be treated as a function can be passed into many STL functions

Outlines Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

C++ STL Containers four different types of containers sequence containers arrays (require C++11) vectors deque FIFO & FILO capable forward_list single-linked list (C++11) list doubly-linked list

C++ STL Containers four different types of containers container adaptors provide an interface that relies on a container class stack FILO only queue FIFO only priority_queue modified queue first element is always the largest

C++ STL Containers four different types of containers associative containers map store keys and values together set only store keys (value is the key) multi- allow non-unique keys use a compare function to sort elements by key

C++ STL Containers four different types of containers unordered associative containers (require C++11) unordered_(multi)map/set unordered_map will work as a hash map! can take a key, a value, and a hash function hash maps are inherently unordered, which is why a regular map container won t work

Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources

C++ GUI Intro GUI Graphical User Interface before now, we ve been using a CLI, or a Command Line Interface many GUI libraries/toolkits (for C++ and other languages) are for a specific operating system avoid these, they tie you to a single OS

C++ GUI Qt one of the most popular software dev packages for creating applications with GUIs works across multiple platforms Qt uses standard C++ also has implementations for SQL databases, XML, interacting with multimedia, etc. IDE is called Qt Creator; cross-platform qt-project.org

Project Alphas due Monday at midnight! DO NOT TURN YOUR ALPHAS IN LATE will grade the most recent submission your alphas and final project do not have to run (or compile) on eniac however, you still must submit all your files, including a Makefile