0% found this document useful (0 votes)
7 views50 pages

Lecture09 MapsDictionaries

The document provides an overview of data structures related to maps and dictionaries, including their definitions, operations, and implementations. It discusses various methods such as hash tables, their performance, and collision handling techniques like separate chaining and linear probing. Additionally, it covers algorithms for basic operations like get, put, and remove in the context of maps.

Uploaded by

Đức An Phạm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views50 pages

Lecture09 MapsDictionaries

The document provides an overview of data structures related to maps and dictionaries, including their definitions, operations, and implementations. It discusses various methods such as hash tables, their performance, and collision handling techniques like separate chaining and linear probing. Additionally, it covers algorithms for basic operations like get, put, and remove in the context of maps.

Uploaded by

Đức An Phạm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structures and

Algorithms
"

Maps and Dictionaries!


Outline"

• Maps!
• Hash tables!
• Dictionaries!
• Skip Lists!

Phạm Bảo Sơn - DSA


Maps"
Maps"
• 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!
– student-record database!
Phạm Bảo Sơn - DSA
The Map ADT"
• Map ADT methods:!
– get(k): if the map M has an entry with key k, return
its assoiciated 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
Example"
Operation !Output ! !Map!
!!
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)!

Phạm Bảo Sơn - DSA


A Simple List-Based Map"
• We can efficiently 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!

header nodes/positions trailer

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}!

Phạm Bảo Sơn - DSA


The remove(k) Algorithm"
Algorithm remove(k): ! !!
B =S.positions() ! !!
while B.hasNext() do ""
!p = B.next() ! !!
!if p.element().key() = k then " !!
! !t = p.element().value() ! !!
! !S.remove(p) ! !!
! !n = n – 1 !{decrement number of entries}!
! !return t !{return the removed value}!
return null " "{there is no entry with key equal to k}!
Phạm Bảo Sơn - DSA
Performance of a List-Based
Map"
• Performance:!
– put takes O(n) time since we have to search the sequence to
check if the given key exists (O(1) if keys are always unique) .!
– 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 or for maps in which puts are the
most common operations with unique keys (known
beforehand and simplified put method), while
searches and removals are rarely performed (e.g.,
historical record of logins to a workstation)!
Phạm Bảo Sơn - DSA
Hash Tables"

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"

• Expected time: O(1)!


• Bucket array!
• Hash function!

Phạm Bảo Sơn - DSA


Hash Functions and
Hash Tables"
• A hash function h maps keys of a given type to integers in a fixed
interval [0, N - 1]
• Example:
h(x) = x mod N
is a hash function for integer keys
• The integer h(x) is called the hash value of key x
• A hash table for a given key type consists of
– Hash function h!
– Array (called table) of size N
• When implementing a map with a hash table, the goal is to store
item (k, o) at index i = h(k)

Phạm Bảo Sơn - DSA


Example"
• We design a hash table for 0 ∅
a map storing entries as 1 025-612-0001

(SSN, Name), where SSN 2 981-101-0002


3 ∅
(social security number) is a 4 451-229-0004
nine-digit positive integer!


• 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

Phạm Bảo Sơn - DSA


Drawbacks"

• Space is proportional to N:!


– Waste of space if N >> n!
• Keys are required to be integers in the
range [0, N-1] -> need “good” hashing
function:!
– Minimize collision!
– Fast and easy to compute !

Phạm Bảo Sơn - DSA


Hash Functions"

• A hash function is • The hash code is


usually specified as the applied first, and the
compression function
composition of two
is applied next on the
functions:! result, i.e.,
!Hash code: !h(x) = h2(h1(x))
h1: keys → integers • The goal of the hash
!Compression function: function is to
“disperse” the keys
h2: integers → [0, N - 1] in an apparently
random way!
Phạm Bảo Sơn - DSA
Hash Codes"
• Memory address:! • Component sum:!
– We reinterpret the memory
address of the key object as an – We partition the bits of
integer (default hash code of all the key into components
Java objects)! of fixed length (e.g., 16 or
– Good in general, except for 32 bits) and we sum the
numeric and string keys (same components (ignoring
key should have the same hash
code)!
overflows)!
• Integer cast:! – Suitable for numeric keys
– We reinterpret the bits of the key of fixed length greater
as an integer! than or equal to the
– Suitable for keys of length less number of bits of the
than or equal to the number of integer type (e.g., long
bits of the integer type (e.g., and double in Java)!
byte, short, int and float in Java)!

