Conditional Course Lecture 4 Hash Tables I: Separate Chaining and - - PowerPoint PPT Presentation

โ–ถ
conditional course
SMART_READER_LITE
LIVE PREVIEW

Conditional Course Lecture 4 Hash Tables I: Separate Chaining and - - PowerPoint PPT Presentation

Algorithms and Data Structures Conditional Course Lecture 4 Hash Tables I: Separate Chaining and Open Addressing Fabian Kuhn Algorithms and Complexity Fabian Kuhn Algorithms and Data Structures Abstract Data Types: Dictionary Dictionary:


slide-1
SLIDE 1

Algorithms and Data Structures Fabian Kuhn

Lecture 4 Hash Tables I: Separate Chaining and Open Addressing

Algorithms and Data Structures Conditional Course

Fabian Kuhn Algorithms and Complexity

slide-2
SLIDE 2

Algorithms and Data Structures Fabian Kuhn

Dictionary: (also: maps, associative arrays)

  • holds a collection of elements where each element is represented

by a unique key Operations:

  • create

: creates an empty dictionary

  • D.insert(key, value) : inserts a new (key,value)-pair

โ€“ If there already is an entry with the same key, the old entry is replaced

  • D.find(key)

: returns entry with key key

โ€“ If there is such an entry (returns some default value otherwise)

  • D.delete(key)

: deletes entry with key key

2

Abstract Data Types: Dictionary

slide-3
SLIDE 3

Algorithms and Data Structures Fabian Kuhn

  • So far, we saw 3 simple dictionary implementations
  • Often the most important operation: find
  • Can we improve find even more?
  • Can we make all operations fast?

3

Dictionary so far

๐‘œ: current number of elements in dictionary

Linked List (unsorted) Array (unsorted) Array (sorted) insert

๐‘ท(๐Ÿ) ๐‘ท(๐Ÿ) ๐‘ท(๐’)

delete

๐‘ท(๐’) ๐‘ท(๐’) ๐‘ท(๐’)

find

๐‘ท(๐’) ๐‘ท(๐’) ๐‘ท ๐ฆ๐ฉ๐ก ๐’

slide-4
SLIDE 4

Algorithms and Data Structures Fabian Kuhn

With an array, we can make everything fast, ...if the array is sufficiently large. Assumption: Keys are integers between 0 and ๐‘ โˆ’ 1 find(2) ๏ƒ  โ€œValue 1โ€ insert(6, โ€œPhilippโ€) delete(4)

4

Direct Addressing

None 1 None 2 Value 1 3 None 4 Value 2 5 None 6 None 7 Value 3 8 None โ‹ฎ โ‹ฎ ๐‘ โˆ’ 1 None Philipp None

slide-5
SLIDE 5

Algorithms and Data Structures Fabian Kuhn

  • 1. Direct addressing requires too much space!

โ€“ If each key can be an arbitrary int (32 bit): We need an array of size 232 โ‰ˆ 4 โ‹… 109. For 64 bit integers, we even need more than 1019 entries โ€ฆ

  • 2. What if the keys are no integers?

โ€“ Where do we store the (key,value)-pair (โ€œPhilippโ€, โ€œassistentโ€)? โ€“ Where do we store the key 3.14159? โ€“ Pythagoras: โ€œEverything is numberโ€ โ€œEverythingโ€ can be stored as a sequence of bits: Interpret bit sequence as integer โ€“ Makes the space problem even worse!

5

Direct Addressing : Problems

slide-6
SLIDE 6

Algorithms and Data Structures Fabian Kuhn

Problem

  • Huge space ๐‘‡ of possible keys
  • Number ๐‘œ of acutally used keys is much smaller

โ€“ We would like to use an array of size โ‰ˆ ๐‘œ (resp. ๐‘ƒ(๐‘œ))โ€ฆ

  • How can be map ๐‘ keys to ๐‘ƒ ๐‘œ array positions?

Hashing : Idea

๐‘ต possible keys

๐‘œ keys

random mapping

6

size ๐‘ƒ(๐‘œ)

slide-7
SLIDE 7

Algorithms and Data Structures Fabian Kuhn

