# Welcome back

## Find a book, put up your feet, stay awhile

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more

Download

Standard view

Full view

of .

Look up keyword

Like this

Share on social networks

4Activity

×

0 of .

Results for: No results containing your search query

P. 1

Skip Lists: A Probabilistic Alternative to Balanced TreesRatings: (0)|Views: 306|Likes: 2

Published by newtonapple

Skip lists are a data structure that can be used in place of balanced trees. Skip lists use probabilistic balancing rather than strictly enforced balancing and as a result the algorithms for insertion and deletion in skip lists are much simpler and significantly faster than equivalent algorithms for balanced trees.

Skip lists are a data structure that can be used in place of balanced trees. Skip lists use probabilistic balancing rather than strictly enforced balancing and as a result the algorithms for insertion and deletion in skip lists are much simpler and significantly faster than equivalent algorithms for balanced trees.

See more

See less

https://www.scribd.com/doc/21669728/Skip-Lists-A-Probabilistic-Alternative-to-Balanced-Trees

08/09/2010

text

original

Skip Lists: A Probabilistic Alternative toBalanced Trees

Skip lists are a data structure that can be used in place of balanced trees.Skip lists use probabilistic balancing rather than strictly enforced balancingand as a result the algorithms for insertion and deletion in skip lists aremuch simpler and significantly faster than equivalent algorithms for balanced trees.

William Pugh

Binary trees can be used for representing abstract data typessuch as dictionaries and ordered lists. They work well whenthe elements are inserted in a random order. Some sequencesof operations, such as inserting the elements in order, producedegenerate data structures that give very poor performance. If it were possible to randomly permute the list of items to be in-serted, trees would work well with high probability for any in-put sequence. In most cases queries must be answered on-line,so randomly permuting the input is impractical.

Balanced

treealgorithms re-arrange the tree as operations are performed tomaintain certain balance conditions and assure good perfor-mance.S

kip lists

are a probabilistic alternative to balanced trees.Skip lists are balanced by consulting a random number gen-erator. Although skip lists have bad worst-case performance,no input sequence consistently produces the worst-case per-formance (much like quicksort when the pivot element is cho-sen randomly). It is very unlikely a skip list data structure willbe significantly unbalanced (e.g., for a dictionary of morethan 250 elements, the chance that a search will take morethan 3 times the expected time is less than one in a million).Skip lists have balance properties similar to that of searchtrees built by random insertions, yet do not require insertionsto be random.Balancing a data structure probabilistically is easier thanexplicitly maintaining the balance. For many applications,skip lists are a more natural representation than trees, alsoleading to simpler algorithms. The simplicity of skip list algo-rithms makes them easier to implement and provides signifi-cant constant factor speed improvements over balanced treeand self-adjusting tree algorithms. Skip lists are also veryspace efficient. They can easily be configured to require anaverage of 1

1

/

3

pointers per element (or even less) and do notrequire balance or priority information to be stored with eachnode.

SKIP LISTS

We might need to examine every node of the list when search-ing a linked list (

Figure

1

a

). If the list is stored in sorted orderand every other node of the list also has a pointer to the nodetwo ahead it in the list (

Figure

1

b

), we have to examine nomore than

n

/2

+ 1 nodes (where

n

is the length of the list).Also giving every fourth node a pointer four ahead (

Figure

1

c

) requires that no more than

n

/4

+

2 nodes be examined.If every (2

i

)

th

node has a pointer 2

i

nodes ahead (

Figure

1

d

),the number of nodes that must be examined can be reduced to

log2

n

while only doubling the number of pointers. Thisdata structure could be used for fast searching, but insertionand deletion would be impractical.A node that has

k

forward pointers is called a

level

k

node.If every (2

i

)

th

node has a pointer 2

i

nodes ahead, then levelsof nodes are distributed in a simple pattern: 50% are level 1,25% are level 2, 12.5% are level 3 and so on. What wouldhappen if the levels of nodes were chosen randomly, but in thesame proportions (e.g., as in Figure 1

e

)? A node’s

i

th

forwardpointer, instead of pointing 2

i

–1

nodes ahead, points to thenext node of level

i

