Hash Tables. Lower bounds on sorting Hash functions. March 05, 2018 Cinda Heeren / Geoffrey Tien 1

Size: px
Start display at page:

Download "Hash Tables. Lower bounds on sorting Hash functions. March 05, 2018 Cinda Heeren / Geoffrey Tien 1"

Transcription

1 Hash Tables Lower bounds on sorting Hash functions Cinda Heeren / Geoffrey Tien 1

2 Detour back to sorting Lower bounds on worst case Selection sort, Insertion sort, Quicksort, Merge sort Worst case complexities Θ n 2, Θ n 2, Θ n 2, Θ n log n respectively All sort based on comparison of two values, a and b The algorithm performs some action based on the result a < b, a > b, a b, a b, a = b Each comparison is a decision point in the algorithm Do one thing if comparison is true, do another if false The algorithm can be expressed as a (binary) decision tree The big question: Is it possible for a comparison-based sorting algorithm to have better asymptotic worst-case performance than Merge sort? Cinda Heeren / Geoffrey Tien 2

3 Lower bounds on sorting Given some permutation of items to be sorted, in positions a, b, c, d, e The leaves represent the sorted output for some particular input permutation a < b < c < sorted Y a < c a b c d N Y a < b Cinda Heeren / Geoffrey Tien 3 N a < c b < c b < c b < c b < c Y N Y N Y N Y N sorted sorted sorted Y sorted Longest path: maximum decisions in worst case Each path from root to a leaf is the sequence of decisions made to sort some input permutation N < c < b < a sorted

4 Lower bounds on sorting How many leaves are there in this decision tree? How many starting permutations are there? The algorithm must be able to correctly sort every permutation n! input permutations, therefore n! different paths to a leaf If there were fewer than n! paths, it means the algorithm is unable to correctly sort some input permutations What is the minimum height of a tree with n! leaves? A perfect tree with n! leaves Decision tree model Cinda Heeren / Geoffrey Tien 4

5 Lower bounds on sorting A tree with h levels has 2 h+1 1 nodes Bottom level has 2 h nodes (i.e. leaves), 2 h = n! log 2 h = log n! h log n i 1 n / 2 i 1 n! logi log n / 2 n log n / 2 2 n log n Height of a perfect decision tree The longest decision path can be no shorter than this The worst case of any comparison-based sorting algorithm can be no better than Ω n log n But there are non-comparison-based sorting algorithms that can perform better (with assumptions). Stay tuned in CPSC 320 (maybe)! Cinda Heeren / Geoffrey Tien 5

6 Review: dictionary ADT Stores values associated with user-specified keys Values may be any (homogenous) type Keys may be any (homogenous) comparable type Dictionary operations Create Destroy Insert Find Remove Insert Feet Useful for something, presumably Find(Z125 Pro) Z125 Pro Fun in the sun! Stumpjumper The favourite baby of VanCity planners Z125 Pro Fun in the sun! GL1800 Quiet comfort Cinda Heeren / Geoffrey Tien 6

7 Implementing a dictionary Using some of the data structures we have seen so far Worst case complexities for Insert, Remove, Find Insert Remove Find Unordered array O 1 O n O n Ordered array O n O n O log n Unordered list O 1 O n O n Ordered list O n O n O n BST O n O n O n AVL tree O log n O log n O log n So you think AVL trees are pretty good? Let's go back to basics arrays! What if we are allowed to leave gaps in our array, and we know the index of the element we want to access? Cinda Heeren / Geoffrey Tien 7

8 A simple example Suppose a company has 300 numbered lockers in its office building, and assigns a locker to every employee it hires Suppose also, the company currently has 250 employees, and maintains information such as locker (employee) number, name, position, salary, etc. every month when computing payroll, the company system must access an employee record, given a locker number Locker number Employee name Position, etc. 103 Jorge Lorenzo Scott Redding Dani Pedrosa Katsuyuki Nakasuga Valentino Rossi Why not simply use an array with 300 (plus one) elements? We can do all insert/remove/find in O 1 time That's even better than AVL tree, but how does it scale? Cinda Heeren / Geoffrey Tien 8