Key Space ๐‘ป, ๐‘ป = ๐‘ต (all possible keys) Array size ๐’ (โ‰ˆ maximum #keys we want to store) Hash Function ๐’Š: ๐‘ป โ†’ {๐Ÿ, โ€ฆ , ๐’ โˆ’ ๐Ÿ}

  • Maps keys of key space ๐‘‡ to array positions
  • โ„Ž should be as close as possible to a random function

โ€“ all numbers in {0, โ€ฆ , ๐‘› โˆ’ 1} mapped to from roughly the same #keys โ€“ similar keys should be mapped to different positions

  • โ„Ž should be computable as fast as possible

โ€“ if possible in time ๐‘ƒ(1) โ€“ will be considered a basic operation in the following (cost = 1)

7

Hash Functions

slide-8
SLIDE 8

Algorithms and Data Structures Fabian Kuhn

1. insert(๐‘™1, ๐‘ค1) 2. insert(๐‘™2, ๐‘ค2) 3. insert(๐‘™3, ๐‘ค3)

8

Hash Tables

None 1 None 2 None 3 None 4 None 5 None 6 None 7 None 8 None โ‹ฎ โ‹ฎ ๐‘› โˆ’ 1 None

Hash table

๐’๐Ÿ (๐’๐Ÿ, ๐’˜๐Ÿ) ๐’๐Ÿ‘ ๐’๐Ÿ‘, ๐’˜๐Ÿ‘ ๐’๐Ÿ’ โ„Ž ๐‘™3 = 3

collision!

slide-9
SLIDE 9

Algorithms and Data Structures Fabian Kuhn

Collision: Two keys ๐‘™1, ๐‘™2 collide if โ„Ž ๐‘™1 = โ„Ž(๐‘™2). What should we do in case of a collision?

  • Can we choose hash function such that there are no collisions?

โ€“ This is only possible if we know the used keys before choosing the hash function. โ€“ Even then, choosing such a hash function can be very expensive.

  • Use another hash function?

โ€“ One would need to choose a new hash function for every new collision โ€“ A new hash function means that one needs to relocate all the already inserted values in the hash table.

  • Further ideas?

9

Hash Tables : Collisions

slide-10
SLIDE 10

Algorithms and Data Structures Fabian Kuhn

Approaches for Dealing With Collisions

  • Assumption: Keys ๐‘™1 and ๐‘™2 collide
  • 1. Store both (key,value) pairs at the same position

โ€“ The hash table needs to have space to store multiple entries at each position. โ€“ We do not want to just increase the size of the table (then, we chould have just started with a larger tableโ€ฆ) โ€“ Solution: Use linked lists

  • 2. Store second key at a different position

โ€“ Can for example be done with a second hash function โ€“ Problem: At the alternative position, there could again be a collision โ€“ There are multiple solutions โ€“ One solution: use many possible new positions (One has to make sure that these positions are usually not usedโ€ฆ)

10

Hash Tables : Collisions

slide-11
SLIDE 11

Algorithms and Data Structures Fabian Kuhn

  • Each position of the hash table points to a linked list

11

Separate Chaining

None 1 None 2 None 3 4 None 5 None 6 None 7 8 None โ‹ฎ โ‹ฎ ๐‘› โˆ’ 1 None

Hash table

๐’˜๐Ÿ ๐’˜๐Ÿ’ ๐’˜๐Ÿ‘ Space usage: ๐‘ท(๐’ + ๐’)

  • table size ๐‘›, no. of elements ๐‘œ
slide-12
SLIDE 12

Algorithms and Data Structures Fabian Kuhn

To make it simple, first for the case without collisionsโ€ฆ create: ๐‘ท ๐Ÿ insert: ๐‘ท(๐Ÿ) find: ๐‘ท(๐Ÿ) delete: ๐‘ท(๐Ÿ)

  • As long as there are no collisions, hash tables are extremely fast

(if hash functions can be evaluated in constant time)

  • We will see that this is also true with collisionsโ€ฆ

12

Runtime Hash Table Operations

slide-13
SLIDE 13

Algorithms and Data Structures Fabian Kuhn

Now, letโ€™s consider collisionsโ€ฆ create: ๐‘ท 1 insert: ๐‘ท(1 + length of list)

โ€“ If one does not need to check if the key is already contained, insert can even be always be done in time ๐‘ƒ 1 .

find: ๐‘ท(1 + length of list) delete: ๐‘ท(1 + length of list)

  • We therefore has to see how long the lists become.

13

Runtime Separate Chaining

slide-14
SLIDE 14

Algorithms and Data Structures Fabian Kuhn

Worst case for separate chaining:

  • All keys that appear have the same hash value
  • Results in a linked list of length ๐‘œ
  • Probability for random โ„Ž:

14

Separate Chaining : Worst Case

None 1 None 2 None 3 4 None 5 None 6 None 7 None 8 None โ‹ฎ โ‹ฎ m โˆ’ 1 None

Hashtabelle

๐’๐Ÿ‘ ๐’๐Ÿ โ„Ž ๐‘™1 = 3

๐Ÿ ๐’

๐’โˆ’๐Ÿ

slide-15
SLIDE 15

Algorithms and Data Structures Fabian Kuhn

  • Cost of insert, find, and delete depends on the length of the

corresponding list

  • How long do the lists become?

โ€“ Assumption: Size of hash table ๐‘›, number of entries ๐‘œ โ€“ Additional assumption: Hash function โ„Ž behaves as a random function

  • List lengths correspond to the following random experiment

๐’ bins and ๐’ balls

  • Each ball is thrown (independently) into a random bin
  • Longest list = maximal no. of balls in the same bin
  • Average list length = average no. of balls per bin

๐‘› bins, ๐‘œ balls ๏ƒ  average #balls per bin: ฮค

๐‘œ ๐‘›

15

Length of Linked Lists

slide-16
SLIDE 16

Algorithms and Data Structures Fabian Kuhn

  • Worst-case runtime = ฮ˜ max #balls per bin

with high probability (whp) โˆˆ ๐‘ƒ ฮค

๐‘œ ๐‘› +

เต—

log ๐‘œ log log ๐‘œ

โ€“ for ๐‘œ โ‰ค ๐‘› : ๐‘ƒ เต—

log ๐‘œ log log ๐‘œ

  • The longest list will have length ฮ˜

เต—

log ๐‘œ log log ๐‘œ .

16

Balls and Bins

๐’ balls ๐’ bins

slide-17
SLIDE 17

Algorithms and Data Structures Fabian Kuhn

Expected runtime (for every key):

  • Key in table:

โ€“ List length of a random entry โ€“ Corresponds to #balls in bin of a random ball

  • Key not in table:

โ€“ Length of a random list, i.e., #balls in a random bin

17

Balls and Bins

๐’ balls ๐’ bins

slide-18
SLIDE 18

Algorithms and Data Structures Fabian Kuhn

Load ๐œท of hash table: ๐œท โ‰” ๐’ ๐’ Cost of search:

  • Search for key ๐‘ฆ that is not contained in hash table

โ„Ž(๐‘ฆ) is a uniformly random position ๏ƒ  expected list length = average list length = ๐›ฝ Expected runtime: ๐‘ท(๐Ÿ + ๐œท)

18

Expected Runtime of Find

find(๐‘ฆ) โ„Ž(๐‘ฆ) time: ๐‘ƒ(1) go through a random list: ๐‘ƒ(๐›ฝ)

slide-19
SLIDE 19

Algorithms and Data Structures Fabian Kuhn

Load ๐œท of hash table: ๐œท โ‰” ๐’ ๐’ Cost of search :

  • Search for key ๐‘ฆ that is contained in hash table

How many keys ๐‘ง โ‰  ๐‘ฆ are in the list of ๐‘ฆ?

  • The other keys are distributed randomly, the expected number

thus corresponds to the expected number of entries in a random list of a hash table with ๐‘œ โˆ’ 1 entries (all entries except ๐‘ฆ).

  • This is:

๐‘œโˆ’1 ๐‘› < ๐‘œ ๐‘› = ๐›ฝ ๏ƒ  expected list length of ๐‘ฆ < 1 + ๐›ฝ

Expected runtime: ๐‘ท(๐Ÿ + ๐œท)

19

Expected Runtime of Find

slide-20
SLIDE 20

Algorithms and Data Structures Fabian Kuhn

create:

  • runtime ๐‘ƒ 1

insert, find & delete:

  • worst case: ๐šฐ(๐’)
  • worst case with high probability (for random โ„Ž): ๐‘ท ๐œท +

๐ฆ๐ฉ๐ก ๐’ ๐ฆ๐ฉ๐ก ๐ฆ๐ฉ๐ก ๐’

  • Expected runtime (for fixed key ๐‘ฆ): ๐‘ท ๐Ÿ + ๐œท

โ€“ holds for successful and unsuccessful searches โ€“ if ๐›ฝ = ๐‘ƒ 1 (i.e., hash table has size ฮฉ ๐‘œ ), this is ๐‘ƒ(1)

  • Hash tables are extremely efficient and

typically have ๐‘ท ๐Ÿ runtime for all operations.

20

Runtimes Separate Chaining

slide-21
SLIDE 21

Algorithms and Data Structures Fabian Kuhn

Idea:

  • Use two hash functions โ„Ž1 and โ„Ž2
  • Store key ๐‘ฆ in the shorter of the two lists at โ„Ž1(๐‘ฆ) and โ„Ž2 ๐‘ฆ

Balls and Bins:

  • Put ball in bins with fewer balls
  • For ๐‘œ balls, ๐‘› bins: maximal no. of balls per bin (whp):

ฮค ๐’ ๐’ + ๐‘ท(๐ฆ๐ฉ๐ก ๐ฆ๐ฉ๐ก ๐’)

  • Known as โ€œpower of two choicesโ€

21

Shorter List Lengths

1. 2.

slide-22
SLIDE 22

Algorithms and Data Structures Fabian Kuhn

Goal:

  • store everything directly in the hash table (in the array)
  • open addressing = closed hashing
  • no lists

Basic idea:

  • In case of collisions, we need to have alternative positions
  • Extend hash function to get

โ„Ž: ๐‘‡ ร— 0, โ€ฆ , ๐‘› โˆ’ 1 โ†’ {0, โ€ฆ , ๐‘› โˆ’ 1}

โ€“ Provides hash values โ„Ž ๐‘ฆ, 0 , โ„Ž ๐‘ฆ, 1 , โ„Ž ๐‘ฆ, 2 , โ€ฆ , โ„Ž(๐‘ฆ, ๐‘› โˆ’ 1) โ€“ For every ๐‘ฆ โˆˆ ๐‘‡, โ„Ž(๐‘ฆ, ๐‘—) should cover all ๐‘› values (for different ๐‘—)

  • Inserting a new element with key ๐‘ฆ:

โ€“ Try positions one after the other (until a free one is found) โ„Ž ๐‘ฆ, 0 , โ„Ž ๐‘ฆ, 1 , โ„Ž ๐‘ฆ, 2 , โ€ฆ , โ„Ž(๐‘ฆ, ๐‘› โˆ’ 1)

22

Hashing with Open Addressing

slide-23
SLIDE 23

Algorithms and Data Structures Fabian Kuhn

Idea:

  • If โ„Ž ๐‘ฆ is occupied, try the subsequent position:

๐’Š ๐’š, ๐’‹ = ๐’Š ๐’š + ๐’‹ ๐ง๐ฉ๐ž ๐’ for ๐‘— = 0, โ€ฆ , ๐‘› โˆ’ 1

  • Example:

Insert the following keys

โ€“ ๐‘ฆ1, โ„Ž ๐‘ฆ1 = 3 โ€“ ๐‘ฆ2, โ„Ž ๐‘ฆ2 = 5 โ€“ ๐‘ฆ3, โ„Ž ๐‘ฆ3 = 3 โ€“ ๐‘ฆ4, โ„Ž ๐‘ฆ4 = 8 โ€“ ๐‘ฆ5, โ„Ž ๐‘ฆ5 = 4 โ€“ ๐‘ฆ6, โ„Ž ๐‘ฆ6 = 6 โ€“ โ€ฆ

23

Linear Probing

1 2 3 4 5 6 7 8 โ‹ฎ โ‹ฎ ๐‘› โˆ’ 1

๐’š๐Ÿ ๐’š๐Ÿ‘ ๐’š๐Ÿ’ ๐’š๐Ÿ’ ๐’š๐Ÿ“ ๐’š๐Ÿ” ๐’š๐Ÿ” ๐’š๐Ÿ” ๐’š๐Ÿ• ๐’š๐Ÿ•

slide-24
SLIDE 24

Algorithms and Data Structures Fabian Kuhn

Advantages:

  • very simple to implement
  • all array positions are considered as alternatives
  • good cache locality

Disadvantages:

  • As soon as there are collisions, we get clusters.
  • Clusters grow if hashing into one of the positions of a cluster.
  • Clusters of size ๐‘™ in each step grow with probability

ฮค (๐‘™ + 2) ๐‘›

  • The larger the clusters, the faster they grow!!

24

Linear Probing

slide-25
SLIDE 25

Algorithms and Data Structures Fabian Kuhn

Idea:

  • Choose sequence that does not lead to clusters:

๐’Š ๐’š, ๐’‹ = ๐’Š ๐’š + ๐’…๐Ÿ๐’‹ + ๐’…๐Ÿ‘๐’‹๐Ÿ‘ ๐ง๐ฉ๐ž ๐’ for ๐‘— = 0, โ€ฆ , ๐‘› โˆ’ 1 Advantages:

  • does not create clusters of consecutive entries
  • covers all ๐‘› positions if parameters are chosen carefully

Disadvantages:

  • can still lead to some kind of clusters
  • problem: first hash values determines the whole sequence!
  • Asymptotically at best as good as hashing with separate chaining

25

Quadratic Probing

โ„Ž ๐‘ฆ = โ„Ž ๐‘ง โŸน โ„Ž ๐‘ฆ, ๐‘— = โ„Ž(๐‘ง, ๐‘—)

slide-26
SLIDE 26

Algorithms and Data Structures Fabian Kuhn

Idea: Use two hash functions ๐’Š ๐’š, ๐’‹ = ๐’Š๐Ÿ ๐’š + ๐’‹ โ‹… ๐’Š๐Ÿ‘ ๐’š ๐ง๐ฉ๐ž ๐’ Advantages:

  • If m is a prime number, all ๐‘› positions are covered
  • Probing function depends on ๐‘ฆ in two ways
  • Avoids drawbacks of linear and quadratic probing
  • Probability that two keys ๐‘ฆ and ๐‘ฆโ€ฒ generate the same sequence of

positions: โ„Ž1 ๐‘ฆ = โ„Ž1 ๐‘ฆโ€ฒ โˆง โ„Ž2 ๐‘ฆ = โ„Ž2 ๐‘ฆโ€ฒ โŸน prob = 1 ๐‘›2

  • Works well in practice!

26

Double Hashing

slide-27
SLIDE 27

Algorithms and Data Structures Fabian Kuhn

Open Adressing:

  • Key ๐‘ฆ can be at the following positions:

โ„Ž ๐‘ฆ, 0 , โ„Ž ๐‘ฆ, 1 , โ„Ž ๐‘ฆ, 2 , โ€ฆ , โ„Ž ๐‘ฆ, ๐‘› โˆ’ 1 Find Operation?

i = 0 while i < m and H[h(x,i)] != None and H[h(x,i)].key != x: i += 1 if i < m: return (H[h(x,i)].key == x)

When inserting ๐‘ฆ, ๐‘ฆ is inserted at position ๐ผ โ„Ž ๐‘ฆ, ๐‘— if ๐ผ[โ„Ž ๐‘ฆ, ๐‘˜ ] is

  • ccupied for all ๐‘˜ < ๐‘—.

27

Open Addressing: Find Operation

hash table

๐‘ฐ

โ„Ž(๐‘ฆ, 1) โ„Ž(๐‘ฆ, 0) โ„Ž(๐‘ฆ, 2) โ„Ž(๐‘ฆ, 3)

๐’š

slide-28
SLIDE 28

Algorithms and Data Structures Fabian Kuhn

Open Addressing:

  • Key ๐‘ฆ can be at the following positions:

โ„Ž ๐‘ฆ, 0 , โ„Ž ๐‘ฆ, 1 , โ„Ž ๐‘ฆ, 2 , โ€ฆ , โ„Ž ๐‘ฆ, ๐‘› โˆ’ 1 Delete Operation

i = 0 while i < m and H[h(x,i)] != None and H[h(x,i)].key != x: i += 1 if i < m and H[h(x,i)].key == x: H[h(x,i)] = deleted

When inserting ๐‘ฆ, ๐‘ฆ is inserted at position ๐ผ โ„Ž ๐‘ฆ, ๐‘— if ๐ผ[โ„Ž ๐‘ฆ, ๐‘˜ ] is

  • ccupied for all ๐‘˜ < ๐‘—.

28

Open Addressing: Delete Operation

๐‘ฐ

โ„Ž(๐‘ฆ, 1) โ„Ž(๐‘ฆ, 0) โ„Ž(๐‘ฆ, 2) โ„Ž(๐‘ฆ, 3)

๐’š

slide-29
SLIDE 29

Algorithms and Data Structures Fabian Kuhn

Open Addressing:

  • Key ๐‘ฆ can be at the following positions:

โ„Ž ๐‘ฆ, 0 , โ„Ž ๐‘ฆ, 1 , โ„Ž ๐‘ฆ, 2 , โ€ฆ , โ„Ž ๐‘ฆ, ๐‘› โˆ’ 1 Find Operation

i = 0 while i < m and H[h(x,i)] != None and H[h(x,i)].key != x: i += 1 if i < m: return (H[h(x,i)].key == x)

When inserting ๐‘ฆ, ๐‘ฆ is inserted at position ๐ผ โ„Ž ๐‘ฆ, ๐‘— if ๐ผ[โ„Ž ๐‘ฆ, ๐‘˜ ] is

  • ccupied for all ๐‘˜ < ๐‘—.

29

Open Addressing: Find Operation

๐‘ฐ

โ„Ž(๐‘ฆ, 1) โ„Ž(๐‘ฆ, 0) โ„Ž(๐‘ฆ, 2) โ„Ž(๐‘ฆ, 3)

๐’š

slide-30
SLIDE 30

Algorithms and Data Structures Fabian Kuhn

Open Addressing:

  • All keys / values are stored directly in the array

โ€“ deleted entries have to be marked

  • No lists necessary

โ€“ avoids the required overheadโ€ฆ

  • Only fast if load

๐›ฝ = ๐‘œ ๐‘› is not too largeโ€ฆ

โ€“ but then, it is faster in practice than separate chainingโ€ฆ

  • ๐›ฝ > 1 is impossible!

โ€“ because there are only ๐‘› positions available

30

Open Addressing : Summary

slide-31
SLIDE 31

Algorithms and Data Structures Fabian Kuhn

So far, we have seen: efficient method to implement a dictionary

  • All operations typically have runtime ๐‘ƒ 1

โ€“ If the hash functions are random enough and if they can be evaluated in constant time. โ€“ The worst-case runtime is somewhat higher, in every application of hash functions, there will be some more expensive operations.

We will see:

  • How to choose a good hash function?
  • What to do if the hash table becomes too small?
  • Hashing can be implemented such that the find cost is ๐‘ƒ(1) in

every case.

31

Summary Hashing

slide-32
SLIDE 32

Algorithms and Data Structures Fabian Kuhn

Hash tables (dictionary):

https://docs.python.org/2/library/stdtypes.html#mapping-types-dict

  • Generate new table:

table = {}

  • Insert (key,value) pair:

table.update({key : value})

  • Find key:

key in table table.get(key) table.get(key, default_value)

  • Delete key:

del table[key] table.pop(key, default_value)

32

Hashing in Python

slide-33
SLIDE 33

Algorithms and Data Structures Fabian Kuhn

Java class HashMap:

  • Create new hash table (keys of type K, values of type V)

HashMap<K,V> table = new HashMap<K,V>();

  • Insert (key,value) pair (key of type K, value of type V)

table.put(key, value)

  • Find key

table.get(key) table.containsKey(key)

  • Delete key

table.remove(key)

  • Similar class HashSet: manages only set of keys

33

Hashing in Java

slide-34
SLIDE 34

Algorithms and Data Structures Fabian Kuhn

There is not one standard class hash_map:

  • Should be available in almost all C++ compilers

http://www.sgi.com/tech/stl/hash_map.html unordered_map:

  • Since C++11 in Standard STL

http://www.cplusplus.com/reference/unordered_map/unordered_map/

34

Hashing in C++

slide-35
SLIDE 35

Algorithms and Data Structures Fabian Kuhn

C++ classes hash_map / unordered_ map:

  • Neue Hashtab. erzeugen (Schlรผssel vom Typ K, Werte vom Typ V)

unordered_map<K,V> table;

  • Einfรผgen von (key,value)-Paar (key vom Typ K, value vom Typ V)

table.insert(key, value)

  • Suchen nach key

table[key] oder table.at(key) table.count(key) > 0

  • Lรถschen von key

table.erase(key)

35

Hashing in C++

slide-36
SLIDE 36

Algorithms and Data Structures Fabian Kuhn

Attention

  • One can use hash_map / unordered_map in C++ like an array

โ€“ The array elements are the keys

  • But:

T[key] inserts key, if it is not contained T.at(key) throws an exception if key is not contained in map.

36

Hashing in C++