CMSC 341 Lecture 12.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Advertisements

Hashing General idea Hash function Separate Chaining Open Addressing
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Log Files. O(n) Data Structure Exercises 16.1.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
1.  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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Fundamental Structures of Computer Science II
Hashing.
May 3rd – Hashing & Graphs
CMSC 341 Hashing.
Hashing - resolving collisions
Hashing Alexandra Stefan.
Week 8 - Wednesday CS221.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Hashing Alexandra Stefan.
Hashing Exercises.
November 1st – Exam Review
CMSC 341 Lecture 13 Leftist Heaps
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Monday, April 16, 2018 Announcements… For Today…
Advanced Associative Structures
CSE 326: Data Structures: Midterm Review
Hash In-Class Quiz.
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Data Structures and Algorithms
Dictionaries and Their Implementations
Collision Resolution Neil Tang 02/18/2010
Resolving collisions: Open addressing
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
CMSC 341 Hashing 12/2/2018.
Data Structures and Algorithms
Hashing Alexandra Stefan.
CMSC 341 Splay Trees.
Balanced Binary Search Trees
CMSC 341 Hashing 2/18/2019.
A Hash Table with Chaining
CMSC 341 Hashing.
Advanced Implementation of Tables
CMSC 341 Splay Trees.
Advanced Implementation of Tables
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
CMSC 341 Hashing 4/11/2019.
Collision Resolution Neil Tang 02/21/2008
CMSC 341 Hashing 4/27/2019.
Ch Hash Tables Array or linked list Binary search trees
CSE 326: Data Structures Lecture #9 AVL II
Collision Handling Collisions occur when different elements are mapped to the same cell.
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
CMSC 341 Lecture 12.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EE 312 Software Design and Implementation I
Splay Trees Binary search trees.
Lecture-Hashing.
Presentation transcript:

CMSC 341 Lecture 12

Announcements Postponing Priority Queues until later

HashTable Class template <class HashedObj> class HashTable { public: explicit HashTable(const HashedObj) &notFound, size=101); HashTable(const HashTable &rhs) : ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND),theLists(rhs.theLists){} const HashedObj &find(const HashedObj &x) const; void makeEmpty(); void insert (const HashedObj &x); void remove (const HashedObj &x); const HashTable &operator=(const HashTable &rhs); private: vector<List<HashedObj>> theLists; const HashedObj ITEM_NOT_FOUND; };

Handling Collisions Revisited Open addressing (aka closed hashing) all elements stored in the table itself (so table should be large. Rule of thumb: M>= 2N) upon collision, item is hashed to a new (open) slot. Hash function h: U X {0,1,2,….}  {0,1,…,M-1} h(k,I) = (h’(k) + f(I))mod m for some h’: U  {0,1,…,M-1} and f(0) = 0 Each try is called a probe

Linear Probing Function: f(i) = ci Example: h’(k) = k mod 10 in a table of size 10 , f(i) = i U={89,18,49,58,69} Example: h’(k) = k mod 10 in a table of size 10 (not prime, but easy to calculate) U={89,18,49,58,69} f(I) = I 1. 89 hashes to 9 2. 18 hashes to 8 3. 49 hashes to 9, collides with 89 h(k,1) = (49%10+1)%10=0 4. 58 hashes to 8, collides with 18 h(k,1)=(50%10+1)%10=9, collides with 89 h(k,2)=(50%10+2)%10=0, collides with 49 h(k,3)=(50%10+3)%10=1 5. 69 hashes to 9, collides with 89 h(69,1) = (h’(69)+f(1))mod 10 = 0, collides with 49 h(69,2) = (h’(69+f(2))mod 10 =0, collides with58 h(69,3) = (h’(69)+f(3))mod 10 = 2

Linear Probing (cont) Problem: Clustering when table starts to fill up, performance  O(N) Asymptotic Performance insertion and unsuccessful find, average # probes  1/2(1+1/(1-)2) if   1, the denominator goes to zero and the number of probes goes to infinity

Linear Probing (cont) Remove Can’t just use the hash function(s) to find the object,and remove it, because objects that were inserted after x were hashed based on x’s presence. Can just mark the cell as deleted so it won’t be found anymore. Other elements still in right cells Table can fill with lots of deleted junk

Quadratic Probing Function: f(i) = c2i2 + c1i + c0 Example: f(i) = i2, m=10 U={89,18,49,58,69} 1. 89 hashes to 9 2. 18 hashes to 8 3. 49 hashes to 9, collision with 89 4. 598 hashes to 8, collides with 18 5. 69 hashes to 9

Quadratic Probing (cont.) Advantage: reduced clustering problem Disadvantages: reduced number of sequences no guarantee that empty slot will be found if tablesize is not prime For example: h(k,I) = (h’(k)+I^2)mod M h(k, I) = h(k) + 0 + 1 + 4 + 9 + 16 + 25 + 26 with m = 10, h’(k) = k mod 10 key I=0 1 2 3 4 5 | 6 7 8 9 10 0 0 1 4 9 6 5 | 6 9 4 1 0 1 1 2 5 0 7 6 | 7 0 5 2 1 2 2 3 6 1 8 7 | 8 1 6 3 2 duplicate values to right of line I cannot exceed 5 in this table, only 6 slot available for each key If table size is prime, an open slot is guaranteed when <= 1/2.

Double Hashing Use two hash functions: h’1(k), h’2(k) h(k,I) = (h’1(k) + ih’2(k)) mod M Choosing h’2(k) don’t allow h’2(k) = 0 for any k. a good choice: h’2(k) = R - (k mod R) with R a prime smaller than M Characteristics No clustering problem Requires a second hash function

Balanced Trees Problem specific tree configuration dependent on order in which nodes are inserted may become noticeably unbalanced, leading to performance approaching worst case -- O(n) Solution ensure that trees remain balanced (or pretty balanced), no matter what

AVL Trees Method impose balance condition: The height of the left and right subtrees of a node can differ by no more than one. adjust structure after each insertion or deletion to maintain balance condition; uses rotation Pros and Cons guarantee O(lg n) performance lots of adjustments can make insertion expensive

Rotation Operation To rotate about a node t and its left child l: t->left = l->right; l->right = t; To rotate about a node t and its right child r: t->right = r->left; r->left = t;

Splay Trees Concept adjust tree in response to accesses to make common operations efficient after access node is moved to root by splaying Performance amortized such that m operations take O(m lg n) where n is the number of insertions

Splay Operation Traverse tree from node x to root, rotating along the way until x is the root Each rotation If x is root, do nothing. If x has no grandparent, rotate x about its parent. If x has a grandparent, if x and its parent are both left children or both right children, rotate the parent about the grandparent, then rotate x about its parent if x and its parent are opposite type children (one left and the other right), rotate x about its parent, then rotate x about its new parent (former grandparent)