Skip Lists: Supports Same Operations As Red-Black Tree. Store Keys in Sorted Linked List, With The Following Twist
Skip Lists: Supports Same Operations As Red-Black Tree. Store Keys in Sorted Linked List, With The Following Twist
Skip Lists
Supports same operations as red-black tree.
Key Idea: Store keys in sorted linked list, with
the following twist:
Provide extra pointers so that portions of the list can
be skipped over while searching.
Jim Anderson Comp 750, Fall 2009 SL - 2
Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Level Rule: A fraction p of the nodes with level i pointers also have
level i+1 pointers (p = 1/2 above). Levels determined probabilistically.
Jim Anderson Comp 750, Fall 2009 SL - 3
Operations
Min, Max, Succ, Pred:
O(1) time, assuming doubly linked list with both
head and tail pointers.
In the following, we assume singly-linked list, as in
the paper.
Search:
O(lg n) average case, O(n) worst case.
Delete, Insert:
Proportional to search time.
Our analysis will focus on Search.
Jim Anderson Comp 750, Fall 2009 SL - 4
A Randomized Algorithm
Or is it?
The level of a node is picked at random (subject
to Level Rule) from 1,, MaxLevel.
Note: In real life, we often use pseudo random
number generators
Example:
What are the
consequences
of this?
Z
0
:= seed;
Z
i
:= (aZ
i
+ c)(mod m);
U
i
:= Z
i
/m
Jim Anderson Comp 750, Fall 2009 SL - 5
Properties
Advantages:
Much easier to code than red-black trees.
Faster than other schemes [according to Pugh].
Disadvantages:
Not quite as fast as red-black trees in practice
[solution manual for Corman et al.]. ???
Jim Anderson Comp 750, Fall 2009 SL - 6
Search
Search(list, searchkey)
x := listheader;
for i := listlevel downto 1 do
while xforward[i]key < searchkey do
x := xforward[i]
od
od;
x := xforward[1];
if xkey = searchkey then
return xvalue
else
return NIL
fi
Jim Anderson Comp 750, Fall 2009 SL - 7
Insert
Insert(list, searchkey, newvalue)
x := listheader;
for i := listlevel downto 1 do
while xforward[i]key < searchkey do
x := xforward[i]
od;
update[i] := x
od;
x := xforward[1];
if xkey = searchkey then
xvalue := newvalue
else
newlevel := random(1,,MaxLevel);
if newlevel > listlevel then
for i := listlevel + 1 to newlevel do
update[i] := listheader
od;
listlevel := newlevel
fi;
x := MakeNode(newlevel, searchkey, newvalue);
for i := 1 to newlevel do
xforward[i] := update[i]forward[i];
update[i]forward[i] := x
od
fi
Key Idea: Search,
then splice in.
Jim Anderson Comp 750, Fall 2009 SL - 8
Delete
Delete(list, searchkey)
x := listheader;
for i := listlevel downto 1 do
while xforward[i]key < searchkey do
x := xforward[i]
od;
update[i] := x
od;
x := xforward[1];
if xkey = searchkey then
for i := 1 to listlevel do
if update[i]forward[i] = x then
break
fi;
update[i]forward[i] := xforward[i]
od;
free(x);
while listlevel > 1 and listheaderforward[listlevel] = NIL do
listlevel := listlevel 1
od
fi
Key Idea: Search,
then splice out.
Jim Anderson Comp 750, Fall 2009 SL - 9
Average-Case Time Analysis
3
6
12
15
17
MaxLevel
M
L(n)
21
Key: Deduce expected search time.
Trick: Trace search path backwards.
Example: Search(21)
Think of taking this path in reverse.
Want to know the expected number of hops to get to the beginning
of the path.
Let n = number of nodes at time of search.
MaxLevel is assumed to be
sufficiently big, i.e., at
least L(n).
Jim Anderson Comp 750, Fall 2009 SL - 10
Aside: A Useful Probability
Question: What is P[arbitrary node has a pointer at level k]?
Arbitrary node x
has a level 1 pointer with probability 1,
has a level 2 pointer with probability p,
has a level 3 pointer with probability p
2
,
has a level k pointer with probability p
k-1
.
Thus, P[arbitrary node has a pointer at level k] = p
k-1
.
Jim Anderson Comp 750, Fall 2009 SL - 11
Definition of Level L(n)
Definition: Level L(n) = log
1/p
(n).
Significance of L(n): Expected number of nodes with a pointer at level
L(n) is 1/p work done above level L(n) is in the noise.
Proof:
E[number of nodes with pointer at level L(n)]
= n P[arbitrary node has a pointer at level L(n)]
= 1/p
= p
L(n)-1
= (1/p) p
log
1/p
n
= 1/(pn)
n
k
n
n
p
p
n
p n k
p k
p
p
p
p
p
p p p
n
p
1
log
log
1
log
log
log
log log log
Let
1
/ 1
/ 1
/ 1
/ 1 / 1 / 1
log
/ 1
=
=
=
=
=
=
=
=
|
|
.
|
\
|
|
|
.
|
\
|
=
|
|
.
|
\
|
=
= =
) 1 (
1
1
) 1 (
)! ( )! 1 (
!
) 1 (
, defining
] 1 [ ] [
] [ P
) ( E
1
1
1
1 ) (
1 ) ( 1 ) (
0
0
slide} previous on shown {as
1
} of n {definitio
)] 1 ( [
) 1 (
1
) 1 (
1
1
1 ) (
1
) 1 (
1
0
) 1 ( ) 1 ( 1
1
p
q p n
nq
q q nq
q q
j
n
nq
q q
i
n
nq
n L
n
j n j
n
j
i n i
n
i
=
=
=
+ =
|
|
.
|
\
|
=
|
|
.
|
\
|
=
=
=
s
=
+ =
+ =
+ =
A is O(lg n)
Jim Anderson Comp 750, Fall 2009 SL - 17
Another Derivation
Note that if p = 1/3, then when tracing the path backwards, we move
left twice as often as up.
In general,
Total moves = moves up + moves left
p
n L
n L
p
n L
p
n L
n L
p
p
n L
1 ) (
1 ) (
1
) (
1
1 ) (
) 1 ) ( (
1
1 ) (
=
+ + =
+ =
Jim Anderson Comp 750, Fall 2009 SL - 18
From First Principles
If such quick derivations
make you feel uncomfortable,
you can always use first
principles
Let the random variable V
k
denote the number of hops
required to climb up k levels,
i.e., C(k) = E[V
k
].
|
|
.
|
\
|
=
=
=
|
|
.
|
\
|
|
|
.
|
\
|
=
|
|
.
|
\
|
= =
= =
k i
k i k
k i k
k i
k i k
k
k i
k
k
p p
k
i
k
k i k
i
k
k i k
i
i
k
i
i
p p
k
i
i
p p p
k
i
i V
i V i
V
) 1 (
)! ( !
!
)]! 1 ( ) 1 [( )! 1 (
)! 1 (
1
1
that Note
) 1 (
1
1
) 1 (
1
1
] [ P that Note
] [ P
] [ E
) 1 ( ) 1 ( 1
Jim Anderson Comp 750, Fall 2009 SL - 19
First Principles (Continued)
(
|
|
.
|
\
|
+ =
|
|
.
|
\
|
=
|
|
.
|
\
|
|
|
.
|
\
|
+ =
=
+
k i
k i
k
k i
k i
k
k i k
k i
k i k
k i
p
k
i
kp
p
k
i
kp
k C
... k C
k C
p p
k
i
k
p p
k
i
k
k C
) 1 ( 1
) 1 (
) (
) 1 ( of s term
in ) ( define try to Now,
) 1 (
1
1
) 1 (
) 1 (
1
) 1 (
) 1 (
1
1
1 1
1
) ( ) 1 ( ) 1 (
1
) 1 ( ) 1 (
) 1 (
1
1
) 1 (
1
) 1 ( ) 1 ( ) 1 (
1
1
) 1 (
1
) 1 (
1
1
) 1 (
1
) 1 (
1
1
1
CLRS), of 7 - C.1 Ex. (See
1
1
1
g Usin
1
1
1 1
k C p k pC
k
k
p
k
i
kp p
p
k
i
p k p
k
k
p
k
i
p p
k
i
kp
p
k
i
p
k
i
kp
p
k
i
p
k
i
kp
k
i
k
i
k
i
k i
k i
k
k i
k i
k
k i
k i
k i
k i
k
k i
k i
k i
k i
k
k i
k i
k i
k i
k
+
|
|
.
|
\
|
+
|
|
.
|
\
|
=
(
|
|
.
|
\
|
+
|
|
.
|
\
|
=
(
|
|
.
|
\
|
+
|
|
.
|
\
|
=
(
|
|
.
|
\
|
+
|
|
.
|
\
|
+ =
|
|
.
|
\
|
+
|
|
.
|
\
|
=
|
|
.
|
\
|
+ =
+ =
+ =
Jim Anderson Comp 750, Fall 2009 SL - 20
First Principles (Continued)
) 1 (
) 2 (
2
) 1 (
1
) (
) 1 (
1
) (
) ( ) 1 ( ) 1 (
1
) (
So,
kC
k C
k
k
k C
k
k
k C
k pC
k
k
k pC
k C p k pC
k
k
k C
=
=
+
p
k
k C
p
p
p
p
p
p
p
p i
p
p
p i p C
i
i
i
i
=
=
=
=
=
) ( Thus,
1
1
)] 1 ( 1 [
1
1
) 1 (
1
) 1 ( ) 1 (
2
2
0
1
1
Makes sense: no. of hops to climb
k levels is k times the no. of hops to
climb one level.
Jim Anderson Comp 750, Fall 2009 SL - 21
Computation of C
Let random variable M = maximum level at time of search.
Let random variable A = M L(n), i.e., C = E[A].
Let ML be a shorthand for the constant MaxLevel.
| |
=
+ > =
= + + >
+ + > + > +
+ > + > +
+ > + > =
+ + > + > =
+ = =
= A s
= A =
) (
1
) (
1
) (
1
) (
0
) (
) ( 1
] ) ( [ P
0 ] 1 ) ( ) ( [ P : Note
] 1 ) ( ) ( [ P )) ( ( )] ( ) ( [ P )) ( (
] 3 ) ( [ P 2 ] 2 ) ( [ P 2
] 2 ) ( [ P ] 1 ) ( [ P
] 1 ) ( [ P ] ) ( [ P
] ) ( [ P
] [ P
] [ P
n L ML
i
n L ML
i
n L ML
i
n L ML
i
n L ML
n L i
i n L M
n L ML n L M
n L ML n L M n L ML n L ML n L M n L ML
n L M n L M
n L M n L M
i n L M i n L M i
i n L M i
i i
i i
C
=
s
=
=
=
s
+ >
+ > s
=
=
=
+
=
1
1
} / 1 before, shown {as
]} ) ( [ P for derived we expression the {using
] ) ( [ P
} previously shown {as
0
1 ) (
0
) (
1
1
) (
) (
1
1 ) (
) (
1
Jim Anderson Comp 750, Fall 2009 SL - 24
Putting it all Together
The length of the search path is upper bounded by
) (log
1
1
log
1
1 ) (
1
1 1 1 ) (
/ 1
n O
p p
n
p p
n L
p p p
n L
C B A
p
=
+ =
+ =
+ +
s
+ +
Jim Anderson Comp 750, Fall 2009 SL - 25
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
Jim Anderson Comp 750, Fall 2009 SL - 26
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x 6 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 27
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
yes!
Jim Anderson Comp 750, Fall 2009 SL - 28
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
< 21?
Jim Anderson Comp 750, Fall 2009 SL - 29
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
no!
Jim Anderson Comp 750, Fall 2009 SL - 30
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
25 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 31
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
no!
Jim Anderson Comp 750, Fall 2009 SL - 32
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
9 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 33
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
yes!
Jim Anderson Comp 750, Fall 2009 SL - 34
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
17 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 35
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
yes!
Jim Anderson Comp 750, Fall 2009 SL - 36
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
25 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 37
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
No!
Jim Anderson Comp 750, Fall 2009 SL - 38
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
19 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 39
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
yes!
Jim Anderson Comp 750, Fall 2009 SL - 40
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
21 < 21?
Jim Anderson Comp 750, Fall 2009 SL - 41
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
no!
Jim Anderson Comp 750, Fall 2009 SL - 42
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
loop terminates -- move
forward one
Jim Anderson Comp 750, Fall 2009 SL - 43
Search Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Search(21)
x
return value associated with key 21
Jim Anderson Comp 750, Fall 2009 SL - 44
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
Jim Anderson Comp 750, Fall 2009 SL - 45
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x 6 < 22?
Jim Anderson Comp 750, Fall 2009 SL - 46
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
yes!
Jim Anderson Comp 750, Fall 2009 SL - 47
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
< 22?
Jim Anderson Comp 750, Fall 2009 SL - 48
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
no!
update[4]
Jim Anderson Comp 750, Fall 2009 SL - 49
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
25 < 22?
update[4]
Jim Anderson Comp 750, Fall 2009 SL - 50
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
no!
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 51
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
9 < 22?
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 52
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
yes!
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 53
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
17 < 22?
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 54
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
yes!
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 55
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
25 < 22?
update[4] update[3]
Jim Anderson Comp 750, Fall 2009 SL - 56
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
No!
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 57
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
19 < 22?
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 58
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
yes!
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 59
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
21 < 22?
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 60
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
yes!
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 61
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
25 < 22?
update[4] update[3]
update[2]
Jim Anderson Comp 750, Fall 2009 SL - 62
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
no!
update[4] update[3]
update[2]
update[1]
Jim Anderson Comp 750, Fall 2009 SL - 63
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x end loop,
advance x
one place
update[4] update[3]
update[2]
update[1]
Jim Anderson Comp 750, Fall 2009 SL - 64
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
25 = 22,
so insert
22.
Suppose
xs level
is 5.
update[4] update[3]
update[2]
update[1]
Jim Anderson Comp 750, Fall 2009 SL - 65
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
increase
current
list level
to 5.
update[4] update[3]
update[2]
update[1]
update[5]
Jim Anderson Comp 750, Fall 2009 SL - 66
Insert Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Insert(22)
x
Make
new node
and splice
in.
update[4] update[3]
update[2]
update[1]
update[5]
22
Jim Anderson Comp 750, Fall 2009 SL - 67
Delete Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Delete(22)
x
After the
search
part of
Delete,
we have
this.
update[4] update[3]
update[2]
update[1]
update[5]
22
Jim Anderson Comp 750, Fall 2009 SL - 68
Delete Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Delete(22)
Splice
out 22.
update[4] update[3]
update[2]
update[1]
update[5]
Jim Anderson Comp 750, Fall 2009 SL - 69
Delete Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 4
hd ptr
Level 1
MaxLevel
(a constant)
Head
Delete(22)
Decrement
list level.
update[4] update[3]
update[2]
update[1]
update[5]
Jim Anderson Comp 750, Fall 2009 SL - 70
Another Delete Example
3
6
7
9
12
17
19 21
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Delete(21)
x
After the
search
part of
Delete,
we have
this.
update[4] update[3]
update[2]
update[1]
update[5]
22
Jim Anderson Comp 750, Fall 2009 SL - 71
Another Delete Example
3
6
7
9
12
17
19
25
26
Tail (NIL)
List
level = 5
hd ptr
Level 1
MaxLevel
(a constant)
Head
Delete(21)
Splice out
x. Note: we
skip over
update[5]
through
update[2].
update[4] update[3]
update[2]
update[1]
update[5]
22