Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am
|
|
- Daniella Murphy
- 6 years ago
- Views:
Transcription
1 Announcements Today s topic: Hashing (Ch. 10) Next topic: Graphs Break around 11:45am Container structures so far Array lists O(1) access O(n) insertion/deletion (average case), better at end Linked lists O(n) access O(n) insertion/deletion (average case), better at front and back Binary search trees O(log n) access if balanced O(log n) insertion/deletion if balanced Heaps O(1) access of min/max O(log n) insertion O(log n) deletion (average case) Can we do even better? 1 2 Sets set: A collection of unique s (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains) The client doesn't think of a set as having indices; we just add things to the set in general and don't worry about order IntSet ADT interface Let's think about how to write an implementation of a set. To simplify the problem, we only store ints in our set for now. As is (usually) done in the Java Collections Framework, we will define sets as an ADT by creating a Set interface. Core operations are: add, contains, remove. set.contains("to") set.contains("be") "the" "if" "of" "to" "down" "from" "by" "she" "in" "you" "why" "him" set true false 3 public interface IntSet { void add(int ); boolean contains(int ); void clear(); boolean isempty(); void remove(int ); int size(); 4 1
2 BST as a set We can implement a set as a binary search tree. O(log n) performance for: add contains Remove But there are other ways to implement a set perhaps with better performance. Is there a way to use an array s fast, O(1), access? root Unfilled array set? Consider storing a set in an unfilled array. It doesn't really matter what order the elements appear in a set, so long as they can be added and searched quickly. What would make a good ordering for the elements? If we store them in the next available index, as in a list, set.add(9); set.add(23); set.add(8); set.add(-3); set.add(49); set.add(12); size 6 How efficient is add? contains? remove? O(1), O(n), O(n) 5 6 Sorted array set? Suppose we store the elements in an unfilled array, but in sorted order rather than order of insertion. set.add(9); set.add(23); set.add(8); set.add(-3); set.add(49); set.add(12); size 6 How efficient is add? contains? remove? O(n), O(log n), O(n) A strange idea Silly idea: When client adds i, store it at index i in the array. Would this work? Problems/drawbacks of this approach? How to work around them? set.add(7); set.add(1); set.add(9); set.add(18); set.add(12); size
3 Hashing Hash: To map a large domain of s to a smaller fixed domain. Typically, mapping a set of elements to integer indices in an array. Idea: Store any given element in a particular predictable index. That way, adding/removing/looking for it are constant-time (O(1)). hash table: An array that stores elements via hashing. Hash function: An algorithm that maps s to indices. hash code: The output of a hash function for a given. In previous slide, our "hash function" was: hash(i) i Potentially requires a large array. Doesn't work for negative numbers. Array could be very sparse, mostly empty (memory waste). Hashing overview element hash() The hash function, hash(), maps a range of elements of arbitrary type into an integer range [0, M-1]. hash code (integer index) hash table (array, M buckets) M Improved hash function Sketch of implementation To deal with negative numbers: hash(i) abs(i) To deal with large numbers: hash(i) abs(i) % length set.add(37); // abs(37) % 10 == 7 set.add(-2); // abs(-2) % 10 == 2 set.add(49); // abs(49) % 10 == 9 public class HashIntSet implements IntSet { private int[] elements; // the hash table public void add(int ) { elements[hash()] = ; size 3 // inside HashIntSet class private int hash(int i) { return Math.abs(i) % elements.length; 11 public boolean contains(int ) { return elements[hash()] == ; public void remove(int ) { elements[hash()] = 0; Runtime of add, contains, and remove: O(1) Are there any problems with this approach? 12 3
4 Hash function In general, any function that maps from the space of elements to the space of array indices is a valid hash function but a good hash function spreads the indices out over the entire hash table (array). A good hash function also tries to avoid collisions - multiple elements having the same index in the hash table. Collisions Collision: When hash function maps 2 s to same index. set.add(11); set.add(49); set.add(24); set.add(37); set.add(54); // collides with 24! Uniform hashing assumption: Hashing is most efficient when index s spread throughout the table 13 Collision resolution: An algorithm for fixing collisions Probing Separate chaining etc. Probing Probing: Resolving a collision by moving to another index. Linear probing: Moves to the next available index (wraps if needed). set.add(11); set.add(49); set.add(24); set.add(37); set.add(54); // collides with 24; must probe variation: quadratic probing moves increasingly far away: +1, +4, +9, 15 Implementing HashIntSet Let s implement an int set using a hash table with linear probing. For simplicity, assume that the set cannot store 0s for now. public class HashIntSet implements IntSet { private int[] elements; private int size; // constructs new empty set public HashIntSet() { elements = new int[10]; size = 0; // hash function maps s to indices private int hash(int ) { return Math.abs() % elements.length; 16 4
5 The add operation How do we add an element to the hash table? Use the hash function to find the proper bucket index. If we see a 0, put it there, i.e., 0 means an available slot. If not, move forward until we find an empty (0) index to store it. If we see that the is already in the table, don't re-add it. set.add(54); set.add(); // client code size 6 Implementing add How do we add an element to the hash table? public void add(int ) { int h = hash(); while (elements[h]!= 0 && elements[h]!= ) { // Linear probing h = (h + 1) % elements.length; // for empty slot. if (elements[h]!= ) { // Avoid duplicates. elements[h] = ; // Add it here. size++; The contains operation How do we search for an element in the hash table? Use the hash function to find the proper bucket index. Loop forward until we either find the, or an empty index (0). If find the, it is contained (true). If we find 0, it is not (false). We assume that the table is never full. set.contains(24) set.contains() set.contains(35) // true // true // false size 6 Implementing contains public boolean contains(int ) { int h = hash(); while (elements[h]!= 0) { if (elements[h] == ) { return true; h = (h + 1) % elements.length; return false; // Linear probing // to search // not found
6 The remove operation Implementing remove We cannot remove by simply zeroing out an element: set.remove(54); // set index 5 to 0 set.contains() // false??? oops Instead, we replace it by a special "removed" placeholder (can be re-used on add, but keep searching on contains) XX public void remove(int ) { int h = hash(); while (elements[h]!= 0 && elements[h]!= ) { h = (h + 1) % elements.length; if (elements[h] == ) { elements[h] = -999; // "removed" flag size--; set.remove(54); set.remove(11); set.remove(34); // client code Patching add, contains private static final int REMOVED = -999; // add needs patching. public void add(int ) { int h = hash(); while (elements[h]!= 0 && elements[h]!= && elements[h]!= REMOVED) { h = (h + 1) % elements.length; if (elements[h]!= ) { elements[h] = ; size++; // contains does not need patching; // it should keep going on a -999, which it already does public boolean contains(int ) { int h = hash(); while (elements[h]!= 0 && elements[h]!= ) { h = (h + 1) % elements.length; return elements[h] == ; 23 Problem: full array Clustering: Clumps of elements at neighboring indexes. Slows down the hash table lookup; you must loop through them. set.add(11); set.add(49); set.add(24); set.add(37); set.add(54); // collides with 24 set.add(); // collides with 24, then 54 set.add(86); // collides with, then size 0 Where does each go in the array? How many indices must be examined to answer contains(94)? What will happen if the array completely fills up? 24 6
7 Rehashing Rehash: Using a larger array when the table is too full. Cannot simply copy the old array to a new one. (Why not?) Load factor: ratio of (# of elements ) / (hash table length) Many collections rehash when load factor size size 8 Hash table sizes Can use prime numbers as hash table sizes to reduce collisions. Also improves spread / reduces clustering on rehash. set.add(11); // 11 % 13 == 11 set.add(39); // 39 % 13 == 0 set.add(21); // 21 % 13 == 8 set.add(29); // 29 % 13 == 3 set.add(71); // 81 % 13 == 6 set.add(41); // 41 % 13 == 2 set.add(101); // 101 % 13 == size 7 Google: Why setting Hash Table length to a Prime Number is a good practice? Iterator for a hash table How would you implement an iterator for a hash table using linear probing, e.g., HashIntSet? And also for one with separate chaining (next page)? How would we implement tostring on our HashIntSet? Separate chaining Separate chaining: Solving collisions by storing a list at each index. add/contains/remove must traverse lists, but the lists are short impossible to "run out" of indices, unlike with probing System.out.println(set); // [11, 24, 54, 37, 49] 27 private class Node { public int data; 54 public Node next; Will see an alternative approach to implement chains later in MyHashMap.java Iterator for one with separate chaining?
8 Implementing HashIntSet Let s implement a hash set of ints using separate chaining. public class HashIntSet implements IntSet { // array of linked lists; // elements[i] = front of list #i (null if empty) private Node[] elements; private int size; // constructs new empty set public HashIntSet() { elements = new Node[10]; size = 0; The add operation How do we add an element to the hash table? When you want to modify a linked list, you must either change the list s front reference, or the next field of a node in the list. Where in the list should we add the new element? Must make sure to avoid duplicates. set.add(24); // hash function maps s to indexes private int hash(int ) { return Math.abs() % elements.length; new node Implementing add public void add(int ) { if (!contains()) { int h = hash(); // add to front Node newnode = new Node(); // of list #h newnode.next = elements[h]; elements[h] = newnode; size++; The contains operation How do we search for an element in the hash table? Must loop through the linked list for the appropriate hash index, looking for the desired set.contains() // true set.contains(84) // false set.contains(53) // false
9 Implementing contains public boolean contains(int ) { Node current = elements[hash()]; while (current!= null) { if (current.data == ) { return true; current = current.next; return false; The remove operation How do we remove an element from the hash table? Cases to consider: front (24), non-front (), not found (94), null (32) To remove a node from a linked list, you must either change the list's front reference, or the next field of the previous node in the list. set.remove(54); current Implementing remove public void remove(int ) { int h = hash(); if (elements[h]!= null && elements[h].data == ) { elements[h] = elements[h].next; // front case size--; else { Node current = elements[h]; // non-front case while (current!= null && current.next!= null) { if (current.next.data == ) { current.next = current.next.next; size--; return; current = current.next; 35 Rehashing with chaining Separate chaining handles rehashing similarly to linear probing. Loop over the list in each hash bucket; re-add each element
10 Hash set of objects public class HashSet<E> implements Set<E> { private class Node { public E data; public Node next; It is easy to hash an integer i (use index abs(i) % length ). How can we hash other types of s (such as objects)? The hashcode method in Java All Java objects contain the following method (in Object): public int hashcode(); Returns an integer hash code for this object. We can call hashcode on any object to find its preferred index. HashSet, HashMap, and the other built-in "hash" collections call hashcode internally on their elements to store the data. We can modify our set s hash function to be the following: private int hash(e e) { return Math.abs(e.hashCode()) % elements.length; Hash tables in Java HashTable class stores key/ pairs does not allow null for either key or older, slower class (thread-safe, synchronized) HashSet class implements Set interface, internal storage container that is a hash table fast (unsynchronized) cf. TreeSet class, internal storage container is a Red Black Tree HashMap class Implements Map interface, internal storage container for keys is a hash table allows null for key or fast (unsynchronized) Maps Also known as: table, search table, dictionary, associative array, or associative container A data structure optimized for a very specific kind of search / access with a bag we access by asking "is X present" with a list we access by asking "give me item number X" with a queue we access by asking "give me the item that has been in the collection the longest." In a map we access by asking "give me the associated with this key." cf. TreeMap class
11 Keys and s Dictionary analogy: The key in a dictionary is a word: foo The in a dictionary is the definition: First on the standard list of metasyntactic variables used in syntax examples A key and its associated form a pair that is stored in a map To retrieve a the key for that must be supplied A List can be viewed as a Map with integer keys (indices) Keys must be unique, meaning a given key can only represent one but one may be represented by multiple keys Implementing a HashMap A hash map is like a set where the nodes store key/ pairs: public class HashMap<K, V> implements Map<K, V> { // key map.put("marty", ); map.put("jeff", 21); map.put("kasey", 20); map.put("stef", 35); "Stef" 35 "Marty" Must modify your Node class to store a key and a "Jeff" 21 "Kasey" Map ADT interface Let s think about how to write our own implementation of a map. As is (usually) done in the Java Collections Framework, we will define map as an ADT by creating a Map interface. Core operations: put (add), get, containskey, remove public interface Map<K, V> { void clear(); boolean containskey(k key); V get(k key); boolean isempty(); void put(k key, V ); void remove(k key); int size(); 43 HashMap vs. HashSet The hashing is always done on the keys, not the s. The contains method is now containskey; and in remove, you search for a node whose key matches a given key. The add method is now put; if the given key is already there, you must replace its old with the new one. map.put("bill", 66); // replace 49 with 66 "Stef" 35 "Marty" "Abby" 57 "Bill" "Jeff" 21 "Kasey"
12 Java s TreeMap Uses a Red - Black tree to implement a Map relies on the compareto method of the keys slower than HashMap keys stored in sorted order (cf. Are keys in HashMap in sorted order?) Sample map problem Determine the frequency of words in a file. File f = new File(fileName); Scanner s = new Scanner(f); Map<String,Integer> counts = new HashMap<String,Integer>(); while(s.hasnext()){ String word = s.next(); if (!counts.containskey(word)) counts.put(word, 1); else counts.put(word, counts.get(word) + 1); Implementing hashcode You can write your own hashcode methods in classes you write. All classes come with a default version based on memory address. Your overridden version should somehow "add up" the object's state. Often you scale/multiply parts of the result to distribute the results. public class Point { private int x; private int y; public int hashcode() { // better than just returning (x + y); // spreads out numbers, fewer collisions return 137 * x + 23 * y; Good hashcode behavior A well-written hashcode method should behave: Consistently with itself (must produce same results on each call): o.hashcode() == o.hashcode(), if o's state doesn't change Consistently with equality: a.equals(b) must imply a.hashcode() == b.hashcode(),!a.equals(b) does NOT necessarily imply that a.hashcode()!= b.hashcode() (why not?) When a class has an equals or hashcode, it should have both. Good distribution of hash codes: For a large set of objects with distinct states, they will generally return unique hash codes rather than all colliding into the same hash bucket
13 Example: String hashcode hashcode tricks The hashcode function inside a String class looks like this: public int hashcode() { int hash = 0; for (int i = 0; i < this.length(); i++) { hash = 31 * hash + this.charat(i); return hash; As with any general hashing function, collisions are possible. Example: "Ea" and "FB" have the same hash. Early versions of Java examined only the first 16 characters. For some common data this led to poor hash table performance. 49 If one of your object s fields is an object, call its hashcode: public int hashcode() { // Student return 531 * firstname.hashcode() + ; To incorporate a double or boolean, use the hashcode method from the Double or Boolean wrapper classes: public int hashcode() { // BankAccount return 37 * Double.Of(balance).hashCode() + Boolean.Of(isCheckingAccount).hashCode(); Guava includes an Objects.hashCode() method that takes any number of s and combines them into one hash code. public int hashcode() { // BankAccount return Objects.hashCode(name, id, balance); 50 Hash tables vs. BST vs. heaps on search Example: using hash tables BSTs: has complete ordering information See UseHashSet.java, Student.java, StudentReader.java Heaps: has incomplete ordering information See UseHashMap.java Hash tables: has no order information See Hash.java
14 Example: implementing hash tables Next topic Using java.util.linkedlist as a chain in each bucket See MyHashSet.java Graphs See MyHashMap.java 53 54
Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am
Announcements Today s topic: Hashing (Ch. 10) Next topic: Graphs Break around 11:45am 1 Container structures so far Array lists O(1) access O(n) insertion/deletion (average case), better at end Linked
More informationCSE 143. Lecture 28: Hashing
CSE 143 Lecture 28: Hashing SearchTree as a set We implemented a class SearchTree to store a BST of ints: Our BST is essentially a set of integers. Operations we support: add contains remove... -3 overallroot
More informationBuilding Java Programs
Building Java Programs Generics, hashing reading: 18.1 2 3 Wednesday Notecards Difference between int[] and List? How does HashSet get sorted? 4 Hashing hash: To map a value to an integer index.
More informationReview. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )
Review CSE 143 Java Hashing Want to implement Sets of objects Want fast contains( ), add( ) One strategy: a sorted list OK contains( ): use binary search Slow add( ): have to maintain list in sorted order
More informationHash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications
hashing -- idea collision resolution Hash tables closed addressing (chaining) open addressing techniques hash function Java hashcode() for HashMap and HashSet big-o time bounds applications Hash tables
More informationEXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 2 COMP103 INTRODUCTION
More informationHash 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 informationHash table basics mod 83 ate. ate
Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate hashcode() 82 83 84 48594983 mod 83 ate 1. Section 2: 15+ min
More informationCSC 321: Data Structures. Fall 2016
CSC : Data Structures Fall 6 Hash tables HashSet & HashMap hash table, hash function collisions Ø linear probing, lazy deletion, primary clustering Ø quadratic probing, rehashing Ø chaining HashSet & HashMap
More informationIntroducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved
Introducing Hashing Chapter 21 Contents What Is Hashing? Hash Functions Computing Hash Codes Compressing a Hash Code into an Index for the Hash Table A demo of hashing (after) ARRAY insert hash index =
More informationHash table basics. ate à. à à mod à 83
Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate à hashcode() à 48594983à mod à 83 82 83 ate 84 } EditorTrees
More informationData Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada
CS102 Hash Tables Prof. Tejada 1 Vectors, Linked Lists, Stack, Queues, Deques Can t provide fast insertion/removal and fast lookup at the same time The Limitations of Data Structure Binary Search Trees,
More informationIntroduction hashing: a technique used for storing and retrieving information as quickly as possible.
Lecture IX: Hashing Introduction hashing: a technique used for storing and retrieving information as quickly as possible. used to perform optimal searches and is useful in implementing symbol tables. Why
More informationcsci 210: Data Structures Maps and Hash Tables
csci 210: Data Structures Maps and Hash Tables Summary Topics the Map ADT Map vs Dictionary implementation of Map: hash tables READING: GT textbook chapter 9.1 and 9.2 Map ADT A Map is an abstract data
More informationHash table basics mod 83 ate. ate
Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate hashcode() 82 83 84 48594983 mod 83 ate Topics: weeks 1-6 Reading,
More informationLecture 10: Introduction to Hash Tables
Lecture 10: Introduction to Hash Tables CSE 373: Data Structures and Algorithms CSE 373 19 WI - KASEY CHAMPION 1 Warm Up CSE 373 SP 18 - KASEY CHAMPION 2 Administrivia HW 2 part 1 grades went out HW 2
More informationEXAMINATIONS 2016 TRIMESTER 2
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2016 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
More informationHash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1
Hash Open Indexing Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up Consider a StringDictionary using separate chaining with an internal capacity of 10. Assume our buckets are implemented
More informationEXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction
More informationStandard ADTs. Lecture 19 CS2110 Summer 2009
Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2
More informationCSC 321: Data Structures. Fall 2017
CSC : Data Structures Fall 7 Hash tables HashSet & HashMap hash table, hash function collisions Ø linear probing, lazy deletion, clustering, rehashing Ø chaining Java hashcode method HW6: finite state
More informationImplementing Hash and AVL
Implementing Hash and AVL Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up CSE 373 SP 18 - KASEY CHAMPION 2 Announcements 1. Go look at your HW 1 scores, seems a lot are missing
More informationEXAMINATIONS 2017 TRIMESTER 2
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2017 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
More informationAbstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework
Announcements Abstract data types (again) PS 5 ready Tutoring schedule updated with more hours Today s topic: The Java Collections Framework Reading: Section 7.5 An ADT is a model of a collection of data
More informationAnnouncements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)
HASHING CS2110 Announcements 2 Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!) Ideal Data Structure 3 Data Structure add(val x) get(int i) contains(val x) ArrayList 2 1 3 0!(#)!(1)!(#)
More informationSOLUTIONS. COMP103 Introduction to Data Structures and Algorithms
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 MID YEAR COMP103 Introduction to Data
More informationEXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 MID YEAR COMP103 Introduction to Data
More informationMapping Structures. Chapter An Example: Language Dictionaries
Chapter 11 Mapping Structures Some data exhibit a sequential relationship between elements. Other data exhibit a hierarchical relationship between elements. And yet, some data exhibit a mapping relationship
More informationLecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40
Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table
More informationTable ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum
Table ADT and Sorting Algorithm topics continuing (or reviewing?) CS 24 curriculum A table ADT (a.k.a. Dictionary, Map) Table public interface: // Put information in the table, and a unique key to identify
More informationHashing as a Dictionary Implementation
Hashing as a Dictionary Implementation Chapter 22 Contents The Efficiency of Hashing The Load Factor The Cost of Open Addressing The Cost of Separate Chaining Rehashing Comparing Schemes for Collision
More information1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation
1.00 Lecture 32 Hashing Reading for next time: Big Java 18.1-18.3 Motivation Can we search in better than O( lg n ) time, which is what a binary search tree provides? For example, the operation of a computer
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 37 April 22, 2016 Encapsulation & Hashing How is the Game Project going so far? 1. not started 2. got an idea 3. submitted design proposal 4. started
More informationHash Tables. Gunnar Gotshalks. Maps 1
Hash Tables Maps 1 Definition A hash table has the following components» An array called a table of size N» A mathematical function called a hash function that maps keys to valid array indices hash_function:
More informationData Structures and Object-Oriented Design VIII. Spring 2014 Carola Wenk
Data Structures and Object-Oriented Design VIII Spring 2014 Carola Wenk Collections and Maps The Collection interface is for storage and access, while a Map interface is geared towards associating keys
More informationModel Solutions. COMP 103: Test May, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes
More informationJava HashMap Interview Questions
Java HashMap Interview Questions codespaghetti.com/java-hashmap-interview-questions/ HashMap Java HashMap Interview Questions, Algorithms and Programs. Table of Contents: CHAPTER 1: Java HashMap Interview
More informationTopic 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 information11/27/12. CS202 Fall 2012 Lecture 11/15. Hashing. What: WiCS CS Courses: Inside Scoop When: Monday, Nov 19th from 5-7pm Where: SEO 1000
What: WiCS CS Courses: Inside Scoop When: Monday, Nov 19th from -pm Where: SEO 1 Having trouble figuring out what classes to take next semester? Wish you had information on what CS course to take when?
More informationHashTable 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 informationAnnouncements. Hash Functions. Hash Functions 4/17/18 HASHING
Announcements Submit Prelim conflicts by tomorrow night A7 Due FRIDAY A8 will be released on Thursday HASHING CS110 Spring 018 Hash Functions Hash Functions 1 0 4 1 Requirements: 1) deterministic ) return
More informationCSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 21 (Based on Paul Kube course materials) CSE 100 Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table cost
More informationLecture 18. Collision Resolution
Lecture 18 Collision Resolution Introduction In this lesson we will discuss several collision resolution strategies. The key thing in hashing is to find an easy to compute hash function. However, collisions
More information27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection
Objectives Collection Collections Framework Concrete collections Collection algorithm By Võ Văn Hải Faculty of Information Technologies Summer 2012 Legacy collection 1 2 2/27 Collections Framework "Collection"
More informationFamily Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Model Solutions COMP 103: Test
More informationLinked lists (6.5, 16)
Linked lists (6.5, 16) Linked lists Inserting and removing elements in the middle of a dynamic array takes O(n) time (though inserting at the end takes O(1) time) (and you can also delete from the middle
More informationTopic 10: The Java Collections Framework (and Iterators)
Topic 10: The Java Collections Framework (and Iterators) A set of interfaces and classes to help manage collections of data. Why study the Collections Framework? very useful in many different kinds of
More informationThe dictionary problem
6 Hashing The dictionary problem Different approaches to the dictionary problem: previously: Structuring the set of currently stored keys: lists, trees, graphs,... structuring the complete universe of
More informationCIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point
1 CIT-590 Final Exam Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point Instructions: You will have two hours to complete this exam. If you finish in the last 15 minutes,
More informationPriority Queue Sorting
Priority Queue Sorting We can use a priority queue to sort a list of comparable elements 1. Insert the elements one by one with a series of insert operations 2. Remove the elements in sorted order with
More informationCS 310: Maps and Sets and Trees
CS 310: Maps and Sets and Trees Chris Kauman Week 8-2 Logistics Oce hours Wednesday normal: 4:30-5:30pm Thursday 2-3pm special oce hours Reading Weiss Ch. 6 (Java Library Classes/Interfaces) 6.7 Sets,
More informationCS2110: Software Development Methods. Maps and Sets in Java
CS2110: Software Development Methods Maps and Sets in Java These slides are to help with the lab, Finding Your Way with Maps This lab uses Maps, and Sets too (but just a little). Readings from textbook:
More informationEXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 Trimester 1, MID-TERM TEST COMP103 Introduction
More information11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures
Collections CSE 143 Java Collections Most programs need to store and access collections of data Collections are worth studying because... They are widely useful in programming They provide examples of
More informationDepth-wise Hashing with Deep Hashing Structures. A two dimensional representation of a Deep Table
Proceedings of Student Research Day, CSIS, Pace University, May 9th, 2003 Depth-wise Hashing with Deep Hashing Structures Edward Capriolo Abstract The objective of this research is to implement a variation
More informationCOMP 103 RECAP-TODAY. Hashing: collisions. Collisions: open hashing/buckets/chaining. Dealing with Collisions: Two approaches
COMP 103 2017-T1 Lecture 31 Hashing: collisions Marcus Frean, Lindsay Groves, Peter Andreae and Thomas Kuehne, VUW Lindsay Groves School of Engineering and Computer Science, Victoria University of Wellington
More informationCOM1020/COM6101: Further Java Programming
(1/19) COM1020/COM6101: Further Java Programming AKA: Object-Oriented Programming, Advanced Java Programming http://www.dcs.shef.ac.uk/ sjr/com1020/ Lecture 7: Collections Accessed by Content Steve Renals
More informationData Structures And Algorithms
Data Structures And Algorithms Hashing Eng. Anis Nazer First Semester 2017-2018 Searching Search: find if a key exists in a given set Searching algorithms: linear (sequential) search binary search Search
More informationFall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up
CSM B Heaps & Hashing Fall 0 Mentoring : October 3, 0 Min-Heapify This. In general, there are 4 ways to heapify. Which ways actually work? Level order, bubbling up Level order, bubbling down Reverse level
More informationHASH TABLES. Hash Tables Page 1
HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric
More informationLecture 13: AVL Trees and Binary Heaps
Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1. ( ) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally
More informationCSE373 Fall 2013, Second Midterm Examination November 15, 2013
CSE373 Fall 2013, Second Midterm Examination November 15, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please
More informationLinked Lists. References and objects
Linked Lists slides created by Marty Stepp http://www.cs.washington.edu/143/ Modified by Sarah Heckman Reading: RS Chapter 16 References and objects In Java, objects and arrays use reference semantics.
More informationHash Tables. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Data Dictionary Revisited
Unit 9, Part 4 Hash Tables Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Data Dictionary Revisited We've considered several data structures that allow us to store and search for data
More informationEXAMINATIONS 2010 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2010 END YEAR COMP103 Introduction to Data
More informationCS 310: Maps and Sets
CS 310: Maps and Sets Chris Kauffman Week 9-1 Logistics Goals Today HW2 Discussion Maps and Sets HW2 Discussion Milestones due Thu 7/6 Discuss AdditiveList Iterator Implementation O(1) Undo/Redo Reading
More informationHashing Techniques. Material based on slides by George Bebis
Hashing Techniques Material based on slides by George Bebis https://www.cse.unr.edu/~bebis/cs477/lect/hashing.ppt The Search Problem Find items with keys matching a given search key Given an array A, containing
More information1. [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 informationHash Tables. Hashing Probing Separate Chaining Hash Function
Hash Tables Hashing Probing Separate Chaining Hash Function Introduction In Chapter 4 we saw: linear search O( n ) binary search O( log n ) Can we improve the search operation to achieve better than O(
More informationCSE 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 informationCSE 143 Lecture 14 AnagramSolver and Hashing
CSE 143 Lecture 14 AnagramSolver and Hashing slides created by Ethan Apter http://www.cs.washington.edu/143/ Ada Lovelace (1815-1852) Ada Lovelace is considered the first computer programmer for her work
More informationCS 3410 Ch 20 Hash Tables
CS 341 Ch 2 Hash Tables Sections 2.1-2.7 Pages 773-82 2.1 Basic Ideas 1. A hash table is a data structure that supports insert, remove, and find in constant time, but there is no order to the items stored.
More informationCollections, Maps and Generics
Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.
More informationMore on Hashing: Collisions. See Chapter 20 of the text.
More on Hashing: Collisions See Chapter 20 of the text. Collisions Let's do an example -- add some people to a hash table of size 7. Name h = hash(name) h%7 Ben 66667 6 Bob 66965 3 Steven -1808493797-5
More informationCMSC 132: Object-Oriented Programming II. Hash Tables
CMSC 132: Object-Oriented Programming II Hash Tables CMSC 132 Summer 2017 1 Key Value Map Red Black Tree: O(Log n) BST: O(n) 2-3-4 Tree: O(log n) Can we do better? CMSC 132 Summer 2017 2 Hash Tables a
More informationCSC263 Week 5. Larry Zhang.
CSC263 Week 5 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements PS3 marks out, class average 81.3% Assignment 1 due next week. Response to feedbacks -- tutorials We spent too much time on working
More informationHO #13 Fall 2015 Gary Chan. Hashing (N:12)
HO #13 Fall 2015 Gary Chan Hashing (N:12) Outline Motivation Hashing Algorithms and Improving the Hash Functions Collisions Strategies Open addressing and linear probing Separate chaining COMP2012H (Hashing)
More informationAnnouncements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am
Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps
More informationCOMP 103 Introduction to Data Structures and Algorithms
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:..................... EXAMINATIONS 2005 END-YEAR COMP 103 Introduction to Data
More informationCMSC 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 informationAbstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011
CS/ENGRD 110 Object-Oriented Programming and Data Structures Spring 011 Thorsten Joachims Lecture 16: Standard ADTs Abstract Data Types (ADTs) A method for achieving abstraction for data structures and
More informationHash[ string key ] ==> integer value
Hashing 1 Overview Hash[ string key ] ==> integer value Hash Table Data Structure : Use-case To support insertion, deletion and search in average-case constant time Assumption: Order of elements irrelevant
More informationLecture 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 informationHASH TABLES. Goal is to store elements k,v at index i = h k
CH 9.2 : HASH TABLES 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND
More informationModel Solutions. COMP 103: Test April, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 40 minutes
More informationCSC 321: Data Structures. Fall 2016
CSC 321: Data Structures Fall 2016 Balanced and other trees balanced BSTs: AVL trees, red-black trees TreeSet & TreeMap implementations heaps priority queue implementation heap sort 1 Balancing trees recall:
More informationCSC 321: Data Structures. Fall 2017
CSC 321: Data Structures Fall 2017 Balanced and other trees balanced BSTs: AVL trees, red-black trees TreeSet & TreeMap implementations heaps priority queue implementation heap sort 1 Balancing trees recall:
More informationWITH SOLUTIONS!!! WARNING: THIS COPY CONTAINS SOLUTIONS!!! COMP103 Introduction to Data Structures and Algorithms
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2007 SUMMER TRIMESTER COMP103 Introduction
More informationU N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM Time Allowed: TWO HOURS ********
More informationHierarchical data structures. Announcements. Motivation for trees. Tree overview
Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps
More informationJava Collections Framework reloaded
Java Collections Framework reloaded October 1, 2004 Java Collections - 2004-10-01 p. 1/23 Outline Interfaces Implementations Ordering Java 1.5 Java Collections - 2004-10-01 p. 2/23 Components Interfaces:
More informationCSE 143. Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 7: Linked List Basics reading: 16.2 References vs. objects variable = value; a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box;
More informationDynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable
Hashing Dynamic Dictionaries Operations: create insert find remove max/ min write out in sorted order Only defined for object classes that are Comparable Hash tables Operations: create insert find remove
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 37 April 28, 2014 TreeSet and HashSet Announcements Game project due Wednesday at midnight (HARD deadline) Check your grades online, should be up-
More informationCS61B Spring 2016 Guerrilla Section 6 Worksheet
Spring 2016 5 May 2016 Directions: In groups of 4-5, work on the following exercises. Do not proceed to the next exercise until everyone in your group has the answer and understands why the answer is what
More information(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?
CSM B Hashing & Heaps Spring 0 Week 0: March 0, 0 Motivation. (a) In the worst case, how long does it take to index into a linked list? Θ(N) (b) In the worst case, how long does it take to index into an
More informationHEAPS & PRIORITY QUEUES
HEAPS & PRIORITY QUEUES Lecture 13 CS2110 Spring 2018 Announcements 2 A4 goes out today! Prelim 1: regrades are open a few rubrics have changed No Recitations next week (Fall Break Mon & Tue) We ll spend
More informationModel Solutions. COMP 103: Mid-term Test. 19th of August, 2016
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 11 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 9-10... Hash tables ADT Stack ADT Queue ADT Deque ADT Priority Queue Hash tables Today Hash tables 1 Hash
More informationmith College Computer Science Sets and Hashing CSC212 Fall 2014 Dominique Thiébaut
mith College Computer Science Sets and Hashing CSC22 Fall 204 Dominique Thiébaut dthiebaut@smith.edu The Problem: Large amount of live tweets Want the list of all tweeters Each only listed once What data
More information