Queues and Stacks.  Can receive multiple requests from multiple sources ◦ How do we services these requests?  First come, first serve processing 

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
CSCE 3400 Data Structures & Algorithm Analysis
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
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.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Comp 335 File Structures Hashing.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
Data Structures and Algorithms Lecture (Searching) Instructor: Quratulain Date: 4 and 8 December, 2009 Faculty of Computer Science, IBA.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
1 Chapter 7 Skip Lists and Hashing Part 2: Hashing.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Hashing by Rafael Jaffarove CS157b. Motivation  Fast data access  Search  Insertion  Deletion  Ideal seek time is O(1)
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Data Structures Using C++ 2E
CSCI 210 Data Structures and Algorithms
Slides by Steve Armstrong LeTourneau University Longview, TX
Data Structures Using C++ 2E
CS313D: Advanced Programming Language
Hash Tables.
Chapter 21 Hashing: Implementing Dictionaries and Sets
CSCE 3110 Data Structures & Algorithm Analysis
CS202 - Fundamental Structures of Computer Science II
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Collision Handling Collisions occur when different elements are mapped to the same cell.
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Queues and Stacks

 Can receive multiple requests from multiple sources ◦ How do we services these requests?  First come, first serve processing  Priority based processing ◦ Buffering of requests, as they might arrive faster than they can be processed  You could always use a List structure, with an integer value associated with the item, and then append it to the List using the Add() method ◦ Inefficient

 Two jobs added to List  Job 1 is processed and slot becomes available  Job 3 grabs first available slot, and Job 4 gets the next available slot  nextJobPost keeps track of the “next” job to be processed in the List

 List will continue to grow, even if jobs are processed right away ◦ The default is to double the size, when the list requires additional “slots”  No reclaiming of the already used slots is done with Lists  If you do reclaim the “used” slots in the List, then your first-come, first-serve processing scheme will not work  A List represents a linear order

 When adding an item, once the last item is used, the “next” will “wrap around” to the 0 th item in the array/list ◦ A “modulus” function is used to “wrap around”  What happens if all items are filled, and you still need another item? ◦ Resize the circular array…!  This is done in the Queue class

 Add / remove buffer items ◦ First-come, first-serve (FIFO) ◦ Manage space utilization ◦ Uses Generics  Type-safe  Methods ◦ Enqueue()  Adds elements at the “tail” index  If not enough space, default growth factor of 2.0 is used to resize  Class constructor can specify other growth factor ◦ Dequeue()  Returns the current element from the “head” index  Sets the “head” element to null and increments the “head” index ◦ Peek()  Allows you to see the head element, without a dequeue, or increasing the head index counter ◦ Contains()  Determine if a specific item exists in the Queue ◦ ToArray()  Returns an array containing the Queue’s elements

 LIFO structure  Uses a circular array, as does the Queue  Methods ◦ Push()  Adds an item to the stack ◦ Pop()  Removes and returns the item on the “top” of the stack  Size is increased, as required (same as the Queue’s growth factor)  Call Stack as used by the CLR is an example of this structure ◦ When calling a function, Push its information onto the stack ◦ When returning from that routine, Pop it from the stack and expose the routine to which it returns control

 Problem: We often don’t know the “position” of an element within an array ◦ Potentially we process all elements before finding the one we need  Reduce the O-time to O(1) ◦ Build an array capable of holding all SS#’s ◦ Each element would hold a record based on the SS# as a “key” ◦ Waste  10 9 possible values, but you only have 1,000 employees  Utilization would be % of the array  Hashing allows us to “compress” this ordinal indexing

 Use the last 4 digits (or 3, or 5) of the SS# ◦ Mathematical transformation (mapping) of a nine- digit value to a four-digit value ◦ Array ranges from 0000 to 9999  Constant lookup time (O-time)  Better utilization of space  Hash table ◦ Array which uses hashing to compress the indexers  Hash function ◦ Function which performs the hashing

 H(x) = last four digits of x  Collisions ◦ When multiple inputs to a hash function result in identical outputs  10 5 collisions for SS#’s ending in “0000” ◦ Collision of hash value results in attempting to store into a “slot” already occupied by a prior hash result

 Collision frequency is directly correlated to the hash function ◦ SS# assumes that the last four digits are uniformly distributed  If year of birth, or geographical location alters the distribution  Increases collisions ◦ Collision avoidance is the selection of an appropriate hashing algorithm ◦ Collision resolution is locating another slot in the hashtable for entry placement

 Linear probing ◦ If collision in slot i occurs, proceed to the next available slot (i+1), theni+2 and so on, if required  Alice = 1234, Bob=1234, Cal=1237, Danny=1235, Edward=1235  Insert Alice  Insert Bob  Insert Cal  Insert Danny  Insert Edward

 Searching ◦ Start at the hash location, and then perform a linear search from there until the value is located  When/if you reach an empty slot your search value is NOT in that hashtable  Linear probing not very good resolution ◦ Leads to clustering of values  Ideally you’d like a uniform distribution of values  Quadratic probing ◦ Slot s is taken  Probe s+1 2, then s-1 2, then s+2 2, then s-2 2, and so on…  Can still lead to clustering

 Rehashing ◦ Used by the.NET Framework Hashtable class ◦ Adding an item to the table  Provide item and unique key to access the item  Item and key can be of any type ◦ Retrieving item  Index the Hashtable by key