or higher. Insertions or deletions wouldrequire only local modifications; the level of a node, chosenrandomly when the node is inserted, need never change. Somearrangements of levels would give poor execution times, butwe will see that such arrangements are rare. Because thesedata structures are linked lists with extra pointers that skipover intermediate nodes, I named them

skip lists

.

SKIP LIST ALGORITHMS

This section gives algorithms to search for, insert and deleteelements in a dictionary or symbol table. The

Search

opera-tion returns the contents of the value associated with the de-sired key or

failure

if the key is not present. The

Insert

opera-tion associates a specified key with a new value (inserting thekey if it had not already been present). The

Delete

operationdeletes the specified key. It is easy to support additional oper-ations such as “find the minimum key” or “find the next key”.Each element is represented by a node, the level of whichis chosen randomly when the node is inserted without regardfor the number of elements in the data structure. A

level

i

node has

i

forward

pointers, indexed 1 through

i

. We do notneed to store the level of a node in the node. Levels arecapped at some appropriate constant

MaxLevel

. The

level

of alist is the maximum level currently in the list (or 1 if the list isempty). The

header

of a list has forward pointers at levels onethrough

MaxLevel

. The forward pointers of the header atlevels higher than the current maximum level of the list pointto NIL.

abcd e

25367912171921252636791217192125263679121719212526367912171921263679121719212526NILNILNILNILNILF

IGURE

1 - Linked lists with additional pointers

Search(list, searchKey)x := list

→

header

-- loop invariant: x

→

key

<

searchKey

for

i := list

→

level

downto

1

dowhile

x

→

forward[i]

→

key < searchKey

do

x :=

x

→

forward[i]

--

x

→

key

<

searchKey

≤

x

→

forward[1]

→

key

x :=

x

→

forward[1]

if

x

→

key = searchKey

then

return

x

→

value

else

return

failure

F

IGURE

2 - Skip list search algorithm

Initialization

An element NIL is allocated and given a key greater than anylegal key. All levels of all skip lists are terminated with NIL.A new list is initialized so that the the

level

of the list is equalto 1 and all forward pointers of the list’s header point to NIL.

Search Algorithm

We search for an element by traversing forward pointers thatdo not overshoot the node containing the element beingsearched for (Figure 2). When no more progress can be madeat the current level of forward pointers, the search movesdown to the next level. When we can make no more progressat level 1, we must be immediately in front of the node thatcontains the desired element (if it is in the list).

Insertion and Deletion Algorithms

To insert or delete a node, we simply search and splice, asshown in Figure 3. Figure 4 gives algorithms for insertion anddeletion. A vector

update

is maintained so that when thesearch is complete (and we are ready to perform the splice),

update

[

i

] contains a pointer to the rightmost node of level

i

orhigher that is to the left of the location of the inser-tion/deletion.If an insertion generates a node with a level greater thanthe previous maximum level of the list, we update the maxi-mum level of the list and initialize the appropriate portions of the update vector. After each deletion, we check if we havedeleted the maximum element of the list and if so, decreasethe maximum level of the list.

Choosing a Random Level

Initially, we discussed a probability distribution where half of the nodes that have level

i

pointers also have level

i

+1 point-ers. To get away from magic constants, we say that a fraction

p

of the nodes with level

i

pointers also have level

i

+1 point-ers. (for our original discussion,

p

= 1/2). Levels are generatedrandomly by an algorithm equivalent to the one in Figure 5.Levels are generated without reference to the number of ele-ments in the list.

At what level do we start a search? Defining

L

(

n

)

In a skip list of 16 elements generated with

p

= 1/2, we mighthappen to have 9 elements of level 1, 3 elements of level 2, 3elements of level 3 and 1 element of level 14 (this would bevery unlikely, but it could happen). How should we handlethis? If we use the standard algorithm and start our search atlevel 14, we will do a lot of useless work.Where should we start the search? Our analysis suggeststhat ideally we would start a search at the level

L

where weexpect 1/

p

nodes. This happens when

L

= log

1/

p

n

. Since wewill be referring frequently to this formula, we will use

L

(

n

)to denote log

1/

p

n.

There are a number of solutions to the problem of decidinghow to handle the case where there is an element with anunusually large level in the list.•

Don’t worry, be happy.

Simply start a search at the highestlevel present in the list. As we will see in our analysis, theprobability that the maximum level in a list of

n