Phạm Bảo Sơn - DSA


Hash Codes (cont.)"
• Polynomial accumulation:! • Polynomial p(z) can be
– Order is important!
evaluated in O(n) time
– We partition the bits of the key
into a sequence of components using Horner’s rule:!
of fixed length (e.g., 8, 16 or 32 – The following
bits)
! !a0 a1 … an-1! polynomials are
– We evaluate the polynomial! successively computed,
p(z) = an-1 + an-2z + an-3z2 + … each from the previous
… + a0zn-1 one in O(1) time!
!at a fixed value z, ignoring p0(z) = an-1
overflows!
pi (z) = an-i-1 + zpi-1(z)
– Especially suitable for strings
(e.g., the choice z = 33 gives at (i = 1, 2, …, n -1)
most 6 collisions on a set of • We have p(z) = pn-1(z)
50,000 English words)!
Phạm Bảo Sơn - DSA
Compression Functions"
• Division:! • Multiply, Add and
– h2 (y) = y mod N! Divide (MAD):!
– The size N of the hash
table is usually chosen to – h2 (y) = (ay + b) mod N
be a prime ! – N is prime, a and b
– {200, 205, 210, 215,.., are nonnegative
600}: 6 collisions with integers such that
N=100, No collision with ! a mod N ≠ 0
N=101!
– Not enough with – Otherwise, every
repeated patterns of integer would map to
hash codes pN+q for the same value b !
different values of p !
Phạm Bảo Sơn - DSA
Collision Handling"
• Collisions occur when 0 ∅
different elements are 1 025-612-0001

mapped to the same cell! 2 ∅


3 ∅
• Separate Chaining: let 4 451-229-0004 981-101-0004
each cell in the table point
to a linked list of entries
that map there! • Separate chaining is
• Load factor: n/N < 1! simple, but requires
additional memory
outside the table!

Phạm Bảo Sơn - DSA


Map Methods with Separate Chaining
used for Collisions"
• Delegate operations to a list-based map at each cell:!
!

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!

Phạm Bảo Sơn - DSA


Linear Probing"
• Open addressing: the
colliding item is placed in a
• Example:!
different cell of the table" – h(x) = x mod 13
• Linear probing handles
collisions by placing the
– Insert keys 18, 41,
colliding item in the next 22, 44, 59, 32, 31,
(circularly) available table cell! 73, in this order!
• Each table cell inspected is
referred to as a “probe”!
• Colliding items lump together,
causing future collisions to 0 1 2 3 4 5 6 7 8 9 10 11 12
cause a longer sequence of
probes!
41 18 44 59 32 22 31 73
0 1 2 3 4 5 6 7 8 9 10 11 12
Phạm Bảo Sơn - DSA
Search with Linear Probing"
• Consider a hash table A that Algorithm get(k)
uses linear probing! i ← h(k)
p←0
• get(k)
repeat
– We start at cell h(k) !
c ← A[i]
– We probe consecutive
if c = ∅

locations until one of the



return null
following occurs!
• An item with key k is
else if c.key () = k
found, or! return c.element()
• An empty cell is found, or! else
• N cells have been i ← (i + 1) mod N
unsuccessfully probed ! p←p+1
until p = N

return null

Phạm Bảo Sơn - DSA


Updates with Linear Probing"
• To handle insertions and • put(k, o)!
deletions, we introduce – We throw an exception
a special object, called if the table is full!
AVAILABLE, which
replaces deleted – We start at cell h(k) !
elements! – We probe consecutive
• remove(k)! cells until one of the
– We search for an entry following occurs!
with key k ! • A cell i is found that is
either empty or stores
– If such an entry (k, o) is AVAILABLE, or!
found, we replace it with • N cells have been
the special item
AVAILABLE and we unsuccessfully probed!
return element o! – We store entry (k, o) in
– Else, we return null cell i
Phạm Bảo Sơn - DSA
Double Hashing"
• Double hashing uses a
secondary hash function d(k) • Common choice of
and handles collisions by compression function for the
placing an item in the first secondary hash function: !!
available cell of the series d2(k) = q – (k mod q)
(i + jd(k)) mod N ! !where!
for j = 0, 1, … , N - 1 – q<N
• The secondary hash function – q is a prime!
d(k) cannot have zero values! • The possible values for d2(k)
• The table size N must be a are
prime to allow probing of all ! 1, 2, … , q
the cells!