9 A larger example Suppose we want to store some census data on Canadians with telephone numbers telephone number in the format (123) , can be conveniently converted to a number between 0 and 9,999,999,999, let's use this as an array index But, consider that the entire population of Canada is approximately 35,151,728 (as of 2016 census) Over 99.6% of the array will be empty And it probably will not fit in your computer's RAM anyway So the full-ranged array doesn't scale well, but let's hold onto that idea. What if the data we want to store don't have a convenient integer field? Cinda Heeren / Geoffrey Tien 9

10 Storing strings What if we had to store data by name? We would need to convert strings to integer indices Here is one way to encode strings as integers Assign a value between 1 and 26 to each letter a = 1, z = 26 (regardless of case) Sum the letter values in the string "dog" = = 26 loves to eat Milk-Bone pees on the rug Insert("dog", description) Find("god") 26 We found an entry, return true! "god" = = 26 Cinda Heeren / Geoffrey Tien 10

11 Finding unique string values Ideally we would like to have a unique integer for each possible string The "sum the letters" encoding scheme does not achieve this There is a simple method to achieve this goal As before, assign each letter a value between 1 and 26 Treat the string as a base 26 number Multiply the letter's value by 26 i, where i is the position of the letter in the word: "dog" = 4* * *26 0 = 3,101 "god" = 7* * *26 0 = 5,126 But, suppose we store strings of length 10, there are possible combinations most of which are meaningless, e.g. "achcyertxa" Cinda Heeren / Geoffrey Tien 11

12 A different approach Don't determine the array size by the maximum possible number of keys Fix the array size based on the amount of data to be stored Map the key value (phone number or name or some other data) to an array element We still need to convert the key value to an integer index using a hash function This is the basic idea behind hash tables Cinda Heeren / Geoffrey Tien 12

13 Hash tables A hash table consists of an array to store data Data often consists of complex types, or pointers to such objects One attribute of the object is designated as the table's key A hash function maps a key to an array index in 2 steps The key should be converted to an integer And then that integer mapped to an array index using some function (often the modulo function) Cinda Heeren / Geoffrey Tien 13

14 Collisions A hash function may map two different keys to the same index Referred to as a collision Consider mapping phone numbers to an array of size 1,000 where h = phone mod 1,000 Both and map to the same index (6,045,551,987 mod 1,000 = 987) A good hash function can significantly reduce the number of collisions It is still necessary to have a policy to deal with any collisions that may occur Collisions are actually unavoidable due to pigeonhole principle Cinda Heeren / Geoffrey Tien 14

15 Collisions Pigeonhole principle (informally) Try to fit k + 1 pigeons into k pigeon-sized holes Try to get 33 CPSC 221 students into a lab section with 32 seats Try to hash without collision m keys into n array indices with m > n Formally: Let X and Y be finite sets where X > Y If f: X Y, then f x 1 = f x 2 for some x 1, x 2 X, where x 1 x 2 f x 1 X Y x 2 f x 1 = f x 2 Cinda Heeren / Geoffrey Tien 15

16 Hash functions A hash function is a function that map key values to array indexes Hash functions are performed in two steps Map the key value to an integer Map the integer to a legal array index Hash functions should have the following properties Fast Deterministic Uniformity Cinda Heeren / Geoffrey Tien 16

17 Hash function speed Hash functions should be fast and easy to calculate Access to a hash table should be nearly instantaneous and in constant time Most common hash functions require a single division on the representation of the key Converting the key to a number should also be able to be performed quickly Cinda Heeren / Geoffrey Tien 17

