pfds 5 2+6_4_2

33
PFDS §5.2+6.4.2 Queues @shtaag 2012519日土曜日

Upload: shigekazu-takei

Post on 12-Nov-2014

645 views

Category:

Lifestyle


2 download

DESCRIPTION

purely functional data structures

TRANSCRIPT

Page 1: Pfds 5 2+6_4_2

PFDS §5.2+6.4.2 Queues@shtaag

2012年5月19日土曜日

Page 2: Pfds 5 2+6_4_2

First...

http://www.kmonos.net/pub/Presen/PFDS.pdf

2012年5月19日土曜日

Page 3: Pfds 5 2+6_4_2

FIFO Queue(2リストキュー)

type foo Queue = foo list x foo list

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

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

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

O(1)

O(1)

O(1)

リスト終端への挿入にconsを用いるため、リスト後半をreverseして保持

2012年5月19日土曜日

Page 4: Pfds 5 2+6_4_2

FIFO Queue(2リストキュー)

invariant to maintain

fはrが[ ]の時のみ[ ]

これがないとf = [ ]時にheadがr終端からの取り出しになりheadがO(n)かかる

2012年5月19日土曜日

Page 5: Pfds 5 2+6_4_2

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)

2012年5月19日土曜日

Page 6: Pfds 5 2+6_4_2

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

Banker’s method

2012年5月19日土曜日

Page 7: Pfds 5 2+6_4_2

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

snoc 2 -> [1] [2]

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

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

tail -> [3] []

tail -> [] []

2

1

1

3

1

1

9

2

2

2

1

1

1

9

real amortized

+1

+1

-2

checkf w/ 1 step

checkf w/ 2 step

2012年5月19日土曜日

Page 8: Pfds 5 2+6_4_2

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

snoc 2 -> [1] [2]

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

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

tail -> [3] []

tail -> [] []

2

1

1

3

1

1

9

2

2

2

1

1

1

9

real amortized

積み立てておいたcreditを消費

+1

+1

-2

2012年5月19日土曜日

Page 9: Pfds 5 2+6_4_2

Φ = 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

Physicist’s method

2012年5月19日土曜日

Page 10: Pfds 5 2+6_4_2

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

snoc 2 -> [1] [2]

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

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

tail -> [3] []

tail -> [] []

2

1

1

3

1

1

9

2

2

2

1

1

1

9

real amortized

+1

+1

-2

2012年5月19日土曜日

Page 11: Pfds 5 2+6_4_2

§6.4.2 with Lazy Evaluation

O(1) in amortized time

2012年5月19日土曜日

Page 12: Pfds 5 2+6_4_2

Queue with O(1) amortized time.

Persistent

Lazy Evaluation

Strict Working Copy

§6.4.2 with Lazy Evaluation

2012年5月19日土曜日

Page 13: Pfds 5 2+6_4_2

with the Physicist’s Method.

§6.4.2 with Lazy Evaluation

strict(non-amortized)

costunshared

cost

sharedcost

change inpotential

amortizedcost

2012年5月19日土曜日

Page 14: Pfds 5 2+6_4_2

with the Physicist’s Method.

§6.4.2 with Lazy Evaluation

strict(non-amortized)

cost

paidshared cost

change inpotential

paid shared cost = actually executed cost

2012年5月19日土曜日

Page 15: Pfds 5 2+6_4_2

with the Physicist’s Method.

§6.4.2 with Lazy Evaluation

strict(non-amortized)

cost

paidshared cost

change inpotential

max potential=

max shared cost ???

2012年5月19日土曜日

Page 16: Pfds 5 2+6_4_2

Implementation of PhysicistsQueue

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

2012年5月19日土曜日

Page 17: Pfds 5 2+6_4_2

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 lengthsto guarantee front >= rear

working copy of front list

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

2012年5月19日土曜日

Page 18: Pfds 5 2+6_4_2

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|)

2012年5月19日土曜日

Page 19: Pfds 5 2+6_4_2

if potential = 0

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

snoc

unshared = 1

2012年5月19日土曜日

Page 20: Pfds 5 2+6_4_2

if potential = 0

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

snoc

shared cost of rotation = 2m + 1

force suspended front ++ rear

2012年5月19日土曜日

Page 21: Pfds 5 2+6_4_2

if potential = 0

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

snoc

change of potential = + 2m

= accumulated debt

create debt

2012年5月19日土曜日

Page 22: Pfds 5 2+6_4_2

if potential = 0

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

snoc

complete cost = 2

unshared = 1

shared = 2m + 1

change in potential = 2m

2012年5月19日土曜日

Page 23: Pfds 5 2+6_4_2

if potential > 0

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

potential = 2

snoc

unshared = 1

change in potential = -1

2012年5月19日土曜日

Page 24: Pfds 5 2+6_4_2

Therefore...

snoc is O(1) amortized time

the cost is 2 or 4

2012年5月19日土曜日

Page 25: Pfds 5 2+6_4_2

if potential = 0

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

tail

[ 8] [ 8] [ 2, 4]

2012年5月19日土曜日

Page 26: Pfds 5 2+6_4_2

if potential = 0

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

tail

unshared cost = 2

shared cost = 2m + 1

change in potential = 2m

2012年5月19日土曜日

Page 27: Pfds 5 2+6_4_2

if potential > 0

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

potential = 2

tail

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

2012年5月19日土曜日

Page 28: Pfds 5 2+6_4_2

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)

2012年5月19日土曜日

Page 29: Pfds 5 2+6_4_2

Therefore...

tail is in O(1) amortized time

the cost is 4 or 3

2012年5月19日土曜日

Page 30: Pfds 5 2+6_4_2

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.

2012年5月19日土曜日

Page 31: Pfds 5 2+6_4_2

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

2012年5月19日土曜日

Page 32: Pfds 5 2+6_4_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

2012年5月19日土曜日

Page 33: Pfds 5 2+6_4_2

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

2012年5月19日土曜日