![Page 1: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/1.jpg)
1
Chapter 9: Medians and Order Statistics
![Page 2: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/2.jpg)
2
About this lecture
• Finding max, min in an unsorted array (upper bound and lower bound)
• Finding both max and min (upper bound)
• Selecting the kth smallest element
kth smallest element ≡ kth order statistics
![Page 3: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/3.jpg)
3
Finding Maximum
in unsorted array
![Page 4: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/4.jpg)
4
Finding Maximum (Method I)• Let S denote the input set of n items• To find the maximum of S, we can:Step 1: Set max = item 1Step 2: for k = 2, 3, …, n
if (item k is larger than max) Update max = item k;
Step 3: return max;
# comparisons = n – 1
![Page 5: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/5.jpg)
5
Finding Maximum (Method II)
• Define a function Find-Max as follows:Find-Max(R, k) /* R is a set with k items */
1. if (k ≤ 2) return maximum of R;
2. Partition items of R into pairs;3. Delete smaller item from R in each pair;4. return Find-Max(R, k - );
Calling Find-Max(S, n) gives the maximum of S
2/k
2/k
![Page 6: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/6.jpg)
6
Finding Maximum (Method II)
Let T(n) = # comparisons for Find-Max with problem size n
So, T(n) = T(n - n/2) + n/2 for n ≥ 3T(2) = 1
Solving the recurrence (by substitution), we get T(n) = n - 1
![Page 7: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/7.jpg)
7
Lower Bound
Question: Can we find the maximum using fewer than n – 1 comparisons?
Answer: No ! To ensure that an item x is not the maximum, there must be at leastone comparison in which x is the smaller of the compared itemsSo, we need to ensure n-1 items not max at least n – 1 comparisons are needed
![Page 8: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/8.jpg)
8
Finding Both Max and Min
in unsorted array
![Page 9: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/9.jpg)
9
Finding Both Max and Min
Can we find both max and min quickly?
Solution 1: First, find max with n – 1 comparisonsThen, find min with n – 1 comparisons Total = 2n – 2 comparisons
Is there a better solution ??
![Page 10: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/10.jpg)
10
Finding Both Max and Min
Better Solution: (Case 1: if n is even)
First, partition items into n/2 pairs;
…
Next, compare items within each pair;
…
= larger = smaller
![Page 11: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/11.jpg)
11
Finding Both Max and Min
Then, max = Find-Max in larger itemsmin = Find-Min in smaller items
…
…
Find-Max Find-Min
# comparisons = 3n/2 – 2
![Page 12: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/12.jpg)
12
Finding Both Max and Min
Better Solution: (Case 2: if n is odd)
We find max and min of first n - 1 items;if (last item is larger than max)
Update max = last item;if (last item is smaller than min )
Update min = last item;
# comparisons = 3(n-1)/2
![Page 13: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/13.jpg)
13
Finding Both Max and Min
Conclusion: To find both max and min:
if n is odd: 3(n-1)/2 comparisonsif n is even: 3n/2 – 2 comparisons
Combining: at most 3n/2 comparisons
better than finding max and min separately
![Page 14: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/14.jpg)
14
Selecting kth smallest item
in unsorted array
![Page 15: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/15.jpg)
15
Selection in Linear Time
• In next slides, we describe a recursive call Select(S, k)
which supports finding the kth smallest element in S
• Recursion is used for two purposes:(1) selecting a good pivot (as in Quicksort)(2) solving a smaller sub-problem
![Page 16: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/16.jpg)
16
Select(S, k) /* First, find a good pivot */
1. If |S|less than a small number then use selection sort to return the answerElse Partition S into |S|/5 groups, each group has five items (one group may have fewer items);
2. Sort each group separately;3. Collect median of each group into S’;4. Find median m of S’:
m = Select(S’,|S|/5/2);
![Page 17: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/17.jpg)
17
5. Let q = # items of S smaller than m;6. If (k == q + 1)
return m;/* Partition with pivot */7. Else partition S into X and Y
X = {items smaller than m} Y = {items larger than m}
/* Next, form a sub-problem */8. If (k < q + 1)
return Select(X, k)9. Else
return Select(Y, k–(q+1));
![Page 18: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/18.jpg)
18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
![Page 19: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/19.jpg)
19
Selection in Linear Time
Questions:
1. Why is the previous algorithm correct?(Prove by Induction)
2. What is its running time?
![Page 20: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/20.jpg)
20
Running Time
• In our selection algorithm, we chose m, which is the median of medians, to be a pivot and partition S into two sets X and Y
• In fact, if we choose any other item as the pivot, the algorithm is still correct
• Why don’t we just pick an arbitrary pivot so that we can save some time ??
![Page 21: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/21.jpg)
21
Running Time
• A closer look reviews that the worst-case running time depends on |X| and |Y|
• Precisely, if T(|S|) denote the worst-case running time of the algorithm on S, then
T(|S|) = T(|S|/5) + Θ(|S|) + max {T(|X|),T(|Y|) }
![Page 22: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/22.jpg)
22
Running Time
• Later, we show that if we choose m, the “median of medians”, as the pivot,
both |X| and |Y| will be at most 3|S|/4
• Consequently,
T(n) = T(n /5) + Θ(n) + T(3n/4)
T(n) = Θ(n) (obtained by substitution)
![Page 23: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/23.jpg)
23
Substitution
cnanccncn
anccnanccnccn
anncncnT
≤++−+=
++≤++++≤
+++≤
),710/(710/9
610/75/)610/7(5/)(
if 0710/ ≤++− anccn
![Page 24: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/24.jpg)
24
Median of Medians
• Let’s begin with n/5 sorted groups, each has 5 items (one group may have fewer)
…
= larger = smaller= median
![Page 25: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/25.jpg)
25
Median of Medians• Then, we obtain the median of medians, m
= mGroups with median smaller than m
Groups with median larger than m
![Page 26: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/26.jpg)
26
Median of Medians
The number of items with value greater than m is at least
3(n/5/2 – 2)
each full group has 3 ‘crossed’ items min # of
groups
two groups may not have 3 ‘crossed’
items
number of items: at least 3n/10 – 6
![Page 27: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/27.jpg)
27
Median of Medians
Previous page implies that at most7n/10 + 6 items
are smaller than m
For large enough n (say, n ≥ 140)7n/10 + 6 ≤ 3n/4
|X| is at most 3n/4 for large enough n
![Page 28: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/28.jpg)
28
Median of MediansSimilarly, we can show that at most
7n/10 + 6 items are larger than m |Y| is at most 3n/4 for large enough n
Conclusion:The “median of medians” helps us control the worst-case size of the sub-problem without it, the algorithm runs in Θ(n2)
time in the worst-case
![Page 29: Chapter 9: Medians and Order Statisticshscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Ch 9 10.pdf · Chapter 9: Medians and Order Statistics. 2 About this lecture • Finding max,](https://reader033.vdocument.in/reader033/viewer/2022042708/5f3a317460e4ad739e376af3/html5/thumbnails/29.jpg)
29
Homework
• Exercises 9.1-1 (Due Nov. 5)
• Exercises: 9.3-1, 9.3-3
• Problem: 9-1