Cormen Algo-lec12

download Cormen Algo-lec12

of 31

Transcript of Cormen Algo-lec12

  • 8/14/2019 Cormen Algo-lec12

    1/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.1

    Introduction to Algorithms

    6.046J/18.401J

    LECTURE 12Skip Lists

    Data structure

    Randomized insertion With-high-probability bound

    Analysis

    Coin flipping

    Prof. Erik D. Demaine

  • 8/14/2019 Cormen Algo-lec12

    2/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.2

    Skip lists

    Simple randomized dynamic search structure

    Invented by William Pugh in 1989 Easy to implement

    Maintains a dynamic set ofn elements inO(lg n) time per operation in expectation and

    with high probability

    Strong guarantee on tail of distribution ofT(n)

    O(lg n) almost always

  • 8/14/2019 Cormen Algo-lec12

    3/31

  • 8/14/2019 Cormen Algo-lec12

    4/31October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.4

    Two linked lists

    Suppose we had two sorted linked lists

    (on subsets of the elements)

    Each element can appear in one or both lists

    How can we speed up searches?

    1414 2323 3434 4242 5050 5959 6666 7272 7979

  • 8/14/2019 Cormen Algo-lec12

    5/31October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 4242 7272

  • 8/14/2019 Cormen Algo-lec12

    6/31October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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 inL2 until element found (or not)

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 4242 7272

  • 8/14/2019 Cormen Algo-lec12

    7/31October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.7

    Searching in two linked lists

    EXAMPLE: SEARCH(59)

    Too far:59 < 72

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 4242 72721414 3434 4242

    4242 5050 5959

    7272

  • 8/14/2019 Cormen Algo-lec12

    8/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.8

    Design of two linked lists

    QUESTION: Which nodes should be inL1?

    In a subway, the popular stations

    Here we care about worst-case performance

    Best approach: Evenly space the nodes inL1

    But how many nodes should be inL1?

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 4242 72721414 3434 4242

    4242 5050 5959

  • 8/14/2019 Cormen Algo-lec12

    9/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.9

    Analysis of two linked lists

    ANALYSIS:

    Search cost is roughly

    Minimized (up to

    constant factors) when terms are equal

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 4242 72721414 3434 4242

    4242 5050 5959

    1

    2

    1 L

    LL +

    nLnLL === 122

    1

  • 8/14/2019 Cormen Algo-lec12

    10/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.10

    Analysis of two linked lists

    ANALYSIS:

    ,

    Search cost is roughly

    nn

    nnL

    L

    L 21

    2

    1 =+=+

    nL =1

    nL =2

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 4242 6666

    n n n

    n

  • 8/14/2019 Cormen Algo-lec12

    11/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.11

    More linked lists

    What if we had more sorted linked lists?

    2 sorted lists

    3 sorted lists

    ksorted lists

    lg n sorted lists

    n

    2 33 nk

    nk

    nnnn

    lg2lglg

    =

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 4242 6666

    n n n

    n

  • 8/14/2019 Cormen Algo-lec12

    12/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.12

    lg n linked lists

    lg n sorted linked lists are like a binary tree

    (in fact, level-linked B+-tree; see Problem Set 5)

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 66665050 7979

    1414 5050 7979

    1414 7979

  • 8/14/2019 Cormen Algo-lec12

    13/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.13

    Searching in lg n linked lists

    EXAMPLE: SEARCH(72)

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 66665050 7979

    1414 5050 7979

    1414 79791414 7979

    1414 5050 7979

    5050 6666 7979

    6666 7272

  • 8/14/2019 Cormen Algo-lec12

    14/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.14

    Skip lists

    Ideal skip listis this lg n linked list structure

    Skip list data structure maintains roughly this

    structure subject to updates (insert/delete)

    1414 2323 3434 4242 5050 5959 6666 7272 7979

    1414 3434 66665050 7979

    1414 5050 7979

    1414 7979

  • 8/14/2019 Cormen Algo-lec12

    15/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.15

    INSERT(x)

    To insert an elementx into a skip list:

    SEARCH(x) to see wherex 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 addx?

  • 8/14/2019 Cormen Algo-lec12

    16/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.16

    INSERT(x)

    QUESTION: To which other lists should we addx?

    IDEA: Flip a (fair) coin; ifHEADS,

    promotex to next level up and flip again

    Probability of promotion to next level = 1/2

    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?

  • 8/14/2019 Cormen Algo-lec12

    17/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.17

    Example of skip list

    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 withthe same algorithm 2323 3434 4242 5050

    3434 5050

    5050

  • 8/14/2019 Cormen Algo-lec12

    18/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.18

    Skip lists

    Askip listis 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) removesx from all lists containing it

  • 8/14/2019 Cormen Algo-lec12

    19/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.19

    Skip lists

    Askip listis 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) removesx from all lists containing it

    How good are skip lists? (speed/balance)

    INTUITIVELY: Pretty good on average

    CLAIM: Really, really good, almost always

  • 8/14/2019 Cormen Algo-lec12

    20/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.20

    With-high-probability theorem

    THEOREM: With high probability, every search

    in an n-element skip list costs O(lg n)

  • 8/14/2019 Cormen Algo-lec12

    21/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.21

    With-high-probability theorem

    THEOREM: With high probability, every searchin a skip list costs O(lg n)

    INFORMALLY: EventEoccurs with highprobability (w.h.p.) if, for any 1, there is anappropriate choice of constants for which

    Eoccurs with probability at least 1 O(1/n) In fact, constant in O(lg n) depends on

    FORMALLY: Parameterized eventE occurswith high probability if, for any 1, there isan appropriate choice of constants for which

    E occurs with probability at least 1 c/n

  • 8/14/2019 Cormen Algo-lec12

    22/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.22

    With-high-probability theorem

    THEOREM: With high probability, every search

    in a skip list costs O(lg n)

    INFORMALLY: EventEoccurs with high

    probability (w.h.p.) if, for any 1, there is an

    appropriate choice of constants for whichEoccurs 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

  • 8/14/2019 Cormen Algo-lec12

    23/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.23

    Booles inequality / union bound

    Recall:

    BOOLES INEQUALITY / UNION BOUND:For any random eventsE1,E2, ,Ek,

    Pr{E1

    E2

    Ek} Pr{E1} + Pr{E2} + + Pr{Ek}

    Application to with-high-probability events:

    Ifk= nO(1), and eachEi occurs with high

    probability, then so doesE1 E2 Ek

  • 8/14/2019 Cormen Algo-lec12

    24/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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{more than c lg n levels} n Pr{elementxpromoted at least c lg n times}

    (by Booles Inequality)= n (1/2c lg n)

    = n (1/nc)

    = 1/nc 1

  • 8/14/2019 Cormen Algo-lec12

    25/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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

    1/nc 1

    This probability ispolynomially 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.

  • 8/14/2019 Cormen Algo-lec12

    26/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.26

    Proof of theorem

    THEOREM: With high probability, every searchin an n-element skip list costs O(lg n)

    COOL IDEA: Analyze search backwardsleaf to root Search starts [ends] at leaf (node in bottom level)

    At each node visited: If node wasnt 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)

  • 8/14/2019 Cormen Algo-lec12

    27/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.27

    Proof of theorem

    THEOREM: With high probability, every searchin an n-element skip list costs O(lg n)

    COOL IDEA: Analyze search backwardsleaf to rootPROOF:

    Search makes up and left movesuntil it reaches the 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

  • 8/14/2019 Cormen Algo-lec12

    28/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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 of10)

  • 8/14/2019 Cormen Algo-lec12

    29/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.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{at most c lg n HEADs}

    ncnc

    nc

    nclg9lg

    2

    1

    2

    1

    lg

    lg10

    orders HEADs TAILs

    nc

    ncnc

    lg9

    21

    lglg10

    overestimateon orders TAILs

  • 8/14/2019 Cormen Algo-lec12

    30/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.30

    Coin flipping analysis (contd)

    Recall bounds on :

    Pr{at most c lg n HEADs}nc

    ncnc

    lg9

    21

    lglg10

    x

    y xx

    x

    ye

    x

    y

    x

    y

    ncnc

    ncnce

    lg9lg

    21

    lglg10

    ( )

    ncnc

    e

    lg9lg

    210

    =ncnce lg9lg)10lg( 22 =

    nce lg]9)10[lg(2 =n/1= for [ ] ce = )10lg(9

    C i f i i i ( )

  • 8/14/2019 Cormen Algo-lec12

    31/31

    October 26, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.31

    Coin flipping analysis (contd)

    Pr{at most c lg n HEADs} 1/n for = [9lg(10e)]

    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.