randomized algorithms - treaps 1 8/24/2015 randomized skip list skip list or, in hebrew:...

29
Randomized Algorithms - Treaps 1 06/18/22 Randomized Skip List Skip List Or, In Hebrew: גגגגגג גגגגג

Upload: charles-wood

Post on 25-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 104/19/23

Randomized Skip List

Skip List Or, In Hebrew: רשימות דילוג

Page 2: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 204/19/23

Outline

Motivation for the

skipping

Skip List definitions and

description

Analyzing performance

The role of randomness

Page 3: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 304/19/23

Outline

Motivation for the

skipping

Skip List definitions and

description

Analyzing performance

The role of randomness

Page 4: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms – Skip List 404/19/23

Starting from scratch Initial goal just search, no updates

(insert, delete).

Simplest Data Structure?

linked list!

Search? O(n)!

Can we do it faster?

yes we can!

Page 5: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms – Skip List 504/19/23

Developing the skip list Let’s add an express lane.

Can quickly jump from express stop to next

express stop, or from any stop to next normal

stop.

To search, first search in the express layer until

about to go to far, then go down and search in

the local layer.

Page 6: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms – Skip List 604/19/23

Search cost What is the search cost?

Page 7: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms – Skip List 704/19/23

Search cost – Cont.

This is minimized when:

Page 8: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms – Skip List 804/19/23

Discovering skip lists If we keep adding linked list layers we get:

Page 9: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 904/19/23

Outline

Motivation for the

skipping

Skip List definitions and

description

Analyzing performance

The role of randomness

Page 10: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1004/19/23

Initial definitions Let S be a totally ordered set of n elements.

A leveling with r levels of S, is a sequence of

nested subsets (called levels) :

where and

Given a leveling for S, the level of any element x

in s is defined as:

LLLL rr 121...

Page 11: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1104/19/23

Skip List Description Given any leveling of the set S, we define the skip

list corresponding to this structure like this:

The level is stored in a sorted link list.

Each node x in this linked list has a pile of

nodes above it.

There are horizontal and vertical pointers between

nodes.

For convenience, we assume that two special elements

and belong to each of the levels.

Page 12: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1204/19/23

Skip List Example For example, for this leveling:

This is the skip list corresponding to it:

}35,15,2{

}45,35,23,15,2{

}47,45,35,32,31,23,20,15,11,5,2{

3

2

1

LLL

Page 13: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1304/19/23

Skip list as binary tree

An interval level I, is the set of

elements of S, spanned by a

specific horizontal pointer at level

i.

For example, for the previous skip

list, this is the interval at level 2:

Page 14: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1404/19/23

Skip list as binary tree The interval partition structure is more

conveniently viewed as a tree, where each node corresponds to an interval.

If an interval J at level i+1 contains as a subsets an interval I at level i, then node J is the parent of node I in the tree.

For interval I at level i+1, C(I) denotes the number of children of Interval I at level i.

Page 15: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1504/19/23

Searching skip lists

Consider an element y, that is not necessarily

an member of S, and assume we want to

search for it in skip list s.

Let be the interval at level j that contains

y.

Page 16: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1604/19/23

Searching skip lists – Cont.

We can now view the nested sequence of

intervals

as a root-leaf path in the tree representation

of the skip list.

Page 17: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1704/19/23

Random skip list To complete the description of the skip list, we

have to specify the choice of the leveling that underlies it.

The basic idea is to chose a random leveling, thereby defining a random skip list.

A random leveling is defined as follows:given the choice of level the level is defined by independently

choosing to retain each element with probability ½.

Page 18: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1804/19/23

Outline

Motivation for the

skipping

Skip List definitions and

description

Analyzing performance

The role of randomness

Page 19: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 1904/19/23

Search cost What is the expected time to find an element

in a random skip list?

We will show it is O(logn) with high

probability.

What is the expected space of a random skip

list?

O(n). Why?

Page 20: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2004/19/23

Random procedure An alternative view of the random construction is as

follows:

Let l(x) for every be independent random variable,

with geometric distribution.

Let r be one more than the maximum of these random

variables.

Place x in each of the levels, .….. .

As with random priorities in treaps, a random

level is chosen once for every element in it’s

insertion.

Page 21: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2104/19/23

The expected number of levels Lemma: the number of levels r in a

random leveling of a set S of size n is

O(logn) with high probability.

Proof: look at the board!

Does it mean that the E[r]=O(logn)? Why?

Is it enough? No!

Page 22: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2204/19/23

The search path length The last result implies that the tree

representing the skip list has height

o(logn) with high probability.

Unfortunately, since the tree need not be

binary, it does not immediately follows

that the search time is similarly bounded.

The implementation of Find(x,s)

corresponds to walking down the path

Page 23: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2304/19/23

The search path length – Cont. Walking down the path

is as follows:

At level j, starting at the node , use a vertical pointer

to descend to the leftmost child of the current interval;

then using the horizontal pointers, move rightward till the

node

The cost of FIND(x,s) proportional to the number of

levels as well as the number of intervals visited at

each level.

Page 24: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2404/19/23

The search path length is O(logn) Lemma 2: Let y be any element and consider the search path

followed by FIND(y, S) in a random skip list for the set S of size n, then:

A. is the length of the search path.

B.

)(log])))((1([

1

nOyCEr

jjI

r

jjyC I

1

)))((1(

Page 25: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2504/19/23

Proof of Lemma 2 Proof of A: the number of nodes visited at level j does not

exceed the number of children of the interval

therefore in each level, you walk through

elements and in total . Proof of B: look at the board! This result shows that the expected time of search is

o(logn).

))((1 yC I j

r

jjyC I

1

)))((1(

Page 26: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2604/19/23

Insert and delete in skip list Insert and delete operation can be done

both in o(logn) expected time also. To insert an element y:

a random level l(y) (may exceed r) should be chosen for y as described earlier.

Then a search operation should find the search path of y

Then update the correct intervals, and add pointers.

Delete operation is just the converse of insert.

Page 27: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2704/19/23

Outline

Motivation for the

skipping

Skip List definitions and

description

Analyzing performance

The role of randomness

Page 28: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2804/19/23

The role of randomness

What is the role of the randomness in skip lists?

The random leveling of S enables us to avoid

complicated “balancing” operations.

This simplifies our algorithm and decreases the

overhead.

Page 29: Randomized Algorithms - Treaps 1 8/24/2015 Randomized Skip List Skip List Or, In Hebrew: רשימות דילוג

Randomized Algorithms - Treaps 2904/19/23

. -cont The role of randomness

It also saves us the need to “remember” the

state of the node or the system.

Unlike binary search trees, skip lists behavior is

indifferent to the input distribution.