Lecture09 MapsDictionaries
Lecture09 MapsDictionaries
Algorithms
"
• Maps!
• Hash tables!
• Dictionaries!
• Skip Lists!
9 c 6 c 5 c 8 c
entries
Phạm Bảo Sơn - DSA
The get(k) Algorithm"
Algorithm get(k):!
!B = S.positions() {B is an iterator of the positions in S}"
"while B.hasNext() do"
! !p = B.next() !{the next position in B}!
" "if p.element().key() = k !then"
" " "return p.element().value()!
"return null {there is no entry with key equal to k}!
!
!
!
Phạm Bảo Sơn - DSA
The put(k,v) Algorithm"
Algorithm put(k,v): ! ! ! !!
B != S.positions() ! !!
while B.hasNext() do ""
!p = B.next() ! !!
!if p.element().key() = k then" !!
! !t = p.element().value() !!
! !B.replace(p,(k,v)) !!
! !return t!{return the old value} !!
S.insertLast((k,v)) ! ! !!
n=n+1 !{increment variable storing number of entries}!
return null "{there was no previous entry with key equal to k}!
0 ∅
1 025-612-0001
2 981-101-0002
3 ∅
4 451-229-0004
Recall the Map ADT"
• Map ADT methods:!
– get(k): if the map M has an entry with key k, return
its associated value; else, return null !
– put(k, v): insert entry (k, v) into the map M; if key k
is not already in M, then return null; else, return
old value associated with k!
– remove(k): if the map M has an entry with key k,
remove it from M and return its associated value;
else, return null !
– size(), isEmpty()!
– keys(): return an iterator of the keys in M!
– values(): return an iterator of the values in M!
Phạm Bảo Sơn - DSA
Hash table"
…
• Our hash table uses an
array of size N = 10,000 and 9997 ∅
9998 200-751-9998
the hash function 9999 ∅
h(x) = last four digits of x
Algorithm get(k): ! !!
Output: The value associated with the key k in the map, or null if there is no !!
!entry with key equal to k in the map !!
return A[h(k)].get(k) !{delegate the get to the list-based map at A[h(k)]}!
!!
Algorithm put(k,v): ! !!
Output: If there is an existing entry in our map with key equal to k, then we !!
!return its value (replacing it with v); otherwise, we return null ""
t = A[h(k)].put(k,v) !{delegate the put to the list-based map at A[h(k)]}!
if t = null then " "{k is a new key}!
!n = n + 1 !!
return t!
!!
Algorithm remove(k): ! !!
Output: The (removed) value associated with key k in the map, or null if there!
!is no entry with key equal to k in the map !!
t = A[h(k)].remove(k) {delegate the remove to the list-based map at A[h(k)]}!
if t ≠ null then " {k was found}!
!n = n - 1 !!
return t!
<
6
2 9
>
1 4 =
8
Dictionary ADT"
• Dictionary ADT methods:!
• The dictionary ADT models a – find(k): if the dictionary has
searchable collection of key- an entry with key k, returns
element entries: ordered and it, else, returns null !
unordered.!
– findAll(k): returns an iterator
• The main operations of a dictionary of all entries with key k!
are searching, inserting, and
deleting items! – insert(k, o): inserts and
returns the entry (k, o) !
• Multiple items with the same key
– remove(e): remove the entry
are allowed!
e from the dictionary!
• Applications:!
– entries(): returns an iterator
– word-definition pairs! of the entries in the
– credit card authorizations! dictionary!
– DNS mapping of host names (e.g.,
datastructures.net) to internet IP – size(), isEmpty()!
addresses (e.g., 128.148.34.101)!
• Unordered dictionaries.!
• We can also create a hash-table
dictionary implementation.!
• If we use separate chaining to handle
collisions, then each operation can be
delegated to a list-based dictionary
stored at each hash table cell.!
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
Phạm Bảo Sơn - DSA l=m =h
Search Table"
• A search table is a dictionary implemented by means of a sorted
array!
– We store the items of the dictionary in an array-based sequence,
sorted by key!
– We use an external comparator for the keys!
• Performance:!
– find takes O(log n) time, using binary search!
– insert takes O(n) time since in the worst case we have to shift n/2
items to make room for the new item!
– remove takes O(n) time since in the worst case we have to shift n/2
items to compact the items after the removal!
• A search table is effective only for dictionaries of small size or
for dictionaries on which searches are the most common
operations, while insertions and removals are rarely performed
(e.g., credit card authorizations)!
S3 -∞ +∞
S2 -∞ 15 +∞
S1 -∞ 15 23 +∞
S0 -∞ 10 15 23 36 +∞
What is a Skip List"
• A skip list for a set S of distinct (key, element) items is a series of
lists S0, S1 , … , Sh such that!
– Each list Si contains the special keys +∞ and -∞ !
– List S0 contains the keys of S in nondecreasing order
– Each list is a subsequence of the previous one, i.e.,
! ! !S0 ⊆ S1 ⊆ … ⊆ Sh
– List Sh contains only the two special keys!
• We show how to use a skip list to implement the dictionary ADT!
S3 -∞ +∞
S2 -∞ 31 +∞
S1 -∞ 23 31 34 64 +∞
S0 -∞ 12 23 26 31 34 44 56 64 78 +∞
Phạm Bảo Sơn - DSA
Search"
• We search for a key x in a a skip list as follows:!
– We start at the first position of the top list !
– At the current position p, we compare x with y ← key(next(p))
x = y: we return element(next(p))
x > y: we “scan forward” !
x < y: we “drop down”!
– If we try to drop down past the bottom list, we return null
• Example: search for 78!
S3 -∞ +∞
S2 -∞ 31 +∞
S1 -∞ 23 31 34 64 +∞
S0 -∞ 12 23 26 31 34 44 56 64 78 +∞
Phạm Bảo Sơn - DSA
Randomized Algorithms"
• A randomized algorithm • We analyze the expected
performs coin tosses (i.e., running time of a randomized
uses random bits) to control algorithm under the following
its execution! assumptions!
– the coins are unbiased, and !
• It contains statements of the – the coin tosses are independent!
type! • The worst-case running time of
b ← random() a randomized algorithm is often
if b = 0 large but has very low
do A … probability (e.g., it occurs when
else { b = 1} all the coin tosses give
“heads”)!
do B …
• We use a randomized algorithm
• Its running time depends on to insert items into a skip list!
the outcomes of the coin
tosses!
S3 -∞ +∞
p2
S2 -∞ 34 +∞ S2 -∞ +∞
p1
S1 -∞ 23 34 +∞ S1 -∞ 23 +∞
p0
S0 -∞ 12 23 34 45 +∞ S0 -∞ 12 23 45 +∞
Phạm Bảo Sơn - DSA
Implementation"
• We can implement a skip list
with quad-nodes!
• A quad-node stores:!
– entry!
– link to the node prev! quad-node
– link to the node next!
– link to the node below! x
– link to the node above!
• Also, we define special keys
PLUS_INF and MINUS_INF,
and we modify the key
comparator to handle them !