Queues Look!up tables Hash tables An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements. Maps When dequeuing, the element that has been in the queue for the longest time is removed. Lecture 11 Queues MasterMind Riley 7.6-7.8 + 8 Cormen 11.1, 11.2. 11.4 (not the analytical parts) Queue ADT 2 Queue Specification 3 4
An example Determining whether a string is a palindrome or not. A string is a palindrome if (and only if) it reads the same forwards and backwards. Focus on letters and digits; skip delimiters and blank space, and treat lower-case and upper-case as equals. Can be defined recursively: The empty string and a 1-character string is a palindrome. Longer strings are palindromes if the first and last characters are equal and the substring in-between the first and the last is a palindrome. Examples Ni talar bra latin. (that is, NITALARBRALATIN ) A Toyota!! ( ATOYOTA ) Won't lovers revolt now? Madam, I help Martin. I trample him --- Adam etc 5 6 Implementations of Queues A list with two iterators; one at the beginning and one at the end. Keep the list sorted at all time so that the element at the beginning has spent longest time in the queue. Dequeue at the beginning. Enqueue at the end. 7 8
Using Stacks Efficiency A queue can be implemented using two stacks. The stack solution consumes more time. However, the amount of work is still constant per element. Call them S1 and S2. Enqueue pushes elements on S1. Dequeue pops elements from S2. If S2 is empty, all elements in S1 are moved over to S2 one element at a time by popping them from S1 and pushing them onto S2. Then dequeue pops the top of S2 unless it is still empty, in which case an exception should be thrown. You can t dequeue something from an empty queue. A push onto S1, a pop from S1, a push onto S2, and a pop from S2. This assumes a reasonable implementation of the stacks. All operations take constant time. So, asymptotically, using a list or two stacks leads to the same time complexity: O(1) per element. A kind of analysis called amortized. 9 10 11 12 Bounded Queues Can be implemented with an array and in the form of a Ring Buffer.
Maps Also known as look-up tables functions A set of pairs {(keyi,valuei)} and methods to create, change, inspect and search such sets. A key occurs in at most one pair. Example: lookupvalue(k) returns a value v such that (k,v) is a pair in the look-up table (or it fails if there is no such pair). 13 14 16 Simple Implementations A list. An array. Arbitrary size Access via linear search Fixed size Instant access if consequtive keys from an ordered universe Easy update insert/delete could be a problem 15
Hashing Hash table implemented using an array. Good to use a size that is a prime number. The array contains buckets with references. A hash function h(x):key->index Should preferrably give different results when applied to different keys. Should be easy to compute. The pair (k,v) is stored in the bucket with index h(k). The hash function is used to scatter the pairs within the array. The load factor is the percentage of the buckets in use. 17 18 20 Issues How do we obtain a key? hashcode() for objects in Java. What about collisions!? What do we do when two pairs are hashed to the same bucket? Open hashing: Collect all pairs that fall into a bucket in a list (or some other data structure)! lookupvalue: First find the right bucket using the hash function. Then traverse the list. Closed hashing: Find an empty bucket somewhere else in the array! 19
Variants of closed hashing Linear probing: Start at the occupied bucket and move on through the array one step at a time (with wrap-around) until an empty bucket is found. Double hashing: Like linear probing but use a step length determined by another function. The step length and the size of the hash table (the array) should be relative primes (don t share any prime factors). 21 22 23 24
- Beware of bugs in the above code; I have only proved it correct, not tried it. Donald E. Knuth Professor Emeritus of The Art of Computer Programming (at Stanford) 25 26