Design & Analysis of Algorithm Map

Slides:



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

Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
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:
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hash Tables1   © 2010 Goodrich, Tamassia.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Hash Tables1  
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Maps Rem Collier Room A1.02 School of Computer Science and Informatics
Sets and Maps Chapter 9.
Hash Tables 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.
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,
Sections 10.5 – 10.6 Hashing.
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
11 Map ADTs Map concepts. Map applications.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Multiway Search Trees Data may not fit into main memory
Efficiency of in Binary Trees
Hashing CSE 2011 Winter July 2018.
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Heaps 8/2/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,
Priority Queues Chuan-Ming Liu
Lecture 22 Binary Search Trees Chapter 10 of textbook
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.
JCF Hashmap & Hashset Winter 2005 CS-2851 Dr. Mark L. Hornick.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Advanced Associative Structures
Hash Table.
Dictionaries < > = Dictionaries Dictionaries
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Data Structures Maps and Hash.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Part-D1 Priority Queues
Maps.
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Ordered Maps & Dictionaries
Copyright © Aiman Hanna All rights reserved
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CS 583 Analysis of Algorithms
Advanced Implementation of Tables
Advanced Implementation of Tables
Sets and Maps Chapter 9.
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Topic 6: Binary Search Tree Data structure Operations
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,
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 二○一九年九月二十四日 ADT for Map:
Lecture-Hashing.
Presentation transcript:

Design & Analysis of Algorithm Map Informatics Department Parahyangan Catholic University

Map A map allows us to store elements so they can be located quickly using keys. Specifically, a map stores key-value pairs (k,v), which we call entries, where k is the key and v is its corresponding value. Map requires that each key be unique.

Map’s Operations size() Return the number of entries in M isEmpty() Test whether M is empty get(k) If M contains an entry (k,v), then return v, else return null put(k,v) if M does not have an entry with key equal to k, then add entry (k,v) to M and return null; else, replace the existing value with v and return the old value

Map’s Operations remove(k) If M have an entry with key equal to k, remove that entry from M and return its value; else return null keySet() Returns an iterable collection containing all the keys stored in M (so keySet().iterator() returns an iterator of keys) values() Returns an iterable collection containing all the values stored in M (so values().iterator() returns an iterator of values) entrySet() Returns an iterable collection containing all the key-value entries in M (so entrySet().iterator() returns an iterator of entries)

Null as a sentinel get(k), put(k,v), and remove(k) returns null when map M does not have an entry with key equal to k A special value such as this is known as a sentinel The disadvantage with using null as a sentinel is that we cannot store an entry with value null (i.e., entry (k, null)).

Example put(5,A)  null put(7,B)  null put(5,C)  A get(5)  C get(4)  null remove(5)  C put(3,D)  null entrySet()  {(7,B),(3,D)} keySet()  {7,3} values()  {B,D} (3,D) (5,C) (5,A) (7,B)

Representation #1 using Linked - List A simple way of implementing a map is to store its n entries in a doubly linked list S Fundamental operations get(k), put(k,v), and remove(k) involves simple scans on S  O(n) time

Representation #2 using Hash Table Expected running time for get(k), put(k,v), and remove(k) is O(1) But worst case is still O(n)

Java’s Hash Table Implementation The Java Collections Framework provides a hash table implementation in the class java.util.HashMap This class implements the java.util.Map interface, hence it performs all the methods of Map, as well as some other methods such as clear(), which removes all the entries in the map This class implements hash table using separate chaining method

Ordered Map In some applications, simply looking up values based on associated keys is not enough We often also wants to keep the entries in a map sorted according to some total order In an ordered map, we want to perform the usual map operations, but also maintain an order relation for the keys in our map and use this order in some of the map operations

Ordered Map’s Additional Methods firstEntry(k) Returns the entry with smallest key value; If the map is empty, then it returns null lastEntry(k) Returns the entry with largest key value; If the map is empty, then it returns null

Ordered Map’s Additional Methods ceilingEntry(k) Returns the entry with the least key value ≥ k; If there is no such entry, then it returns null floorEntry(k) Returns the entry with the greatest key value ≤ k; If there is no such entry, then it returns null higherEntry(k) Returns the entry with the least key value > k; If there is no such entry, then it returns null lowerEntry(k) Returns the entry with the greatest key value < k; If there is no such entry, then it returns null

Representation #3 (ordered map) using ordered array list We store the map’s entries in an array list S in increasing order of keys An ordered array list allows faster searching than a sorted linked list (i.e., Binary search v.s. sequential search)  O(lg n) Update operations put(k,v) and remove(k) may need to shift all the entries in the array list  takes O(n) time

Representation #4 (ordered map) using Binary Search Tree Other alternative is to use Binary Search Tree to store map’s entries Fundamental operations get(k), put(k,v), and remove(k) are simply search, insert, and delete BST operation  takes O(h) time For random keys, expected tree height is O(lg n), so all fundamental operations are expected to be O(lg n) time

Java’s Ordered Map Implementation The Java Collections Framework provides an ordered map implementation in the class java.util.TreeMap This class implements java.util.NavigableMap interface, which includes all java.util.sortedMap’s operations The implementation uses Red-Black Tree, which is a balanced tree (thus the tree’s height is guaranteed to be O(lg n))