




已阅读5页,还剩34页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 11/17/2019,Introduction to Algorithms,9 Medians and Order Statistics,2 11/17/2019,Order Statistics,The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is the nth order statistic The median is the n/2 order statistic If n is even, there are 2 medians,3 11/17/2019,Selection Problem,Input: A set A of n (distinct) numbers and a number i, with 1 i n. Output: The element x A that is larger than exactly i - 1 other elements of A. How can we calculate order statistics? What is the running time?,4 11/17/2019,9.1 Minimum and Maximum,5 11/17/2019,Minimum and Maximum,How many comparisons are needed to find the minimum element in a set? The maximum? MINIMUM(A) 1 min A1 2 for i 2 to lengthA 3 do if min Ai 4 then min Ai 5 return min,6 11/17/2019,Simultaneous Minimum and Maximum,Can we find the minimum and maximum with less than twice the cost? Yes: Walk through elements by pairs Compare each element in pair to the other Compare the largest to maximum, smallest to minimum Total cost: 3 comparisons per 2 elements 3 n/2 = O(3n/2),7 11/17/2019,9.2 Selection in expected linear time,8 11/17/2019,The Selection Problem,A more interesting problem is selection: finding the ith smallest element of a set We will show: A practical randomized algorithm with O(n) expected running time A cool algorithm of theoretical interest only with O(n) worst-case running time,9 11/17/2019,Randomized Selection,Key idea: use partition() from quicksort But, only need to examine one subarray This savings shows up in running time: O(n) We will again use a slightly different partition q = RandomizedPartition(A, p, r), Aq, Aq,q,p,r,10 11/17/2019,Randomized Selection,RandomizedSelect(A, p, r, i) if (p = r) then return Ap; q = RandomizedPartition(A, p, r) k = q - p + 1; if (i = k) then return Aq; if (i k) then return RandomizedSelect(A, p, q-1, i); else return RandomizedSelect(A, q+1, r, i-k);, Aq, Aq,k,q,p,r,11 11/17/2019,Randomized Selection,Analyzing RandomizedSelect() Worst case: partition always 0:n-1 T(n) = T(n-1) + O(n) = ? = O(n2) (arithmetic series) No better than sorting! “Best” case: suppose a 9:1 partition T(n) = T(9n/10) + O(n) = ? = O(n) (Master Theorem, case 3) Better than sorting! What if this had been a 99:1 split?,12 11/17/2019,Randomized Selection,13 11/17/2019,Randomized Selection,14 11/17/2019,Calculating expectation,15 11/17/2019,Calculating expectation,16 11/17/2019,Calculating expectation,17 11/17/2019,Calculating expectation,18 11/17/2019,Calculating expectation,19 11/17/2019,Randomized Selection,Lets show that T(n) = O(n) by substitution,What happened here?,20 11/17/2019,What happened here?,“Split” the recurrence,What happened here?,What happened here?,What happened here?,Randomized Selection,Assume T(n) cn for sufficiently large c:,The recurrence we started with,Substitute T(n) cn for T(k),Expand arithmetic series,Multiply it out,21 11/17/2019,What happened here?,Subtract c/2,What happened here?,What happened here?,What happened here?,Randomized Selection,Assume T(n) cn for sufficiently large c:,The recurrence so far,Multiply it out,Rearrange the arithmetic,What we set out to prove,22 11/17/2019,Summary of randomized order-statistic selection, Works fast: linear expected time. Excellent algorithm in practice. But, the worst case is very bad: (n2).,23 11/17/2019,9.3 Selection in worst-case linear time,24 11/17/2019,Worst-Case Linear-Time Selection,What follows is a worst-case linear time algorithm, really of theoretical interest only Basic idea: Generate a good partitioning element Call this element x,25 11/17/2019,Worst-Case Linear-Time Selection,The algorithm in words: 1. Divide n elements into groups of 5 2. Find median of each group (How? How long?) 3. Use Select() recursively to find median x of the n/5 medians 4. Partition the n elements around x. Let k = rank(x) 5. if (i = k) then return x if (i k) use Select() recursively to find (i-k)th smallest element in last partition,26 11/17/2019,Choosing the pivot,27 11/17/2019,Choosing the pivot,28 11/17/2019,Choosing the pivot,29 11/17/2019,Choosing the pivot,30 11/17/2019,Analysis,31 11/17/2019,Analysis(Assume all elements are distinct.),32 11/17/2019,Analysis(Assume all elements are distinct.),33 11/17/2019,Worst-Case Linear-Time Selection,How many of the 5-element medians are x? At least 1/2 of the medians = n/5 / 2 = n/10 How many elements are x? At least 3 n/10 elements For large n, 3 n/10 n/4 (How large?) So at least n/4 elements x Similarly: at least n/4 elements x,34 11/17/2019,Worst-Case Linear-Time Selection,35 11/17/2019,Worst-Case Linear-Time Selection,Thus after partitioning around x, step 5 will call Select() on at most 3n/4 elements The recurrence is therefore:,?,?,?,?,?,n/5 n/5,Substitute T(n) = cn,Combine fractions,Express in desired form,What we set out to prove,36 11/17/2019,Linear-Time Median Selection,Given a “black box” O(n) median algorithm, what can we do? ith order statistic: Find median x Partition input around x if (i (n+1)/2) recursively find ith element of first half else find (i - (n+1)/2)th element in second half T(n) = T(n/2) + O(n) = O(n) Can you think of an application to sorting?,37 11/17/2019,Worst-Case Linear-Time Selection,Intuitively: Work at each level is a constant fr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 1.2 化学实验与科学探究说课稿-2024-2025学年九年级化学人教版(2024)上册
- 6.2.1.排列教学设计-2023-2024学年高二下学期数学人教A版(2019)选择性必修第三册
- 灌区管理考试题目及答案
- 古代学子考试题目及答案
- 公司贷款考试题目及答案
- 工会干事考试题及答案
- 2025仓库抵押借款合同
- 高级火影考试题目及答案
- 社区智慧养老服务体系的优化与创新方向
- 居住区景观适老化元素的视觉感知与认知分析
- T/CHES 98-2023取水口设施标准化建设与管理技术规程
- 专项项目贡献证明书与业绩认可函(8篇)
- 2025年广东省广州市中考二模英语试题(含答案)
- 消防员心理测试题库及答案解析
- 贷后管理协议合同
- 罗才军《少年闰土》省公开课一等奖全国示范课微课金奖课件
- 放射科造影剂过敏反应应急处理预案
- 触电事故应急演练方案
- 2025年上海市高考英语热点复习:阅读理解说明文
- (完整版)八上新闻拟标题专项训练题
- 国家管网集团合同范本
评论
0/150
提交评论