October 26, 2005Copyright © 2001-5 by Erik D. Demaine and Charles E. LeisersonL11.1 Introduction to Algorithms 6.046J/18.401J LECTURE12 Skip Lists Data.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
CMSC420: Skip Lists Kinga Dobolyi Based off notes by Dave Mount.
© 2004 Goodrich, Tamassia Skip Lists1 S0S0 S1S1 S2S2 S3S
Skip Lists. Outline and Reading What is a skip list (§9.4) – Operations (§9.4.1) – Search – Insertion – Deletion Implementation Analysis (§9.4.2) – Space.
SKIP LISTS Amihood Amir Incorporationg the slides of Goodrich and Tamassia (2004)
Introduction to Algorithms Quicksort
CSC 172 DATA STRUCTURES. SKIP LISTS Read Weiss
Analysis of Algorithms
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
1 Lecture 8: Data structures for databases II Jose M. Peña
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
© 2004 Goodrich, Tamassia Skip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Tirgul 5 AVL trees.
Skip Lists Intuition and Definition –Efficient searching Insertion Other Operations Randomized Analysis Some figures and ideas from Erik Demaine and Shafi.
4a-Searching-More1 More Searching Dan Barrish-Flood.
BST Data Structure A BST node contains: A BST contains
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3 
1 Database Tuning Rasmus Pagh and S. Srinivasa Rao IT University of Copenhagen Spring 2007 February 8, 2007 Tree Indexes Lecture based on [RG, Chapter.
CS4432: Database Systems II
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CSE 373 Data Structures Lecture 15
Randomized Algorithms - Treaps
Introduction To Algorithms CS 445 Discussion Session 2 Instructor: Dr Alon Efrat TA : Pooja Vaswani 02/14/2005.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
Skip Lists Mrutyunjay. Introduction ▪ Linked Lists Benefits & Drawbacks: – Benefits: – Easy Insert and Deletes, implementations. – Drawbacks: – Hard to.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
Skip Lists 二○一七年四月二十五日
1 Multi-Level Indexing and B-Trees. 2 Statement of the Problem When indexes grow too large they have to be stored on secondary storage. However, there.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
2/19/2016 3:18 PMSkip Lists1  S0S0 S1S1 S2S2 S3S3    2315.
Skip Lists – Why? BSTs –Worse case insertion, search O(n) –Best case insertion, search O(log n) –Where your run fits in O(n) – O(log n) depends on the.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Skip Lists S3   S2   S1   S0  
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
15-121: Introduction to Data Structures
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Skip Lists.
Randomized Algorithms CS648
Skip Lists S3 + - S2 + - S1 + - S0 + -
Enumerating Distances Using Spanners of Bounded Degree
Skip Lists S3 + - S2 + - S1 + - S0 + -
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Introduction to Algorithms
Presentation transcript:

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.1 Introduction to Algorithms 6.046J/18.401J LECTURE12 Skip Lists Data structure Randomized insertion With-high-probability bound Analysis Coin flipping Prof. Erik D. Demaine

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.2 Skip lists Simple randomized dynamicsearch structure – Invented by William Pugh in 1989 – Easy to implement Maintains a dynamic set of n elements in O(lgn) time per operation in expectation and with high probability – Strong guarantee on tail of distribution of T(n) – O(lgn) “almost always”

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.3 One linked list Start from simplest data structure: (sorted) linked list Searches take Θ(n) time in worst case How can we speed up searches?

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.4 Two linked lists Suppose we had twosorted linked lists (on subsets of the elements) Each element can appear in one or both lists How can we speed up searches?

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.5 Two linked lists as a subway IDEA: Express and local subway lines (àla New York City 7th Avenue Line) Express line connects a few of the stations Local line connects all stations Links between lines at common stations

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.6 Searching in two linked lists SEARCH (x) : Walk right in top linked list (L1) until going right would go too far Walk down to bottom linked list (L2) Walk right in L2 until element found (or not)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.7 Searching in two linked lists EXAMPLE: SEARCH (59) Too far: 59 < 72

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.8 Design of two linked lists QUESTION: Which nodes should be in L1? In a subway, the “popular stations” Here we care about worst-case performance Best approach: Evenly space the nodes in L1 But how many nodesshould be in L1?

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.9 Analysis of two linked lists ANALYSIS: Search cost is roughly Minimized (up to constant factors) when terms are equal

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.10 Analysis of two linked lists ANALYSIS: Search cost is roughly

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.11 More linked lists What if we had more sorted linked lists? 2 sorted lists ⇒ 3 sorted lists ⇒ k sorted lists ⇒ lgn sorted lists ⇒

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.12 lgn linked lists lgn sorted linked lists are like a binary tree (in fact, level-linked B+-tree; see Problem Set 5)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.13 Searching in lg n linked lists EXAMPLE: SEARCH (72)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.14 Skip lists Idealskip list is this lg n linked list structure Skip list data structure maintains roughly this structure subject to updates (insert/delete)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.15 INSERT (x) To insert an element x into a skip list: SEARCH (x) to see where x fits in bottom list Always insert into bottom list INVARIANT: Bottom list contains all elements Insert into some of the lists above… QUESTION: To which other lists should we add x?

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.16 INSERT (x) QUESTION: To which other lists should we add x? IDEA: Flip a (fair) coin; if HEADS, promote x to next level up and flip again Probability of promotion to next level = ½ On average: –1/2 of the elements promoted 0 levels –1/4 of the elements promoted 1 level –1/8 of the elements promoted 2 levels –etc. Approx. balanced ?

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.17 EXERCISE: Try building a skip list from scratch by repeated insertion using a real coin Small change: Add special −∞ value to every list ⇒ can search with the same algorithm Example of skip list

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.18 Skip lists A skip list is the result of insertions (and deletions) from an initially empty structure (containing just −∞) INSERT(x) uses random coin flips to decide promotion level DELETE(x) removes x from all lists containing it

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.18 Skip lists A skip list is the result of insertions (and deletions) from an initially empty structure (containing just −∞) INSERT(x) uses random coin flips to decide promotion level DELETE(x) removes x from all lists containing it How good are skip lists? (speed/balance) INTUITIVELY: Pretty good on average CLAIM: Really, really good, almost always

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.20 With-high-probability theorem THEOREM: With high probability, every search in an n-element skip list costs O(lg n)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.22 With-high-probability theorem THEOREM: With high probability, every search in a skip list costs O (lg n) INFORMALLY:Event E occurs with high probability(w.h.p.) if, for any α≥1, there is an appropriate choice of constants for which E occurs with probability at least 1 −O(1/n α ) – In fact, constant in O(lg n) depends on α FORMALLY: Parameterized event E α occurs with high probabilityif, for any α≥1, there is an appropriate choice of constants for which E α occurs with probability at least 1 −c α /n α

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.21 With-high-probability theorem THEOREM: With high probability, every search in a skip list costs O (lg n) INFORMALLY:Event E occurs with high probability(w.h.p.) if, for any α≥1, there is an appropriate choice of constants for which E occurs with probability at least 1 −O(1/n α ) IDEA: Can make error probability O (1/n α ) very small by setting αlarge, e.g., 100 Almost certainly, bound remains true for entire execution of polynomial-time algorithm

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.23 Boole’s inequality / union bound Recall: BOOLE’SINEQUALITY/ UNIONBOUND: For any random events E 1, E 2, …, E k, P r {E 1 ∪ E 2 ∪ … ∪ E k } ≤P r {E 1 } + P r {E 2 } + …+ P r {E k } Application to with-high-probability events: If k= n O(1), and each E i occurs with high probability, then so does E 1 ∩E 2 ∩…∩E k

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.24 Analysis Warmup LEMMA: With high probability, n -element skip list has O(lg n) levels PROOF: Error probability for having at most c lg n levels = Pr {morethan c lg n levels} ≤n·Pr {element x promoted at least c lg n times} (by Boole’s Inequality)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.25 Analysis Warmup LEMMA: With high probability, n -element skip list has O(lg n) levels PROOF: Error probability for having at most c lg n levels This probability is polynomially small, i.e., at most n α for α= c−1. We can make αarbitrarily large by choosing the constant c in the O(lg n) bound accordingly.

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.26 Proof of theorem THEOREM: With high probability, every search in an n-elementskip list costs O(lg n) COOL IDEA: Analyze search backwards—leaf to root Search starts [ends] at leaf (node in bottom level) At each node visited: – If node wasn’t promoted higher (got TAILS here), then we go [came from] left – If node was promoted higher (got HEADS here), then we go [came from] up Search stops [starts] at the root (or−∞)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.27 Proof of theorem THEOREM: With high probability, every search in an n-elementskip list costs O(lg n) COOL IDEA: Analyze search backwards—leaf to root PROOF: Search makes “up”and “left”moves until itreachesthe root (or −∞) Number of “up”moves <number of levels ≤ c lg n w.h.p. (Lemma) ⇒ w.h.p., number of moves is at most the number of times we need to flip a coin to get c lg n HEADs

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.28 Coin flipping analysis CLAIM: Number of coin flips until c lg n HEADs = Θ(lg n) with high probability PROOF: Obviously Ω(lg n) : at least c lg n Prove O(lg n) “by example”: Say we make 10 c lg n flips When are there at least c lg n HEADs? (Later generalize to arbitrary values of 10)

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.29 Coin flipping analysis CLAIM: Number of coin flips until c lg n HEADs = Θ(lg n) with high probability PROOF: Pr{ exactly c lg n HEADs } Pr{ atmost c lg n HEADs } Orders HEADs TAILs overestimate on orders TAILs

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.30 Coin flipping analysis (cont’d) Recall bounds on Pr{ atmost c lg n HEADs }

October 26, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL11.31 Coin flipping analysis (cont’d) Pr{ atmost c lg n HEADs} ≤1/n α for α= [9−lg(10e)]c KEY PROPERTY: α→∞ as 10 →∞, for any c So set 10, i.e., constant in O(lg n) bound, large enough to meet desired α This completes the proof of the coin-flipping claim and the proof of the theorem.