Computer notes - Skip List

1,768 views

Published on

A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , … , Sh such that Each list Si contains the special keys +¥ and -¥ List S0 contains the keys of S in nondecreasing order

Published in: Technology, Travel
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,768
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
48
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • End of lecture 39, Start of lecture 40.
  • End of lecture 40.
  • Computer notes - Skip List

    1. 1. Class No.34 Data Structures http://ecomputernotes.com
    2. 2. Skip List: formally <ul><li>A skip list for a set S of distinct (key, element) items is a series of lists S 0 , S 1 , … , S h such that </li></ul><ul><ul><li>Each list S i contains the special keys  and  </li></ul></ul><ul><ul><li>List S 0 contains the keys of S in nondecreasing order </li></ul></ul><ul><ul><li>Each list is a subsequence of the previous one, i.e., S 0  S 1  …  S h </li></ul></ul><ul><ul><li>List S h contains only the two special keys </li></ul></ul>
    3. 3. Skip List: formally http://ecomputernotes.com 56 64 78  31 34 44  12 23 26 S 0 64  31 34  23 S 1  31  S 2   S 3
    4. 4. Skip List: Search <ul><li>We search for a key x as follows: </li></ul><ul><ul><li>We start at the first position of the top list </li></ul></ul><ul><ul><li>At the current position p , we compare x with y  key ( after ( p )) </li></ul></ul><ul><ul><li>x  y : we return element ( after ( p )) </li></ul></ul><ul><ul><li>x  y : we “scan forward” </li></ul></ul><ul><ul><li>x  y : we “drop down” </li></ul></ul><ul><ul><li>If we try to drop down past the bottom list, we return NO_SUCH_KEY </li></ul></ul>http://ecomputernotes.com
    5. 5. Skip List: Search <ul><li>Example: search for 78 </li></ul>S 0 S 1 S 2 S 3  31  64  31 34  23 56 64 78  31 34 44  12 23 26 http://ecomputernotes.com  
    6. 6. <ul><li>To insert an item ( x , o ) into a skip list, we use a randomized algorithm: </li></ul><ul><ul><li>We repeatedly toss a coin until we get tails, and we denote with i the number of times the coin came up heads </li></ul></ul><ul><ul><li>If i  h , we add to the skip list new lists S h  1 , … , S i  1 , each containing only the two special keys </li></ul></ul>Skip List: Insertion http://ecomputernotes.com
    7. 7. <ul><li>To insert an item ( x , o ) into a skip list, we use a randomized algorithm: (cont) </li></ul><ul><ul><li>We search for x in the skip list and find the positions p 0 , p 1 , …, p i of the items with largest key less than x in each list S 0 , S 1 , … , S i </li></ul></ul><ul><ul><li>For j  0, …, i , we insert item ( x , o ) into list S j after position p j </li></ul></ul>Skip List: Insertion http://ecomputernotes.com
    8. 8. <ul><li>Example: insert key 15 , with i  2 </li></ul>Skip List: Insertion   10 36   23 23   S 0 S 1 S 2 p 0 p 1 p 2 http://ecomputernotes.com   S 0 S 1 S 2 S 3   10 36 23 15   15   23 15
    9. 9. Randomized Algorithms <ul><li>A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution </li></ul><ul><li>It contains statements of the type </li></ul><ul><ul><li>b  random () </li></ul></ul><ul><ul><li>if b <= 0.5 // head </li></ul></ul><ul><ul><li>do A … </li></ul></ul><ul><ul><li>else // tail </li></ul></ul><ul><ul><li>do B … </li></ul></ul><ul><li>Its running time depends on the outcomes of the coin tosses, i.e, head or tail </li></ul>http://ecomputernotes.com
    10. 10. Skip List: Deletion <ul><li>To remove an item with key x from a skip list, we proceed as follows: </li></ul><ul><ul><li>We search for x in the skip list and find the positions p 0 , p 1 , …, p i of the items with key x , where position p j is in list S j </li></ul></ul><ul><ul><li>We remove positions p 0 , p 1 , …, p i from the lists S 0 , S 1 , … , S i </li></ul></ul><ul><ul><li>We remove all but one list containing only the two special keys </li></ul></ul>http://ecomputernotes.com
    11. 11. Skip List: Deletion <ul><li>Example: remove key 34 </li></ul>  S 0 S 1 S 2 S 3   45 12 23 34   34   23 34 p 0 p 1 p 2 http://ecomputernotes.com   45 12   23 23   S 0 S 1 S 2

    ×