maps Nov. 8/9, 2017 1 Map (Mathematics) codomain domain A map - - PowerPoint PPT Presentation

maps
SMART_READER_LITE
LIVE PREVIEW

maps Nov. 8/9, 2017 1 Map (Mathematics) codomain domain A map - - PowerPoint PPT Presentation

COMP 250 Lecture 26 maps Nov. 8/9, 2017 1 Map (Mathematics) codomain domain A map is a set of pairs { (x, f(x)) }. Each x in domain maps to exactly one f(x) in codomain, but it can happen that f(x1) = f(x2) for different


slide-1
SLIDE 1

1

COMP 250

Lecture 26

maps

  • Nov. 8/9, 2017
slide-2
SLIDE 2

Map (Mathematics)

2

“domain” “codomain”

A map is a set of pairs { (x, f(x)) }. Each x in domain maps to exactly one f(x) in codomain, but it can happen that f(x1) = f(x2) for different x1, x2, i.e. many-to-one.

slide-3
SLIDE 3

Familiar examples

3

Calculus 1 and 2 (“functions”): f : real numbers  real numbers Asymptotic complexity in CS: t : input size  number of steps in a algorithm.

slide-4
SLIDE 4

Map (in COMP 250)

4

keys (type K) values (type V) A map is a set of (key, value) pairs. For each key, there is at most one value.

slide-5
SLIDE 5

5

keys (type K) values (type V) The black dots here indicate objects (or potential

  • bjects) of type K or V that are not in the map.
slide-6
SLIDE 6

6

Map Address book Caller ID Student file Index at back of book Keys Name Phone # ID or Name keyword Values Address, email.. Name Student record List of book pages

slide-7
SLIDE 7

7

Map Address book Caller ID Student file Index at back of book Keys Name Phone # ID or Name keyword Values Address, email.. Name Student record List of book pages

slide-8
SLIDE 8

8

Map Address book Caller ID Student file Index at back of book Keys Name Phone # ID or Name keyword Values Address, email.. Name Student record List of book pages

slide-9
SLIDE 9

9

Map Address book Caller ID Student file Index at back of book Keys Name Phone # ID or Name keyword Values Address, email.. Name Student record List of book pages

slide-10
SLIDE 10

10

Map Address book Caller ID Student file Index at back of book Keys Name Phone # ID or Name keyword Values Address, email.. Name Student record List of book pages

slide-11
SLIDE 11

Map Entries

11

keys values Each (key, value) pair is called an entry. In this example, there are four entries.

slide-12
SLIDE 12

Example

12

In COMP 250 this semester, the above mapping has ~600 entries. Most McGill students are not taking COMP 250 this semester. Student ID also happens to be part of the student record. Student IDs Student records

slide-13
SLIDE 13

Map ADT

  • put( key, value ) // add
  • get(key) // why not get(key, value) ?
  • remove(key)

13

slide-14
SLIDE 14

Data Structures for Maps

14

How to organize a set of (key, value) pairs, i.e. entries ?

slide-15
SLIDE 15

Array list

15

1 2 3 4

null null

put( key, value ) get(key) remove(key)

slide-16
SLIDE 16

Singly (or Doubly) linked list

16

head tail put( key, value ) get(key) remove(key)

slide-17
SLIDE 17

Important property of keys

17

Two different keys can have (map to) the same value. One key cannot have (map to) two values.

slide-18
SLIDE 18

18

Special case #1: what if keys are comparable ?

slide-19
SLIDE 19

Array list (sorted by key)

19

1 2 3 4

null null

put( key, value ) get(key) remove(key)

slide-20
SLIDE 20

Binary Search Tree (sorted by key)

20

put( key, value ) get(key) remove(key)

slide-21
SLIDE 21

minHeap (priority defined by key)

21

put( key, value ) get(key) remove(key)

slide-22
SLIDE 22

Special case #1: what if keys are comparable ? Special case #2: what if keys are unique positive integers in small range ?

22

Then, we could use an array

  • f type V (value) and have

O(1) access. This would not work well if keys are 9 digit student IDs.

4 9 12 22 3 6 8 14

slide-23
SLIDE 23

Special case #1: what if keys are comparable ? Special case #2: what if keys are unique positive integers in small range ?

23

