CS 261 Fall 2006 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS 261 Fall 2006

Description:

Ordinary linked lists and vectors have fast (O(1)) addition, but slow search ... Skip Lists have it all - fast (O(log n)) addition, search and removal times! ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 26
Provided by: osue6
Category:
Tags: aret | fall

less

Transcript and Presenter's Notes

Title: CS 261 Fall 2006


1
CS 261 - Fall 2006
  • Skip Lists

2
Skip Lists Advantages
  • Ordinary linked lists and vectors have fast
    (O(1)) addition, but slow search
  • Sorted Vectors have fast search (O(log
    n)) but slow insertion
  • Skip Lists have it all - fast (O(log n))
    addition, search and removal times!
  • Cost - Slightly more complicated

3
Start with an ordered listLets begin with a
simple ordered list, sentinel on the left, single
links.
4
Start of the struct definition
  • struct skiplist
  • struct skiplink sentinel
  • int size

5
Operations
  • Add (Object newValue) - find correct location,
    add the value
  • Contains (Object testValue) - find correct
    location, see if its what we want
  • Remove (Object testValue) - find correct
    location, possibly remove it
  • See anything in common?

6
Slide Right
  • struct skiplink slideRight (struct skiplink
    current, EleType testValue)
  • while ((current-gtnext ! 0)
    LT(current-gtnext-gtvalue, testValue))
  • current current-gtnext
  • return current
  • Finds the link RIGHT BEFORE the place the value
    we want should be, if it is there

7
Add (EleType newValue)
  • void add (struct skiplist lst, EleType newValue)
  • struct skiplink current
  • slideRight(lst-gtsentinel, newValue)
  • struct link newLink (struct link )
    malloc(sizeof(struct link))
  • assert (newLink ! 0)
  • newLink-gtvalue newValue
  • newLink-gtnext current-gtnext
  • current-gtnext newLink
  • lst-gtsize

8
Contains (EleType testValue)
  • int contains (struct skip lst, EleType
    testValue)
  • struct skiplink current
  • slideRight
    (lst-gtleftSentinel, testValue)
  • if ((current-gtnext ! 0)
  • (EQ(testValue, current-gtnext-gtvalue)))
  • return true
  • return false
  • / reminder, this is one level list, not
    real skiplist /

9
Remove (Object testValue)
  • void remove (struct skiplist lst, EleType
    testValue)
  • struct skiplink current
  • slideRight (lst-gtleftSentinel,
    testValue)
  • if ((current-gtnext ! 0)
  • (EQ(testValue, current-gtnext-gtvalue)))
  • current-gtnext current-gtnext-gtnext
  • free(current-gtnext)
  • lst-gtsize--

10
Problem with Sorted List
  • Whats the use?
  • Add is still O(n), so is contains, so is remove.
  • No better than an ordinary list.
  • Major problem with list - sequential access

11
Why no binary search?What if we kept a link to
middle of list?
12
But need to keep going
13
And going and going
14
In theory it would work..
  • In theory this would work
  • Would give us nice O(log n) search
  • But would be really hard to maintain as values
    were inserted and removed
  • What about if we relax the rules, and dont try
    to be so precise..

15
The power of probability
  • Instead of being precise, keep a hierarchy of
    ordered lists with downward pointers
  • Each list has approximately half the elements of
    the one below
  • So, if the bottom list has n elements, how many
    levels are there??

16
Picture of Skip List
17
Slightly bigger list
18
Contains (Object testValue)
  • Starting at topmost sentinel, slide right
  • If next value is it, return true
  • If next value is not what we are looking for,
    move down and slide right
  • If we get to bottom without finding it, return
    false

19
Notes about contains
  • See how it makes a zig-zag from top to bottom
  • On average, slide only moves one or two positions
  • So basically proportional to height of structure
  • Which is????? O( ?? )

20
Remove Algorithm
  • Start at topmost sentinal
  • Loop as follows
  • Slide right. If next element is the one we want,
    remove it
  • If no down element, reduce size
  • Move down

21
Notice about Remove
  • Only want to decrement size counter if at bottom
    level
  • Again, makes zig-zag motion to bottom, is
    proportional to height
  • So it is again O(log n)

22
Add (Object newElement)
  • Now we come to addition - the most complex
    operation.
  • Intuitive idea - add to bottom row (remember to
    increment count)
  • Move upwards, flipping a coin
  • As long as heads, add to next higher row
  • At top, again flip, if heads make new row

23
Add ExampleInsert the following 9 14 7 3
20Coin toss T H T H H T T H T (insert if heads)
24
The Power of Chance
  • At each step we flip a coin
  • So the exact structure is not predictable, and
    will be different even if you add the same
    elements in same order over again
  • But the gross structure is predictable, because
    if you flip a coin N times as N gets large you
    expect N/2 to be heads.

25
Notes on Add
  • Again proportional to height of structure, not
    number of nodes in list
  • So also O(log n)
  • So all three operations are O(log n) !
  • By far the fastest overall Bag structure we have
    seen!
  • (Downside - does use a lot of memory as values
    are repeated. But who cares about memory? )
Write a Comment
User Comments (0)
About PowerShow.com