Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S...

54
Skip Lists Eduardo Nakamura [email protected] 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.

Transcript of Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S...

Page 2: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 3: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 4: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Building a Skip List

lower sentinel upper sentinel

Page 5: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

12–∞ +∞17 25 31 38 44 55

S0

S1

Building a Skip List

Page 6: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 7: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 8: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 9: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 10: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

“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

Page 11: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 12: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 13: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 14: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 15: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 16: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 17: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 18: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 19: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 20: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 21: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 22: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 23: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 24: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 25: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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;

Page 26: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 27: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 28: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 29: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 30: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 31: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 32: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 33: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 34: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 35: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 36: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 37: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 38: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 39: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 40: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 41: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 42: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 43: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 44: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 45: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 46: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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…

Page 47: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 48: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 49: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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;

Page 50: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 51: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 52: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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

Page 53: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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)

Page 54: Skip Lists - Texas A&M University · 2020. 8. 11. · Skip List 1. A list of h+1 lists S h ⊆ S h–1 ⊆ … ⊆ S 1 ⊆ S 0 2. h is the height of the skip list 3. S i+1 has roughly

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