51
maps & dictionaries Go&Ta 9.3

maps & dictionaries

  • Upload
    reilly

  • View
    72

  • Download
    0

Embed Size (px)

DESCRIPTION

maps & dictionaries. Go&Ta 9.3. map. A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting, and deleting items Multiple entries with the same key are not allowed Applications: address book (key=name, value = address) - PowerPoint PPT Presentation

Citation preview

Page 1: maps & dictionaries

maps & dictionaries

Go&Ta 9.3

Page 2: maps & dictionaries

2

• A map models a searchable collection of key-value entries• The main operations of a map are for searching, inserting, and

deleting items• Multiple entries with the same key are not allowed• Applications:

– address book (key=name, value = address)– student-record database (key=student id, value = student

record)

map

Page 3: maps & dictionaries

3

map

The map ADT requires that each key is unique, so the association of keys to values defines a mapping

Page 4: maps & dictionaries

4

map

Page 5: maps & dictionaries
Page 6: maps & dictionaries
Page 7: maps & dictionaries
Page 8: maps & dictionaries

Maps 8

Operation Output Map

isEmpty() true Øput(5,A) null (5,A)put(7,B) null (5,A),(7,B)put(2,C) null (5,A),(7,B),(2,C)put(8,D) null (5,A),(7,B),(2,C),(8,D)put(2,E) C (5,A),(7,B),(2,E),(8,D)get(7) B (5,A),(7,B),(2,E),(8,D)get(4) null (5,A),(7,B),(2,E),(8,D)get(2) E (5,A),(7,B),(2,E),(8,D)size() 4 (5,A),(7,B),(2,E),(8,D)remove(5) A (7,B),(2,E),(8,D)remove(2) E (7,B),(8,D)get(2) null (7,B),(8,D)isEmpty() false (7,B),(8,D)

mapexample

Page 9: maps & dictionaries

9

A Simple List-Based Map

• We can implement a map using an unsorted list – We store the items of the map in a list S (based on a

doubly-linked list), in arbitrary order

tailhead

entries

9 c 6 g 5 a 8 r

map

Page 10: maps & dictionaries

10

Performance of a List-Based Map

• Performance:– put, 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 unsorted list implementation is effective only for maps of small size.

• All of the fundamental operations take O(n) time.• Would like something faster…

map

Page 11: maps & dictionaries

An example of using a mapHashMap

Page 12: maps & dictionaries

This was some experiments with Rebecca. We have a number of patches (centres of population) that may be infected by some disease. There’s a probability that disease can travel from one location to another (a function of distance and size of patch). Therefore a state is a set of infected patches.if we have n patches there are 2^n states. I can represent a state as a BitSet (n bits) and a bitis on iff that patch is infected.

So, I took on the task of building a harness to check out the feasibility of capturing every statevisited by a simulation and how often we visited each of these states. Rebecca was tryingto identify quasi-stationary states. This empirical study would then be compared to an analyticalmodel to measure the difference between theory and practice

So, I thought I’d use a map, where entries were states, each state with a counter of the number of timesvisited. I could use the BitSet as the key.

Page 13: maps & dictionaries

This was some experiments with Rebecca. We have a number of patches (centres of population) that may be infected by some disease. There’s a probability that disease can travel from one location to another (a function of distance and size of patch). Therefore a state is a set of infected patches.if we have n patches there are 2^n states. I can represent a state as a BitSet (n bits) and a bitis on iff that patch is infected.

So, I took on the task of building a harness to check out the feasibility of capturing every statevisited by a simulation and how often we visited each of these states. Rebecca was tryingto identify quasi-stationary states. This empirical study would then be compared to an analyticalmodel to measure the difference between theory and practice

So, I thought I’d use a map, where entries were states, each state with a counter of the number of timesvisited. I could use the BitSet as the key.

I forgot to say, Simon was doing the theory bit …

Page 14: maps & dictionaries

This was some experiments with Rebecca. We have a number of patches (centres of population) that may be infected by some disease. There’s a probability that disease can travel from one location to another (a function of distance and size of patch). Therefore a state is a set of infected patches.if we have n patches there are 2^n states. I can represent a state as a BitSet (n bits) and a bitis on iff that patch is infected.

So, I took on the task of building a harness to check out the feasibility of capturing every statevisited by a simulation and how often we visited each of these states. Rebecca was tryingto identify quasi-stationary states. This empirical study would then be compared to an analyticalmodel to measure the difference between theory and practice

So, I thought I’d use a map, where entries were states, each state with a counter of the number of timesvisited. I could use the BitSet as the key.

I forgot to say, Simon was doing the theory bit …

So, here’s a state

Page 15: maps & dictionaries
Page 16: maps & dictionaries

… and I can create a state from a BitSet or an integer

Page 17: maps & dictionaries

… and when I revisit increment it’s counter

Page 18: maps & dictionaries

… convert a BitSet to an integer… convert an integer to a BitSet… classic stuff (show off?)

Page 19: maps & dictionaries

So, now I want to pretend I have n possible patches and I’m going to run my simulationfor m iterations and capture all states with frequency of occurrence, and I wanted itto be quick, and I wanted it to be compact, and I wanted it to be simple … and I used a HashMap

