Amortize analysis of Deque with 2 Stack
Transcript of Amortize analysis of Deque with 2 Stack
![Page 1: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/1.jpg)
計算量のはなし2
~DequeはStack2つでつくれる~
![Page 2: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/2.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method
![Page 3: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/3.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method
![Page 4: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/4.jpg)
有名なことわざ
DequeはStack2つでつくれる
![Page 5: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/5.jpg)
Deque
● 次の操作に対応したデータ構造
– 列の先頭に値を追加 (pushF)– 列の末尾に値を追加 (pushB)– 列の先頭から値を取り出す (popF)– 列の末尾から値を取り出す (popB)
![Page 6: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/6.jpg)
Deque: 図示
● ここに空のDequeがあるじゃろ
– 左が先頭(Front), 右が末尾 (Back)
![Page 7: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/7.jpg)
Deque: 図示
● pushF(3)
3
![Page 8: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/8.jpg)
Deque: 図示
● pushF(3), pushB(4)
3 4
![Page 9: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/9.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5)
3 45
![Page 10: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/10.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB()
34
54
![Page 11: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/11.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB()
35
![Page 12: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/12.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB(), popF()
35
![Page 13: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/13.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB(), popF()
3
![Page 14: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/14.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB(), popF()pushB(4)
3 4
![Page 15: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/15.jpg)
Deque: 図示
● pushF(3), pushB(4), pushF(5), popB(), popF()pushB(4), pushF(3)
3 433
![Page 16: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/16.jpg)
有名なことわざ(再掲)
DequeはStack2つでつくれる
![Page 17: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/17.jpg)
Deque(Stack×2): 図示
● 空のDequeの下にStackが2つあるじゃろ
– 左が先頭(Front), 右が末尾 (Back)
![Page 18: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/18.jpg)
432
Deque(Stack×2): 図示
● pushF(1), pushF(2), pushF(3), pushF(4)
4321
1
![Page 19: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/19.jpg)
432
Deque(Stack×2): 図示
● pushF(1), pushF(2), pushF(3), pushF(4)popB()
4321
1
![Page 20: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/20.jpg)
32
Deque(Stack×2): 図示
● pushF(1), pushF(2), pushF(3), pushF(4)popB()
4321
1エイヤッ
4
![Page 21: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/21.jpg)
32
Deque(Stack×2): 図示
● pushF(1), pushF(2), pushF(3), pushF(4)popB()
4321
14
![Page 22: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/22.jpg)
32
Deque(Stack×2): 図示
● pushF(1), pushF(2), pushF(3), pushF(4)popB()
321
1
![Page 23: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/23.jpg)
32
Deque(Stack×2): 大事な所
● PopしたいのにStackがからの時、もう片方のStackの半分を移してくる
4321
1エイヤッ
4
![Page 24: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/24.jpg)
フハハ
DequeはこのようにしてStack2つでつくれるのじゃ
![Page 25: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/25.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method
![Page 26: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/26.jpg)
半分うつすのやばそう
● Stack2つで実装したDequeの計算量を評価してみよう
![Page 27: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/27.jpg)
操作一覧
● pushBack ● pushFront● popBack● popFront
![Page 28: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/28.jpg)
操作一覧
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack● popFront
![Page 29: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/29.jpg)
操作一覧
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack だいたいO(1)● popFront だいたいO(1)
![Page 30: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/30.jpg)
半分移すとき時間かかる
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack だいたいO(1) ときどき O(N)● popFront だいたいO(1) ときどき O(N)
![Page 31: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/31.jpg)
半分移すとき時間かかる
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack だいたいO(1) ときどき O(N)● popFront だいたいO(1) ときどき O(N)
● でもO(N)であることは本当に「ときどき」で、まれにしか起こらない
![Page 32: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/32.jpg)
半分移すとき時間かかる
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack だいたいO(1) ときどき O(N)● popFront だいたいO(1) ときどき O(N)
● でもO(N)であることは本当に「ときどき」で、まれにしか起こらない
![Page 33: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/33.jpg)
半分移すとき時間かかる
● pushBack どうせO(1) ● pushFront どうせO(1)● popBack だいたいO(1) ときどき O(N)● popFront だいたいO(1) ときどき O(N)
● でもO(N)であることは本当に「ときどき」で、まれにしか起こらない
● O(N)っていいたくない
![Page 34: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/34.jpg)
そんなときに便利なのが
ならし計算量
![Page 35: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/35.jpg)
ならし計算量とは
● データ構造に対する操作の計算量の評価で使われる「計算量もどき」
● 時と場合によって計算量がかわるが、均すとO(f(x))になるとき
「ならし計算量がO(f(x))である」
という
![Page 36: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/36.jpg)
ならし計算量とは
● データ構造に対する操作の計算量の評価で使われる「計算量もどき」
● 時と場合によって計算量がかわるが、均すとO(f(x))になるとき
「ならし計算量がO(f(x))である」
という
![Page 37: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/37.jpg)
ならし計算量とは
● データ構造に対する操作の計算量の評価で使われる「計算量もどき」
● 時と場合によって計算量がかわるが、均すとO(f(x))になるとき
「ならし計算量がO(f(x))である」
という
?
![Page 38: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/38.jpg)
「均す」とは
● データ構造の中で、操作をN回やった時、「実際の計算量」の総和と「ならし計算量」の総和がだいたい同じになる
● 例:
– Dequeのpopの実際の計算量● O(1)だったりO(N)だったり
– Dequeのpopのならし計算量● O(1)
![Page 39: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/39.jpg)
Dequeの計算量
● Dequeの操作のならし計算量は全てO(1)
– ほんまか● ならし計算量がO(1)ということは N 回操作し
たときの「実際の計算量」がO(N)であるということ
– 実験してみるとそれっぽい● これから2つの方法でこれを証明する
![Page 40: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/40.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method
![Page 41: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/41.jpg)
AccountingMethod
● 直訳:会計法(法律っぽい)● 初めあなたの所持金は0円です● あなたが操作をするたびに、「ならし計算量」
円だけお金がふってきます● あなたが操作をするたびに、「実際の計算
量」円払わなければなりません● あなたはどのタイミングでも所持金が負に
なってはいけません
![Page 42: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/42.jpg)
AccountingMethod
● 何円ふってくるか、うまく決めて所持金が負にならなければそれがならし計算量
● うまく思いつこう!
![Page 43: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/43.jpg)
AccountingMethod
● 以下のように設定するとうまくいく
– 操作をすると 5 円ふってくる
![Page 44: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/44.jpg)
AccountingMethod
● 以下のように設定するとうまくいく
– 操作をすると 5 円ふってくる
● これでうまくいくことをこれから証明
![Page 45: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/45.jpg)
AccountingMethod:5円の証明
● pushBack,pushFront
– 実際の計算量は 1– よって1円払わないといけない
● popBack,popFrontのエイヤッしないやつ
– 実際の計算量は 1– 1円払わないといけない
● どちらの場合も差し引いて4円貯金が増える
![Page 46: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/46.jpg)
AccountingMethod:5円の証明
● popBack,popFrontのエイヤッするとき
– もう片方のStackの中身(N個とする)をいったん全部出して、半分ずつ入れ分ける
– 実際の計算量は 2N– 差し引いて2N-5円貯金が減る
● このとき借金しなければよい
![Page 47: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/47.jpg)
AccountingMethod:5円の証明
● 実は以下の命題が成り立つ
– 左側のStackにL個、右側のStackにR個の要素が入ってる時、4×max(L, R)円以上の貯金がある
– 帰納法で証明可能
![Page 48: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/48.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
![Page 49: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/49.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
– pushとエイヤッしないpopは 貯金が4円増えるので成立し続ける
![Page 50: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/50.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
– pushとエイヤッしないpopは 貯金が4円増えるので成立し続ける
– エイヤッするとき、もともと貯金が4N円以上あるので操作後は2N円以上が保証される
![Page 51: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/51.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
– pushとエイヤッしないpopは 貯金が4円増えるので成立し続ける
– エイヤッするとき、もともと貯金が4N円以上あるので操作後は2N円以上が保証される
– エイヤッ後は4×max(L,R)は2Nになる
![Page 52: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/52.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
– pushとエイヤッしないpopは 貯金が4円増えるので成立し続ける
– エイヤッするとき、もともと貯金が4N円以上あるので操作後は2N円以上が保証される
– エイヤッ後は4×max(L,R)は2Nになる 成立し続ける
![Page 53: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/53.jpg)
AccountingMethod:4*max(L,R)の証明
● 命題:貯金が4×max(L,R)円以上● はじめの状態(空の状態)では成立● 成立している状態からどの操作をしても命題が成立
し続けることを示せば良い
– pushとエイヤッしないpopは 貯金が4円増えるので成立し続ける
– エイヤッするとき、もともと貯金が4N円以上あるので操作後は2N円以上が保証される
– エイヤッ後は4×max(L,R)は2Nになる 成立し続ける
(Q.E.D.)
![Page 54: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/54.jpg)
これが
Accounting Method
![Page 55: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/55.jpg)
AccountingMethod
● ならしO(1)を求めるのに向いてる
– ふってくる値段を貯金がなくならないように充分大きくするだけ
● ふってくる値段は操作毎に変えても良い
– popは4円– pushは1円とかでも大丈夫
● 今回のようにデータ構造の状態に対して貯金の下限を示したりして使う
![Page 56: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/56.jpg)
AccountingMethod:演習問題
● 以下をAccountingMethodで示してみよ
● 動的にメモリを確保する配列(vector)のpush_backのならし計算量:O(1)
– メモリがなくなったら別の場所にいまの2倍の大きさのメモリを確保します
![Page 57: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/57.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method
![Page 58: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/58.jpg)
PotentialMethod
● データ構造の状態に対してPotentialという値を定義する
● ある操作をしてPotentailがP1からP2に変わったとする
● そのならし計算量を「実際の計算量」 + P2 – P1として定義する
![Page 59: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/59.jpg)
PotentialMethod:なんでそんなことするの
● 長い処理を考える
![Page 60: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/60.jpg)
PotentialMethod:なんでそんなことするの
● 長い処理を考える
状態 Potential 実計算量 均計算量
C1
C2
C3
CN
::
P1
P2
P3
PN
::
X1
X2
XN
::
![Page 61: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/61.jpg)
PotentialMethod:なんでそんなことするの
● ポテンシャルと実計算量から均計算量を出す
状態 Potential 実計算量 均計算量
C1
C2
C3
CN
::
P1
P2
P3
PN
::
X1
X2
XN
::
X1+P2-P1
X2+P3-P2
XN+PN-(ry
::
![Page 62: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/62.jpg)
PotentialMethod:なんでそんなことするの
● 計算量の総和を考えてみる
状態 Potential 実計算量 均計算量
C1
C2
C3
CN
::
P1
P2
P3
PN
::
X1
X2
XN
::
X1+P2-P1
X2+P3-P2
XN+PN-(ry
::
![Page 63: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/63.jpg)
PotentialMethod:なんでそんなことするの
● 計算量の総和を考えてみる
● 実計算量
– X1 + X2 + X3 + … + XN● 均計算量
– (X1 + P2 – P1) + (X2 + P3 – P2) + … (ry
↓– X1 + X2 + X3 + … + XN + (PN – P1)
![Page 64: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/64.jpg)
PotentialMethod:なんでそんなことするの
● 計算量の総和を考えてみる
● 実計算量
– X1 + X2 + X3 + … + XN● 均計算量
– (X1 + P2 – P1) + (X2 + P3 – P2) + … (ry
↓– X1 + X2 + X3 + … + XN + (PN – P1)
![Page 65: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/65.jpg)
PotentialMethod:なんでそんなことするの
● 「実計算量の総和」と「均計算量の総和」の差が PN – P1
–終状態と始状態のPotentialの差– これが「均計算量の総和」以下のオーダー
なら、実計算量と均計算量のオーダーが一致する
– 均計算量の定義と合致
![Page 66: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/66.jpg)
PotentialMethod:Dequeのポテンシャル
● うまいポテンシャルを見つけて、どんな操作も均計算量がO(1)であることを示せば良い
![Page 67: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/67.jpg)
PotentialMethod:Dequeのポテンシャル
● うまいポテンシャルを見つけて、どんな操作も均計算量がO(1)であることを示せば良い
● 今回は左側のStackの要素数をL、右側のStackの要素数をRとして
4*max(L, R)
をPotentialとするとうまくいく
![Page 68: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/68.jpg)
PotentialMethod:4*max(L,R)の証明
● 各操作の均計算量
– push●ポテンシャルはたかだか4増える● 実計算量は1● よってならし計算量は5
![Page 69: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/69.jpg)
PotentialMethod:4*max(L,R)の証明
● 各操作の均計算量
– pop(エイヤッなし)●ポテンシャルは増えない● 実計算量は1● よってならし計算量は1以下
![Page 70: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/70.jpg)
PotentialMethod:4*max(L,R)の証明
● 各操作の均計算量
– pop(エイヤッあり)●ポテンシャルは4*(N/2)減る● 実計算量は2N● よってならし計算量は0
![Page 71: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/71.jpg)
PotentialMethod:4*max(L,R)の証明
● PN-P1
– N回操作した後の均計算量の総和はO(N)– PN-P1の最大値は4N=O(N)
● ずっと同じ方からpushしたとき– よっしゃ
![Page 72: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/72.jpg)
PotentialMethod:4*max(L,R)の証明
● ポテンシャル4*max(L,R)での均計算量がすべてO(1)でした
![Page 73: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/73.jpg)
PotentialMethod:4*max(L,R)の証明
● ポテンシャル4*max(L,R)での均計算量がすべてO(1)でした
(Q.E.D.)
![Page 74: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/74.jpg)
これが
Potential Method
![Page 75: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/75.jpg)
PotentialMethod
● ならしO(1)以外を求めるのにも有用
– フィボナッチヒープのO(logN)– スプレー木のO(logN)– UnionFind木のO(α(n)) (アッカーマン関数の逆関数)
● 4*max(L,R)はAccountingMethodにもでてきた値
– AccountingもPotentialも歩み寄り方が違うだけで、やりたい証明は同じ
![Page 76: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/76.jpg)
PotentialMethod:演習問題
● 以下をPotentialMethodで示してみよ
● 動的にメモリを確保する配列(vector)のpush_backのならし計算量:O(1)
– メモリがなくなったら別の場所にいまの2倍の大きさのメモリを確保します
![Page 77: Amortize analysis of Deque with 2 Stack](https://reader034.fdocument.pub/reader034/viewer/2022042722/58a7f5a91a28abfa488b4f4d/html5/thumbnails/77.jpg)
目次
● 有名なことわざの紹介● ならし計算量のはなし● Accounting Method● Potential Method● おしまい