18 Deterministic hash functions A hash function must be deterministic For a given input it must always return the same value Otherwise it will not generate the same array index And the item will not be found in the hash table Hash functions should therefore not be determined by System time Memory location Pseudo-random numbers Cinda Heeren / Geoffrey Tien 18

19 Scattering data A typical hash function usually results in some collisions Where two different search keys map to the same index A perfect hash function avoids collisions entirely Each search key value maps to a different index The goal is to reduce the number and effect of collisions To achieve this the data should be distributed evenly over the table Cinda Heeren / Geoffrey Tien 19

20 Possible values Any set of values stored in a hash table is an instance of the universe of possible values The universe of possible values may be much larger than the instance we wish to store There are many possible combinations of 10 letters But we might want a hash table to store 1,000 names Cinda Heeren / Geoffrey Tien 20

21 Uniformity A good hash function generates each value in the output range with the same probability That is, each legal hash table index has the same chance of being generated This property should hold for the universe of possible values and for the expected inputs The expected inputs should also be scattered evenly over the hash table Cinda Heeren / Geoffrey Tien 21

22 A bad hash function A hash table is to store 1,000 numeric estimates that can range from 1 to 1,000,000 Hash function h(estimate) = estimate % n Where n = array size = 1,000 Is the distribution of values from the universe of all possible values uniform? What about the distribution of expected values? Cinda Heeren / Geoffrey Tien 22

23 Another bad hash function A hash table is to store 676 names The hash function considers just the first two letters of a name Each letter is given a value where a = 1, b = 2, Function = (1 st letter * 26 + value of 2 nd letter) % 676 Is the distribution of values from the universe of all possible values uniform? What about the distribution of expected values? Cinda Heeren / Geoffrey Tien 23

24 General principles Use the entire search key in the hash function If the hash function uses modulo arithmetic make the table size a prime number A simple and (usually) effective hash function is Convert the key value to an integer, x h x = x mod tablesize Where tablesize is the first prime number larger than twice the size of the number of expected values Determining a good hash function is a complex subject for now, just use provided hash functions Cinda Heeren / Geoffrey Tien 24

25 Readings for this lesson Carrano & Henry Chapter (Hash functions) Next class: Carrano & Henry, Chapter (Collision resolution) Cinda Heeren / Geoffrey Tien 25

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1 Hash Tables Hash functions Open addressing Cinda Heeren / Geoffrey Tien 1 Hash functions A hash function is a function that map key values to array indexes Hash functions are performed in two steps Map

More information

Understand how to deal with collisions

Understand how to deal with collisions Understand the basic structure of a hash table and its associated hash function Understand what makes a good (and a bad) hash function Understand how to deal with collisions Open addressing Separate chaining

More information

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1 Hash Tables Hash functions Open addressing November 24, 2017 Hassan Khosravi / Geoffrey Tien 1 Review: hash table purpose We want to have rapid access to a dictionary entry based on a search key The key

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1 Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned

More information

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1 Queue ADT Cinda Heeren / Geoffrey Tien 1 PA1 testing Your code must compile with our private test file Any non-compiling submissions will receive zero Note that only functions that are called will be compiled

More information

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005

More information

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

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

Data Structures. Topic #6

Data Structures. Topic #6 Data Structures Topic #6 Today s Agenda Table Abstract Data Types Work by value rather than position May be implemented using a variety of data structures such as arrays (statically, dynamically allocated)

More information

TABLES AND HASHING. Chapter 13

TABLES AND HASHING. Chapter 13 Data Structures Dr Ahmed Rafat Abas Computer Science Dept, Faculty of Computer and Information, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg/ TABLES AND HASHING Chapter 13

More information

AAL 217: DATA STRUCTURES

AAL 217: DATA STRUCTURES Chapter # 4: Hashing AAL 217: DATA STRUCTURES The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions, and finds in constant average

More information

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)

More information

CSCI 104 Hash Tables & Functions. Mark Redekopp David Kempe

