180 likes | 489 Views
알고리즘 설계 및 분석. 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.
E N D
알고리즘 설계 및 분석 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. Digital Media Lab.
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.
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.
Eg) stored array : find_max 0 comp 1. compare one-by-one 2. tournament (n-1) comp. large (n-1) comp Digital Media Lab.
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.
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.
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.
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.
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.
Θ(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.
≤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.
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.
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.
Worst-case time complexity analysis Digital Media Lab.
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.
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.
≤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.