Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maps & dictionaries Go&Ta 9.3. 2 A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting,

Similar presentations


Presentation on theme: "Maps & dictionaries Go&Ta 9.3. 2 A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting,"— Presentation transcript:

1 maps & dictionaries Go&Ta 9.3

2 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

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

4 4 map

5

6

7

8 Maps8 OperationOutputMap 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

9 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 tail head entries 9 c 6 g 5 a 8 r map

10 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

11 An example of using a map HashMap

12 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 bit is on iff that patch is infected. So, I took on the task of building a harness to check out the feasibility of capturing every state visited by a simulation and how often we visited each of these states. Rebecca was trying to identify quasi-stationary states. This empirical study would then be compared to an analytical model 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 times visited. I could use the BitSet as the key.

13 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 bit is on iff that patch is infected. So, I took on the task of building a harness to check out the feasibility of capturing every state visited by a simulation and how often we visited each of these states. Rebecca was trying to identify quasi-stationary states. This empirical study would then be compared to an analytical model 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 times visited. I could use the BitSet as the key. I forgot to say, Simon was doing the theory bit …

14 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 bit is on iff that patch is infected. So, I took on the task of building a harness to check out the feasibility of capturing every state visited by a simulation and how often we visited each of these states. Rebecca was trying to identify quasi-stationary states. This empirical study would then be compared to an analytical model 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 times visited. I could use the BitSet as the key. I forgot to say, Simon was doing the theory bit … So, here’s a state

15

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

17 … and when I revisit increment it’s counter

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

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

20

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

22 … and then I print them out

23

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

25 … 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?

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

27 … and now it’s in order

28 … and that raises another question.

29 How do they implement a TreeMap?

30

31

32 … and then I wonder …

33 Am I going round in circles?

34

35 dictionaries

36

37 a reality check

38 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

39 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

40 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)

41 OperationOutputDictionary isEmpty()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) dictionary example

42 dictionaries unordered

43 dictionariesunordered list size() isEmpty() find(k) insert(k,v) remove(k) findAll(k) size() is O(1) … maintain a counter isEmpty() is O(1) … test on size find(k) is O(n) … linear search insert(k,v) is O(1) … addLast or addFirst remove(k) is O(n) … we need to find entry findAll(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

44 dictionaries hash table

45 dictionarieshash table size() isEmpty() find(k) insert(k,v) remove(k) findAll(k) Need to deal with duplicates … i.e. natural collisions!

46 dictionaries ordered array

47 dictionariesordered array size() isEmpty() find(k) insert(k,v) remove(k) findAll(k) size() is O(1) … maintain a counter isEmpty() is O(1) … test on size find(k) is O(log(n)) … binary search insert(k,v) is O(n) … binary search then search left and right!!! remove(k) is O(n) … as above findAll(k) is O(n) … mix of binary search then left & right linear search

48 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?

49 chocolate

50 No. You don’t deserve it

51 Given a pair where K is the key and V the value Implement a structure where L is a list of V See Entry, Dictionary & Test In directory map


Download ppt "Maps & dictionaries Go&Ta 9.3. 2 A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting,"

Similar presentations


Ads by Google