CSCI 104 Hash Tables & Functions. Mark Redekopp David Kempe CSCI 104 Hash Tables & Functions Mark Redekopp David Kempe Dictionaries/Maps An array maps integers to values Given i, array[i] returns the value in O(1) Dictionaries map keys to values Given key, k, map[k]

More information

CSE 326: Data Structures Lecture #11 B-Trees

CSE 326: Data Structures Lecture #11 B-Trees CSE 326: Data Structures Lecture #11 B-Trees Alon Halevy Spring Quarter 2001 B-Tree Properties Properties maximum branching factor of M the root has between 2 and M children other internal nodes have between!m/2"

More information

9/24/ Hash functions

9/24/ Hash functions 11.3 Hash functions A good hash function satis es (approximately) the assumption of SUH: each key is equally likely to hash to any of the slots, independently of the other keys We typically have no way

More information

III Data Structures. Dynamic sets

III Data Structures. Dynamic sets III Data Structures Elementary Data Structures Hash Tables Binary Search Trees Red-Black Trees Dynamic sets Sets are fundamental to computer science Algorithms may require several different types of operations

More information

Topic HashTable and Table ADT

Topic HashTable and Table ADT Topic HashTable and Table ADT Hashing, Hash Function & Hashtable Search, Insertion & Deletion of elements based on Keys So far, By comparing keys! Linear data structures Non-linear data structures Time

More information

CS 270 Algorithms. Oliver Kullmann. Generalising arrays. Direct addressing. Hashing in general. Hashing through chaining. Reading from CLRS for week 7

CS 270 Algorithms. Oliver Kullmann. Generalising arrays. Direct addressing. Hashing in general. Hashing through chaining. Reading from CLRS for week 7 Week 9 General remarks tables 1 2 3 We continue data structures by discussing hash tables. Reading from CLRS for week 7 1 Chapter 11, Sections 11.1, 11.2, 11.3. 4 5 6 Recall: Dictionaries Applications

More information

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

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course Today s Topics Review Uses and motivations of hash tables Major concerns with hash tables Properties Hash function Hash

More information

Hash Table and Hashing

Hash Table and Hashing Hash Table and Hashing The tree structures discussed so far assume that we can only work with the input keys by comparing them. No other operation is considered. In practice, it is often true that an input

More information

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1 Abstract Data Types Stack January 26, 2018 Cinda Heeren / Geoffrey Tien 1 Abstract data types and data structures An Abstract Data Type (ADT) is: A collection of data Describes what data are stored but

More information

Worst-case running time for RANDOMIZED-SELECT

Worst-case running time for RANDOMIZED-SELECT Worst-case running time for RANDOMIZED-SELECT is ), even to nd the minimum The algorithm has a linear expected running time, though, and because it is randomized, no particular input elicits the worst-case

More information

CPSC 331 Term Test #2 March 26, 2007

CPSC 331 Term Test #2 March 26, 2007 CPSC 331 Term Test #2 March 26, 2007 Name: Please DO NOT write your ID number on this page. Instructions: Answer all questions in the space provided. Point form answers are acceptable if complete enough

More information

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

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech Hashing Dr. Ronaldo Menezes Hugo Serrano Agenda Motivation Prehash Hashing Hash Functions Collisions Separate Chaining Open Addressing Motivation Hash Table Its one of the most important data structures

More information

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe 1 CSCI 104 Rafael Ferreira da Silva rafsilva@isi.edu Slides adapted from: Mark Redekopp and David Kempe HASH TABLES 2 3 Dictionaries/Maps An array maps integers to values Given i, array[i] returns the

More information

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

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far Chapter 5 Hashing 2 Introduction hashing performs basic operations, such as insertion, deletion, and finds in average time better than other ADTs we ve seen so far 3 Hashing a hash table is merely an hashing

More information

Unit #5: Hash Functions and the Pigeonhole Principle

