1 / 18

알고리즘 설계 및 분석

알고리즘 설계 및 분석. Foundations of Algorithm 유관우. Chap. 8 Searching Problem (p307). lower bound for searching Theorem: any deterministic alg. that searches x among n keys only by comparisons of keys : At least binary search : mid = (low+high)/2 Θ (log n) time : optimal.

ronni
Download Presentation

알고리즘 설계 및 분석

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 알고리즘 설계 및 분석 Foundations of Algorithm 유관우

  2. Chap. 8 Searching Problem (p307) • lower bound for searching Theorem: any deterministic alg. that searches x among n keys only by comparisons of keys : At least • binary search : mid = (low+high)/2 • Θ(log n) time : optimal. Digital Media Lab.

  3. interpolation search : maybe faster. off-line algorithms • On-line alg.? binary search trees, B- trees,… Θ(log n) time • Hashing : on-line, Θ(1) time avg.  No comp. Digital Media Lab.

  4. Selection problem(p332) • find kth-largest(or kth-smallest) element. Theorem 8.7 Any deterministic alg. to find_max in every possible input : ≥ (n - 1) comparisons • upper bound for find_max : (n-1) comparisons. “every possible input” Digital Media Lab.

  5. Eg) stored array : find_max  0 comp 1. compare one-by-one 2. tournament (n-1) comp. large (n-1) comp Digital Media Lab.

  6. Find both min & max. (1) find_max, and then find_min. (2n-3) comparisons. (2) { small = s[1]; large = s[1]; for I = 2 to n do if s[i] < small then small = s[i]; else if s[i] > large then large = s[i]; } Best case? Worst-case? s[1] is smallest : (2n-2) Avg.-case ≈ 3n/2 comp. Digital Media Lab.

  7. n/2 winners largest n elements ≈(n/2-1) comp. n/2 losers smallest ≈n/2 comp. (3) T(n) = 3n/2 -2 if n is even. 3n/2-3/2 if n is odd. • better method ? No • Theorem 8.8.  (3) is optimal. Digital Media Lab.

  8. 18 18 15 16 15 18 12 16 4 15 5 12 10 18 12 • finding the second largest key (1) find_max twice : (2n-3) comp.’s (2) tournament method : “2nd largest key is lost to the largest.” Assume n = 2k (pad -∞ if not). Eg) Digital Media Lab.

  9. maintain linked list for next use • time for find_max : • log n keys in max’s linked list  total time : (n + log n - 2) • In general, • Theorem 8.9 : finding 2nd largest key Digital Media Lab.

  10. Finding the k-th smallest key. 1. Find_min k times : Θ(n*k) 2. Sort & pick kth key : Θ(n log n) 3. Construct heap & k delete_min’s : Θ(n +k log n) (= Θ(n log n) if k = n/2) 4. Θ(n) time algorithm? Yes. • randomization algorithm : practical • deterministic algorithm : theoretical Digital Media Lab.

  11. Θ(n2) algorithm function selection (low, high, k); { if low == high then selection = S[low]; else{ partition (low, high, pivotpoint); if k == pivotpoint then selection = S[pivotpoint]; else if k < pivotpoint then selection = selection (low, pivotpoint-1, k); else selection = selection(pivotpoint+1,high, k - pivotpoint); } } Digital Media Lab.

  12. ≤pivot ≥pivot low high pivotpoint Partition (low, high, pivotpoint) • K_th_smallest = selection(1, n, k); w(n) = n(n-1)/2 A(n) 3n Digital Media Lab.

  13. n/5 medians MM n/5 • Θ(n) deterministic algorithm. • Median-of-medians (MM) • procedure partition2 (n, S[1..n], low, high, pivotpoint) • Find MM && rearragne! ≤MM ≥MM low high pivotpoint ≤7n/10-3/2 ≤7n/10-3/2 Digital Media Lab.

  14. function select2 (n, S[1..n], low, high, k); { if low == high then select2 = S[low]; else{ partition2 (n, S, low, high, pivotpoint); if k == pivotpoint then select2 = S[pivotpoint]; else if k < pivotpoint then select2 = select2(n, S, low, pivotpoint-1, k); else select2(n, S, pivotpoint+1, high, k - pivotpoint); } } Digital Media Lab.

  15. Worst-case time complexity analysis Digital Media Lab.

  16. Digression : • W(n)=W(p*n)+W(q*n)+a*n. If p+q< 1, W(n)=Θ(n). • if m=3 ? • m=5, 7, 9,11,… O.K. n/3 Digital Media Lab.

  17. Probabilistic Algorithm • Monte Carlo algorithm. • correct answer w.h.p. (with high probability). • Las Vegas algorithm (Sherwood Algorithm). • always correct answer & efficient w.h.p. Eg) Randomized partition. procedure partition3 (low, high, pivotpoint) { pick random index in [low..high] partition by using S[random] as pivot. } Digital Media Lab.

  18. ≤pivot ≥pivot low high function selet3 (low, high, k); • Expected-value time complexity E(n,k) ≤ 4n ∈ Θ(n) • Worst-case time complexity W(n) = Θ(n2) • But, more appropriate W(n) =Θ(n) w.h.p. pivotpoint Digital Media Lab.

More Related