0% found this document useful (0 votes)
8 views

10A Implementing Dictionaries by Map

Uploaded by

Sophia Lindholm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

10A Implementing Dictionaries by Map

Uploaded by

Sophia Lindholm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Implementing Dictionaries by Map

Cosmin E. Oancea
[email protected]

Department of Computer Science (DIKU)


University of Copenhagen

November 2023 KOM-IT Lecture Slides


Today’s Lecture

learn how to work with dictionaries,


i.e., the Map data-structure in Javascript;

which allow sublinear (constant time) insertion, deletion and


query of key-value pairs;

essentially, we use dictionaries (Map) whenever using arrays


would result in suboptimal complexity!
Motivation
If we use (unsorted) arrays, what is the worst-time complexity of
the following programs:
(1) check whether a word is valid, i.e., it appears in a dictionary;

(2) eliminate all the duplicates in an array of n words


(assuming there are few duplicates);

(3) for all the words in a text, count up how many times each
word appears in the text.
Motivation
If we use (unsorted) arrays, what is the worst-time complexity of
the following programs:
(1) check whether a word is valid, i.e., it appears in a dictionary;

(2) eliminate all the duplicates in an array of n words


(assuming there are few duplicates);

(3) for all the words in a text, count up how many times each
word appears in the text.

Answers:
(1) linear time in the size of the dictionary, i.e., O(n) on average
Run the prefix-sum example;

(2) quadratic time in n, i.e., O(n2 );

(3) quadratic time in n, i.e., O(n2 ).


Methods of Map data structure

l e t c o n t a c t s = new Map ( ) ;
c o n t a c t s . s e t ( ’ Sarah ’ , {phone : ” 25551234 ” , c i t y : ” Aa r h u s ” } ) ;
c o n t a c t s . has ( ’ Sarah ’ ) ; / / t r u e
c o n t a c t s . g e t ( ’ Anna ’ ) ; / / u n d e f i n e d
c o n t a c t s . s e t ( ’ Anna ’ , {phone : ” 72344321 ” , c i t y : ” Odense ” } ) ;
c o n t a c t s . s e t ( ’ Sarah ’ , {phone : ” 25555555 ” , c i t y : ” Aa r h u s ” } ) ;
c o n t a c t s . g e t ( ’ Sarah ’ ) ; / / { phone : ”2 5 5 5 5 5 5 5 ” , c i t y : ” Aa r h u s ”}
co n t a ct s . delete ( ’ Jens ’ ) ; / / f a l s e
c o n t a c t s . d e l e t e ( ’ Sarah ’ ) ; / / t r u e
co n s o l e . l o g ( c o n t a c t s . s i z e ) ; / / 1

All methods take constant time O(1):


set(key,value) updates the value of a key or adds a new key-value
binding;
has(key) returns true if the key is in the dictionary;
get(key) returns the value associated to that key in the
dictionary (and undefined if not in dictionary).
delete(key) deletes that key-value pair from the dictionary and
returns whether that key was in the dictionary.
What Not to Do!
Do not add new fields to a Map object other than with set.
l e t wrongMap = new Map ( ) ;
wrongMap [ ’ bla ’ ] = ’ blaa ’ ;
wrongMap [ ’ bla2 ’ ] = ’ blaaa2 ’ ;

This way of setting properties does not interact well with the Map
data structures, and you will get easily confused:
What Not to Do!
Do not add new fields to a Map object other than with set.
l e t wrongMap = new Map ( ) ;
wrongMap [ ’ bla ’ ] = ’ blaa ’ ;
wrongMap [ ’ bla2 ’ ] = ’ blaaa2 ’ ;

This way of setting properties does not interact well with the Map
data structures, and you will get easily confused:
wrongMap . has ( ’ bla ’ ) // false
wrongMap . d e l e t e ( ’ bla ’ ) // false
co n s o l e . l o g ( wrongMap ) / / Map { b l a : ’ blaa ’ , bla2 : ’ blaaa2 ’ }
Iterating through a Map
Elements are iterated in the same order as they were inserted.
(This could be useful, for example for removing duplicates.)
l e t myMap = new Map ( ) ;
myMap . s e t ( 9 , ’ nine ’ ) ;
myMap . s e t ( 4 , ’ four ’ ) ;
myMap . s e t ( 5 , ’ f i ve ’ ) ;

f o r ( l e t [ key , va l u e ] o f myMap ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e )
}
/ / 9 = nine
// 4 = four
// 5 = five

f o r ( l e t [ key , va l u e ] o f myMap . e n t r i e s ( ) ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e )
}
/ / 9 = nine
// 4 = four
// 5 = five

myMap . f o r E a c h ( f u n c t i o n ( va l u e , ke y ) {
co n s o l e . l o g ( ke y + ’ = ’ + va l u e ) } )
Iterating through a Map
Elements are iterated in the same order as they were inserted.
(This could be useful, for example for removing duplicates.)
l e t myMap = new Map ( ) ;
myMap . s e t ( 9 , ’ nine ’ ) ;
myMap . s e t ( 4 , ’ four ’ ) ;
myMap . s e t ( 5 , ’ f i ve ’ ) ;

