skip lists - people.engr.tamu.eduskip list 1. a list of h+1 lists s h ⊆ s h–1 ⊆ … ⊆ s 1...

Post on 21-Feb-2021

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Skip ListsEduardo Nakamura

nakamura@tamu.edu

Icons made by Freepik, Smashicons, Roundicons, Flat Icons, Cursor Creative, Vectors Market, Pixel Perfect, DinosoftLabs, Nikita Golubev, Icon Monk, Pixel Buddha from https://www.flaticon.com and is licensed by Creative Commons BY 3.0. For more, check the Flaticon Basic License. These slides should not be published or distributed without the author’s permission.

Outline and Reading

Skip list (§9.4)

Search and update operations in a skip list (§9.4.1)

Probabilistic analysis of skip lists (§9.4.2)

Skip List

1. A list of h+1 lists Sh ⊆ Sh–1 ⊆ … ⊆ S1 ⊆ S0

2. h is the height of the skip list

3. Si+1 has roughly half of the elements from Si, randomly chosen

S0 has all entries (key;value) in non-decreasing order

Sh has no entry (key;value)

4. Search “mimics” a binary search

Sketching the idea

12–∞ +∞17 20 25 31 38 39 44 50 55S0

Building a Skip List

lower sentinel upper sentinel

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

S0

S1

Building a Skip List

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

S0

S1

S2

Building a Skip List

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

S0

S1

S2

S3

Building a Skip List

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

S0

S1

S2

S3

S4

Building a Skip List

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Building a Skip List

h = 5 (height of the skip list)

“Quadruply linked list” (quad-nodes)

key and value Pointer to up, down, left, and right nodes

Traverse horizontally and vertically using

above() and below()

before() and after()

Bounded by sentinels (–∞ and +∞)

Implementing a skip list

above()

below()

before() after()0 B

righ

t

left

up

down

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

50

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Searching a Skip List

skipSearch(50)

Pseudo-code for skipSearch()

Algorithm skipSearch(k)Input: A search key kOutput: Position p in the list S0 such that the entry at p has key ≤ k

p ← Sh.begin();

while below(p) ≠ null do

p ← below(p); //drop down

while k ≥ key(after(p)) do

p ← after(p); //scan forward

end while

end while

return p;

Pseudo-code for find()

Algorithm find(k)Input: A search key kOutput: Position p in the list S0 such that the entry at p has key = k, null otherwise

p ← skipSearch();

if key(p) ≠ k thenreturn null;

elsereturn p;

end if

Exercise

1. Search for key 55 (show the visited nodes and links)

2. Search for key 20 (show the visited nodes and links)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Exercise

1. Search for key 55 (show the visited nodes and links)

2. Search for key 20 (show the visited nodes and links)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Exercise

1. Search for key 55 (show the visited nodes and links)

2. Search for key 20 (show the visited nodes and links)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 31 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 31 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 31 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Removing from a Skip List

erase(31)

Pseudo-code for erase()

Algorithm erase(k)Input: Key kOutput: None

current ← skipSearch(k);

if key(current) ≠ k then

return;

end if

current ← above(current);

doerase(current.below()); //erase lower levelcurrent ← above(current); //move to upper level

until current = null

Exercise

1. Remove for key 55 (show the visited nodes and links)

2. Remove for key 17 (show the visited nodes and links)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

41

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

41

41

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

41

41

41

In this case (tails), the

algorithm would stop…

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

insert(41)

41

41

41

41

41

41

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Inserting in a Skip List

41

41

41

41

41

–∞ +∞S5

41

Pseudo-code for insert(k,v)Algorithm insert(k,v):

Input: Key k and value vOutput: Topmost position of the entry inserted in the listp ← skipSearch(k);q ← null;e ← (k,v);i ← −1;repeat

i ← i + 1;if i ≥ h then

h ← h + 1; // add a new level to the skip list t ← after(s); // s is Sh.begin() s ← insertAfterAbove(null,s,(−∞,null));insertAfterAbove(s,t,(+∞,null));

end ifwhile above(p) = null do

p ← before(p); //scan backwardp ← above(p); //jump up to higher level q ← insertAfterAbove(p,q,e); //add to the new tower

end while until coinFlip() = tails;n ← n + 1;return q;

Exercise

1. Insert key 22 (show the visited nodes and links)

2. Insert for key 57 (show the visited nodes and links)

12–∞ +∞17 20 25 38 39 44 50 55

12–∞ +∞17 25 38 44 55

–∞ +∞17 25 55

–∞ +∞17 55

–∞ +∞17

–∞ +∞

S0

S1

S2

S3

S4

S5

Space analysis

Fact 1:The probability of i consecutive heads by flipping a coin is 1/2i

Fact 2: If each of n items is present in a set with probability P, the expected

size of the set is nP

By Fact 1: we insert an item in list Si with probability 1/2i

# of nodes =

By Fact 2: the expected size of list Si is n/2i , where n is the # of items

∑h

i = 0

n2i

= ∑h

i = 0

n 2–i < 2n

< 1

= 2n 1 –1

2h+1

Space analysis

Space is O(n)

# of nodes = ∑h

i = 0

n2i

= ∑h

i = 0

n 2–i < 2n

< 1

= 2n 1 –1

2h+1

Time analysis (find)

Fact 3: The expected number of coin tosses required to get tails is 2

Two things are accounted for time analysis: 1. the number of drop-down steps, plus 2. the number of scan-forward steps

Drop-down: O(h) = O(log n)

Scan-forward: O(log n) = O(1) (fact 3) for each O(log n) levels

Expected time is O(log n)

A skip list is a data structure for dictionaries

Randomized insertion algorithm

In a skip list with n items

The expected space used is O(n)

The expected search, insertion and deletion time is O(log n)

Skip lists are fast and simple to implement in practice

Summary

top related