Unit #5: Hash Functions and the Pigeonhole Principle Unit #5: Hash Functions and the Pigeonhole Principle CPSC 221: Basic Algorithms and Data Structures Jan Manuch 217S1: May June 217 Unit Outline Constant-Time Dictionaries? Hash Table Outline Hash Functions

More information

UNIT III BALANCED SEARCH TREES AND INDEXING

UNIT III BALANCED SEARCH TREES AND INDEXING UNIT III BALANCED SEARCH TREES AND INDEXING OBJECTIVE The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions and finds in constant

More information

ECE250: Algorithms and Data Structures Midterm Review

ECE250: Algorithms and Data Structures Midterm Review ECE250: Algorithms and Data Structures Midterm Review Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures (Overflow) Hashing Marius Kloft This Module Introduction 2 Abstract Data Types 1 Complexity analysis 1 Styles of algorithms 1 Lists, stacks, queues 2 Sorting (lists) 3 Searching

More information

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5. 5. Hashing 5.1 General Idea 5.2 Hash Function 5.3 Separate Chaining 5.4 Open Addressing 5.5 Rehashing 5.6 Extendible Hashing Malek Mouhoub, CS340 Fall 2004 1 5. Hashing Sequential access : O(n). Binary

More information

COMP171. Hashing.

COMP171. Hashing. COMP171 Hashing Hashing 2 Hashing Again, a (dynamic) set of elements in which we do search, insert, and delete Linear ones: lists, stacks, queues, Nonlinear ones: trees, graphs (relations between elements

More information

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once. Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based

More information

Week 9. Hash tables. 1 Generalising arrays. 2 Direct addressing. 3 Hashing in general. 4 Hashing through chaining. 5 Hash functions.

Week 9. Hash tables. 1 Generalising arrays. 2 Direct addressing. 3 Hashing in general. 4 Hashing through chaining. 5 Hash functions. Week 9 tables 1 2 3 ing in ing in ing 4 ing 5 6 General remarks We continue data structures by discussing hash tables. For this year, we only consider the first four sections (not sections and ). Only

More information

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Lecture 5 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: Randomized Search Trees by Aragon & Seidel, Algorithmica 1996, http://sims.berkeley.edu/~aragon/pubs/rst96.pdf;

More information

Lecture 7: Efficient Collections via Hashing

Lecture 7: Efficient Collections via Hashing Lecture 7: Efficient Collections via Hashing These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. 1 Announcements Lab 6 due Friday Lab 7 out tomorrow

More information

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017 University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 207 Midterm Examination Instructor: Dr. Ladan Tahvildari, PEng, SMIEEE Date: Wednesday,

More information

II (Sorting and) Order Statistics

II (Sorting and) Order Statistics II (Sorting and) Order Statistics Heapsort Quicksort Sorting in Linear Time Medians and Order Statistics 8 Sorting in Linear Time The sorting algorithms introduced thus far are comparison sorts Any comparison

More information

CITS2200 Data Structures and Algorithms. Topic 15. Hash Tables

CITS2200 Data Structures and Algorithms. Topic 15. Hash Tables CITS2200 Data Structures and Algorithms Topic 15 Hash Tables Introduction to hashing basic ideas Hash functions properties, 2-universal functions, hashing non-integers Collision resolution bucketing and

More information

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION DESIGN AND ANALYSIS OF ALGORITHMS Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION http://milanvachhani.blogspot.in EXAMPLES FROM THE SORTING WORLD Sorting provides a good set of examples for analyzing

More information

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT (Cont'd) Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Last time we introduced the idea behind Hash Tables, and discussed hash functions and the issue of collisions.

More information

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 8 (Based on Paul Kube course materials) CSE 100 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

More information

CSE 214 Computer Science II Searching

CSE 214 Computer Science II Searching CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a

More information

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014 University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014 Midterm Examination Instructor: Ladan Tahvildari, PhD, PEng, SMIEEE Date: Tuesday,

More information

CMSC 341 Hashing. Based on slides from previous iterations of this course

CMSC 341 Hashing. Based on slides from previous iterations of this course CMSC 341 Hashing Based on slides from previous iterations of this course Hashing Searching n Consider the problem of searching an array for a given value q If the array is not sorted, the search requires

More information

Friday Four Square! 4:15PM, Outside Gates

Friday Four Square! 4:15PM, Outside Gates Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.

More information

HASH TABLES.

HASH TABLES. 1 HASH TABLES http://en.wikipedia.org/wiki/hash_table 2 Hash Table A hash table (or hash map) is a data structure that maps keys (identifiers) into a certain location (bucket) A hash function changes the

More information

Question Bank Subject: Advanced Data Structures Class: SE Computer

Question Bank Subject: Advanced Data Structures Class: SE Computer Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.

More information

Binary Search Trees Treesort

Binary Search Trees Treesort Treesort CS 311 Data Structures and Algorithms Lecture Slides Friday, November 13, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009

More information

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n)

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n) Hash-Tables Introduction Dictionary Dictionary stores key-value pairs Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n) Balanced BST O(log n) O(log n) O(log n) Dictionary

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Questions. 6. Suppose we were to define a hash code on strings s by:

