演算法概論 Introduction to Algorithms
Department of Computer Science and Information Engineering,
Chaoyang University of Technology
朝陽科技大學資工系
Speaker: Fuw-Yi Yang 楊伏夷
伏夷非征番,
道德經 察政章(Chapter 58) 伏者潛藏也
道紀章(Chapter 14) 道無形象, 視之不可見者曰夷
Fuw-Yi Yang
1
Reference:
K. H. Rosen, Discrete Mathematics and Its Applications, 5th ed.
J. A. Dossey, A. D. Otto, L. E. Spence and C. V. Eynden, Discrete
Mathematics, 4th ed.
R.1 Introduction
R.2 Recurrence Relations
R.3 Modeling with Recurrence Relations
R.4 Solving Recurrence Relations
R.5 First-Order Linear Difference Equation
R.6 Divide-and-Conquer Algorithms and Recurrence Relations
R.7 Prune-and-Search
R.8 Dynamic Programming
Fuw-Yi Yang
2
R.7 Prune-and-Search
The Selection Problem
The Selection Problem
A prune-and-Search algorithm to find the kth smallest element
Input: A set S of n elements
Output: the kth smallest element of S
1. If |S| ≦ 5, solve the problem by any method
2. Divide S into n/5 subsets.
3. Sort each subset of elements.
4. Recursively find the element x which is the medians of the median
of the n/5 subsets.
5. Partition S into S1, S2, and S3, which contains the elements less
than, equal to, and greater than x, respectively. (next page)
Fuw-Yi Yang
3
R.7 Prune-and-Search
The Selection Problem
5. Partition S into S1, S2, and S3, which contains the elements less
than, equal to, and greater than x, respectively.
6. If |S1| ≧ k, then discard S2 and S3 and solve the problem that selects
the kth smallest element from S1 during the next iteration;
else if |S1| + |S2| ≧ k, then x is the kth smallest element of S;
otherwise, let k = k - |S1| - |S2|. Solve the problem that selects
the kth smallest element from S3 during the next iteration.
Fuw-Yi Yang
4
R.7 Prune-and-Search
The Selection Problem—worst case
It follows that the number of elements greater than x is at least
3(1/2 n/5 - 2) ≧ 3n/10 – 6.
Similarly, at least 3n/10 – 6 elements are less than x.
Thus, in the worst case, step 6 calls algorithm recursively on at most
7n/10 + 6 elements.
s
s
s
s
s
s
s
s
x
b
b
b
b
b
b
b
b
b
b
b
Fuw-Yi Yang
5
R.7 Prune-and-Search
The Selection Problem—worst case
11, 10, 23, 40, 32, 25, 76, 45, 88, 28, 13, 32, 53, 29, 31, 19, 98, 54,
16, 42, 67, 73, 55, 62, 34, 88, 63, 17, 25, 74, 35, 67, 99, 48, 78, 93,
92, 97
10
13
16
25
34
17
35
92
11
29
19
28
55
25
48
93
23
31
42 (x)
45
62
63
67
97
32
32
54
76
67
74
78
40
53
98
88
73
88
99
23
63
31
67
42 (x)
97
45
62
Fuw-Yi Yang
6
R.7 Prune-and-Search
The Selection Problem
We make the assumption that any input of fewer than 140 elements
requires O(1) time. We can therefore obtain the recurrence
T(n) ≦ O(1) if n < 140,
T(n) ≦ T(n/5 ) + T(7n/10 + 6 ) + O(n) if n > 140.
T(n) ≦ c(n/5 ) + c(7n/10 + 6 ) + an
≦ cn/5 + c + 7cn/10 + 6c + an
= 9cn/10 + 7c + an
= cn + (-cn/10 + 7c + an)
Which is at most cn if (-cn/10 + 7c + an) ≦0.
To satisfy the inequality above, n > 140, c ≧20a.
Fuw-Yi Yang
7
R.7 Prune-and-Search
Example R.7.1 Binary Search.
procedure binary search (x: integer, a1, a2, …, an: increasing integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while i < j begin m := integer ((i + j) /2)
if x > am then i := m + 1
else j : = m
end
if x = ai then location := i
else location := 0
Fuw-Yi Yang
8
R.7 Prune-and-Search
The binary search algorithm reduces the search for an element in a
search sequence of size n to the binary search for this element in a
search sequence of size n/2, when n is even. Hence, the problem of
size n has been reduced to one problem of size n/2. Two comparisons
are needed to implement this reduction, one to determine which half of
the list to use and the other to determine whether any terms of the list
remain. Hence if f(n) is the number of comparisons required to search
for an element in a search sequence of size n, then f(n) = f(n/2) + 2,
when n is even. Find its time complexity.
Solution:
Fuw-Yi Yang
9
R.7 Prune-and-Search
Solution: The recurrence relation is f(n) = f(n/2) + 2,
i.e. a = 1, c = 2.
From algorithm, it can be seen f(1) = 1.
If n is a power of b, n = bk, it follows that
f(n) = f(1) + 2k
= 1 + 2 log bn.
Thus f(n) is O(log n).
Fuw-Yi Yang
10
© Copyright 2026 Paperzz