Phạm Bảo Sơn - DSA


Example of Double Hashing"
k h (k ) d (k ) Probes
• Consider a hash 18 5 3 5
table storing integer 41 2 1 2
22 9 6 9
keys that handles 44 5 5 5 10
collision with double 59 7 4 7
32 6 3 6
hashing! 31 5 4 5 9 0
– N = 13 ! 73 8 4 8
– h(k) = k mod 13 !
– d(k) = 7 - k mod 7
0 1 2 3 4 5 6 7 8 9 10 11 12
• Insert keys 18, 41,
22, 44, 59, 32, 31,
73, in this order! 31 41 18 32 59 73 22 44
0 1 2 3 4 5 6 7 8 9 10 11 12
Phạm Bảo Sơn - DSA
Performance of
Hashing"
• In the worst case, searches,
insertions and removals on a • The expected running time
hash table take O(n) time! of all the dictionary ADT
• The worst case occurs when operations in a hash table is
all the keys inserted into the O(1) !
map collide! • In practice, hashing is very
• The load factor α = n/N fast provided the load factor
affects the performance of a is not close to 100%!
hash table! • Applications of hash tables:!
• Assuming that the hash – small databases!
values are like random – compilers!
numbers, it can be shown – browser caches!
that the expected number of • Open addressing is not faster
probes for an insertion with than chaining method if space is
open addressing is an issue.
!1 / (1 - α)

Phạm Bảo Sơn - DSA
Example"

• Counting Word Frequencies.!

Phạm Bảo Sơn - DSA


Dictionaries"

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

Phạm Bảo Sơn - DSA


Example"
Operation ! !Output! !Dictionary !!
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(8,D) ! !(8,D) ! !(5,A),(7,B),(2,C),(8,D)!
insert(2,E) ! !(2,E) ! !(5,A),(7,B),(2,C),(8,D),(2,E)!
find(7) ! ! !(7,B) ! !(5,A),(7,B),(2,C),(8,D),(2,E)!
find(4) ! ! !null " "(5,A),(7,B),(2,C),(8,D),(2,E)!
find(2) ! ! !(2,C) ! !(5,A),(7,B),(2,C),(8,D),(2,E)!
findAll(2) ! !(2,C),(2,E) !(5,A),(7,B),(2,C),(8,D),(2,E)!
size() ! ! !5 ! !(5,A),(7,B),(2,C),(8,D),(2,E)!
remove(find(5))! !(5,A) ! !(7,B),(2,C),(8,D),(2,E)!
find(5) ! ! !null " "(7,B),(2,C),(8,D),(2,E)!
!

Phạm Bảo Sơn - DSA


A List-Based Dictionary"
• A log file or audit trail is a dictionary implemented by means of
an unsorted sequence!
– We store the items of the dictionary in a sequence (based on a
doubly-linked list or array), in arbitrary order!
• Performance:!
– insert takes O(1) time since we can insert the new item at the
beginning or at the end of the sequence!
– find 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 log file is effective only for dictionaries of small size or for
dictionaries on which insertions are the most common
operations, while searches and removals are rarely performed
(e.g., historical record of logins to a workstation)!

Phạm Bảo Sơn - DSA


The findAll(k) Algorithm"
Algorithm findAll(k): ! !!
Input: A key k ! !!
Output: An iterator of entries with key equal to k !
!!
Create an initially-empty list L ! !!
B = D.entries() ! !!
while B.hasNext() do " !!
!e = B.next() ! !!
"if e.key() = k then " !!
! !L.insertLast(e) ! !!
return L.elements()!
!
Phạm Bảo Sơn - DSA
The insert and remove
Methods"
Algorithm insert(k,v): ! !!
Input: A key k and value v ! !!
Output: The entry (k,v) added to D ! !!
Create a new entry e = (k,v) !!
S.insertLast(e) !{S is unordered}!
return e!
!!
Algorithm remove(e): ! !!
Input: An entry e ! !!
Output: The removed entry e or null if e was not in D !!
{We don’t assume here that e stores its location in S}!
B = S.positions() ! !!
while B.hasNext() do " !!
!p = B.next() ! !!
"if p.element() = e then " !!
! !S.remove(p) ! !!
" "return e ! !!
return null "{there is no entry e in D}!
!
Phạm Bảo Sơn - DSA
Hash Table Implementation"