General Case: Keys might not be comparable. Keys might be not be positive integers. e.g. Keys might be strings or some other type.

slide-24
SLIDE 24

24

3 6 8 14

Strategy for the General Case (Hash Maps – next lecture): Try to define a map from keys to small range of positive integers (array index), and then store the corresponding values in the array.

Recall notation: black dots are not part of the map.

slide-25
SLIDE 25

25

Rest of today: Define a map from keys to large range of positive integers. Such a map is called a hash code.

slide-26
SLIDE 26

“default” hashcode() map in Java

26

  • bjects in a Java

program (runtime)

  • bject’s starting (“base”)

address in JVM memory (24 bits) { 0, 1, 2, … , 224 − 1}

By default, “obj1 == obj2” means “obj1.hashcode() == obj2.hashcode()” 1-to-1 (not many-to-1)

slide-27
SLIDE 27

String.hashcode() in Java

27

Strings int (32 bit) For each String, define an integer.

slide-28
SLIDE 28

Example hash code for Strings

28

(not used in Java) e.g.

ASCII values of ‘a’, ‘e’, ‘t’ are 97, 101, 116.

ℎ 𝑡 ≡

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡[𝑗] ℎ "𝑓𝑏𝑢" = ℎ "𝑏𝑢𝑓" = ℎ "𝑢𝑓𝑏"

slide-29
SLIDE 29

String.hashcode() in Java

29

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 𝑦𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() where 𝑦 = 31.

slide-30
SLIDE 30

String.hashcode() in Java

30

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 𝑦𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() where 𝑦 = 31.

e.g. s = “eat” then s.hashcode() = 101 * 312 + 97 * 31 + 116 s.length = 3 s[0] s[1] s[2] ‘e’ ‘a’ ‘t’

slide-31
SLIDE 31

String.hashcode() in Java

31

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 𝑦𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() where 𝑦 = 31.

e.g. s = “ate” then s.hashcode() = 97 * 312 + 116 * 31 + 101 s.length = 3 s[0] s[1] s[2] ‘a’ ‘t’ ‘e’

slide-32
SLIDE 32

String.hashcode() in Java

32

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 ∗ (31)𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() If s1.hashCode() == s2.hashCode() then … ?

slide-33
SLIDE 33

String.hashcode() in Java

33

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 ∗ (31)𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() If s1.hashCode() == s2.hashCode() then … ? s1 may or may not be the same string as s2.

slide-34
SLIDE 34

String.hashcode() in Java

34

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 ∗ (31)𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() If s1.hashCode() == s2.hashCode() then … ? s1 may or may not be the same string as s2. If s1.hashCode() != s2.hashCode() then … ?

slide-35
SLIDE 35

String.hashcode() in Java

35

𝑗=0 𝑡.𝑚𝑓𝑜𝑕𝑢ℎ−1

𝑡 𝑗 ∗ (31)𝑡.𝑚𝑓𝑜𝑕𝑢ℎ −1 − 𝑗 s.hashCode() If s1.hashCode() == s2.hashCode() then … s1 may or may not be the same string as s2. If s1.hashCode() != s2.hashCode() then … s1 is a different string than s2.

slide-36
SLIDE 36

ASIDE: Use Horner’s rule for efficient polynomial evaluation

36

s[0] * 𝑦3 + s[1] * 𝑦2 + s[2]* 𝑦 + s[3]

There is no need to compute each 𝑦𝑗 separately.

slide-37
SLIDE 37

ASIDE: Use Horner’s rule for efficient polynomial evaluation

37

s[0] * 313 + s[1] * 312 + s[2]* 31 + s[3] = ( s[0] * 312 + s[1] * 311 + s[2] ) * 31 + s[3] = ( ( s[0] * 311 + s[1]) ∗ 31 + s[2] ) * 31 + s[3]

h = 0 for (i = 0; i < 𝑡. 𝑚𝑓𝑜𝑕𝑢ℎ; i++) h = h*31 + 𝑡[i]

For a degree 𝑜 polynomial, Horner’s rule uses O(n) multiplications, not O(𝑜2).

slide-38
SLIDE 38

Next lecture: hash maps

38

keys (K) integers values (V)

hashcode()

We want to map the keys to a small range of positive integers. How ?

3 6