このページは http://www.slideshare.net/ShigekazuTakei/pfds-5-2642 の内容を掲載しています。

掲載を希望されないスライド著者の方は、削除申請よりご連絡下さい。

埋込み型プレイヤーを使用せず、常に元のサイトでご覧になりたい方は、自動遷移設定をご利用下さい。

- PFDS 8.4.3 Real-Time Deques5年以上前 by 昌平 村山
- 純粋関数型データ構造5ヶ月前 by wat-aro
- 純粋関数型アルゴリズム入門約6年前 by Kimikazu Kato

- FIFO Queue（2リストキュー）

type foo Queue = foo list x foo list

fun head (x :: f, r) = x

O(1)

fun tail (x :: f, r) = (f, r)

O(1)

fun snoc ((f, r), x) = (f, x :: r)

O(1)

リスト終端への挿入にconsを用いるため、

リスト後半をreverseして保持 - FIFO Queue（2リストキュー）

invariant to maintain

ｆはｒが[ ]の時のみ[ ]

これがないとf = [ ]時にheadがr終端からの取り出

しになりheadがO(n)かかる - FIFO Queue（2リストキュー）

to maintain invariant...

fun checkf ([ ], r) = (rev r, [ ])

| checkf q = q

fun snoc ((f, r), x) = checkf (f, x :: r)

fun tail (x :: f, r) = checkf (f, r) - Banker’s method

snoc = 1 step + 1 credit

tail (w/o reverse) = 1 step

tail (w/ reverse) = (m+1) steps - m credits

tail . tail . tail . snoc 3 . snoc 2 . snoc 1 - real

amortized

snoc 1 -> [] [1] -> [1] []

2

2

checkf w/ 1 step

snoc 2 -> [1] [2]

1

2 +1

snoc 3 -> [1] [3,2]

1

2

+1

tail -> [] [3,2] -> [2,3] []

3

1

-2

tail -> [3] []

checkf w/ 2 step

1

1

tail -> [] []

1

1

9

9 - real

amortized

snoc 1 -> [] [1] -> [1] []

2

2

snoc 2 -> [1] [2]

1

2 +1

snoc 3 -> [1] [3,2]

1

2

+1

tail -> [] [3,2] -> [2,3] []

3

1

-2

tail -> [3] []

1

1

tail -> [] []

1

積み立てておいたcredit 1

を消費

9

9 - Physicist’s method

Φ = length of the rear list

snoc = 1 step + 1 potential (for 1 elem)

tail (w/o reverse) = 1 step

tail (w/ reverse) = (m+1) steps - m potentials - real

amortized

snoc 1 -> [] [1] -> [1] []

2

2

snoc 2 -> [1] [2]

1

2 +1

snoc 3 -> [1] [3,2]

1

2

+1

tail -> [] [3,2] -> [2,3] []

3

1

-2

tail -> [3] []

1

1

tail -> [] []

1

1

9

9 - §6.4.2 with Lazy Evaluation

O(1)

in amortized time - §6.4.2 with Lazy Evaluation

Queue with O(1) amortized time.

Persistent

Lazy Evaluation

Strict Working Copy - §6.4.2 with Lazy Evaluation

with the Physicist’s Method.

change in

shared

potential

strict

cost

(non-amortized)

cost

amortized

unshared

cost

cost - §6.4.2 with Lazy Evaluation

with the Physicist’s Method.

change in

potential

strict

paid

(non-amortized)

shared cost

cost

paid shared cost

= actually executed cost - §6.4.2 with Lazy Evaluation

with the Physicist’s Method.

max potential

=

max shared cost ???

change in

potential

strict

paid

(non-amortized)

shared cost

cost - Implementation of PhysicistsQueue

type a Queue = a list x int x a list susp x int x a list - type a Queue = a list x int x a list susp x int x a list

suspended front list + ordinal rear list

lengths for front and rear lists

to calculate the diff of front and rear lengths

to guarantee front >= rear

working copy of front list

front is suspended, so necessary to keep the access to the prefix for head

queries. - Potential

length of working copy

diff of front and rear lengths

if those values are = 0, then rotation happens

definition of potential

Ψ(q) = min (2|w|, |f| - |r|) - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

snoc

unshared = 1 - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

snoc

shared cost of rotation = 2m + 1

force suspended front ++ rear - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

snoc

change of potential = + 2m

create debt

= accumulated debt - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

snoc

complete cost = 2

unshared = 1

shared = 2m + 1

change in potential = 2m - if potential > 0

[ 5, 8, 2, 4] [ 5, 8, 2, 4] [ 1, 3]

potential = 2

snoc

unshared = 1

change in potential = -1 - Therefore...

snoc is O(1) amortized time

the cost is 2 or 4 - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

tail

[ 8] [ 8] [ 2, 4] - if potential = 0

[ 5, 8] [ 5, 8] [ 2, 4]

tail

unshared cost = 2

shared cost = 2m + 1

change in potential = 2m - if potential > 0

[ 5, 8, 2, 4] [ 5, 8, 2, 4] [ 1, 3]

potential = 2

tail

[ 8, 2, 4] [ 8, 2, 4] [ 1, 3] - if potential > 0

[ 5, 8, 2, 4] [ 5, 8, 2, 4] [ 1, 3]

potential = 2

tail

unshared = 1 (from working copy)

paid shared = 1 (from front)

change in potential = -1 for w and -1 for (f-r) - Therefore...

tail is in O(1) amortized time

the cost is 4 or 3 - Theorem 6.2

The amortized costs of snoc and tail are at most 2

and 4, respectively

This leads to that every operation is O(1) order. - snoc

without rotation

snoc ((w, lenf, f, lenr, r), x)

= check (w, lenf, f , lenr+1, x :: r)

cost = 1 & decrease in potential = 1

operation for |f| - |r|

amortized cost = 1 - (-1)

= 2 - tail

without rotation

tail (x :: w, lenf, f, lenr, r)

= check (w, lenf-1, $tl (force f), lenr, r)

cost = 2 & decrease in potential = 2

operation for |w| & operation for |f| - |r|

amortized cost = 2 - (-2)

= 4 - With Rotation

|f| = m and |r| = m + 1

shared cost of rotation = 2m + 1

potential of resulting queue = 2|w|

= 2m

amortized cost of snoc

1 + (2m + 1) - 2m = 2

amortized cost of tail

2 + (2m + 1) - 2m = 3