Docsity

Class Note - Skip Lists - Data Structures | CMSC 420, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Class Note - Skip Lists - Data Structures | CMSC 420 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Skip Lists

CMSC 420

Linked Lists Benefits & Drawbacks

  • Benefits:

Easy to insert & delete in O(1) time

Don’t need to estimate total memory needed

  • Drawbacks:

Hard to search in less than O(n) time

(binary search doesn’t work, eg.)

Hard to jump to the middle

  • Skip Lists:

fix these drawbacks

good data structure for a dictionary ADT

Perfect Skip Lists

header

sentinel

Perfect Skip Lists

  • Keys in sorted order.
  • O(log^ n )^ levels
  • Each higher level contains 1/2 the elements of the

level below it.

  • Header & sentinel nodes are in every level

Perfect Skip Lists, continued

Find 71

71 < Inf?

71 < 91?

71 < 96?

Comparison

71 < 31?

Change

current

location

When search for k:

If k = key, done!

If k < next key, go down a level

If k ≥ next key, go right

In other words,

  • To find an item, we scan along the shortest list until

we would “pass” the desired item.

  • At that point, we drop down to a slightly more

complete list at one level lower.

  • Remember: sorted sequential searching...

for (i = 0; i < n; i++)

if (X[i] >= K) break ;

if (X[i] != K) return FAIL;

Search Time:

  • O(log n) levels --- because you cut the # of items in

half at each level

  • Will visit at most 2 nodes per level:

If you visit more, then you could have done it on

one level higher up.

  • Therefore, search time is O(log n).

Insert & Delete

  • Insert & delete might need to rearrange the entire

list

  • Like Perfect Binary Search Trees, Perfect Skip Lists

are too structured to support efficient updates.

  • Idea:

Relax the requirement that each level have exactly half

the items of the previous level

Instead: design structure so that we expect 1/2 the items

to be carried up to the next level

Skip Lists are a randomized data structure: the same

sequence of inserts / deletes may produce different

structures depending on the outcome of random coin

flips.

Randomized Skip List:

Insertion:

Insert 87

Deletion:

Delete 87

Deletion:

Delete 87

Skip List Analysis

  • Expected number of levels = O(log n)

E[# nodes at level 1] = n/

E[# nodes at level 2] = n/

...

E[# nodes at level log n] = 1

  • Still need to prove that # of steps at each level is

small.

Backwards Analysis

Consider the reverse of the path you took to find k :

Note that you always move up if you can.

(because you always enter a node from its topmost

level when doing a find)