

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of abstract data types (adt) and their implementation for data structures such as stacks, queues, priority queues, dictionaries, and hash maps. It also discusses data structure building blocks, overflow handling, and hash functions.
Typology: Lecture notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!


























method for achieving abstraction for data structures and algorithms
model
operations Describes what each operation does, but not how it does it An
is independent of its implementation In Java, an interface correspondswell to an ADT The interface describes theoperations, but says nothing at allabout how they are implemented Example: Stack interface/ADT public interface Stack { public void push(Object x); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); }
Operations: void insert(Object element);boolean contains(Object element);void remove(Object element);boolean isEmpty();void clear();for(Object o: mySet) { ... }
Wide use within other algorithms
“set” with duplicates is sometimes called a multiset or bag A set makes no promises about ordering, but you can still iterate over it.
Operations: void insert(Object key, Object value); void update(Object key, Object value); Object find(Object key); void remove(Object key); boolean isEmpty(); void clear();
Symbol tables Wide use within other algorithms A HashMap is a particular implementation of the Map interface
From interface to implementation
Given that we want to support some interface, the designer still faces a choice - What will be the best way to implement this interface for my expected type of use? - Choice of implementation can reflect many considerations - Major factors we think about - Speed for typical use case - Storage space required
class ArrayStack implements Stack { private Object[] array; //Array that holds the Stackprivate int index = 0; //First empty slot in Stackpublic ArrayStack(int maxSize) { array = new Object[maxSize]; } public void push(Object x) { array[index++] = x; }public Object pop() { return array[--index]; }public Object peek() { return array[index-1]; }public boolean isEmpty() { return index == 0; }public void clear() { index = 0; } } max- 3 2 1 0 4 index O(1) worst-case time foreachoperation
…. What if maxSize is too small?
Recall: operations are add, poll, peek,… For linked-list All operations are O(1) For array with head at A[0] poll takes time O(n) Other ops are O(1) Can overflow For array with wraparound All operations are O(1) Can overflow Linked List head last Array with wraparound head (can overflow) last Array with head always at A[0] (poll( ) becomes expensive) (can overflow) last
Goal: Design a Dictionary (aka Map )
Operations^ – void insert(key, value) - void update(key, value) - Object find(key) - void remove(key) - boolean isEmpty() - void clear() Array implementation: Using anarray of (key,value) pairs Unsorted Sorted insert O(1) O(n) update O(n) O(log n) find O(n) O(log n) remove O(n) O(n) n is the number of items currently heldin the dictionary
be easy to compute
avoid collisions - have roughly equal probability for each table position Typical situation:U = all legal identifiersTypical hash function:h converts each letter to a number, then compute a function of these numbers Best hash functions are highly random This is connected to cryptographyWe’ll return to this in a few minutes
Analysis for Hashing with Chaining
Analyzed in terms of load factor λ = n/m = (items in table)/(table size) - We count the expected number of probes (key comparisons) - Goal: Determine expected number of probes for an unsuccessful search Expected number of probes foran unsuccessful search =average number of items pertable position = n/m = λ Expected number of probes fora successful search = 1 + λ = O( λ ) Worst case is O(n)
We know each operation takes time O(
) where
=n/m
So it gets worse as n gets large relative to m - Table Doubling: - Set a bound for
(call it
0 )
Whenever
reaches this bound:
Create a new table twice as big - Then rehash all the data - As before, operations usually take time O(1) - But sometimes we copy the whole table
Analysis of Table Doubling, Cont’d
Total number of insert operations needed to reach current table
copying work
initial insertions of items = 2n
n
3n inserts
Each insert takes expected time
0
or
so total expected time to build entire table is O(n)
Thus, expected time per operation is
Disadvantages of tabledoubling: Worst-case insertion time of O(n)is definitely achieved (but rarely) Thus, not appropriate for timecritical operations
Input could be a big object like a string or an Animal or some other complex thing - Same input always gives same out - Idea is that hashCode for distinct objects will have a very low likelihood of collisions