D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Advertisements

© 2004 Goodrich, Tamassia Hash Tables1  
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.
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.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Hash Tables1 Part E Hash Tables  
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.
Data Structures Hash Table (aka Dictionary) i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, Andreas Veneris, Glenn Brookshear,
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
D ESIGN & A NALYSIS OF A LGORITHM 05 – N - ARY T REE & B INARY T REE Informatics Department Parahyangan Catholic University.
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University.
Hash Tables1   © 2010 Goodrich, Tamassia.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Hash Tables1  
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
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.
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Map ADT by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Symbol Tables Computing 2 COMP s1. S YMBOL T ABLES Searching: like sorting, searching is a fundamental element of many computational tasks data.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
Maps Nick Mouriski.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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,
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
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
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
11 Map ADTs Map concepts. Map applications.
Design & Analysis of Algorithm Map
Efficiency of in Binary Trees
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
© 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.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Data Structures Maps and Hash.
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
Dictionaries 4/5/2019 1:49 AM Hash Tables  
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 二○一九年九月二十四日 ADT for Map:
Presentation transcript:

D ESIGN & A NALYSIS OF A LGORITHM 07 – M AP Informatics Department Parahyangan Catholic University

M AP 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.

M AP ’ S O PERATIONS 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

M AP ’ S O PERATIONS 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)

N ULL 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) ).

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} (5,A) (7,B) (5,C) (3,D) E XAMPLE

R EPRESENTATION #1 USING L INKED - L IST 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

R EPRESENTATION #2 USING H ASH T ABLE Expected running time for get(k), put(k,v), and remove(k) is O(1) But worst case is still O(n)

J AVA ’ S H ASH T ABLE I MPLEMENTATION 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

O RDERED M AP 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

O RDERED M AP ’ S A DDITIONAL M ETHODS 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

O RDERED M AP ’ S A DDITIONAL M ETHODS 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

R EPRESENTATION #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

R EPRESENTATION #4 ( ORDERED MAP ) USING B INARY S EARCH T REE 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

J AVA ’ S O RDERED M AP I MPLEMENTATION 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))