Questions. 6. Suppose we were to define a hash code on strings s by: Questions 1. Suppose you are given a list of n elements. A brute force method to find duplicates could use two (nested) loops. The outer loop iterates over position i the list, and the inner loop iterates

More information

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Solutions Problem 1. Quiz 1 Solutions Asymptotic orders

More information

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Hashing. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong Department of Computer Science and Engineering Chinese University of Hong Kong In this lecture, we will revisit the dictionary search problem, where we want to locate an integer v in a set of size n or

More information

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing Hashing Algorithms Hash functions Separate Chaining Linear Probing Double Hashing Symbol-Table ADT Records with keys (priorities) basic operations insert search create test if empty destroy copy generic

More information

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Hashing Structures Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Hashing Structures I. Motivation and Review II. Hash Functions III. HashTables I. Implementations

More information

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004 Computer Science 136 Spring 2004 Professor Bruce Final Examination May 19, 2004 Question Points Score 1 10 2 8 3 15 4 12 5 12 6 8 7 10 TOTAL 65 Your name (Please print) I have neither given nor received

More information

Outline. hash tables hash functions open addressing chained hashing

Outline. hash tables hash functions open addressing chained hashing Outline hash tables hash functions open addressing chained hashing 1 hashing hash browns: mixed-up bits of potatoes, cooked together hashing: slicing up and mixing together a hash function takes a larger,

More information

Chapter 8 Sorting in Linear Time

Chapter 8 Sorting in Linear Time Chapter 8 Sorting in Linear Time The slides for this course are based on the course textbook: Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd edition, The MIT Press, McGraw-Hill,

More information

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

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 Hashing 1 Hash Tables We ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees. The implementation of hash tables is called hashing. Hashing is a technique

More information

Midterm 2. Read all of the following information before starting the exam:

Midterm 2. Read all of the following information before starting the exam: Midterm 2 ECE 608 April 7, 2004, 7-9pm Name: Read all of the following information before starting the exam: NOTE: Unanswered questions are worth 30% credit, rounded down. Writing any answer loses this

More information

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018 HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018 Acknowledgement The set of slides have used materials from the following resources Slides for textbook by Dr. Y.

More information

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion, Introduction Chapter 5 Hashing hashing performs basic operations, such as insertion, deletion, and finds in average time 2 Hashing a hash table is merely an of some fixed size hashing converts into locations

More information

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms Comparison Based Sorting Algorithms Algorithms and Data Structures: Lower Bounds for Sorting Definition 1 A sorting algorithm is comparison based if comparisons A[i] < A[j], A[i] A[j], A[i] = A[j], A[i]

