Skip List. CS 561, Lecture 11. Skip List. Skip List

Similar documents
Design and Analysis of Algorithms

Skip Lists. What is a Skip List. Skip Lists 3/19/14

RANDOMIZED ALGORITHMS

Dictionary: an abstract data type

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

WHAT IS A SKIP LIST? S 2 S 1. A skip list for a set S of distinct (key, element) items is a series of lists

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Dictionary: an abstract data type

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

1 Maintaining a Dictionary

SkipList. Searching a key x in a sorted linked list. inserting a key into a Sorted linked list. To insert

Searching, mainly via Hash tables

Data Structures and Algorithms " Search Trees!!

Problem Set 1. CSE 373 Spring Out: February 9, 2016

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Queues. Principles of Computer Science II. Basic features of Queues

Design of discrete-event simulations

Ph219/CS219 Problem Set 3

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Fundamental Algorithms

11.1 Set Cover ILP formulation of set cover Deterministic rounding

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Evolving a New Feature for a Working Program

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Priority queues implemented via heaps

Recitation 7. Treaps and Combining BSTs. 7.1 Announcements. FingerLab is due Friday afternoon. It s worth 125 points.

Analysis of Algorithms CMPSC 565

Advanced topic: Space complexity

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

CS 561, Lecture: Greedy Algorithms. Jared Saia University of New Mexico

CSE 202: Design and Analysis of Algorithms Lecture 3

CMPUT 675: Approximation Algorithms Fall 2014

Hashing. Dictionaries Hashing with chaining Hash functions Linear Probing

Today s Outline. CS 561, Lecture 15. Greedy Algorithms. Activity Selection. Greedy Algorithm Intro Activity Selection Knapsack

where Q is a finite set of states

14.1 Finding frequent elements in stream

INF2220: algorithms and data structures Series 1

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

PRAM lower bounds. 1 Overview. 2 Definitions. 3 Monotone Circuit Value Problem

CS 161: Design and Analysis of Algorithms

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Lecture 3: Sizes of Infinity

Fundamentals of Probability CE 311S

Proving languages to be nonregular

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 12. Random Variables: Distribution and Expectation

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

STA 247 Solutions to Assignment #1

An example of a decidable language that is not a CFL Implementation-level description of a TM State diagram of TM

Error Detection and Correction: Small Applications of Exclusive-Or

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) =

Lecture 4 Thursday Sep 11, 2014

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Note 14

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CS 473: Algorithms. Ruta Mehta. Spring University of Illinois, Urbana-Champaign. Ruta (UIUC) CS473 1 Spring / 32

