Download presentation
Presentation is loading. Please wait.
Published byEileen Mathews Modified over 9 years ago
1
CSC 212 Hash, Dictionaries, and Skip Lists
2
Announcements Homework #4 due Tuesday Attending assessment conf. at UB tomorrow (Drew the short straw; New faculty loses again…) No office hours Will lack Internet access Saturday & Sunday Better look over homework now! Homework #5 will be released Monday Related to homework #4 Hope you all like mushrooms…
3
Announcements Posted grades in Blackboard gradebook Looking for easier way of returning grades Lets you see your current grade Let me know if there are any questions Significant portion of semester remains to raise/lower grades DO NOT use the Blackboard dropbox
4
Announcements Deficiency reports are due to Canisius Will be sent to any student with a D or lower Has little bearing on final grades Students receiving a DF could still get good grade Students NOT receiving a DF could still fail At most, take it as a sign that you should talk to me No one should either get discouraged or rest on their laurels as a result
5
Final Hashing of Hash When “properly” configured hash tables provide O(1) insertion, retrieval, and removal Proper is hard concept to implement Time is asymptotically optimal When improperly configured O(n) time Other methods of implementing Map can do better Space costs are always bounded
6
Warmed over Hash Good hash function is very difficult to write Needs to spread data evenly and randomly across range of hash table entries Best functions tend to be domain specific E.g., written for an exact set of keys Luckily, moderately good functions freely available As are many other configuration rules of thumb
7
Dictionary ADT Dictionary ADT like Map ADT Both store data for later searches & retrievals But, Dictionary allows multiple values to share a similar key value Two definitions of Dictionary ADT Ordered Dictionary ADT Unordered Dictionary ADT
8
Map Limitations Unique keys useful for some applications Protein/DNA sequence pairings ID Field/record in database table Often multiple keys have same value Real-world car & house keys Tracking word and its definitions Credit card authorizations Hostname/IP address pairs Reaction/chemical reagent pairs
9
Map v. Dictionary Functions FunctionalityMapDictionary Insertionput(k, v)insert(k, v) Removalremove(k)remove(e) Searchingget(k)find(k) findAll(k) Informationalsize() isEmpty() size() isEmpty() Differ only in Searching & Removal
10
Dictionary ADT Dictionary’s removal method differs from map only in what you specify to remove Map’s remove took key as parameter Dictionary’s remove(e) removes the entry e Dictionary defines two search methods find(k) returns an arbitrary entry with key k findAll(k) returns Iterator of entries with key k
11
Iterator ADT Iterator is ADT which enables one to step through collection of data Iterator ADT defines only two methods boolean hasNext() – returns if there is another element Object next() – returns the next element in the collection (the ordering for “next” is not specified)
12
Iterator ADT Main idea behind iterator is you have simple pattern to step through objects in for loop iter = dict.findAll(k); for (Object o = iter.next(); iter.hasNext(); o = iter.next()) {... }
13
Using Unordered Dictionary Most common use of unordered dictionary is in log file or audit trail Record all “interesting” events as they happen May later review log, if need arises Search for security violations Track cause of mysterious crash Try to recover files that were open at time of error Common thread: Often add data, but (we hope) rarely use it
14
Implementing a Log File Want fast insertion, accept slower search With what ADT should we implement this?
15
Another scenario You are writing a new search engine Want fast searches, rarely insert new items With what ADT could we use?
16
Slow Add, Fast Search Ordered vector-based implementation can give O(log n) search times Assumes vector has O(1) access time Ordered vector requires O(n) insertion times Why? 13457 8 911141618190
17
Binary Search First divide-and-conquer algorithm this semester Common class of algorithms that recur Get their name by pattern of repeatedly dividing data and then process each part separately Oh, boy! Describes a recursive process! As we will see, run in O(log n) time Major reason for the algorithms’ popularity
18
Binary Search Suppose we are searching for 7 Take average of the low and high indices l = 0 m = (l + h) / 2 = (0 + 12) / 2 = 6 h = 12 13457 8 911141618190 lh
19
Binary Search Suppose we are searching for 7 Take average of the low and high indices l = 0 m = (l + h) / 2 = (0 + 12) / 2 = 6 h = 12 13457 8 911141618190 lhm
20
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 13457 8 911141618190 lhm
21
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 1 3 4 57 8 911141618190 lhm
22
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 13 4 57 8 911141618190 lhm
23
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 13 4 57 8 911141618190 lh
24
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 13 4 5 7 8 911141618190 lhm
25
Binary Search Suppose we are searching for 7 Take average of the low and high indices If m has value searching for, return it Else if m is too big, set h to m - 1 Else ( m is too small), set l to m +1 If l > h, value is not present 13 45 7 8 911141618190 l h m
26
See the Tree s, not the Forest Number of possible elements per phase: 1. 0 – 1213 2. 0 – 56 3. 3 – 53 4. 5 – 51 What pattern does this resemble? Why is this search time O(log n)?
27
Ordered Dictionary ADT Sometimes order of the keys is important May need to more than if a key is in the dictionary Playing “Price Is Right” and want closest key without going over Can be better off wrong with approximate answer than wrong with no answer Ordered Dictionary ADT can then be used
28
Ordered Dictionary ADT Adds two methods to Dictionary ADT: closestKeyBefore(k) – if entry with key k exists, return it; otherwise return entry with next lowest key. If not key is lower than k, signal an error closestKeyAfter(k) – like closest key before, but returns entry with next highest key Note that this differs slightly from books definition of Ordered Dictionary Differs only in whether iterators used
29
Skip Lists Concrete data type for ordered dictionaries Needs specific 2-d implementation structure Makes use of random number generator Randomization improves expected (average case) time, but not worst case time Makes for fun, non-deterministic behavior Expected O(log n) time Average case holds for search, insertion, & removal time
30
Skip Lists Consists of series of lists {S 0, S 1,..., S h } Each stores ordered subset of entries S 0 includes all entries (and +∞ and -∞) S i contains a (random) subset of S i-1, h > i > 0 S h contains only +∞ and -∞
31
Example Skip List 15 -∞ +∞ 15 27 48 27 48 32 S0S0 S2S2 S1S1 S3S3 S4S4
32
Traversing Skip List Can move in two directions in a skip list Horizontally: across entries on a level Vertically: through towers of entries
33
Example Skip List 15 -∞ +∞ 15 27 48 27 48 32 S0S0 S2S2 S1S1 S3S3 S4S4 prev nextabove below
34
Searching in a Skip List Algorithm SkipSearch(k) // return entry with largest key <= k Entry e -∞ in S h // Top left entry in S while below(e) != null do e below(e); // drop down a level while key(after(e)) <= k do e after(e); // scan forward return e;
35
Skip List Search - Example S0S0 S2S2 S1S1 S3S3 S4S4 SkipSearch(40) 15 27 38 42 -∞-∞ -∞-∞ -∞-∞ -∞-∞ -∞-∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞
36
Insertion in a Skip List S Algorithm SkipInsert(k,e) Entry n SkipSearch(k); Entry j insertAfterAbove(n, null,(k,e)); // insert new item on bottom level while random() < ½ do while above(n) == null do n before(n); // scan backward n above(n); // jump up a level j insertAfterAbove(n,j,(k,e));
37
Skip List Search - Example S0S0 S2S2 S1S1 S3S3 S4S4 SkipInsert(57) 15 27 38 57 -∞-∞ -∞-∞ -∞-∞ -∞-∞ -∞-∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞
38
Skip List Search - Example S0S0 S2S2 S1S1 S3S3 S4S4 SkipRemove(38) 15 27 38 57 -∞-∞ -∞-∞ -∞-∞ -∞-∞ -∞-∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞ +∞+∞
39
Daily Quiz Write the SkipNode class that are used for the Nodes in a skip list Should at least include constructor, 5 fields, get & set methods Do not have to write javadoc comments Doing more is always appreciated Only code that compiles will be graded Please test your code before submission
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.