elements issignificantly larger than

L

(

n

) is very small. Starting asearch at the maximum level in the list does not add morethan a small constant to the expected search time. This isthe approach used in the algorithms described in this paper.

Insert(list, searchKey, newValue)

local

update[1..MaxLevel]x := list

→

header

for

i := list

→

level

downto

1

dowhile

x

→

forward[i]

→

key < searchKey

do

x :=

x

→

forward[i]

-- x

→

key < searchKey

≤

x

→

forward[i]

→

key

update[i] :=

xx :=

x

→

forward[1]

if

x

→

key = searchKey

then

x

→

value :=

newValue

else

lvl := randomLevel()

if

lvl > list

→

level

thenfor

i := list

→

level + 1

to

lvl

do

update[i] := list

→

headerlist

→

level := lvlx := makeNode(lvl, searchKey, value)

for

i := 1

to

level

do

x

→

forward[i] :=

update[i]

→

forward[i]update[i]

→

forward[i] := xDelete(list, searchKey)

local

update[1..MaxLevel]x := list

→

header

for

i :=

list

→

level

downto

1

dowhile

x

→

forward[i]

→

key < searchKey

do

x :=

x

→

forward[i]update[i] :=

xx :=

x

→

forward[1]

if

x

→

key = searchKey

thenfor

i := 1

to

list

→

level

doif

update[i]

→

forward[i]

≠

x

then break

update[i]

→

forward[i] := x

→

forward[i]free(x)

while

list

→

level > 1

and

list

→

header

→

forward[list

→

level] = NIL

do

list

→

level := list

→

level – 1

F

IGURE

4 - Skip List insertion and deletion algorithms

randomLevel()lvl := 1

-- random

()

that returns a random value in

[0...1)

while

random() < p

and

lvl < MaxLevel

do

lvl := lvl + 1

return

lvl

F

IGURE

5 - Algorithm to calculate a random level

Search pathupdate

[

i

]

→

forward

[

i

]

original list, 17 to be inserted list after insertion, updated pointers in grey

367912192125263679121921252617NILNILF

IGURE

3 - Pictorial description of steps involved in performing an insertion•

Use less than you are given.

Although an element may con-tain room for 14 pointers, we don’t need to use all 14. Wecan choose to utilize only

L

(

n

) levels. There are a numberof ways to implement this, but they all complicate the algo-rithms and do not noticeably improve performance, so thisapproach is not recommended.•

Fix the dice.

If we generate a random level that is more thanone greater than the current maximum level in the list, wesimply use one plus the current maximum level in the list asthe level of the new node. In practice and intuitively, thischange seems to work well. However, it totally destroys ourability to analyze the resulting algorithms, since the level of a node is no longer completely random. Programmersshould probably feel free to implement this, purists shouldavoid it.

Determining

MaxLevel

Since we can safely cap levels at

L

(

n

), we should choose

MaxLevel

=

L

(

N

) (where

N

is an upper bound on the numberof elements in a skip list). If

p

= 1/2, using

MaxLevel

= 16 isappropriate for data structures containing up to 2

16

elements.

ANALYSIS OF SKIP LIST ALGORITHMS

The time required to execute the

Search, Delete

and

Insert

operations is dominated by the time required to search for theappropriate element. For the

Insert

and

Delete

operations,there is an additional cost proportional to the level of the nodebeing inserted or deleted. The time required to find an elementis proportional to the length of the search path, which is de-termined by the pattern in which elements with differentlevels appear as we traverse the list.

Probabilistic Philosophy

The structure of a skip list is determined only by the number

- Read and print without ads
- Download to keep your version
- Edit, email or read offline

© Copyright 2015 Scribd Inc.

Language:

Choose the language in which you want to experience Scribd:

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Password Reset Email Sent

Join with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

By joining, you agree to our

read free for one month

Personalized recommendationsbased on books you love

Syncing across all your devices

Join with Facebook

or Join with EmailSorry, we are unable to log you in via Facebook at this time. Please try again later.

Already a member? Sign in.

By joining, you agree to our

to download

Personalized recommendationsbased on books you love

Syncing across all your devices

Continue with Facebook

Sign inJoin with emailSorry, we are unable to log you in via Facebook at this time. Please try again later.

By joining, you agree to our

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd