1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.

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

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing Techniques.
CSE 332 Review Slides Tyler Robison Summer
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
CSE 326: Data Structures Lecture #11 B-Trees Alon Halevy Spring Quarter 2001.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
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.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
IS 2610: Data Structures Searching March 29, 2004.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Trevor Brown – University of Toronto B-slack trees: Space efficient B-trees.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Hashing as a Dictionary Implementation Chapter 19.
Hash Tables - Motivation
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Lecture 9 Binary Trees Trees General Definition Terminology
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Final Exam Review COP4530.
Instructor: Lilian de Greef Quarter: Summer 2017
COMP261 Lecture 23 B Trees.
Hash Tables (Chapter 13) Part 2.
Hashing Exercises.
Advanced Associative Structures
Final Exam Review COP4530.
Hash Tables and Associative Containers
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Advanced Implementation of Tables
Final Review Dr. Yingwu Zhu.
Ch Hash Tables Array or linked list Binary search trees
Implementing the Associative Containers
Presentation transcript:

1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining Separate Chaining Ordered ACs Balanced Search Trees Trees Trees Red-Black Trees Red-Black Trees Lecture 11 Associative Containers

2 Road Map Implementing Associative Containers (ACs) – Hash Tables (Unordered ACs; Ch. 5) – Trees (Ordered; 4) – Red-Black Trees (Ordered; 12) Inheritance and Polymorphism revisited Heaps (PQ implementation: 6) Divide and Conquer Algs. – Mergesort, Quicksort (7) Intro to Graphs (9) – Representations – Searching – Topological Sorting, Shortest Path

Associative Containers Categories – Ordered (OAC) – iterate through elements in key order – Unordered (UAC) – cannot iterate … OACS use binary search trees – set, multiset, map, multimap UACs use hash tables – unordered_set – unordered_multiset – unordered_map – unordered_multimap 3

Hash Tables Hash table – Array of slots – A slot holds One object (open addressing) Collection of objects (separate chaining) Average insert, erase, find ops. take O(1)! – Worst case is O(N), but easy to avoid – Makes for good unordered set ADT 4

Hash Tables (Cont’d) Main idea – Store key k in slot hf (k) – hf: KeySet  SlotSet Complications – | KeySet | >> | SlotSet |, so hf cannot be 1-1 – If two keys map to same slot have a collision – Deletion can be tricky 5

Hash Tables (Cont’d) Collision resolution strategies – Open addressing (probe table for open slot) linear, quadratic probing double hashing – Separate chaining (map to slot that can hold multiple values In this case slot is called bucket Approach taken by STL 6

7 Graphical Overview Table size = m is prime to help distribute keys evenly

8 Open Addressing 2 Steps to Compute Slot 1)i = hf (key) 2)Slot = i % m Open Addressing: Each slot holds just 1 key

9 Open Addressing (Cont’d) 22

10 Collision Resolution Using OA: (Linear Probing)

11 Collision Resolution: Chaining (Cont’d)

12 Collision Resolution with Separate Chaining const size_t TABLE_SIZE = 11; // Prime vector > table (TABLE_SIZE); index = hf (key) % TABLE_SIZE; table[index].push_back (key);

13 Coding Hash Functions // Code hash fn. as function object in C++ // Stateful and easier to use than function pointer struct HashString { size_t operator () (const string& key) const { size_t n = 5381; // Prime size_t i; for (i = 0; i < key.length (); ++i) n = (n * 33 ) + key[i]; // Horner return n; } };

14 Efficiency of Hashing Methods Load factor = n / m = # elems / table size Chaining – represents avg. list length – Avg. probes for successful search ≈ 1 + /2 – Avg. probes for unsuccessful search = – Avg. find, insert, erase: O(1) Worst case O(1) for ? Open Addressing – represents ? – If > 0.5, double table size and rehash all elements to new table

Quadratic probing f(i) = i 2 or f(i) = ±i 2 If the table size is prime, a new element can always be inserted if the table is at least half empty 15

Rehashing If the table gets too full, operations begin to bog down Solution: build a new table twice the size (at least – keep prime) and hash all values from the old table into the new table 16

17 Problems w/ BSTs Can degenerate completely to lists Can become skewed Most ops are O(d) – Want d to be close to lg(N) How to correct skewness?

18 Two BSTs: Same Keys Insertion sequence: 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 (N = 15)

19 Notions of Balance For any node N, depth (N->left) and depth (N->right) differ by at most 1 – AVL Trees All leaves exist at same level (perfectly balanced!) – Trees Number of black nodes on any path from root to leaf is same (black height of tree) – Red-black Trees

20 Binary Search Tree, Red-Black Tree, and AVL Tree

2-3-4 Trees 3 node types – 2-node: 2 children, 1 key – 3-node: 3 children, 2 keys – 4-node: 4 children, 3 keys All leaves at same level Logarithmic find, insert, erase 21

Tree Node Types

Tree How to Search? Space for 4-Node?

Insert for a Tree Top-down – Split 4-nodes as you search for insertion point – Ensures node splits don’t keep propagating upwards Key operation is split of 4-node – Becomes three 2-nodes – Median key is hoisted up and added to parent node

25 Splitting a 4-Node C A B C S TV U A B ST V U

26 Insertion into Tree Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 Insert 8 Insert 4

Insertion (Cont’d) 27 Insert 55 Insert 10

28 Insertion (Cont’d) Split 4-node (4, 12, 25) Insert 11 Insert 9

29 Insertion into Tree (cont’d) Insert 7

Red-Black Trees Can represent tree as binary tree Use 2 colors – Red indicates node is “bound” to parent – Red node cannot have red child Preserves logarithmic find, insert, erase More efficient in time and space 30

31 Red-Black Repr. of Tree

32 Converting a Tree to Red- Black Tree

33 Red-Black Tree Ops Find? – easy Insertions – Insert as red node – Require splitting of “4-node” (top-down insertion) – Use color-flip for split (4 cases) – Requires rotations Deletions – Hard – Several cases – color fix-ups Remember: RB Trees guarantee lg(N) find’s, insert’s, and erase’s

34 Four Cases in Splitting of a 4- Node Case 1Case 2Case 3Case 4

35 Left child of a Black Parent P Case 1

36 Prior to inserting key 55 Case 2

37 Oriented left-left from G Using A Single Right Rotation Case 3 (and G, P, X linear (zig-zig) P rotated right

38 Oriented Left-Right From G After the Color Flip Case 4 (and G, P, X zig-zag)

39 After X is Double Rotated X P G A B C D (X is rotated left-right)

40 Building A Red-Black Tree right-left rotate

41 Building A Red-Black Tree (Cont…)

42 Repr. of Red-Black Node 35