SlideShare una empresa de Scribd logo
1 de 10
Descargar para leer sin conexión
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             O(1)
               fun tail (x :: f, r) = (f, r)        O(1)
               fun snoc ((f, r), x) = (f, x :: r)   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日日曜日
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


2012年3月4日日曜日
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         9
2012年3月4日日曜日
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         9
2012年3月4日日曜日
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




2012年3月4日日曜日
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
2012年3月4日日曜日

Más contenido relacionado

Más de Shigekazu Takei

Más de Shigekazu Takei (7)

Scala@SmartNews_20150221
Scala@SmartNews_20150221Scala@SmartNews_20150221
Scala@SmartNews_20150221
 
Startprintf_2013May18
Startprintf_2013May18Startprintf_2013May18
Startprintf_2013May18
 
Pfds 9 2_2
Pfds 9 2_2Pfds 9 2_2
Pfds 9 2_2
 
Pfds 5 2+6_4_2
Pfds 5 2+6_4_2Pfds 5 2+6_4_2
Pfds 5 2+6_4_2
 
Ids ips
Ids ipsIds ips
Ids ips
 
Stanford ml neuralnetwork
Stanford ml neuralnetworkStanford ml neuralnetwork
Stanford ml neuralnetwork
 
Pfds ex3 9
Pfds ex3 9Pfds ex3 9
Pfds ex3 9
 

purely functional data structures chapter5_2

  • 1. PFDS §5.2 Queues @shtaag 2012年3月4日日曜日
  • 2. First... http://www.kmonos.net/pub/Presen/PFDS.pdf 2012年3月4日日曜日
  • 3. FIFO Queue 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年3月4日日曜日
  • 4. FIFO Queue invariant to maintain fはrが[ ]の時のみ[ ] これがないとf = [ ]時にheadがr終端からの取り出 しになりheadがO(n)かかる 2012年3月4日日曜日
  • 5. 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日日曜日
  • 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 1 2012年3月4日日曜日
  • 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 9 2012年3月4日日曜日
  • 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 9 2012年3月4日日曜日
  • 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 2012年3月4日日曜日
  • 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 9 2012年3月4日日曜日