Ordered Maps & Dictionaries

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables1  
Advertisements

© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
9.3 The Dictionary ADT    … 01/25/98, Taipei, … 03/04/98, Yunlin, … 02/15/98, Douliou, … 01/20/98,
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Maps, Hash Tables and Dictionaries Chapter 10.1, 10.2, 10.3, 10.5.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University.
Hash Tables1   © 2010 Goodrich, Tamassia.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
© 2004 Goodrich, Tamassia Hash Tables1  
Map ADT by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Maps1 © 2010 Goodrich, Tamassia. Maps2  A map models a searchable collection of key-value entries  The main operations of a map are for searching, inserting,
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Maps 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
COMP9024: Data Structures and Algorithms
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Design & Analysis of Algorithm Map
Binary Search Trees < > =
Hash Tables 6/13/2018 Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Cuckoo Hashing.
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
COMP9024: Data Structures and Algorithms
Dictionaries < > = Dictionaries Dictionaries
Bucket-Sort and Radix-Sort
Array Lists, Node Lists & Sequences
Data Structures Maps and Hash.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Breadth-First Search (BFS)
Binary Search Trees < > = Binary Search Trees
Priority Queues 4/6/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Copyright © Aiman Hanna All rights reserved
Maps.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Copyright © Aiman Hanna All rights reserved
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Dictionaries < > = /17/2019 4:20 PM Dictionaries
CH 9 : Maps And Dictionary
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Maps 4/25/2019 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Dictionaries < > = Dictionaries Dictionaries
Dictionaries and Hash Tables
Presentation transcript:

Ordered Maps & Dictionaries Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Coverage Section 9.3: Ordered Maps & Binary Search Section 9.5: Dictionaries 1 3 4 5 7 8 9 11 14 16 18 19 m l h Ordered Maps & Dictionaries

Ordered Maps & Dictionaries In some applications, simply looking up values based on associated keys is not enough. The entries may need to be sorted in the map according in total order. An ordered map would have the usual operations that a map has, but also maintains an order relation for the keys. Keys are assumed to come from a total order; a comparator can be used to provide the needed order between the keys. Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Since the entries are sorted, some additional methods can efficiently be provided: firstEntry(): return the entry with smallest key, or null if the map is empty lastEntry(): return the entry with largest key, or null if the map is empty floorEntry(k): return the entry with the largest key  k ceilingEntry(k): return the entry with the smallest key  k These two operations also return null if the map is empty Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Binary Search Binary search can perform operations get, floorEntry and ceilingEntry on an ordered map implemented by means of an array-based sequence, sorted by key similar to the high-low game at each step, the number of candidate items is halved terminates after O(log n) steps Example: find(7) 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l m h 1 3 4 5 7 8 9 11 14 16 18 19 l=m =h Ordered Maps & Dictionaries

Performance of Ordered Maps We can store the items in an array-based sequence, sorted by key. Performance: get, floorEntry and ceilingEntry take O(log n) time, using binary search put takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item remove take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal Ordered maps are effective only for small size maps or for maps on which searches are the most common operations, while insertions and removals are rarely performed. Since it is ordered we can find out whether the entry is there by searching in logn, then n/2 to insert if it is not there. Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Dictionary ADT Like a map, a dictionary stores key-value entries. Similarly, a dictionary allows the keys and the values to be of any object types. However, in contrast to maps, where the keys must be unique, a dictionary allows for multiple entries to have the same keys. This is very much like an English dictionary, for instance, which allows for a multiple definitions for the same word. The main operations of a dictionary are searching, inserting, and deleting items. Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Dictionary ADT As an ADT, an (unordered) dictionary D supports the following methods: get(k): if the dictionary has an entry with key k, return it, else, return null. If more than one entry exists with key k then arbitrarily return one of them. getAll(k): return an iterable collection of all entries with key k. put(k, v): insert an entry with key k and value v and return that entry. remove(e): remove the entry e from the dictionary and return it; error occurs if e is not in the dictionary. entrySet(): return an iterable collection of the entries in the dictionary. size(), isEmpty() Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Example Operation Output Dictionary put(5,A) (5,A) (5,A) put(7,B) (7,B) (5,A),(7,B) put(2,C) (2,C) (5,A),(7,B),(2,C) put(8,D) (8,D) (5,A),(7,B),(2,C),(8,D) put(2,E) (2,E) (5,A),(7,B),(2,C),(8,D),(2,E) get(7) (7,B) (5,A),(7,B),(2,C),(8,D),(2,E) get(4) null (5,A),(7,B),(2,C),(8,D),(2,E) get(2) (2,C) (5,A),(7,B),(2,C),(8,D),(2,E) getAll(2) (2,C),(2,E) (5,A),(7,B),(2,C),(8,D),(2,E) size() 5 (5,A),(7,B),(2,C),(8,D),(2,E) remove(get(5)) (5,A) (7,B),(2,C),(8,D),(2,E) get(5) null (7,B),(2,C),(8,D),(2,E) Ordered Maps & Dictionaries

A List-Based Dictionary A log file or audit trail is a dictionary implemented by means of an unsorted sequence We store the items of the dictionary in a sequence (based on a doubly-linked list or array), in arbitrary order Performance: put takes O(1) time since we can insert the new item at the beginning or at the end of the sequence get and remove take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key The log file is effective only for dictionaries of small size or for dictionaries on which insertions are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation) Ordered Maps & Dictionaries

The getAll and put Algorithms Algorithm getAll(k) Create an initially-empty list L for e: D do if e.getKey() = k then L.addLast(e) return L Algorithm put(k,v) Create a new entry e = (k,v) S.addLast(e) {S is unordered} return e Ordered Maps & Dictionaries

Ordered Maps & Dictionaries The remove Algorithm Algorithm remove(e): { We don’t assume here that e stores its position in S } B = S.positions() while B.hasNext() do p = B.next() if p.element() = e then S.remove(p) return e return null {there is no entry e in D} Ordered Maps & Dictionaries

Hash Table Implementation We can also create a hash-table dictionary implementation. If we use separate chaining to handle collisions, then each operation can be delegated to a list-based dictionary stored at each hash table cell. In that case, the dictionary methods can be achieved in O(1). Ordered Maps & Dictionaries

Ordered Maps & Dictionaries Search Table A search table is a dictionary implemented by means of a sorted array We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys Performance: get takes O(log n) time, using binary search put takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item remove takes O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal A search table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed. Ordered Maps & Dictionaries