Theorem 1.7 [Bayes' Law]: Assume that,,, are mutually disjoint events in the sample space s.t.. Then Pr( )

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Section 1 (closed-book) Total points 30

10.3 Random variables and their expectation 301

Advanced Algorithms (2IL45)

To find the median, find the 40 th quartile and the 70 th quartile (which are easily found at y=1 and y=2, respectively). Then we interpolate:

Basic Probability Reference Sheet

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Sliding Windows with Limited Storage

1 More finite deterministic automata

CS5371 Theory of Computation. Lecture 15: Computability VI (Post s Problem, Reducibility)

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees

Discrete Mathematics for CS Spring 2007 Luca Trevisan Lecture 27

Turing Machines Part II

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109

Part I: Definitions and Properties

Binary Search Trees. Motivation

Lecture 7: Fingerprinting. David Woodruff Carnegie Mellon University

Mildly Context-Sensitive Grammar Formalisms: Embedded Push-Down Automata

Computation Complexity

Languages, regular languages, finite automata

Automata and Computability. Solutions to Exercises

Introduction to Bioinformatics

Boolean circuits. Lecture Definitions

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

1 Approximate Quantiles and Summaries

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Note 16. Random Variables: Distribution and Expectation

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Homework Assignment 6 Answers

data structures and algorithms lecture 2

CPSC 467: Cryptography and Computer Security

Countability. 1 Motivation. 2 Counting

CS 591, Lecture 9 Data Analytics: Theory and Applications Boston University

Automata and Computability. Solutions to Exercises

N/4 + N/2 + N = 2N 2.

Lecture Notes 3 Convergence (Chapter 5)

UNIVERSITY OF ALABAMA Department of Physics and Astronomy. PH / LeClair Fall Circuits Exercises

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

0 1 d 010. h 0111 i g 01100

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

Transcription:

Skip List CS 561, Lecture 11 Jared Saia University of New Mexico Technically, not a BST, but they implement all of the same operations Very elegant randomized data structure, simple to code but analysis is subtle They guarantee that, with high probability, all the major operations take O(log n) time 1 Skip List Skip List A skip list is basically a collection of doubly-linked lists, L 1, L 2,..., L x, for some integer x Each list has a special head and tail node, the keys of these nodes are assumed to be MAXNUM and +MAXNUM respectively The keys in each list are in sorted order (non-decreasing) Every node is stored in the bottom list For each node in the bottom list, we flip a coin over and over until we get tails. For each heads, we make a duplicate of the node. The duplicates are stacked up in levels and the nodes on each level are strung together in sorted linked lists Each node v stores a search key (key(v)), a pointer to its next lower copy (down(v)), and a pointer to the next node in its level (right(v)). 2 3

Example Search To do a search for a key, x, we start at the leftmost node L in the highest level We then scan through each level as far as we can without passing the target value x and then proceed down to the next level The search ends either when we find the key x or fail to find x on the lowest level 4 5 Search Search Example SkipListFind(x, L){ v = L; while (v!= NULL) and (Key(v)!= x){ if (Key(Right(v)) > x) v = Down(v); else v = Right(v); } return v; } 6 7

Insert Deletion p is a constant between 0 and 1, typically p = 1/2, let rand() return a random value between 0 and 1 Insert(k){ First call Search(k), let pleft be the leftmost elem <= k in L_1 Insert k in L_1, to the right of pleft i = 2; while (rand()<= p){ insert k in the appropriate place in L_i; } Deletion is very simple First do a search for the key to be deleted Then delete that key from all the lists it appears in from the bottom up, making sure to zip up the lists after the deletion 8 9 Analysis Height of Skip List Intuitively, each level of the skip list has about half the number of nodes of the previous level, so we expect the total number of levels to be about O(log n) Similarly, each time we add another level, we cut the search time in half except for a constant overhead So after O(log n) levels, we would expect a search time of O(log n) We will now formalize these two intuitive observations 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) = = ( ) 1 2 log n 2 1 (2 log n ) 2 = 1 n 2 Thus the probability that a particular key i achieves height 2 log n is 1 n 2 10 11

Height of Skip List Height of Skip List Q: What is the probability that any 2 log n? A: We want key achieves height 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 1 i=1 n 2 = n n 2 = 1/n This probability gets small as n gets large In particular, the probability of having a skip list of size exceeding 2 log 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. 12 13 In-Class Exercise Trick Why? A trick for computing expectations of discrete positive random variables: Let X be a discrete r.v., that takes on values from 1 to n n E(X) = P (X i) i=1 n P (X i) = P (X = 1) + P (X = 2) + P (X = 3) +... i=1 + P (X = 2) + P (X = 3) + P (X = 4) +... + P (X = 3) + P (X = 4) + P (X = 5) +... +... = 1 P (X = 1) + 2 P (X = 2) + 3 P (X = 3) +... = E(X) 14 15

In-Class Exercise Search Time Q: How much memory do we expect a skip list to use up? Let X i be the number of lists that element i is inserted in. Q: What is P (X i 1), P (X i 2), P (X i 3)? Q: What is P (X i k) for general k? Q: What is E(X i )? Q: Let X = n i=1 X i. What is E(X)? Its easier to analyze the search time if we imagine running the search backwards Imagine that we start at the found node v in the bottommost list and we trace the path backwards to the top leftmost senitel, L This will give us the length of the search path from L to v which is the time required to do the search 16 17 Backwards Search Backward Search SLFback(v){ while (v!= L){ if (Up(v)!=NIL) v = Up(v); else v = Left(v); }} For every node v in the skip list Up(v) exists with probability 1/2. So for purposes of analysis, SLFBack is the same as the following algorithm: FlipWalk(v){ while (v!= L){ if (COINFLIP == HEADS) v = Up(v); else v = Left(v); }} 18 19

Analysis For this algorithm, the expected number of heads is exactly the same as the expected number of tails Thus the expected run time of the algorithm is twice the expected number of upward jumps Since we already know that the number of upward jumps is O(log n) with high probability, we can conclude that the expected search time is O(log n) 20