//Note the use of the ContainsKey() Method, which returns a Boolean using System; using System.Collections; public class HashtableDemo { private static Hashtable employees = new Hashtable(); public static void Main() { // Add some values to the Hashtable, indexed by a string key employees.Add(" ", "Scott"); employees.Add(" ", "Sam"); employees.Add(" ", "Jisun"); // Access a particular key if (employees.ContainsKey(" ")) { string empName = (string) employees[" "]; Console.WriteLine("Employee 's name is: " + empName); } else Console.WriteLine("Employee is not in the hash table..."); }

// Step through all items in the Hashtable foreach(string key in employees.Keys) Console.WriteLine("Value at employees[\"" + key + "\"] = " + employees[key].ToString());  The order of insertion and order of keys are not necessarily the same ◦ Depends on the slot the key was stored in  depends on the hash value of the key  Depends on the collision resolution used ◦ The output from the above code results in: Value at employees[" "] = Jisun Value at employees[" "] = Scott Value at employees[" "] = Sam

 Function returns an ordinal value ◦ Slot # for the key ◦ Function can accept a key of any type ◦ GetHashCode()  Any object can be represented as a unique number

 Rehashing (double hashing) ◦ Set of hash functions H 1 … H n ◦ H 1 is initially used  If collision, then H 2 is used, and so on  They differ by multiplicative factors ◦ Each slot in the hash table is visited exactly once when hashsize number of probes are made  For a given key, H i and H j cannot hash to the same slot in the table  This can work if the results of (1 + (((GetHash(key) >> 5) + 1) % (hashsize – 1)) and hashsize are “relatively prime”  They share no common factors  Guaranteed to be prime if hashsize is a prime number ◦ Better collision avoidance than linear or quadratic probing

 Hashtable class ◦ Property: loadFactor  Max ratio of items in the Hash to the total slots in the table  0.5at most, half the slots can be used, and the other half must remain empty  Values range from 0.1 to 1.0  Microsoft has a default “scaling factor” of 72%  If you pass 1.0 to the loadFactor property, it’s still only 0.72 behind the scenes  Performance issue

 Hashtable class ◦ Add() method  Performs a check against the loadFactor  If exceeded, the Hashtable is expanded ◦ Expansion  Slot count is approximately doubled  From the current prime number to the next largest prime number value  Hash value depends on the number of total slots  All values in the table need to be rehashed when the table expands  Occurs behind the scenes during Add() method

 loadFactor ◦ Affects the size of the hash table and number of probes required on a collision  High load factor  Denser hash table, but more collisions  Expected number of probes needed when a collision happens  1/(1-loadFactor)  Default 0.72 loadFactor results in 3.5 probes per collision on average  Does not vary based on number of items in the table  Asymptotic access time is O(1)  Much more desirable that the O(n) search time for an array

 Hashtable is “loosely-typed” structure ◦ Developer can add keys and values of any type to the table  Generics allow us to have type-safe implementations of a class  Dictionary class is a “type-safe” class ◦ Types the keys and the values ◦ You must specify the types for keys/values when creating the Dictionary instance ◦ Once created, you can add and remove items, just like the Hashtable

 Collision resolution ◦ Different from the Hashtable ◦ Chaining is used  Secondary data structure is used for the collisions ◦ Each slot in the Dictionary contain an array of elements  A collision prepends the element to the bucket’s list

 8 buckets (example) ◦ Employee object is added to the bucket that its key hashes to  If already occupied, item is prepended  Searching and removing items from a chained hashtable ◦ Time proportional to total items and number of buckets  O(n/m)  n=total elements  m= total buckets ◦ Dictionary class implemented  n=m at all times