Introduction to Data Structures and Algorithms - Assignment 2 | CS 361L, Assignments of Computer Science

Material Type: Assignment; Class: Data Structures and Algorithms; Subject: Computer Science; University: University of New Mexico; Term: Spring 2004;

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-atd-1
koofers-user-atd-1 🇺🇸

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 361, Lecture 24
Jared Saia
University of New Mexico
Outline
Skip Lists
1
Homework
Any questions on the homework?
2
Administrative
This week and next, you can get one extra participation
check by going to section and informing Nate that you’re
there and want a check.
Sections are Thursday 5:30-6:20 DSH 134 and Friday 1:00-
1:50 TAPY 218
Good chance to get info on hw, projects and to review ma-
terial for final
3
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Data Structures and Algorithms - Assignment 2 | CS 361L and more Assignments Computer Science in PDF only on Docsity!

CS 361, Lecture 24

Jared Saia

University of New Mexico

Outline

Skip Lists

Homework

Any questions on the homework?

Administrative

This

week

and

next,

you

can

get

one

extra

participation

there and want a check.check by going to section and informing Nate that you’re

1:50 TAPY 218Sections are Thursday 5:30-6:20 DSH 134 and Friday 1:00-

terial for finalGood chance to get info on hw, projects and to review ma-

Project

Project will be due May 6th in class

Late projects will

not

be accepted

lateon time but will get no credit for a finished project turned inYou can get partial credit for an unfinished project turned in

Final

Final will be May 11th 5:30-7:30pm in our regular classroom

Closed book,

but two pieces of paper and calculators are

allowed

Skip List

operationsTechnically, not a BST, but they implement all of the same

analysis is subtleVery elegant randomized data structure, simple to code but

erations takeThey guarantee that, with high probability, all the major op-

O

(log

(^) n

) time

Skip List

A

skip

list

is

basically

a

collection

of

doubly-linked

lists,

L

1 , L

2 ,... , L

x , for some integer

x

nodes are assumed to beEach list has a special head and tail node, the keys of these

MAXNUM and +MAXNUM re-

spectively

The keys in each list are in sorted order (non-decreasing)

Search Example

−∞

+∞

1 2 3 4 5 6 7 8 9

0

−∞

+∞

1

6

7

9

0

−∞

+∞

1

6

7

3

−∞

+∞

1

7

−∞

+∞

7

−∞

+∞

Insert

p

is a constant between 0 and 1, typically

p

= 1

2, let rand()

while (rand()<= p){i = 2;Insert k in L_1, to the right of pLeftFirst call Search(k), let pLeft be the leftmost elem <= k in L_1 Insert(k){return a random value between 0 and 1

insert k in the appropriate place in L_i;

Deletion

Deletion is very simple

First do a search for the key to be deleted

deletionthe bottom up, making sure to “zip up” the lists after theThen delete that key from all the lists it appears in from

Analysis

number of levels to be aboutber of nodes of the previous level, so we expect the totalIntuitively, each level of the skip list has about half the num-

O

(log

(^) n

)

time in half except for a constant overheadSimilarly, each time we add another level, we cut the search

So after

O

(log

(^) n

) levels, we would expect a search time of

O

(log

(^) n

)

We will now formalize these two intuitive observations

Height of Skip List

For some key,

i , let

X

i

be the maximum height of

i

in the

skip list.

Q: What is the probability that

X

i ≥

2 log

(^) n

?

A: If

p

= 1

2, we have:

P

X

i ≥

2 log

(^) n

)

( 2 1 ) 2 log

(^) n

log

(^) n ) 2

n 2

Thus the probability that a particular key

i

achieves height

2 log

(^) n

is

n 1 2

Height of Skip List

Q:

What

is

the

probability

that

any

key

achieves

height

2 log

(^) n

?

A: We want

P

X

1

2 log

(^) n

or

X

2

2 log

(^) n

or

or

X

n

2 log

(^) n

)

By a Union Bound, this probability is no more than

P (^) ( X

1

k (^) log

(^) n

) +

P

X

2

k (^) log

(^) n

) +

P

X

n

k (^) log

(^) n

)

Which equals:

n

i=1 ∑

n 1 2

=

n

n 2

= 1

/n

Height of Skip List

This probability gets small as

n

gets large

ceeding 2 logIn particular, the probability of having a skip list of size ex-

(^) n

is

o (1)

If an event occurs with probability 1

o (1), we say that it

occurs

with high probability

Key Point:

The height of a skip list is

O

(log

(^) n

) with high

probability.

In-Class Exercise Trick

variables: A trick for computing expectations of discrete positive random

Let

X

be a discrete r.v., that takes on values from 1 to

n

E

X

n

i=1 ∑

P

X

i )

Backward Search

For every node

v

in the skip list Up(v) exists with probability

So for purposes of analysis, SLFBack is the same as

FlipWalk(v){the following algorithm:

while (v != L){

if (COINFLIP == HEADS)

v = Up(v);

else

v = Left(v);

Analysis

the same as the expected number of tailsFor this algorithm, the expected number of heads is exactly

expected number of upward jumpsThus the expected run time of the algorithm is twice the

is Since we already know that the number of upward jumps

O

(log

(^) n

) with high probability, we can conclude that the

expected search time is

O

(log

(^) n

)