More information

ECE250: Algorithms and Data Structures Final Review Course

ECE250: Algorithms and Data Structures Final Review Course ECE250: Algorithms and Data Structures Final Review Course Ladan Tahvildari, PEng, SMIEEE Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo

More information

CS 3114 Data Structures and Algorithms READ THIS NOW!

CS 3114 Data Structures and Algorithms READ THIS NOW! READ THIS NOW! Print your name in the space provided below. There are 9 short-answer questions, priced as marked. The maximum score is 100. When you have finished, sign the pledge at the bottom of this

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

Module 2: Classical Algorithm Design Techniques

Module 2: Classical Algorithm Design Techniques Module 2: Classical Algorithm Design Techniques Dr. Natarajan Meghanathan Associate Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu Module

More information

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1 Algorithms and Data Structures: Lower Bounds for Sorting ADS: lect 7 slide 1 ADS: lect 7 slide 2 Comparison Based Sorting Algorithms Definition 1 A sorting algorithm is comparison based if comparisons

More information

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT https://ece.uwaterloo.ca/~cmoreno/ece250

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT https://ece.uwaterloo.ca/~cmoreno/ece250 (Cont'd) Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Last time we introduced the idea behind Hash Tables, and discussed hash functions and the issue of collisions.

More information

Basic Data Structures (Version 7) Name:

Basic Data Structures (Version 7) Name: Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)

More information

( ) n 3. n 2 ( ) D. Ο

( ) n 3. n 2 ( ) D. Ο CSE 0 Name Test Summer 0 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n n matrices is: A. Θ( n) B. Θ( max( m,n, p) ) C.

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert B+ Tree Review CSE2: Data Abstractions Lecture 10: More B Trees; Hashing Dan Grossman Spring 2010 M-ary tree with room for L data items at each leaf Order property: Subtree between keys x and y contains

More information

Hash Table. A hash function h maps keys of a given type into integers in a fixed interval [0,m-1]

Hash Table. A hash function h maps keys of a given type into integers in a fixed interval [0,m-1] Exercise # 8- Hash Tables Hash Tables Hash Function Uniform Hash Hash Table Direct Addressing A hash function h maps keys of a given type into integers in a fixed interval [0,m-1] 1 Pr h( key) i, where

More information

Data Structures and Algorithm Analysis (CSC317) Hash tables (part2)

Data Structures and Algorithm Analysis (CSC317) Hash tables (part2) Data Structures and Algorithm Analysis (CSC317) Hash tables (part2) Hash table We have elements with key and satellite data Operations performed: Insert, Delete, Search/lookup We don t maintain order information

More information

UNIT 6A Organizing Data: Lists. Last Two Weeks

UNIT 6A Organizing Data: Lists. Last Two Weeks UNIT 6A Organizing Data: Lists 1 Last Two Weeks Algorithms: Searching and sorting Problem solving technique: Recursion Asymptotic worst case analysis using the big O notation 2 1 This Week Observe: The

More information

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques Hashing Manolis Koubarakis 1 The Symbol Table ADT A symbol table T is an abstract storage that contains table entries that are either empty or are pairs of the form (K, I) where K is a key and I is some

More information

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

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1 Hash Tables Outline Definition Hash functions Open hashing Closed hashing collision resolution techniques Efficiency EECS 268 Programming II 1 Overview Implementation style for the Table ADT that is good

More information

Part I Anton Gerdelan

Part I Anton Gerdelan Hash Part I Anton Gerdelan Review labs - pointers and pointers to pointers memory allocation easier if you remember char* and char** are just basic ptr variables that hold an address

More information

Lecture 6: Hashing Steven Skiena

Lecture 6: Hashing Steven Skiena Lecture 6: Hashing Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Dictionary / Dynamic Set Operations Perhaps

More information

Quiz 1 Practice Problems

