Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PFDS 6.4.3

644 views

Published on

  • Be the first to comment

PFDS 6.4.3

  1. 1. PFDS 6.4.3Bottom-Up Mergesort with Sharing @rf0444
  2. 2. Bottom-UP Mergesort 5 2 7 4 1 8 3
  3. 3. Bottom-UP Mergesort 5 2 7 4 1 8 3
  4. 4. Bottom-UP Mergesort 5 2 7 4 1 8 3 5 2 7 1 4 3 8
  5. 5. Bottom-UP Mergesort 5 2 7 4 1 8 3 5 2 7 1 4 3 8 5 2 7 1 3 4 8
  6. 6. Bottom-UP Mergesort 5 2 7 1 3 4 8
  7. 7. Bottom-UP Mergesort 5 2 7 1 3 4 8 2 5 7 1 3 4 8
  8. 8. Bottom-UP Mergesort 5 2 7 1 3 4 8 2 5 7 1 3 4 8 1 2 3 4 5 7 8
  9. 9. with Sharing 7 4 1 8 3 1 3 4 7 8 2 7 4 1 8 3 1 2 3 4 7 85 2 7 4 1 8 3 1 2 3 4 5 7 8
  10. 10. with Sharing7 4 1 8 3
  11. 11. with Sharing7 4 1 8 3 7 1 3 4 8
  12. 12. with Sharingadd 7 4 1 8 3 7 1 3 4 8 2 7 4 1 8 3 2 7 1 3 4 85 2 7 4 1 8 3 5 2 7 1 3 4 8
  13. 13. with Sharingsort 7 1 3 4 8 1 3 4 7 8 2 7 1 3 4 8 1 2 3 4 7 8 5 2 7 1 3 4 8 1 2 3 4 5 7 8
  14. 14. add’s costunshared cost = 1shared cost = addSeg’s cost
  15. 15. add’s costaddSeg’s cost6 5 2 7 1 3 4 8
  16. 16. add’s cost addSeg’s cost 6 5 2 7 1 3 4 8(1 + 1) 5 6 2 7 1 3 4 8
  17. 17. add’s cost addSeg’s cost 6 5 2 7 1 3 4 8(1 + 1) 5 6 2 7 1 3 4 8(1 + 1) + (2 + 2) 2 5 6 7 1 3 4 8
  18. 18. add’s cost addSeg’s cost 6 5 2 7 1 3 4 8(1 + 1) 5 6 2 7 1 3 4 8(1 + 1) + (2 + 2) 2 5 6 7 1 3 4 8(1 + 1) + (2 + 2) + (4 + 4) 1 2 3 4 5 6 7 8
  19. 19. add’s cost addSeg’s cost ・・・add時に、左 k ブロックが埋まっているとすると、 k-1 k-1 k(1 + 1) + (2 + 2) + ... + (2 +2 ) = 2 (2 - 1)
  20. 20. add’s costcomplete cost= unshared cost + shared cost k= 1 + 2 (2 - 1) k+1=2 -1
  21. 21. add’s costamortized cost= complete cost - change in potential
  22. 22. potential iψ(n) = 2n - 2 i Σ0 b i (n mod 2 - 1) =0 ≦ ψ(n) ≦ 2n
  23. 23. potential b の項n 2n 0 1 2 3 ψ(n)0 0 + = 01 2 + -2 = 02 4 + -2 = 23 6 + -2 -4 = 04 8 + -2 = 65 10 + -2 -4 = 46 12 + -2 -6 = 47 14 + -2 -4 -8 = 08 16 + -2 = 149 18 + -2 -4 = 1210 20 + -2 -6 = 12
  24. 24. change in potentialn ψ(n) ψ(n + 1) ψ(n + 1) - ψ(n)0 0 0 01 0 2 22 2 0 -23 0 6 64 6 4 -25 4 4 06 4 0 -47 0 14 148 14 12 -29 12 12 0
  25. 25. change in potential k+1ψ(n + 1) - ψ(n) = 2 - 2 B’ B’ = n + 1 をビット表現したときの 1 の数
  26. 26. add’s costamortized cost= complete cost - change in potential k+1 k+1= (2 - 1) - (2 - 2 B’)= 2 B’ - 1O(log n)
  27. 27. sort’s cost 5 2 7 1 3 4 8
  28. 28. sort’s cost 5 2 7 1 3 4 8(1 + 2) 2 5 7 1 3 4 8
  29. 29. sort’s cost 5 2 7 1 3 4 8(1 + 2) 2 5 7 1 3 4 8(1 + 2) + (1 + 2 + 4) 1 2 3 4 5 7 8
  30. 30. sort’s cost ・・・ k-1(1 + 2) + (1 + 2 + 4) + ... + (1 + 2 + ... + 2 ) k+1= (2 - 4) - (k - 1)= 2n - k - 1
  31. 31. sort’s costamortized cost= (2n - k - 1) + ψ(n)< 4nO(n)

×