• 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.!

Phạm Bảo Sơn - DSA


Binary Search"
• Ordered dictionaries.!
• Binary search performs operation find(k) on a dictionary implemented by
means of an array-based sequence, sorted by key!
– similar to the high-low game!
– at each step, the number of candidate items is halved!
– terminates after a logarithmic number of steps!
• Example: find(7)!

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

Phạm Bảo Sơn - DSA


Skip Lists"

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!

Phạm Bảo Sơn - DSA


Insertion"
• To insert an entry (x, o) into a skip list, we use a randomized
algorithm:!
– We repeatedly toss a coin until we get tails, and we denote with i
the number of times the coin came up heads!
– If i ≥ h, we add to the skip list new lists Sh+1, … , Si +1, each
containing only the two special keys!
– We search for x in the skip list and find the positions p0, p1 , …, pi of
the items with largest key less than x in each list S0, S1, … , Si!
– For j ← 0, …, i, we insert item (x, o) into list Sj after position pj
• Example: insert key 15, with i = 2
S3 -∞ +∞
p2
S2 -∞ +∞ S2 -∞ 15 +∞
p1
S1 -∞ 23 +∞ S1 -∞ 15 23 +∞
p0
S0 -∞ 10 23 36 +∞ S0 -∞ 10 15 23 36 +∞
Phạm Bảo Sơn - DSA
Deletion"
• To remove an entry with key x from a skip list, we proceed as
follows:!
– We search for x in the skip list and find the positions p0, p1 , …, pi of
the items with key x, where position pj is in list Sj
– We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si!
– We remove all but one list containing only the two special keys!
• Example: remove key 34!

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 !

Phạm Bảo Sơn - DSA


Space Usage"
• Consider a skip list with n
• The space used by a skip list entries!
depends on the random bits
– By Fact 1, we insert an entry
used by each invocation of the in list Si with probability 1/2i!
insertion algorithm!
– By Fact 2, the expected size
• We use the following two basic of list Si is n/2i !
probabilistic facts:! • The expected number of
Fact 1: The probability of getting i nodes used by the skip list is!
consecutive heads when
flipping a coin is 1/2i
h h
Fact 2: If each of n entries is n 1
present in a set with probability ∑ 2i ∑ 2i < 2n
= n
p, the expected size of the set i =0 i =0
is np ! Thus, the expected space
usage of a skip list with n
items is O(n)
Phạm Bảo Sơn - DSA
Height"
• The running time of the • Consider a skip list with n
search an insertion entires!
algorithms is affected by the – By Fact 1, we insert an entry
in list Si with probability 1/2i!
height h of the skip list!
– By Fact 3, the probability that
• We show that with high list Si has at least one item is
probability, a skip list with n at most n/2i!
items has height O(log n) • By picking i = 3log n, we have
• We use the following that the probability that S3log n
additional probabilistic fact:! has at least one entry is
at most
Fact 3: If each of n events has
! n/23log n = n/n3 = 1/n2
probability p, the probability
that at least one event • Thus a skip list with n entries
occurs is at most np has height at most 3log n with
probability at least 1 - 1/n2

Phạm Bảo Sơn - DSA


Search and Update Time"
• The search time in a skip list • When we scan forward in a list,
is proportional to! the destination key does not
– the number of drop-down belong to a higher list!
steps, plus! – A scan-forward step is
– the number of scan-forward associated with a former coin
steps! toss that gave tails
• The drop-down steps are • By Fact 4, in each list the
bounded by the height of the expected number of scan-
skip list and thus are O(log n) forward steps is 2!
with high probability! • Thus, the expected number of
• To analyze the scan-forward scan-forward steps is O(log n)!
steps, we use yet another • We conclude that a search in a
probabilistic fact:! skip list takes O(log n)
Fact 4: The expected number of expected time!
coin tosses required in order • The analysis of insertion and
to get tails is 2!
deletion gives similar results!
Phạm Bảo Sơn - DSA
Summary"
• A skip list is a data • Using a more complex
structure for dictionaries probabilistic analysis,
that uses a randomized one can show that
insertion algorithm! these performance
• In a skip list with n bounds also hold with
entries ! high probability!
– The expected space • Skip lists are fast and
used is O(n) simple to implement in
– The expected search, practice!
insertion and deletion
time is O(log n)

Phạm Bảo Sơn - DSA

You might also like