f o r ( l e t ke y o f myMap . ke ys ( ) ) {
co n s o l e . l o g ( ke y )
}
// 9
// 4
// 5

f o r ( l e t va l u e o f myMap . va l u e s ( ) ) {
co n s o l e . l o g ( va l u e )
}
/ / nine
// four
// five
Maps can be constructed from arrays

l e t k v a r r a y = [ [ ’ key1 ’ , ’ value1 ’ ] , [ ’ key2 ’ , ’ value2 ’ ] ] ;

/ / t h i s Map c o n s t r u c t o r t r a n s f o r m s a 2D ke y−v a l u e A r r a y i n t o a map


l e t myMap = new Map ( k v a r r a y ) ;

myMap . g e t ( ’ key1 ’ ) ; / / r e t u r n s ” value1 ”

/ / Use A r r a y . fr o m ( ) t o t r a n s f o r m a map i n t o a 2D ke y−v a l u e A r r a y


co n s o l e . l o g ( A r r a y . from ( myMap ) ) ;
/ / w i l l show you a s i m i l a r a r r a y t o k v a r r a y
Maps can be merged maintaining key uniqueness

l e t f i r s t = new Map ( [
[ 1 , ’ one ’ ] ,
[ 2 , ’ two ’ ] ,
[ 3 , ’ three ’ ] ,
]);

l e t second = new Map ( [


[ 1 , ’ uno ’ ] ,
[ 2 , ’ dos ’ ]
]);

/ / Merge two maps . The l a s t r e p e a t e d k e y wi n s .


l e t merged = new Map ( [ . . . f i r s t , . . . second ] ) ;

co n s o l e . l o g ( merged . g e t ( 1 ) ) ; / / uno
co n s o l e . l o g ( merged . g e t ( 2 ) ) ; / / dos
co n s o l e . l o g ( merged . g e t ( 3 ) ) ; // three
Motivation Revisited
What is the worst-time complexity, if we use a Map:
(1) check whether a word is a valid word, i.e., it appears in a
dictionary;
(2) eliminate all the duplicates in an array of n words
(assuming there are few duplicates);
(3) for all the words in a text, count up how many times each
word appears in the text.
Motivation Revisited
What is the worst-time complexity, if we use a Map:
(1) check whether a word is a valid word, i.e., it appears in a
dictionary;
(2) eliminate all the duplicates in an array of n words
(assuming there are few duplicates);
(3) for all the words in a text, count up how many times each
word appears in the text.

Answers:
(1) constant time O(1): use Map’s method has to check whether
the word is part of the dictionary
(2) linear time O(n): check whether the word exists in dictionary,
add it only when it does not;
(3) linear time O(n): for each word, check whether it is in
dictionary and increment its associated counter value;
Online Programming
Problem: Assume we have a big string, for example representing the content of a large
book, and we want to be able to quickly perform the following task:
TASK: for some arbitrary input words, we want to quickly display the positions at which
they appear in the text (each may appear multiple times);

Solution (How do we reason?)


(1) Compute the start positions of every word in the text:
Online Programming
Problem: Assume we have a big string, for example representing the content of a large
book, and we want to be able to quickly perform the following task:
TASK: for some arbitrary input words, we want to quickly display the positions at which
they appear in the text (each may appear multiple times);

Solution (How do we reason?)


(1) Compute the start positions of every word in the text:
(1.1) break the text/string into an array of space-separated words;
(1.2) build another array containing the length of each word + 1;
(1.3) build another array containing the prefix sums of (1.2) starting
from 0;
(1.4) build another array that removes the unwanted characters from
each word, e.g., the punctuation at the end ;,.:.

(2) Build a Map in which the keys are distinct words and the values are an array
containing the positions at which that word appears in the text
Online Programming
Problem: Assume we have a big string, for example representing the content of a large
book, and we want to be able to quickly perform the following task:
TASK: for some arbitrary input words, we want to quickly display the positions at which
they appear in the text (each may appear multiple times);

Solution (How do we reason?)


(1) Compute the start positions of every word in the text:
(1.1) break the text/string into an array of space-separated words;
(1.2) build another array containing the length of each word + 1;
(1.3) build another array containing the prefix sums of (1.2) starting
from 0;
(1.4) build another array that removes the unwanted characters from
each word, e.g., the punctuation at the end ;,.:.

(2) Build a Map in which the keys are distinct words and the values are an array
containing the positions at which that word appears in the text
(2.1) build an empty Map;
(2.2) fill it by iterating across the elements of arrays (1.3) and (1.4)

(3) Now we can read the arbitrary words requested by the user, and for each, we querry
the dictionary to find the array of positions, which we display.
Summary
We have learned how to use a dictionary (Map)
constant time query (insert,remove,get);
elements can be iterated in insertion order;
useful to get the right complexity in a number of
word-processing tasks.

You might also like