アルゴリズムイントロダクション 8章

2,474 views

Published on

アルゴリズムイントロダクション 第8章の勉強会資料

下記でPPT版,KEY版も配布してます.
http://tniky1.com/study/

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,474
On SlideShare
0
From Embeds
0
Number of Embeds
1,150
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

アルゴリズムイントロダクション 8章

  1. 1. tniky1 http://www.tniky1.com2011 7 8
  2. 2. ( ) Ω(nlgn) ^^; O(n)2011 7 8
  3. 3. 8.1 Ω(nlgn) 8.2 2 1 3 0 2 3 0 3 8.3 329 457 657 839 436 O(n) 8.4 .78 .17 .39 .26 .72 .94 .21 .12 [0..1]2011 7 8
  4. 4. ( ) Ω(nlgn) O(n)2011 7 8
  5. 5. a1 : a2 ≤ > <a1,a2> <a2,a1> Ω(nlgn) Chapter 8 Sorting in Linear Time ≤ 1:2 > ( )=( h) 2:3 1:3 ≤ > ≤ > h 〈1,2,3〉 1:3 〈2,1,3〉 2:3 2 ≤ > ≤ > 〈1,3,2〉 〈3,1,2〉 〈2,3,1〉 〈3,2,1〉 n! Figure 8.1 The decision tree for insertion sort operating on three elements. An internal node an- notated by i: j indicates a comparison between ai and a j . A leaf annotated by the permutation π(1), π(2), . . . , π(n) indicates the ordering aπ(1) ≤ aπ(2) ≤ · · · ≤ aπ(n) . The shaded path indi- cates the decisions made when sorting the input sequence a1 = 6, a2 = 8, a3 = 5 ; the permutation 3, 1, 2 at the leaf indicates that the sorted ordering is a3 = 5 ≤ a1 = 6 ≤ a2 = 8. There are 3! = 6 n possible permutations of the input elements, so the decision tree must have at least 6 leaves. Ω(nlgn)2011 they yield identical information about the relative order of ai and a j . We therefore 7 8
  6. 6. <6, 8, 5> a1 : a2 ≤ > <6, 8, 5> a2 : a3 a1 : a3 ≤ ><6, 8, 5> ≤ > <a1,a2,a3> a1 : a3 <a2,a1,a3> a2 : a3 ≤ > ≤ > <a1,a3,a2> <a3,a1,a2> <a2,a3,a1> <a3,a2,a1> <6, 8, 5> : n!2011 7 8
  7. 7. h h 2 (2 ) h h 2 ≥ n! h ≥ lg(n!) Stiring n! > (n/e) h ≥ lg(n/e) = n lg(n) - n lg(e) n! = Ω(n lgn) n n! Ω(nlgn)2011 7 8
  8. 8. ( ) Ω(nlgn) O(n)2011 7 8
  9. 9. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 2 4 7 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B A C’ !!2011 7 8
  10. 10. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 2 4 7 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 3 A C’ !!2011 7 8
  11. 11. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 2 4 7 6 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 3 A C’ !!2011 7 8
  12. 12. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 2 4 7 6 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 3 A C’ !!2011 7 8
  13. 13. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 1 2 4 7 6 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 3 A C’ !!2011 7 8
  14. 14. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 1 2 4 7 6 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 3 3 A C’ !!2011 7 8
  15. 15. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 1 2 4 7 6 5 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 3 3 A C’ !!2011 7 8
  16. 16. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 1 0 2 4 2 7 4 6 5 7 8 7 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 0 2 2 3 3 3 5 A C’ !!2011 7 8
  17. 17. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 n 0~k(5) 0 2 3 4 5 C 2 0 2 3 0 1 0~k(5) 0 2 3 4 5 C’ 2 1 0 2 2 2 4 2 4 7 4 6 7 5 7 7 8 7 8 (0~k ) =(0~k ) 2 3 4 5 6 7 8 B 0 0 2 2 3 3 3 5 A C’ !!2011 7 8
  18. 18. (Counting Sort) Counting Sort (A, B, k) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 for i ← 0 to k j j do C[i] ← 0 0 2 3 4 5(k) for j ← 1 to length[A] C 2 0 2 3 0 1 i do C[A[j]] ← C[A[j]] + 1 0 2 3 4 5 for i ← 1 to k C’ 2 2 4 7 7 8 do C[i] ← C[i] + C[i-1] for j ← length[A] downto 1 2 3 4 5 6 7 8 do B[C[A[j]]] ← A[j] B 0 0 2 2 3 3 3 5 C[A[j]] ← C[A[j]] - 12011 7 8
  19. 19. (Counting Sort) Counting Sort (A, B, k) O(k) for i ← 0 to k do C[i] ← 0 O(n) for j ← 1 to length[A] do C[A[j]] ← C[A[j]] + 1 O(n) for i ← 1 to k O(k)k=O(n) do C[i] ← C[i] + C[i-1] for j ← length[A] downto 1 O(n) do B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] - 12011 7 8
  20. 20. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 2 4 7 7 8 B 2 3 4 5 6 7 8 ( )2011 7 8
  21. 21. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 2 4 7 7 8 B 2 3 4 5 6 7 8 ( )2011 7 8
  22. 22. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 2 4 7 6 7 8 B 3 2 3 4 5 6 7 8 ( )2011 7 8
  23. 23. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 1 2 4 7 6 7 8 B 0 3 2 3 4 5 6 7 8 ( )2011 7 8
  24. 24. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 1 2 4 7 6 5 7 8 B 0 3 3 2 3 4 5 6 7 8 ( )2011 7 8
  25. 25. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 1 0 2 0 4 2 7 6 4 5 7 7 8 7 B 0 0 2 2 3 3 3 5 2 3 4 5 6 7 8 ( )2011 7 8
  26. 26. (Counting Sort) 2 3 4 5 6 7 8 A 2 5 3 0 2 3 0 3 0 2 3 4 5 C 2 0 2 3 0 1 0 2 3 4 5 C’ 2 2 1 0 2 0 4 2 7 6 4 5 7 7 8 7 B 0 0 2 2 3 3 3 5 2 3 4 5 6 7 8 ( )2011 7 8
  27. 27. (Counting Sort) O(n)2011 7 8
  28. 28. ( )2011 7 8
  29. 29. ( ) ( / )O(n) !2011 7 8
  30. 30. Thank you2011 7 8

×