Page 20: maps & dictionaries
Page 21: maps & dictionaries

… and this is me generating m statesand testing to see if I have visitd them before

Page 22: maps & dictionaries

… and then I print them out

Page 23: maps & dictionaries

… and then I print them out

Page 24: maps & dictionaries

… but Rebecca wanted to do a statistical test, where we rank states …The results aren’t in a structure that’s quite right.

Page 25: maps & dictionaries

… but Rebecca wanted to do a statistical test, where we rank states …The results aren’t in a structure that’s quite right.

Could you sort states?

Page 26: maps & dictionaries

… now keys have to be comparable,And that’s why we have toInt and toBitSet

Page 27: maps & dictionaries

… and now it’s in order

Page 28: maps & dictionaries

… and that raises another question.

Page 29: maps & dictionaries

How do they implement a TreeMap?

Page 30: maps & dictionaries
Page 31: maps & dictionaries
Page 32: maps & dictionaries

… and then I wonder …

Page 33: maps & dictionaries

Am I going round in circles?

Page 34: maps & dictionaries
Page 35: maps & dictionaries

dictionaries

Page 36: maps & dictionaries
Page 37: maps & dictionaries

a reality check

Page 38: maps & dictionaries

The dictionary abstract data type stores key-element pairs (k,v), which we call entries, where

k is the key and v is the value. A dictionary allows for multiple entries with the same key, much

like an English dictionary, where we can have multiple definitions of the same word. The primary

use of a dictionary is to store values so that they can be located quickly using keys.

dictionaries

Page 39: maps & dictionaries

The dictionary abstract data type stores key-element pairs (k,v), which we call entries, where

k is the key and v is the value. A dictionary allows for multiple entries with the same key, much

like an English dictionary, where we can have multiple definitions of the same word. The primary

use of a dictionary is to store values so that they can be located quickly using keys.

dictionaries

We might have an ordered or unordered dictionary

Page 40: maps & dictionaries

dictionariesapi

• size()• isEmpty()• find(k) k is a key, returns matching entry or null• insert(k,v) k is a key with value v, inserts into D and returns entry• remove(k) remove from D entry with key k• findAll(k) returns iterable collection of entries with key k

Given a dictionary D

Note: find(k) finds an arbitrary entry (k,v)

Page 41: maps & dictionaries

Operation Output DictionaryisEmpty() true Øinsert(5,A) (5,A) (5,A)insert(7,B) (7,B) (5,A),(7,B)insert(2,C) (2,C) (5,A),(7,B),(2,C)insert(5,D) (5,D) (5,A),(7,B),(2,C),(5,D)insert(2,C) (2,C) (5,A),(7,B),(2,C),(5,D),(2,C)find(7) (7,B) (5,A),(7,B),(2,C),(5,D),(2,C)find(4) null (5,A),(7,B),(2,C),(5,D),(2,C) find(2) (2,C) (5,A),(7,B),(2,C),(5,D),(2,C) size() 5 (5,A),(7,B),(2,C),(5,D),(2,C) remove(5) (5,A) (7,B),(2,C),(5,D),(2,C) remove(2) (2,C) (7,B),(5,D),(2,C) size() 3 (7,B),(5,D),(2,C) isEmpty() false (7,B),(5,D),(2,C)

dictionaryexample

Page 42: maps & dictionaries

dictionaries

unordered

Page 43: maps & dictionaries

dictionariesunordered list

• size()• isEmpty()• find(k) • insert(k,v)• remove(k) • findAll(k)

size() is O(1) … maintain a counterisEmpty() is O(1) … test on sizefind(k) is O(n) … linear searchinsert(k,v) is O(1) … addLast or addFirstremove(k) is O(n) … we need to find entryfindAll(k) is O(n) … search the entire list!!!

Summary: fast insertion, slow find, findAll, & remove

BUT: if we want to use “move to front” heuristic … maybe good

Page 44: maps & dictionaries

dictionaries

hash table

Page 45: maps & dictionaries

dictionarieshash table

• size()• isEmpty()• find(k) • insert(k,v)• remove(k) • findAll(k)

Need to deal with duplicates … i.e. natural collisions!

Page 46: maps & dictionaries

dictionaries

ordered array

Page 47: maps & dictionaries

dictionariesordered array

• size()• isEmpty()• find(k) • insert(k,v)• remove(k) • findAll(k)

size() is O(1) … maintain a counterisEmpty() is O(1) … test on sizefind(k) is O(log(n)) … binary searchinsert(k,v) is O(n) … binary search then search left and right!!!remove(k) is O(n) … as abovefindAll(k) is O(n) … mix of binary search then left & right linear search

Page 48: maps & dictionaries

dictionariesordered array

In some way it’s a bag!Could we “count” how many times something is in the bag?

Could we then use a binary search tree with nodes having multiple entries?

Page 49: maps & dictionaries

chocolate

Page 50: maps & dictionaries

No. You don’t deserve it

Page 51: maps & dictionaries

Given a pair <K,V> where K is the key and V the value

Implement a structure <K,L> where L is a list of V

See Entry, Dictionary & TestIn directory map