Upload
shigekazu-takei
View
1.067
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
PFDS §5.2 Queues@shtaag
2012年3月4日日曜日
First...
http://www.kmonos.net/pub/Presen/PFDS.pdf
2012年3月4日日曜日
FIFO Queue
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年3月4日日曜日
FIFO Queue
invariant to maintain
fはrが[ ]の時のみ[ ]
これがないとf = [ ]時にheadがr終端からの取り出しになりheadがO(n)かかる
2012年3月4日日曜日
FIFO Queue
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年3月4日日曜日
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年3月4日日曜日
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年3月4日日曜日
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年3月4日日曜日
Φ = 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年3月4日日曜日
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年3月4日日曜日