Pfds 5 2+6_4_2

800 views

Published on

purely functional data structures

Published in: Lifestyle, Economy & Finance
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
800
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Pfds 5 2+6_4_2

  1. 1. PFDS §5.2+6.4.2 Queues @shtaag2012年5月19日土曜日
  2. 2. First... http://www.kmonos.net/pub/Presen/PFDS.pdf2012年5月19日土曜日
  3. 3. 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して保持2012年5月19日土曜日
  4. 4. FIFO Queue(2リストキュー) invariant to maintain fはrが[ ]の時のみ[ ] これがないとf = [ ]時にheadがr終端からの取り出 しになりheadがO(n)かかる2012年5月19日土曜日
  5. 5. 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日土曜日
  6. 6. 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 12012年5月19日土曜日
  7. 7. 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 checkf w/ 2 step tail -> [3] [] 1 1 tail -> [] [] 1 1 9 92012年5月19日土曜日
  8. 8. 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 積み立てておいたcreditを消費 tail -> [] [] 1 1 9 92012年5月19日土曜日
  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 potentials2012年5月19日土曜日
  10. 10. 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 92012年5月19日土曜日
  11. 11. §6.4.2 with Lazy Evaluation O(1) in amortized time2012年5月19日土曜日
  12. 12. §6.4.2 with Lazy Evaluation Queue with O(1) amortized time. Persistent Lazy Evaluation Strict Working Copy2012年5月19日土曜日
  13. 13. §6.4.2 with Lazy Evaluation with the Physicist’s Method. change in shared potential strict cost (non-amortized) cost amortized unshared cost cost2012年5月19日土曜日
  14. 14. §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 cost2012年5月19日土曜日
  15. 15. §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 cost2012年5月19日土曜日
  16. 16. Implementation of PhysicistsQueue type a Queue = a list x int x a list susp x int x a list2012年5月19日土曜日
  17. 17. 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.2012年5月19日土曜日
  18. 18. 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日土曜日
  19. 19. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] snoc unshared = 12012年5月19日土曜日
  20. 20. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] snoc shared cost of rotation = 2m + 1 force suspended front ++ rear2012年5月19日土曜日
  21. 21. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] snoc change of potential = + 2m create debt = accumulated debt2012年5月19日土曜日
  22. 22. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] snoc complete cost = 2 unshared = 1 shared = 2m + 1 change in potential = 2m2012年5月19日土曜日
  23. 23. if potential > 0 [ 5, 8, 2, 4] [ 5, 8, 2, 4] [ 1, 3] potential = 2 snoc unshared = 1 change in potential = -12012年5月19日土曜日
  24. 24. Therefore... snoc is O(1) amortized time the cost is 2 or 42012年5月19日土曜日
  25. 25. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] tail [ 8] [ 8] [ 2, 4]2012年5月19日土曜日
  26. 26. if potential = 0 [ 5, 8] [ 5, 8] [ 2, 4] tail unshared cost = 2 shared cost = 2m + 1 change in potential = 2m2012年5月19日土曜日
  27. 27. 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日土曜日
  28. 28. 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日土曜日
  29. 29. Therefore... tail is in O(1) amortized time the cost is 4 or 32012年5月19日土曜日
  30. 30. 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日土曜日
  31. 31. 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) =22012年5月19日土曜日
  32. 32. 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) =42012年5月19日土曜日
  33. 33. 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 = 32012年5月19日土曜日

×