Quiz 1 Practice Problems Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 1 Asymptotic Notation Quiz 1 Practice Problems Decide whether these

More information

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started

CS4311 Design and Analysis of Algorithms. Lecture 1: Getting Started CS4311 Design and Analysis of Algorithms Lecture 1: Getting Started 1 Study a few simple algorithms for sorting Insertion Sort Selection Sort Merge Sort About this lecture Show why these algorithms are

More information

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms Hashing CmSc 250 Introduction to Algorithms 1. Introduction Hashing is a method of storing elements in a table in a way that reduces the time for search. Elements are assumed to be records with several

More information

CPSC 259 admin notes

CPSC 259 admin notes CPSC 9 admin notes! TAs Office hours next week! Monday during LA 9 - Pearl! Monday during LB Andrew! Monday during LF Marika! Monday during LE Angad! Tuesday during LH 9 Giorgio! Tuesday during LG - Pearl!

More information

Lower Bound on Comparison-based Sorting

Lower Bound on Comparison-based Sorting Lower Bound on Comparison-based Sorting Different sorting algorithms may have different time complexity, how to know whether the running time of an algorithm is best possible? We know of several sorting

More information

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

Hash table basics mod 83 ate. ate. hashcode() Hash table basics ate hashcode() 82 83 84 48594983 mod 83 ate Reminder from syllabus: EditorTrees worth 10% of term grade See schedule page Exam 2 moved to Friday after break. Short pop quiz over AVL rotations

More information

1. Attempt any three of the following: 15

1. Attempt any three of the following: 15 (Time: 2½ hours) Total Marks: 75 N. B.: (1) All questions are compulsory. (2) Make suitable assumptions wherever necessary and state the assumptions made. (3) Answers to the same question must be written

More information

Chapter 20 Hash Tables

Chapter 20 Hash Tables Chapter 20 Hash Tables Dictionary All elements have a unique key. Operations: o Insert element with a specified key. o Search for element by key. o Delete element by key. Random vs. sequential access.

More information

Algorithms. AVL Tree

Algorithms. AVL Tree Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other

More information

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

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018 CSE 332: Data Structures & Parallelism Lecture 10:Hashing Ruth Anderson Autumn 2018 Today Dictionaries Hashing 10/19/2018 2 Motivating Hash Tables For dictionary with n key/value pairs insert find delete

More information

Multiple-choice (35 pt.)

Multiple-choice (35 pt.) CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need

More information

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides Jana Kosecka Linear Time Sorting, Median, Order Statistics Many slides here are based on E. Demaine, D. Luebke slides Insertion sort: Easy to code Fast on small inputs (less than ~50 elements) Fast on

More information

Lecture 12 Notes Hash Tables

Lecture 12 Notes Hash Tables Lecture 12 Notes Hash Tables 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons 1 Introduction In this lecture we re-introduce the dictionaries that were implemented

More information

UNIT 6B Organizing Data: Hash Tables. Comparing Algorithms

UNIT 6B Organizing Data: Hash Tables. Comparing Algorithms UNIT 6B Organizing Data: Hash Tables 1 Comparing Algorithms You are a professor and you want to find an exam in a large pile of n exams. Search the pile using linear search. Per student: O(n) Total for

More information

Hashing. October 19, CMPE 250 Hashing October 19, / 25

Hashing. October 19, CMPE 250 Hashing October 19, / 25 Hashing October 19, 2016 CMPE 250 Hashing October 19, 2016 1 / 25 Dictionary ADT Data structure with just three basic operations: finditem (i): find item with key (identifier) i insert (i): insert i into

More information

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu

More information

CSE 326: Data Structures Sorting Conclusion

CSE 326: Data Structures Sorting Conclusion CSE 36: Data Structures Sorting Conclusion James Fogarty Spring 009 Administrivia Homework Due Homework Assigned Better be working on Project 3 (code due May 7) Sorting Recap Selection Sort Bubble Sort

More information