Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Analysis of Algorithms
Skip Lists
Andres Mendez-Vazquez
October 18, 2015
1 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Dictionaries
Definition
A dictionary is a collection of elements; each of which has a unique search
key.
Uniqueness criteri...
Example: Course records
Dictionary with member records
key ID Student Name HW1
123 Stan Smith 49 · · ·
125 Sue Margolin 45...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
The dictionary ADT operations
Some operations on dictionaries
size(): Returns the size of the dictionary.
empty(): Returns...
Example of unordered dictionary
Example
Consider an empty unordered dictionary, we have then...
Operation Dictionary Outpu...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
How to implement a dictionary?
There are many ways of implementing a dictionary
Sequences / Arrays
Ordered
Unordered
Binar...
How to implement a dictionary?
There are many ways of implementing a dictionary
Sequences / Arrays
Ordered
Unordered
Binar...
How to implement a dictionary?
There are many ways of implementing a dictionary
Sequences / Arrays
Ordered
Unordered
Binar...
How to implement a dictionary?
There are many ways of implementing a dictionary
Sequences / Arrays
Ordered
Unordered
Binar...
Recall Arrays...
Unordered array
Complexity
Searching and removing takes O(n).
Inserting takes O(1).
Applications
This app...
Recall Arrays...
Unordered array
Complexity
Searching and removing takes O(n).
Inserting takes O(1).
Applications
This app...
Recall Arrays...
Unordered array
Complexity
Searching and removing takes O(n).
Inserting takes O(1).
Applications
This app...
Recall Arrays...
Unordered array
Complexity
Searching and removing takes O(n).
Inserting takes O(1).
Applications
This app...
More Arrays
Ordered array
Complexity
Searching takes O(log n) time (binary search).
Insert and removing takes O(n) time.
A...
More Arrays
Ordered array
Complexity
Searching takes O(log n) time (binary search).
Insert and removing takes O(n) time.
A...
More Arrays
Ordered array
Complexity
Searching takes O(log n) time (binary search).
Insert and removing takes O(n) time.
A...
More Arrays
Ordered array
Complexity
Searching takes O(log n) time (binary search).
Insert and removing takes O(n) time.
A...
Binary searches
Features
Narrow down the search range in stages
“High-low” game.
13 / 99
Binary searches
Example find Element(22)
2 4 5 7 8 9 12 14 17 19 22 25 27 28 33
↑
LOW
↑
MID
↑
HIGH
2 4 5 7 8 9 12 14 17 19 ...
Binary searches
Example find Element(22)
2 4 5 7 8 9 12 14 17 19 22 25 27 28 33
↑
LOW
↑
MID
↑
HIGH
2 4 5 7 8 9 12 14 17 19 ...
Binary searches
Example find Element(22)
2 4 5 7 8 9 12 14 17 19 22 25 27 28 33
↑
LOW
↑
MID
↑
HIGH
2 4 5 7 8 9 12 14 17 19 ...
Binary searches
Example find Element(22)
2 4 5 7 8 9 12 14 17 19 22 25 27 28 33
↑
LOW
↑
MID
↑
HIGH
2 4 5 7 8 9 12 14 17 19 ...
Recall binary search trees
Implement a dictionary with a BST
A binary search tree is a binary tree T such that:
Each inter...
Recall binary search trees
Implement a dictionary with a BST
A binary search tree is a binary tree T such that:
Each inter...
Recall binary search trees
Implement a dictionary with a BST
A binary search tree is a binary tree T such that:
Each inter...
Recall binary search trees
Implement a dictionary with a BST
A binary search tree is a binary tree T such that:
Each inter...
Binary searches Trees
Problem!!! Keeping a Well Balanced Binary Search Tree can be
difficult!!!
44
17 88
65
54 82
76
80
32
2...
Not only that...
Binary Search Trees
They are not so well suited for parallel environments.
Unless a heavy modifications ar...
Not only that...
Binary Search Trees
They are not so well suited for parallel environments.
Unless a heavy modifications ar...
Not only that...
Binary Search Trees
They are not so well suited for parallel environments.
Unless a heavy modifications ar...
Not only that...
Binary Search Trees
They are not so well suited for parallel environments.
Unless a heavy modifications ar...
Thus, we have the following possibilities
Unordered array complexities
Insertion: O(1)
Search: O(n)
Ordered array complexi...
Thus, we have the following possibilities
Unordered array complexities
Insertion: O(1)
Search: O(n)
Ordered array complexi...
Thus, we have the following possibilities
Unordered array complexities
Insertion: O(1)
Search: O(n)
Ordered array complexi...
We want something better!!!
For this
We will present a probabilistic data structure known as Skip
List!!!
19 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Starting from Scratch
First
Imagine that you only require to have searches.
A first approximation to it is the use of a lin...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Example
High-Bottom Way System
14
14 23 34 42 47 63
34 42
23 / 99
Thus, we have...
The following rule
To Search first search in the top one (L1) as far as possible, then go
down and search ...
We can use a little bit of optimization
We have the following worst cost
Search Cost High-Bottom Way System = Cost Searchi...
We can use a little bit of optimization
We have the following worst cost
Search Cost High-Bottom Way System = Cost Searchi...
Why?
If we think we are jumping
Segment 1 Segment 2
14
14 23 34
34
26 30
26
Then cost of searching each of the bottom segm...
Why?
If we think we are jumping
Segment 1 Segment 2
14
14 23 34
34
26 30
26
Then cost of searching each of the bottom segm...
Thus, we have...
Then, the cost for a search (when length (L2) = n)
Search Cost = length (L1)+
length (L2)
length (L1)
= l...
Thus, we have...
Then, the cost for a search (when length (L2) = n)
Search Cost = length (L1)+
length (L2)
length (L1)
= l...
Final Cost
We have that the optimal length for L1
length (L1) =
√
n
Plugging back in (Eq. 1)
Search Cost =
√
n +
n
√
n
=
√...
Final Cost
We have that the optimal length for L1
length (L1) =
√
n
Plugging back in (Eq. 1)
Search Cost =
√
n +
n
√
n
=
√...
Data structure with a Square Root Relation
Something like this
29 / 99
Now
For a three layer link list data structure
We get a search cost of 3 × 3
√
n
In general for k layers, we have
k × k
√
...
Now
For a three layer link list data structure
We get a search cost of 3 × 3
√
n
In general for k layers, we have
k × k
√
...
Now
For a three layer link list data structure
We get a search cost of 3 × 3
√
n
In general for k layers, we have
k × k
√
...
Thus
Something Notable
We get the advantages of the binary search trees with a simpler
architecture!!!
31 / 99
Thus
Binary Search Trees
44
17 88
65
54 82
76
80
32
28
29
97
New Architecture
32 / 99
Thus
Binary Search Trees
44
17 88
65
54 82
76
80
32
28
29
97
New Architecture
14
14 23 34 42 47 63
34 42
32 / 99
Now
We are ready to give a
Definition for Skip List
33 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
A Little Bit of History
Skip List
They were invented by William Worthington "Bill" Pugh Jr.!!!
How is him?
He is is an Ame...
A Little Bit of History
Skip List
They were invented by William Worthington "Bill" Pugh Jr.!!!
How is him?
He is is an Ame...
A Little Bit of History
Skip List
They were invented by William Worthington "Bill" Pugh Jr.!!!
How is him?
He is is an Ame...
A Little Bit of History
Skip List
They were invented by William Worthington "Bill" Pugh Jr.!!!
How is him?
He is is an Ame...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Definition
A skip list for a set S of distinct (key,element) items is a series of lists
S0, S1, ..., Sh...
Skip List Definition
Example
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
37 / 99
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Skip list search
We search for a key x in a skip list as follows
We start at the first position of the top list.
At the cur...
Example search for 78
x < p.next.key: “drop down”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
39 / 99
Example search for 78
x > p.next.key: “scan forward”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
40 / 99
Example search for 78
x < p.next.key: “drop down”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
41 / 99
Example search for 78
x > p.next.key: “scan forward”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
42 / 99
Example search for 78
x > p.next.key: “scan forward”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
43 / 99
Example search for 78
x < p.next.key: “drop down”
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
44 / 99
Example search for 78
x == y: we return p.next.element
12
23
23 26
31
31
31 34-
-
-
-
+
+
+
+
34
44 56 64
64
78
p
45 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
How do we implement this data structure?
We can implement a skip list with quad-nodes
A quad-node stores:
Entry Value
Link...
Example
Quad-Node Example
48 / 99
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Skip lists uses Randomization
Use of randomization
We use a randomized algorithm to insert items into a skip list.
Running...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Insertion
To insert
To insert an entry (key, object) into a skip list, we use a randomized algorithm:
We repeatedly toss a...
Example: Insertion of 15 in the skip list
First, we use i = 2 to insert S3 into the skip list
-
-
-
12
23
23 26 +
+
+
-
-
...
Example: Insertion of 15 in the skip list
Clearly, you first search for the predecessor key!!!
-
-
-
12
23
23 26 +
+
+
-
-
...
Example: Insertion of 15 in the skip list
Insert the necessary Quad-Nodes and necessary information
-
-
-
12
23
23 26 +
+
...
Example: Insertion of 15 in the skip list
Insert the necessary Quad-Nodes and necessary information
-
-
-
12
23
23 26 +
+
...
Example: Insertion of 15 in the skip list
Insert the necessary Quad-Nodes and necessary information
-
-
-
12
23
23 26 +
+
...
Example: Insertion of 15 in the skip list
Finally!!!
-
-
-
12
23
23 26 +
+
+
- +
15
15
15
57 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
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 th...
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 th...
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 th...
Example: Delete of 34 in the skip list
We search for 34 in the skip list and find the positions p0, p1, ..., p2 of
the item...
Example: Delete of 34 in the skip list
We search for 34 in the skip list and find the positions p0, p1, ..., p2 of
the item...
Example: Delete of 34 in the skip list
We start doing the deletion!!!
-
-
-
12
-
23
23 45 +
+
+
+
34
34
34
62 / 99
Example: Delete of 34 in the skip list
One Quad-Node after another
-
-
-
12
-
23
23 45 +
+
+
+
34
34
63 / 99
Example: Delete of 34 in the skip list
One Quad-Node after another
-
-
-
12
-
23
23 45 +
+
+
+
34
64 / 99
Example: Delete of 34 in the skip list
One Quad-Node after another
-
-
-
12
-
23
23 45 +
+
+
+
65 / 99
Example: Delete of 34 in the skip list
Remove One Level
-
-
-
12
23
23 45 +
+
+
66 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Space usage
Space usage
The space used by a skip list depends on the random bits used by each
invocation of the insertion ...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Space : O (n)
Theorem
The expected space usage of a skip list with n items is O(n).
Proof
We use the following two basic p...
Proof
Now consider a skip list with n entries
Using Fact 1, an element is inserted in list Si with a probability of
1
2i
N...
Proof
Now consider a skip list with n entries
Using Fact 1, an element is inserted in list Si with a probability of
1
2i
N...
Proof
The expected number of nodes used by the skip list with height h
h
i=0
n
2i
= n
h
i=0
1
2i
Here, we have a problem!!...
Height h
First
The running time of the search and insertion algorithms is affected by the
height h of the skip list.
Second...
Height h
First
The running time of the search and insertion algorithms is affected by the
height h of the skip list.
Second...
For this, we have the following fact!!!
We use the following Fact 3
We can view the level l (xi) = max {j|where xi ∈ Sj} o...
For this, we have the following fact!!!
We use the following Fact 3
We can view the level l (xi) = max {j|where xi ∈ Sj} o...
For this, we have the following fact!!!
We use the following Fact 3
We can view the level l (xi) = max {j|where xi ∈ Sj} o...
Example for l (xi)
We have
-
-
-
12
-
23
23 45 +
+
+
+
34
34
34
1 2 3 1
74 / 99
BTW What is the geometric distribution?
k failures where
k = {1, 2, 3, ...}
Probability mass function
Pr (X = k) = (1 − p)...
BTW What is the geometric distribution?
k failures where
k = {1, 2, 3, ...}
Probability mass function
Pr (X = k) = (1 − p)...
Probability Mass Function
For Different Probabilities
76 / 99
Then
We have the following inequality for the geometric variables
Pr [Xi > t] ≤ (1 − p)t
∀i = 1, 2, ..., n
Because if the ...
Then
We have the following inequality for the geometric variables
Pr [Xi > t] ≤ (1 − p)t
∀i = 1, 2, ..., n
Because if the ...
Observations
The maxi Xi
It represents the list with the one entry apart from the special keys.
-
-
-
12
-
23
23 45 +
+
+
...
Observations
REMEMBER!!!
We are talking about a fair coin, thus p = 1
2.
79 / 99
Height: 3 log2 n with probability at least 1 − 1
n2
Theorem
A skip list with n entries has height at most 3 log2 n with pr...
Proof
Consider a skip list with n entires
By Fact 3, the probability that list St has at least one item is at most n
2t .
...
Proof
Consider a skip list with n entires
By Fact 3, the probability that list St has at least one item is at most n
2t .
...
Look at we want to model
We want to model
The height of the Skip List is at most t = 3 log2 n
Equivalent to the negation o...
Look at we want to model
We want to model
The height of the Skip List is at most t = 3 log2 n
Equivalent to the negation o...
Look at we want to model
We want to model
The height of the Skip List is at most t = 3 log2 n
Equivalent to the negation o...
Finally
The expected number of nodes used by the skip list with height h
Given that h = 3 log2 n
3 log2 n
i=0
n
2i
= n
3 l...
Finally
The expected number of nodes used by the skip list with height h
Given that h = 3 log2 n
3 log2 n
i=0
n
2i
= n
3 l...
We have finally
The Upper Bound on the number of nodes
n
3 log2 n
i=0
1
2i
=n
1 − (1/2)3 log2 n+1
1 − 1/2
=n
1 − 1/2 (1/2lo...
We have finally
The Upper Bound on the number of nodes
n
3 log2 n
i=0
1
2i
=n
1 − (1/2)3 log2 n+1
1 − 1/2
=n
1 − 1/2 (1/2lo...
We have finally
The Upper Bound on the number of nodes
n
3 log2 n
i=0
1
2i
=n
1 − (1/2)3 log2 n+1
1 − 1/2
=n
1 − 1/2 (1/2lo...
Finally
The Upper Bound with probability 1 − 1
n2
2n −
1
2n
≤ 2n = O (n)
85 / 99
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Search and Insertion Times
Something Notable
The expected number of coin tosses required in order to get tails is 2.
We us...
Search and Insertion Times
Something Notable
The expected number of coin tosses required in order to get tails is 2.
We us...
Search and Insertions times
Search time
The search time in skip list is proportional to
the number of drop-down steps + th...
Search and Insertions times
Search time
The search time in skip list is proportional to
the number of drop-down steps + th...
Search and Insertions times
Search time
The search time in skip list is proportional to
the number of drop-down steps + th...
Proof
First
When we scan forward in a list, the destination key does not belong to a
higher list.
A scan-forward step is a...
Proof
First
When we scan forward in a list, the destination key does not belong to a
higher list.
A scan-forward step is a...
Why?
Given the list Si
Then, the scan-forward intervals (Jumps between xi and xi+1) to the right
of Si are
I1 = [x1, x2],I...
Why?
Given the list Si
Then, the scan-forward intervals (Jumps between xi and xi+1) to the right
of Si are
I1 = [x1, x2],I...
We introduce the following concept based on these
intervals
Scan-forward siblings
These are element that you find in the se...
Now
Given that a search is being done, Si contains l forward siblings
It must be the case that given x1, ..., xl scan-forw...
Thus
We have
Since each element of Si is independently chosen to be in Si+1 with
probability p = 1
2.
We have
The number o...
Thus
We have
Since each element of Si is independently chosen to be in Si+1 with
probability p = 1
2.
We have
The number o...
Thus
We have
Since each element of Si is independently chosen to be in Si+1 with
probability p = 1
2.
We have
The number o...
Then
In the worst case scenario
A search is bounded by 2 log2 n = O (log2 n)
An given that a insertion is a (search) + (de...
Then
In the worst case scenario
A search is bounded by 2 log2 n = O (log2 n)
An given that a insertion is a (search) + (de...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Applications
We have
Cyrus IMAP servers offer a "skiplist" backend Data Base
implementation.
Lucene uses skip lists to sear...
Applications
We have
Cyrus IMAP servers offer a "skiplist" backend Data Base
implementation.
Lucene uses skip lists to sear...
Applications
We have
Cyrus IMAP servers offer a "skiplist" backend Data Base
implementation.
Lucene uses skip lists to sear...
Applications
We have
Cyrus IMAP servers offer a "skiplist" backend Data Base
implementation.
Lucene uses skip lists to sear...
Applications
We have
Cyrus IMAP servers offer a "skiplist" backend Data Base
implementation.
Lucene uses skip lists to sear...
Outline
1 Dictionaries
Definitions
Dictionary operations
Dictionary implementation
2 Skip Lists
Why Skip Lists?
The Idea Be...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Summary
Summary
A skip list is a data structure for dictionaries that uses a randomized
insertion algorithm.
In a skip lis...
Thanks
Questions?
99 / 99
Próxima SlideShare
Cargando en…5
×

14 Skip Lists

1.999 visualizaciones

Publicado el

Finally, I was able to put together the talk about skip list... I am still not liking my explanation of the scan-forward part to be bounded by a geometric random variable... However... enjoy

Publicado en: Ingeniería
  • Sé el primero en comentar

14 Skip Lists

  1. 1. Analysis of Algorithms Skip Lists Andres Mendez-Vazquez October 18, 2015 1 / 99
  2. 2. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 2 / 99
  3. 3. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 3 / 99
  4. 4. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  5. 5. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  6. 6. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  7. 7. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  8. 8. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  9. 9. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  10. 10. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  11. 11. Dictionaries Definition A dictionary is a collection of elements; each of which has a unique search key. Uniqueness criteria may be relaxed (multi-set). Do not force uniqueness. Purpose Dictionaries keep track of current members, with periodic insertions and deletions into the set (similar to a database). Examples Membership in a club. Course records. Symbol table (with duplicates). Language dictionary (Webster, RAE, Oxford). 4 / 99
  12. 12. Example: Course records Dictionary with member records key ID Student Name HW1 123 Stan Smith 49 · · · 125 Sue Margolin 45 · · · 128 Billie King 24 · · · ... ... 190 Roy Miller 36 · · · 5 / 99
  13. 13. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 6 / 99
  14. 14. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  15. 15. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  16. 16. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  17. 17. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  18. 18. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  19. 19. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  20. 20. The dictionary ADT operations Some operations on dictionaries size(): Returns the size of the dictionary. empty(): Returns TRUE if the dictionary is empty. findItem(key): Locates the item with the specified key. findAllItems(key): Locates all items with the specified key. removeItem(key): Removes the item with the specified key. removeAllItems(key): Removes all items with the specified key. insertItem(key,element): Inserts a new key-element pair. 7 / 99
  21. 21. Example of unordered dictionary Example Consider an empty unordered dictionary, we have then... Operation Dictionary Output InsertItem(5, A) {(5, A)} InsertItem(7, B) {(5, A) , (7, B)} findItem(7) {(5, A) , (7, B)} B findItem(4) {(5, A) , (7, B)} No Such Key size() {(5, A) , (7, B)} 2 removeItem(5) {(7, B)} A findItem(4) {(7, B)} No Such Key 8 / 99
  22. 22. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 9 / 99
  23. 23. How to implement a dictionary? There are many ways of implementing a dictionary Sequences / Arrays Ordered Unordered Binary search trees Skip lists Hash tables 10 / 99
  24. 24. How to implement a dictionary? There are many ways of implementing a dictionary Sequences / Arrays Ordered Unordered Binary search trees Skip lists Hash tables 10 / 99
  25. 25. How to implement a dictionary? There are many ways of implementing a dictionary Sequences / Arrays Ordered Unordered Binary search trees Skip lists Hash tables 10 / 99
  26. 26. How to implement a dictionary? There are many ways of implementing a dictionary Sequences / Arrays Ordered Unordered Binary search trees Skip lists Hash tables 10 / 99
  27. 27. Recall Arrays... Unordered array Complexity Searching and removing takes O(n). Inserting takes O(1). Applications This approach is good for log files where insertions are frequent but searches and removals are rare. 11 / 99
  28. 28. Recall Arrays... Unordered array Complexity Searching and removing takes O(n). Inserting takes O(1). Applications This approach is good for log files where insertions are frequent but searches and removals are rare. 11 / 99
  29. 29. Recall Arrays... Unordered array Complexity Searching and removing takes O(n). Inserting takes O(1). Applications This approach is good for log files where insertions are frequent but searches and removals are rare. 11 / 99
  30. 30. Recall Arrays... Unordered array Complexity Searching and removing takes O(n). Inserting takes O(1). Applications This approach is good for log files where insertions are frequent but searches and removals are rare. 11 / 99
  31. 31. More Arrays Ordered array Complexity Searching takes O(log n) time (binary search). Insert and removing takes O(n) time. Applications This aproach is good for look-up tables where searches are frequent but insertions and removals are rare. 12 / 99
  32. 32. More Arrays Ordered array Complexity Searching takes O(log n) time (binary search). Insert and removing takes O(n) time. Applications This aproach is good for look-up tables where searches are frequent but insertions and removals are rare. 12 / 99
  33. 33. More Arrays Ordered array Complexity Searching takes O(log n) time (binary search). Insert and removing takes O(n) time. Applications This aproach is good for look-up tables where searches are frequent but insertions and removals are rare. 12 / 99
  34. 34. More Arrays Ordered array Complexity Searching takes O(log n) time (binary search). Insert and removing takes O(n) time. Applications This aproach is good for look-up tables where searches are frequent but insertions and removals are rare. 12 / 99
  35. 35. Binary searches Features Narrow down the search range in stages “High-low” game. 13 / 99
  36. 36. Binary searches Example find Element(22) 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW=MID=HIGH 14 / 99
  37. 37. Binary searches Example find Element(22) 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW=MID=HIGH 14 / 99
  38. 38. Binary searches Example find Element(22) 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW=MID=HIGH 14 / 99
  39. 39. Binary searches Example find Element(22) 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW ↑ MID ↑ HIGH 2 4 5 7 8 9 12 14 17 19 22 25 27 28 33 ↑ LOW=MID=HIGH 14 / 99
  40. 40. Recall binary search trees Implement a dictionary with a BST A binary search tree is a binary tree T such that: Each internal node stores an item (k, e) of a dictionary. Keys stored at nodes in the left subtree of v are less than or equal to k. Keys stored at nodes in the right subtree of v are greater than or equal to k. 15 / 99
  41. 41. Recall binary search trees Implement a dictionary with a BST A binary search tree is a binary tree T such that: Each internal node stores an item (k, e) of a dictionary. Keys stored at nodes in the left subtree of v are less than or equal to k. Keys stored at nodes in the right subtree of v are greater than or equal to k. 15 / 99
  42. 42. Recall binary search trees Implement a dictionary with a BST A binary search tree is a binary tree T such that: Each internal node stores an item (k, e) of a dictionary. Keys stored at nodes in the left subtree of v are less than or equal to k. Keys stored at nodes in the right subtree of v are greater than or equal to k. 15 / 99
  43. 43. Recall binary search trees Implement a dictionary with a BST A binary search tree is a binary tree T such that: Each internal node stores an item (k, e) of a dictionary. Keys stored at nodes in the left subtree of v are less than or equal to k. Keys stored at nodes in the right subtree of v are greater than or equal to k. 15 / 99
  44. 44. Binary searches Trees Problem!!! Keeping a Well Balanced Binary Search Tree can be difficult!!! 44 17 88 65 54 82 76 80 32 28 29 97 16 / 99
  45. 45. Not only that... Binary Search Trees They are not so well suited for parallel environments. Unless a heavy modifications are done In addition We want to have a Compact Data Structure. Using as little memory as possible 17 / 99
  46. 46. Not only that... Binary Search Trees They are not so well suited for parallel environments. Unless a heavy modifications are done In addition We want to have a Compact Data Structure. Using as little memory as possible 17 / 99
  47. 47. Not only that... Binary Search Trees They are not so well suited for parallel environments. Unless a heavy modifications are done In addition We want to have a Compact Data Structure. Using as little memory as possible 17 / 99
  48. 48. Not only that... Binary Search Trees They are not so well suited for parallel environments. Unless a heavy modifications are done In addition We want to have a Compact Data Structure. Using as little memory as possible 17 / 99
  49. 49. Thus, we have the following possibilities Unordered array complexities Insertion: O(1) Search: O(n) Ordered array complexities Insertion: O(n) Search: O(n log n) Well balanced binary trees complexities Insertion: O(log n) Search: O(log n) Big Drawback - Complex parallel Implementation and waste of memory. 18 / 99
  50. 50. Thus, we have the following possibilities Unordered array complexities Insertion: O(1) Search: O(n) Ordered array complexities Insertion: O(n) Search: O(n log n) Well balanced binary trees complexities Insertion: O(log n) Search: O(log n) Big Drawback - Complex parallel Implementation and waste of memory. 18 / 99
  51. 51. Thus, we have the following possibilities Unordered array complexities Insertion: O(1) Search: O(n) Ordered array complexities Insertion: O(n) Search: O(n log n) Well balanced binary trees complexities Insertion: O(log n) Search: O(log n) Big Drawback - Complex parallel Implementation and waste of memory. 18 / 99
  52. 52. We want something better!!! For this We will present a probabilistic data structure known as Skip List!!! 19 / 99
  53. 53. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 20 / 99
  54. 54. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  55. 55. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  56. 56. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  57. 57. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  58. 58. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  59. 59. Starting from Scratch First Imagine that you only require to have searches. A first approximation to it is the use of a link list for it (Θ(n) search complexity). Then, using this How do we speed up searches? Something Notable Use two link list, one a subsequence of the other. Imagine the two lists as a road system 1 The Bottom is the normal road system, L2. 2 The Top is the high way system, L1. 21 / 99
  60. 60. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 22 / 99
  61. 61. Example High-Bottom Way System 14 14 23 34 42 47 63 34 42 23 / 99
  62. 62. Thus, we have... The following rule To Search first search in the top one (L1) as far as possible, then go down and search in the bottom one (L2). 24 / 99
  63. 63. We can use a little bit of optimization We have the following worst cost Search Cost High-Bottom Way System = Cost Searching Top +... Cost Search Bottom Or Search Cost =length (L1) + Cost Search Bottom The interesting part is “Cost Search Bottom” This can be calculated by the following quotient: length (L2) length (L1) 25 / 99
  64. 64. We can use a little bit of optimization We have the following worst cost Search Cost High-Bottom Way System = Cost Searching Top +... Cost Search Bottom Or Search Cost =length (L1) + Cost Search Bottom The interesting part is “Cost Search Bottom” This can be calculated by the following quotient: length (L2) length (L1) 25 / 99
  65. 65. Why? If we think we are jumping Segment 1 Segment 2 14 14 23 34 34 26 30 26 Then cost of searching each of the bottom segments = 2 Thus the ratio is a “decent” approximation to the worst case search length (L2) length (L1) = 5 3 = 1.66 26 / 99
  66. 66. Why? If we think we are jumping Segment 1 Segment 2 14 14 23 34 34 26 30 26 Then cost of searching each of the bottom segments = 2 Thus the ratio is a “decent” approximation to the worst case search length (L2) length (L1) = 5 3 = 1.66 26 / 99
  67. 67. Thus, we have... Then, the cost for a search (when length (L2) = n) Search Cost = length (L1)+ length (L2) length (L1) = length (L1)+ n length (L1) (1) Taking the derivative with respect to length (L1) and making the result equal 0 1 − n length2 (L1) = 0 27 / 99
  68. 68. Thus, we have... Then, the cost for a search (when length (L2) = n) Search Cost = length (L1)+ length (L2) length (L1) = length (L1)+ n length (L1) (1) Taking the derivative with respect to length (L1) and making the result equal 0 1 − n length2 (L1) = 0 27 / 99
  69. 69. Final Cost We have that the optimal length for L1 length (L1) = √ n Plugging back in (Eq. 1) Search Cost = √ n + n √ n = √ n + √ n = 2 × √ n 28 / 99
  70. 70. Final Cost We have that the optimal length for L1 length (L1) = √ n Plugging back in (Eq. 1) Search Cost = √ n + n √ n = √ n + √ n = 2 × √ n 28 / 99
  71. 71. Data structure with a Square Root Relation Something like this 29 / 99
  72. 72. Now For a three layer link list data structure We get a search cost of 3 × 3 √ n In general for k layers, we have k × k √ n Thus, if we make k = log2 n, we get Search Cost = log2 n × log2 n √ n = log2 n × (n) 1/log2 n = log2 n × (n)logn 2 = log2 n × 2 =Θ (log2 n) 30 / 99
  73. 73. Now For a three layer link list data structure We get a search cost of 3 × 3 √ n In general for k layers, we have k × k √ n Thus, if we make k = log2 n, we get Search Cost = log2 n × log2 n √ n = log2 n × (n) 1/log2 n = log2 n × (n)logn 2 = log2 n × 2 =Θ (log2 n) 30 / 99
  74. 74. Now For a three layer link list data structure We get a search cost of 3 × 3 √ n In general for k layers, we have k × k √ n Thus, if we make k = log2 n, we get Search Cost = log2 n × log2 n √ n = log2 n × (n) 1/log2 n = log2 n × (n)logn 2 = log2 n × 2 =Θ (log2 n) 30 / 99
  75. 75. Thus Something Notable We get the advantages of the binary search trees with a simpler architecture!!! 31 / 99
  76. 76. Thus Binary Search Trees 44 17 88 65 54 82 76 80 32 28 29 97 New Architecture 32 / 99
  77. 77. Thus Binary Search Trees 44 17 88 65 54 82 76 80 32 28 29 97 New Architecture 14 14 23 34 42 47 63 34 42 32 / 99
  78. 78. Now We are ready to give a Definition for Skip List 33 / 99
  79. 79. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 34 / 99
  80. 80. A Little Bit of History Skip List They were invented by William Worthington "Bill" Pugh Jr.!!! How is him? He is is an American computer scientist who invented the skip list and the Omega test for deciding Presburger arithmetic. He was the co-author of the static code analysis tool FindBugs. He was highly influential in the development of the current memory model of the Java language together with his PhD student Jeremy Manson. 35 / 99
  81. 81. A Little Bit of History Skip List They were invented by William Worthington "Bill" Pugh Jr.!!! How is him? He is is an American computer scientist who invented the skip list and the Omega test for deciding Presburger arithmetic. He was the co-author of the static code analysis tool FindBugs. He was highly influential in the development of the current memory model of the Java language together with his PhD student Jeremy Manson. 35 / 99
  82. 82. A Little Bit of History Skip List They were invented by William Worthington "Bill" Pugh Jr.!!! How is him? He is is an American computer scientist who invented the skip list and the Omega test for deciding Presburger arithmetic. He was the co-author of the static code analysis tool FindBugs. He was highly influential in the development of the current memory model of the Java language together with his PhD student Jeremy Manson. 35 / 99
  83. 83. A Little Bit of History Skip List They were invented by William Worthington "Bill" Pugh Jr.!!! How is him? He is is an American computer scientist who invented the skip list and the Omega test for deciding Presburger arithmetic. He was the co-author of the static code analysis tool FindBugs. He was highly influential in the development of the current memory model of the Java language together with his PhD student Jeremy Manson. 35 / 99
  84. 84. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  85. 85. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  86. 86. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  87. 87. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  88. 88. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  89. 89. Skip List Definition Definition 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 S0 ⊇ S1 ⊇ S2 ⊇ ... ⊇ Sh List Sh contains only the two special keys 36 / 99
  90. 90. Skip List Definition Example 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 37 / 99
  91. 91. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  92. 92. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  93. 93. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  94. 94. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  95. 95. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  96. 96. Skip list search We search for a key x in 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 == p.next.key x == y: we return p.next.element x > y: we scan forward x < y: we “drop down” If we try to drop down past the bottom list, we return null. 38 / 99
  97. 97. Example search for 78 x < p.next.key: “drop down” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 39 / 99
  98. 98. Example search for 78 x > p.next.key: “scan forward” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 40 / 99
  99. 99. Example search for 78 x < p.next.key: “drop down” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 41 / 99
  100. 100. Example search for 78 x > p.next.key: “scan forward” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 42 / 99
  101. 101. Example search for 78 x > p.next.key: “scan forward” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 43 / 99
  102. 102. Example search for 78 x < p.next.key: “drop down” 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 44 / 99
  103. 103. Example search for 78 x == y: we return p.next.element 12 23 23 26 31 31 31 34- - - - + + + + 34 44 56 64 64 78 p 45 / 99
  104. 104. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 46 / 99
  105. 105. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  106. 106. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  107. 107. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  108. 108. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  109. 109. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  110. 110. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  111. 111. How do we implement this data structure? We can implement a skip list with quad-nodes A quad-node stores: Entry Value Link to the previous node Link to the next node Link to the above node Link to the below node Also we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them. 47 / 99
  112. 112. Example Quad-Node Example 48 / 99
  113. 113. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  114. 114. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  115. 115. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  116. 116. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  117. 117. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  118. 118. Skip lists uses Randomization Use of randomization We use a randomized algorithm to insert items into a skip list. Running time We analyze the expected running time of a randomized algorithm under the following assumptions: The coins are unbiased. The coin tosses are independent. Worst case running time The worst case running time of a randomized algorithm is often large but has very low probability. e.g. It occurs when all the coin tosses give “heads.” 49 / 99
  119. 119. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 50 / 99
  120. 120. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  121. 121. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  122. 122. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  123. 123. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  124. 124. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  125. 125. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  126. 126. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  127. 127. Insertion To insert To insert an entry (key, object) into a skip list, we use a randomized algorithm: We repeatedly toss a coin until we get tails: 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 lists S0, S1, ..., Si. For j ← 0, ..., i, we insert item (key, object) into list Sj after position pj. 51 / 99
  128. 128. Example: Insertion of 15 in the skip list First, we use i = 2 to insert S3 into the skip list - - - 12 23 23 26 + + + - - - 12 23 23 26 + + + - +- + 52 / 99
  129. 129. Example: Insertion of 15 in the skip list Clearly, you first search for the predecessor key!!! - - - 12 23 23 26 + + + - - - 12 23 23 26 + + + - +- + 53 / 99
  130. 130. Example: Insertion of 15 in the skip list Insert the necessary Quad-Nodes and necessary information - - - 12 23 23 26 + + + - + pred 54 / 99
  131. 131. Example: Insertion of 15 in the skip list Insert the necessary Quad-Nodes and necessary information - - - 12 23 23 26 + + + - + pred 15 55 / 99
  132. 132. Example: Insertion of 15 in the skip list Insert the necessary Quad-Nodes and necessary information - - - 12 23 23 26 + + + - + pred 15 15 56 / 99
  133. 133. Example: Insertion of 15 in the skip list Finally!!! - - - 12 23 23 26 + + + - + 15 15 15 57 / 99
  134. 134. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 58 / 99
  135. 135. 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 59 / 99
  136. 136. 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 59 / 99
  137. 137. 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 59 / 99
  138. 138. Example: Delete of 34 in the skip list We search for 34 in the skip list and find the positions p0, p1, ..., p2 of the items with key 34 - - - 12 - 23 23 45 + + + + 34 34 34 60 / 99
  139. 139. Example: Delete of 34 in the skip list We search for 34 in the skip list and find the positions p0, p1, ..., p2 of the items with key 34 - - - 12 - 23 23 45 + + + + 34 34 34 61 / 99
  140. 140. Example: Delete of 34 in the skip list We start doing the deletion!!! - - - 12 - 23 23 45 + + + + 34 34 34 62 / 99
  141. 141. Example: Delete of 34 in the skip list One Quad-Node after another - - - 12 - 23 23 45 + + + + 34 34 63 / 99
  142. 142. Example: Delete of 34 in the skip list One Quad-Node after another - - - 12 - 23 23 45 + + + + 34 64 / 99
  143. 143. Example: Delete of 34 in the skip list One Quad-Node after another - - - 12 - 23 23 45 + + + + 65 / 99
  144. 144. Example: Delete of 34 in the skip list Remove One Level - - - 12 23 23 45 + + + 66 / 99
  145. 145. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 67 / 99
  146. 146. Space usage Space usage The space used by a skip list depends on the random bits used by each invocation of the insertion algorithm. 68 / 99
  147. 147. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  148. 148. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  149. 149. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  150. 150. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  151. 151. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  152. 152. Space : O (n) Theorem The expected space usage of a skip list with n items is O(n). Proof We use the following two basic probabilistic facts: 1 Fact 1: The probability of getting i consecutive heads when flipping a coin is 1 2i . 2 Fact 2: If each of n entries is present in a set with probability p, the expected size of the set is np. 1 How? Remember X = X1 + X2 + ... + Xn where Xi is an indicator function for event Ai = the i element is present in the set. Thus: E [X] = n i=1 E [Xi] = n i=1 Pr {Ai} Equivalence E[XA] and Pr{A} = n i=1 p = np 69 / 99
  153. 153. Proof Now consider a skip list with n entries Using Fact 1, an element is inserted in list Si with a probability of 1 2i Now by Fact 2 The expected size of list Si is n 2i 70 / 99
  154. 154. Proof Now consider a skip list with n entries Using Fact 1, an element is inserted in list Si with a probability of 1 2i Now by Fact 2 The expected size of list Si is n 2i 70 / 99
  155. 155. Proof The expected number of nodes used by the skip list with height h h i=0 n 2i = n h i=0 1 2i Here, we have a problem!!! What is the value of h? 71 / 99
  156. 156. Height h First The running time of the search and insertion algorithms is affected by the height h of the skip list. Second We show that with high probability, a skip list with n items has height O(log n). 72 / 99
  157. 157. Height h First The running time of the search and insertion algorithms is affected by the height h of the skip list. Second We show that with high probability, a skip list with n items has height O(log n). 72 / 99
  158. 158. For this, we have the following fact!!! We use the following Fact 3 We can view the level l (xi) = max {j|where xi ∈ Sj} of the elements in the skip list as the following random variable Xi = l (xi) for each element xi in the skip list. And this is a random variable!!! Remember the insertions!!! Using an unbiased coin!! Thus, all Xi have a geometric distribution. 73 / 99
  159. 159. For this, we have the following fact!!! We use the following Fact 3 We can view the level l (xi) = max {j|where xi ∈ Sj} of the elements in the skip list as the following random variable Xi = l (xi) for each element xi in the skip list. And this is a random variable!!! Remember the insertions!!! Using an unbiased coin!! Thus, all Xi have a geometric distribution. 73 / 99
  160. 160. For this, we have the following fact!!! We use the following Fact 3 We can view the level l (xi) = max {j|where xi ∈ Sj} of the elements in the skip list as the following random variable Xi = l (xi) for each element xi in the skip list. And this is a random variable!!! Remember the insertions!!! Using an unbiased coin!! Thus, all Xi have a geometric distribution. 73 / 99
  161. 161. Example for l (xi) We have - - - 12 - 23 23 45 + + + + 34 34 34 1 2 3 1 74 / 99
  162. 162. BTW What is the geometric distribution? k failures where k = {1, 2, 3, ...} Probability mass function Pr (X = k) = (1 − p)k−1 p 75 / 99
  163. 163. BTW What is the geometric distribution? k failures where k = {1, 2, 3, ...} Probability mass function Pr (X = k) = (1 − p)k−1 p 75 / 99
  164. 164. Probability Mass Function For Different Probabilities 76 / 99
  165. 165. Then We have the following inequality for the geometric variables Pr [Xi > t] ≤ (1 − p)t ∀i = 1, 2, ..., n Because if the cdf F (t) = P (X ≤ t) = 1 − (1 − p)t+1 Then, we have Pr max i Xi > t ≤ n(1 − p)t This comes from Fmaxi Xi (t) = (F (t))n 77 / 99
  166. 166. Then We have the following inequality for the geometric variables Pr [Xi > t] ≤ (1 − p)t ∀i = 1, 2, ..., n Because if the cdf F (t) = P (X ≤ t) = 1 − (1 − p)t+1 Then, we have Pr max i Xi > t ≤ n(1 − p)t This comes from Fmaxi Xi (t) = (F (t))n 77 / 99
  167. 167. Observations The maxi Xi It represents the list with the one entry apart from the special keys. - - - 12 - 23 23 45 + + + + 34 34 34 78 / 99
  168. 168. Observations REMEMBER!!! We are talking about a fair coin, thus p = 1 2. 79 / 99
  169. 169. Height: 3 log2 n with probability at least 1 − 1 n2 Theorem A skip list with n entries has height at most 3 log2 n with probability at least 1 − 1 n2 80 / 99
  170. 170. Proof Consider a skip list with n entires By Fact 3, the probability that list St has at least one item is at most n 2t . P (|St| ≥ 1) = P max i Xi > t = n 2t . By picking t = 3 log n We have that the probability that S3 log2 n has at least one entry is at most: n 23 log2 n = n n3 = 1 n2 . 81 / 99
  171. 171. Proof Consider a skip list with n entires By Fact 3, the probability that list St has at least one item is at most n 2t . P (|St| ≥ 1) = P max i Xi > t = n 2t . By picking t = 3 log n We have that the probability that S3 log2 n has at least one entry is at most: n 23 log2 n = n n3 = 1 n2 . 81 / 99
  172. 172. Look at we want to model We want to model The height of the Skip List is at most t = 3 log2 n Equivalent to the negation of having list S3 log2 n Then, the probability that the height h = 3 log2 n of the skip list is P (Skip List height 3 log2 n) = 1 − 1 n2 82 / 99
  173. 173. Look at we want to model We want to model The height of the Skip List is at most t = 3 log2 n Equivalent to the negation of having list S3 log2 n Then, the probability that the height h = 3 log2 n of the skip list is P (Skip List height 3 log2 n) = 1 − 1 n2 82 / 99
  174. 174. Look at we want to model We want to model The height of the Skip List is at most t = 3 log2 n Equivalent to the negation of having list S3 log2 n Then, the probability that the height h = 3 log2 n of the skip list is P (Skip List height 3 log2 n) = 1 − 1 n2 82 / 99
  175. 175. Finally The expected number of nodes used by the skip list with height h Given that h = 3 log2 n 3 log2 n i=0 n 2i = n 3 log2 n i=0 1 2i Given the geometric sum Sm = m k=0 rk = 1 − rm+1 1 − r 83 / 99
  176. 176. Finally The expected number of nodes used by the skip list with height h Given that h = 3 log2 n 3 log2 n i=0 n 2i = n 3 log2 n i=0 1 2i Given the geometric sum Sm = m k=0 rk = 1 − rm+1 1 − r 83 / 99
  177. 177. We have finally The Upper Bound on the number of nodes n 3 log2 n i=0 1 2i =n 1 − (1/2)3 log2 n+1 1 − 1/2 =n 1 − 1/2 (1/2log2 n)3 1/2 We have then 1 2log2 n = 1 n Then n 1 − 1/2 (1/2log2 n)3 1/2 = n 1 − 1 2n2 1/2 = n 2 − 1 2n2 = 2n − 1 2n 84 / 99
  178. 178. We have finally The Upper Bound on the number of nodes n 3 log2 n i=0 1 2i =n 1 − (1/2)3 log2 n+1 1 − 1/2 =n 1 − 1/2 (1/2log2 n)3 1/2 We have then 1 2log2 n = 1 n Then n 1 − 1/2 (1/2log2 n)3 1/2 = n 1 − 1 2n2 1/2 = n 2 − 1 2n2 = 2n − 1 2n 84 / 99
  179. 179. We have finally The Upper Bound on the number of nodes n 3 log2 n i=0 1 2i =n 1 − (1/2)3 log2 n+1 1 − 1/2 =n 1 − 1/2 (1/2log2 n)3 1/2 We have then 1 2log2 n = 1 n Then n 1 − 1/2 (1/2log2 n)3 1/2 = n 1 − 1 2n2 1/2 = n 2 − 1 2n2 = 2n − 1 2n 84 / 99
  180. 180. Finally The Upper Bound with probability 1 − 1 n2 2n − 1 2n ≤ 2n = O (n) 85 / 99
  181. 181. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 86 / 99
  182. 182. Search and Insertion Times Something Notable The expected number of coin tosses required in order to get tails is 2. We use this To prove that a search in a skip list takes O(log n) expected time. After all insertions require searches!!! 87 / 99
  183. 183. Search and Insertion Times Something Notable The expected number of coin tosses required in order to get tails is 2. We use this To prove that a search in a skip list takes O(log n) expected time. After all insertions require searches!!! 87 / 99
  184. 184. Search and Insertions times Search time The search time in skip list is proportional to the number of drop-down steps + the number of scan-forward steps Drop-down steps The drop-down steps are bounded by the height of the skip list and thus are O (log2 n) with high probability. Theorem A search in a skip list takes O (log2 n) expected time. 88 / 99
  185. 185. Search and Insertions times Search time The search time in skip list is proportional to the number of drop-down steps + the number of scan-forward steps Drop-down steps The drop-down steps are bounded by the height of the skip list and thus are O (log2 n) with high probability. Theorem A search in a skip list takes O (log2 n) expected time. 88 / 99
  186. 186. Search and Insertions times Search time The search time in skip list is proportional to the number of drop-down steps + the number of scan-forward steps Drop-down steps The drop-down steps are bounded by the height of the skip list and thus are O (log2 n) with high probability. Theorem A search in a skip list takes O (log2 n) expected time. 88 / 99
  187. 187. Proof First When we scan forward in a list, the destination key does not belong to a higher list. A scan-forward step is associated with a former coin toss that gave tails By Fact 4, in each list the expected number of scan-forward steps is 2. 89 / 99
  188. 188. Proof First When we scan forward in a list, the destination key does not belong to a higher list. A scan-forward step is associated with a former coin toss that gave tails By Fact 4, in each list the expected number of scan-forward steps is 2. 89 / 99
  189. 189. Why? Given the list Si Then, the scan-forward intervals (Jumps between xi and xi+1) to the right of Si are I1 = [x1, x2],I2 = [x2, x3]...Ik = [xk, +∞] Then These interval exist at level i if and only if all x1, x2, ..., xk belong to Si. 90 / 99
  190. 190. Why? Given the list Si Then, the scan-forward intervals (Jumps between xi and xi+1) to the right of Si are I1 = [x1, x2],I2 = [x2, x3]...Ik = [xk, +∞] Then These interval exist at level i if and only if all x1, x2, ..., xk belong to Si. 90 / 99
  191. 191. We introduce the following concept based on these intervals Scan-forward siblings These are element that you find in the search path before finding an element in the upper list. 91 / 99
  192. 192. Now Given that a search is being done, Si contains l forward siblings It must be the case that given x1, ..., xl scan-forward siblings, we have that x1, ..., xl /∈ Si+1 and xl+1 ∈ Si+1 92 / 99
  193. 193. Thus We have Since each element of Si is independently chosen to be in Si+1 with probability p = 1 2. We have The number of scan-forward siblings is bounded by a geometric random variable Xi with parameter p = 1 2. Thus, we have that The expected number of scan-forward siblings is bounded by 2!!! Expected # Scan-Fordward Siblings at i ≤ E [Xi] = 1 1/2 Mean = 2 93 / 99
  194. 194. Thus We have Since each element of Si is independently chosen to be in Si+1 with probability p = 1 2. We have The number of scan-forward siblings is bounded by a geometric random variable Xi with parameter p = 1 2. Thus, we have that The expected number of scan-forward siblings is bounded by 2!!! Expected # Scan-Fordward Siblings at i ≤ E [Xi] = 1 1/2 Mean = 2 93 / 99
  195. 195. Thus We have Since each element of Si is independently chosen to be in Si+1 with probability p = 1 2. We have The number of scan-forward siblings is bounded by a geometric random variable Xi with parameter p = 1 2. Thus, we have that The expected number of scan-forward siblings is bounded by 2!!! Expected # Scan-Fordward Siblings at i ≤ E [Xi] = 1 1/2 Mean = 2 93 / 99
  196. 196. Then In the worst case scenario A search is bounded by 2 log2 n = O (log2 n) An given that a insertion is a (search) + (deletion bounded by the height) Thus, an insertion is bounded by 2 log2 n + 3 logn n = O (log2 n) 94 / 99
  197. 197. Then In the worst case scenario A search is bounded by 2 log2 n = O (log2 n) An given that a insertion is a (search) + (deletion bounded by the height) Thus, an insertion is bounded by 2 log2 n + 3 logn n = O (log2 n) 94 / 99
  198. 198. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 95 / 99
  199. 199. Applications We have Cyrus IMAP servers offer a "skiplist" backend Data Base implementation. Lucene uses skip lists to search delta-encoded posting lists in logarithmic time. Redis, an ANSI-C open-source persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets. leveldb, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Skip lists are used for efficient statistical computations of running medians. 96 / 99
  200. 200. Applications We have Cyrus IMAP servers offer a "skiplist" backend Data Base implementation. Lucene uses skip lists to search delta-encoded posting lists in logarithmic time. Redis, an ANSI-C open-source persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets. leveldb, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Skip lists are used for efficient statistical computations of running medians. 96 / 99
  201. 201. Applications We have Cyrus IMAP servers offer a "skiplist" backend Data Base implementation. Lucene uses skip lists to search delta-encoded posting lists in logarithmic time. Redis, an ANSI-C open-source persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets. leveldb, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Skip lists are used for efficient statistical computations of running medians. 96 / 99
  202. 202. Applications We have Cyrus IMAP servers offer a "skiplist" backend Data Base implementation. Lucene uses skip lists to search delta-encoded posting lists in logarithmic time. Redis, an ANSI-C open-source persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets. leveldb, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Skip lists are used for efficient statistical computations of running medians. 96 / 99
  203. 203. Applications We have Cyrus IMAP servers offer a "skiplist" backend Data Base implementation. Lucene uses skip lists to search delta-encoded posting lists in logarithmic time. Redis, an ANSI-C open-source persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets. leveldb, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Skip lists are used for efficient statistical computations of running medians. 96 / 99
  204. 204. Outline 1 Dictionaries Definitions Dictionary operations Dictionary implementation 2 Skip Lists Why Skip Lists? The Idea Behind All of It!!! Skip List Definition Skip list implementation Insertion for Skip Lists Deletion in Skip Lists Properties Search and Insertion Times Applications Summary 97 / 99
  205. 205. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  206. 206. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  207. 207. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  208. 208. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  209. 209. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  210. 210. Summary Summary A skip list is a data structure for dictionaries that uses a randomized insertion algorithm. In a skip list with n entries: The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Skip list are fast and simple to implement in practice. 98 / 99
  211. 211. Thanks Questions? 99 / 99

×