/
David  Stotts Computer Science Department David  Stotts Computer Science Department

David Stotts Computer Science Department - PowerPoint Presentation

heartersh
heartersh . @heartersh
Follow
342 views
Uploaded On 2020-06-22

David Stotts Computer Science Department - PPT Presentation

UNC Chapel Hill Data Structures and Analysis COMP 410 Skip Lists some slides adapted from UMd CMSC 420 Bill Pugh et al Generalization of sorted linked list Invented by Bill Pugh ID: 783560

list level dice var level list var dice insert skip roll log levhits node nlevels elt cell find perfect

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "David Stotts Computer Science Departmen..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

David StottsComputer Science DepartmentUNC Chapel Hill

Data Structures and Analysis(COMP 410)

Slide2

Skip Lists( some slides adapted from UMd CMSC 420, Bill Pugh et al. )

Slide3

Generalization of sorted linked listInvented by Bill Pugh (UMd) in 1990Original CACM paper: Pugh, W. (1990). "Skip lists: A probabilistic alternative to balanced trees" (PDF). Communications of the ACM. 33 (6): 668. An alternative to balanced BSTProbabilistic Data Structure

we roll dice, flip coins to build the structureSkip List

but first …

Slide4

“Perfect” skip list

first… no dice123

Multiple lists, multi-level list,

O(log N) levels

Level

i+1

has half as many items as level

i

12

3

4

13

14

17

7

7

17

14

13

12

4

3

x

x

x

x

x

Level

i+1

divides level

i

in half

sentinels

Slide5

“Perfect” skip list

first… no dice123

List nodes are variable size… each with 1 to O(log N) pointers

Sentinels at each end of “empty list”

12

3

4

13

14

17

7

7

17

14

13

12

4

3

x

x

x

x

x

Each level lets you “skip over” many items below in one hop

sentinels

Slide6

“Perfect” skip list

4712

x

x

x

x

x

1

2

3

Search path…

find(7)

Start at top level, move right on a level, down as compare

Slide7

Slide8

Slide9

Slide10

Slide11

Slide12

Now add probabilityImperfect list, randomized order of cell sizesCell size is chosen on add by roll-of-dice

xxx

x

x

Level

i

not guaranteed to have twice level i+1, but random numbers will make it near twice

3

2

1

Level

i

not guaranteed to split level i-1 into equal parts

Slide13

Slide14

Example insert

9

X

X

3

29

74

42

12

X

X

X

insert(9)

Roll dice: get a

1

new node should be a one-level cell

Slide15

Example insert

9

X

X

3

29

74

42

12

X

X

X

insert(35)

Roll dice: get a

3

new node should be a three-level cell

35

Slide16

Example insert

9

X

X

3

29

74

42

12

X

X

X

insert(35)

Roll dice: get a

3

new node should be a three-level cell

35

Slide17

Another example

Slide18

Another example

Slide19

Another example

Slide20

Another example

Slide21

Slide22

function genRandomLevel ( ) { // generate an integer 0 or larger // following a distribution where 0 is 0.5 likely // 1 is .025 likely, 2 is .0125 likely, 3 is .00625, // etc.

var ranLev=0; while (

Math.random

() > 0.5 )

ranLev

++;

return

ranLev

;

}

Rolling the Dice

Slide23

var nLevels = 16;var nTrials = 1000; var SL = makeSkipList(nLevels);var levHits

= [ ];for (var i=0; i<nLevels; i++) { levHits

[

i

]=0; }

for (

var

i

=0;

i

<

nTrials

;

i

++) {

levHits

[

SL.dice

(

nLevels) ]++; }

alert(levHits);

Gives these node level counts:

9965, 5037, 2519, 1263, 619, 289, 149, 84, 30, 24, 14, 6, 0, 1, 0, 0

Checking the distribution

Slide24

OO Signaturenew:  SKLISTinsert: Elt  remove: Elt 

find: Elt  Boolean (searching)size:  Int

+

(non-negative integers)

empty:  Boolean

ADT: SKLIST of

Elt

Slide25

Time complexity of operationsinsert worst: O(n), avg: O(log n)remove worst: O(n), avg: O(log n) find worst: O(n), avg: O(log n)

empty O(1)size O(1)iterator O(n) (traversal)

SKLIST Implementation

Slide26

Slide27

Slide28

Slide29

Slide30

Slide31

Beyond this is just templatesEND

Slide32

Slide33

Slide34