Лекция 1. Амортизационный анализ (amortized analysis)

Download Лекция 1. Амортизационный анализ (amortized analysis)

Post on 18-Jan-2017

569 views

Category:

Software

2 download

Embed Size (px)

TRANSCRIPT

  • 1 (amortized analysis)

    E-mail: mkurnosov@gmail.comWWW: www.mkurnosov.net

    () , 2015

  • 22

    1. ,

    2. ,

    ,

    ( ,

    )

    3.

    :

    O, ,

    n, m

    T(n, m) = 2n2 + 4log2m

    T(n, m) = O(2n2 + 4log2m) = O(max{n2, log2m})

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    3

    ( )

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    4

    ( )

    1

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    5

    ( )

    1

    2 ( worst case)

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    6

    ( )

    1

    2 ( worst case)

    4

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    7

    ( )

    1

    2 ( worst case)

    4

    i = 1, j = 2 to n, n 1 .i = 2, j = 3 to n, n 2 .i = 3, j = 4 to n, n 3 .i = n 1, j = n to n, 1 .

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    8

    ( )

    1

    2 ( worst case)

    4

    T(n) = (n 1) + 4 (n 1) + 2((n 1) + (n 2) + + 1) = ?

  • function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    9

    ( )

    1

    2 ( worst case)

    4

    T(n) = (n 1) + 4 (n 1) + 2((n 1) + (n 2) + + 1) = ?

  • T(n) = 5n 5 + 2((n2 n) / 2) = n2 + 4n 5

    function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    10

    ( )

    1

    2 ( worst case)

    4

  • T(n) = n2 + 4n 5 = O(n2)

    function SelectionSort(v[1..n])

    for i = 1 to n - 1 do

    min = i

    for j = i + 1 to n do

    if v[j] < v[min] then

    min = j

    end if

    end for

    if min != i then

    temp = v[i]

    v[i] = v[min]

    v[min] = temp

    end if

    end for

    end function

    11

    ( )

    1

    2 ( worst case)

    4

  • (binary counter)

    1212

    L ( 2L )

    Increment,

    0

    5 :

    4 3 2 1 0

    16 8 4 2 1

    0 0 0 0 0

  • (binary counter)

    1313

    4 3 2 1 0

    16 8 4 2 1

    0 0 0 0 1

    4 3 2 1 0

    16 8 4 2 1

    0 0 0 1 0

    4 3 2 1 0

    16 8 4 2 1

    0 0 0 1 1

    Increment: 1 2

    Increment: 0 1

    Increment: 2 3

  • (binary counter)

    1414

    4 3 2 1 0

    16 8 4 2 1

    0 0 1 0 0

    4 3 2 1 0

    16 8 4 2 1

    0 0 1 0 1

    4 3 2 1 0

    16 8 4 2 1

    0 0 1 1 0

    Increment: 4 5

    Increment: 3 4

    Increment: 5 6

  • (binary counter)

    15

    function Increment(A)

    i = 0

    while i < L and A[i] = 1 do

    A[i] = 0

    i = i + 1

    end while

    if i < L then

    A[i] = 1

    end if

    end function

    15

    3 2 1 0

    8 4 2 1

    1 0 1 1

    (

    )

    = 1110

  • (binary counter)

    16

    function Increment(A)

    i = 0

    while i < L and A[i] = 1 do

    A[i] = 0

    i = i + 1

    end while

    if i < L then

    A[i] = 1

    end if

    end function

    16

    3 2 1 0

    8 4 2 1

    1 0 1 1 = 1110

    Increment

    A[1..L]

  • (binary counter)

    17

    function Increment(A)

    i = 0

    while i < L and A[i] = 1 do

    A[i] = 0

    i = i + 1

    end while

    if i < L then

    A[i] = 1

    end if

    end function

    17

    Increment

    A[1..L]

    Increment?

  • (binary counter)

    18

    A[1..L] ,

    Increment O(L)

    !

    Increment

    18

    ,

    ?

  • 19

    (amortized analysis) , n

    n ,

    19

  • 20

    ,

    ,

    n

    20

  • 21

    (aggregate analysis)

    (Robert Tarjan) 1985 :

    Tarjan R. Amortized Computational Complexity // SIAM. J. on Algebraic and Discrete Methods, 6(2), 1985.

    P. 306318.

    21

  • 22

    (aggregate analysis)

    (accounting method)

    (potential method)

    ,

    [CLRS ed., . 487]

    22

  • (ggregate analysis)

    23

    (aggregate analysis) , T(n) n

    (amortized cost, )

    T(n) / n

    23

  • (Last In First Out)

    2424

    Push(S, x)

    x

    TPush = O(1)

    Pop(S)

    TPop = O(1)

    MultiPop(S, k)

    TMultiPop = O(min(|S|, k))

    k

  • function MultiPop(S, k)

    while StackEmpty(S) = False and k > 0 do

    StackPop(S)

    k = k 1

    end while

    end function

    (Last In First Out)

    25

    TMultiPop = O(min(|S|, k))15

    4

    22

    78

    34

    12

    Top

    34

    12

    Top

    MultiPop(S, 4) MultiPop(S, 7)

  • v1.0

    26

    T(n) n (Push, Pop, MultiPop)

    1. Pop O(1)

    2. Push O(1)

    3. MultiPop O(n), n n

    n MultiPop

    , T(n) n O(n2),

    O(n2) / n = O(n)

    26

  • v2.0

    27

    T(n) n

    1. Pop ( MultiPop) Push. , Push n ( MultiPop Pop)

    2. n Push, Pop, MultiPop O(n)

    n O(n), ( )

    O(n) / n = O(1)27

  • :

    28

    , (1), MultiPush(S, k), k ?

    28

    MultiPush(S, k)

    k

    TMultiPush = O(k)

  • :

    29

    O(k)

    n

    n

    MultiPush, O(nk)

    ( )

    T(n) / n = O(nk) / n = O(k) 29

    MultiPush(S, k)

    k

  • (binary counter)

    3030

    L ( 2L )

    Increment

    5

    o 1, : 00001

    o 3, : 00011

    o 4, : 00100

    o 5, : 00101

    o 10, : 01010

    4 3 2 1 0

    16 8 4 2 1

    0 1 1 0 0

  • function Increment(A)

    i = 0

    while i < L and A[i] = 1 do

    A[i] = 0

    i = i + 1

    end while

    if i < L then

    A[i] = 1

    end if

    end function

    (binary counter)

    3131

    Increment

    A

    3 2 1 0

    8 4 2 1

    1 0 1 1

  • (binary counter)

    (L = 8 )

    . A[7] A[4] A[3] A[2] A[1] A[0] (# )

    1 0

    0 0 0 0 1 1 1

    2 0 0 0 0 1 0 2 3

    3 0 0 0 0 1 1 1 4

    4 0 0 0 1 0 0 3 7

    5 0 0 0 1 0 1 1 8

    6 0 0 0 1 1 0 2 10

    7 0 0 0 1 1 1 1 11

    8 0 0 1 0 0 0 4 15

    9 0 0 1 0 0 1 1 16

    10 0 0 1 0 1 0 2 18

    11 0 0 1 0 1 1 1 19

    12 0 0 1 1 0 0 3 22

    13 0 0 1 1 0 1 1 23

    14 0 0 1 1 1 0 2 25

    15 0 0 1 1 1 1 1 26

    16 0 1 0 0 0 0 5 31

  • (binary counter)

    (L = 8 )

    . A[7] A[4] A[3] A[2] A[1] A[0] (# )

    1 0

    0 0 0 0 1 1 1

    2 0 0 0 0 1 0 2 3

    3 0 0 0 0 1 1 1 4

    4 0 0 0 1 0 0 3 7

    5 0 0 0 1 0 1 1 8

    6 0 0 0 1 1 0 2 10

    7 0 0 0 1 1 1 1 11

    8 0 0 1 0 0 0 4 15

    9 0 0 1 0 0 1 1 16

    10 0 0 1 0 1 0 2 18

    11 0 0 1 0 1 1 1 19

    12 0 0 1 1 0 0 3 22

    13 0 0 1 1 0 1 1 23

    14 0 0 1 1 1 0 2 25

    15 0 0 1 1 1 1 1 26

    16 0 1 0 0 0 0 5 31

    Increment ( )?

  • (binary counter)

    (L = 8 )

    . A[7] A[4] A[3] A[2] A[1] A[0] (# )

    1 0

    0 0 0 0 1 1 1

    2 0 0 0 0 1 0 2 3

    3 0 0 0 0 1 1 1 4

    4 0 0 0 1 0 0 3 7

    5 0 0 0 1 0 1 1 8

    6 0 0 0 1 1 0 2 10

    7 0 0 0 1 1 1 1 11

    8 0 0 1 0 0 0 4 15

    9 0 0 1 0 0 1 1 16

    10 0 0 1 0 1 0 2 18

    11 0 0 1 0 1 1 1 19

    12 0 0 1 1 0 0 3 22

    13 0 0 1 1 0 1 1 23

    14 0 0 1 1 1 0 2 25

    15 0 0 1 1 1

Recommended

View more >