Data Structures and Analysis (COMP 410)

Slides:



Advertisements
Similar presentations
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
Advertisements

Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
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.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
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:
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
David Stotts Computer Science Department UNC Chapel Hill.
Priority Queues Dr. David Matuszek
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
David Stotts Computer Science Department UNC Chapel Hill.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Abstract Data Type EnhanceEdu.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
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.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Review Functions. Function A function is a special type of relation in which each element of the domain is paired with exactly one element of the range.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Tables and Priority Queues
Maps Rem Collier Room A1.02 School of Computer Science and Informatics
Sets and Maps Chapter 9.
Using the Java Collection Libraries COMP 103 # T2
Data Structures and Analysis (COMP 410)
Design & Analysis of Algorithm Map
Now with a speaking professor! (hopefully...)
CSCI 104 Abstract Data Types
COMP 53 – Week Eleven Hashtables.
Efficiency of in Binary Trees
Data Structures and Analysis (COMP 410)
Hashing Exercises.
Part-D1 Priority Queues
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.
Road Map CS Concepts Data Structures Java Language Java Collections
Data Structures and Analysis (COMP 410)
structures and their relationships." - Linus Torvalds
Data Structures and Analysis (COMP 410)
Hash Table.
Data Structures and Analysis (COMP 410)
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Data Structures and Analysis (COMP 410)
structures and their relationships." - Linus Torvalds
Data Structures and Analysis (COMP 410)
CSE 373: Data Structures and Algorithms
Maps.
Priority Queues.
Ordered Maps & Dictionaries
Recitation Outline C++ STL associative containers Examples
Priority Queues (Chapter 6.6):
CSE 373, Copyright S. Tanimoto, 2002 Hashing -
Data Structures and Analysis (COMP 410)
Data Structures and Analysis (COMP 410)
Hash Tables Computer Science and Engineering
Lists Chapter 8.
Priority Queues.
Priority Queues.
Priority Queues (Chapter 6):
CO4301 – Advanced Games Development Week 12 Using Trees
structures and their relationships." - Linus Torvalds
Week 6 - Monday CS221.
Presentation transcript:

Data Structures and Analysis (COMP 410) David Stotts Computer Science Department UNC Chapel Hill

Sets and Maps

SET ADT Set is an abstract type add: SET x elt  SET remove: SET x elt  SET contains : SET x elt  boolean size, empty, etc. The add will enforce uniqueness… only one instance of an element may be in a SET A variant of SET called MULTISET will allow multiple instances of an element, then you need a count function for an element (contains is count>0 )

MAP Map is an abstract type We insert/retrieve items using the key a collection stores “pairs” (key,value) We insert/retrieve items using the key Goal is to use the associated value We are using this in the PrQUE spot in min bin heap found from priority want to retrieve and use the data (process, name, diagnosis, etc.)

MAP ADT Map is an abstract type get: MAP x key  value put: MAP x key x value  MAP remove: MAP x key  MAP hasKey : MAP x key  Boolean keys: MAP  SET of key values: MAP  “blob” of value size, empty, etc. “put” will enforce uniqueness of keys… only one of each key several keys may MAP to the same value “keys” returns a SET since keys are unique “values” returns a list/collection of the values (and the elements in the collection might not be unique)

MAP is like a Function MAP is like a mathematical function… A math function f(x)  y give a domain element x, get back a range element y MAP(k)  v or, get(k)  v give a key k, get back a value v

MAP is like a Function Math functions are often continuous f(x)  3x + 5 where x is a real can’t enumerate all elements of the domain In a MAP the function is discrete, a set of ordered pairs (domain, range) { (123,”dave”), (234,”jane”), (345,”kim”), (456,“pete”), (567,“amy”), (678,”bob”) } keys (domain elements) are a finite enumerable set

MAP is like a Function 1 to 1 Keys must be unique Each key maps to only one value Here is a MAP that might represent SSN mapped to names 1 to 1 Keys must be unique Values are chosen to be unique 123 jane 234 dave No kim 567 pete 345 bob 456 678 amy keys values

MAP is like a Function No Not necessarily 1 to 1 Keys must be unique Each key maps to only one value Here is a MAP that might represent product mapped to price No Not necessarily 1 to 1 Keys must be unique Values can be duplicated fuji 2.50 gala 2.25 redD 1.75 honey goldD 1.25 mac keys values

MAP Implementation MAP can be implemented like SET We store a (key,value) pair rather than only a key Organized as a BST (balanced) we get this put: O(log N) time, worst case and average get: O(log N) time, worst case and average hasKey: O(log N) time, worst case and average remove: O(log N) time, worst case and average

MAP Implementation Java Collections has Tree Map Hash Map TreeMap MAP elements stored in balanced BST Hash Map MAP elements stored using Hash Table Next chapter, after break (chapter 5)

Hash MAP Why use hashing for MAP ? Average case times: put: O(1) get: O(1) hasKey: O(1) remove: O(1) Worst case times: (the downside) put: O(N) get: O(N) hasKey: O(N) remove: O(N) We will see that with care, these are rare

Beyond this is just templates END Beyond this is just templates

Example 9 21 18 11 19 3 12 24 16 41 0 1 2 3 4 5 6 7 8 9 10 11 ...