Chap-Example and Exercise.ppt

Download Report

Transcript Chap-Example and Exercise.ppt

演算法概論 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
演算法 概論
Chapter 2 Algorithm and Problem Analysis
Chapter 3 Greedy Method
Chapter 4 Prune-and-Search
Chapter 5 Divide and Conquer
Fuw-Yi Yang
2
Chapter 2 Algorithm and Problem Analysis
2.1 比較 4n2 和 2n/4 在不同的 n 值時的函數值.
n
1
2
3
4
5
6
7
8
9
10
11
4n2
4
16
36
64
100
144
196
256
324
400
484
2n/4
0.5
1
2
4
8
16
32
64
128
256
512
Fuw-Yi Yang
3
Chapter 2 Algorithm and Problem Analysis
2.3 a. 證明 f(n) = 5n2 – 6n (n2).
f(n) ≦ 6n2 , for 1 ≦ n, i.e. f(n) O(n2) , for 1 ≦ n;
4n2 ≦ f(n) , for 6 ≦ n, i.e. f(n) (n2) , for 6 ≦ n;
Thus, f(n) (n2) , for 6 ≦ n
Fuw-Yi Yang
4
Chapter 2 Algorithm and Problem Analysis
2.3 b. 證明 f(n) = n! O(nn).
f(n) ≦ nn , for 1 ≦ n,
Thus f(n) O(nn) , for 1 ≦ n.
Fuw-Yi Yang
5
Chapter 2 Algorithm and Problem Analysis
2.6 求下列演算z法片段的時間複雜度.
for i := 1 to m do
// m+1次(包括最後一次跳出迴圈的判斷)
for j := 1 to p do
// p+1次
{ c [ i ][ j ] := 0;
// mp次
for k := 1 to n do //mp(n+1)次
c[ i ][ j ] := c [ i ][ j ] + a [ i ][ k ] * b [ k ][ j ]; //mpn次
}
根據上述計算全部執行次數為:(m+1)+(p+1)+mp+mp(n+1)+mpn
複雜度為O(mpn)
Fuw-Yi Yang
6
Chapter 2 Algorithm and Problem Analysis
2.19 求下列遞迴生成關係式的時間複雜度.
f(n) = f(n/3) + O(n)
= f(1) + O(30) + O(31) + … + O(3k) // let n = 3k
= f(1) + O(30) + O(31) + … + O(n)
O(n) .
Fuw-Yi Yang
7
Chapter 3 Greedy Method
Huffman code
Assume that C is a set of n characters and that each character c
 C is an object with an attribute c.freq giving its frequency.
The algorithm builds the tree T corresponding to the optimal
code in a bottom-up manner. It begins with a set of |C| leaves and
performs a sequence of |C| - 1 merging operations to create the
final tree.
The algorithm uses a min-priority queue Q, keyed on the freq
attribute, to identify the two least-frequent objects to merge
together.
Fuw-Yi Yang
8
Chapter 3 Greedy Method
Huffman code
Huffman(C)
1. n = |C|
2. Q = C
3. for i = 1 to n - 1
4.
Allocate a new node z
5.
z.left = x = extract-min(Q)
6.
z.right = y = extract-min(Q)
7.
z.freq = x.freq + y.freq
8.
Insert(Q, z)
9. Return Extract-min(Q) // return the root of the tree
Fuw-Yi Yang
9
Chapter 3 Greedy Method
Huffman code--Heap
Parent(i)
1. Return i/2
Left(i)
1. Return 2i
Right(i)
1. Return 2i + 1
Fuw-Yi Yang
10
Chapter 3 Greedy Method
Huffman code--Heap
Min-Heapify(A, i)
1. l = Left(i)
2. r = Right(i)
3. if l  A.heap-size and A[l] < A[i] then smallest = l
4.
else smallest = i
5. if r  A.heap-size and A[r] < A[smallest] then smallest t = r
6. if smallest  i then Exchange A[i] with A[smallest]
7.
Min-Heapify (A, smallest)
Fuw-Yi Yang
11
Chapter 3 Greedy Method
Huffman code--Heap
Max-Heapify(A, i)
1. l = Left(i)
2. r = Right(i)
3. if l  A.heap-size and A[l] > A[i] then largest = l
4.
else largest = i
5. if r  A.heap-size and A[r] > A[largest] then largest = r
6. if largest  i then Exchange A[i] with A[largest]
7.
Max-Heapify (A, largest)
Fuw-Yi Yang
12
Chapter 3 Greedy Method
Huffman code--Heap
Build-Max-Heap(A)
1. A.heap-size = A.length
2. for i = A.length / 2 downto 1
3.
Max-Heapify(A, i)
Build-Min-Heap(A)
1. A.heap-size = A.length
2. for i = A.length / 2 downto 1
3.
Min-Heapify(A, i)
Fuw-Yi Yang
13
Chapter 3 Greedy Method
Huffman code—Complexity of
Build-Max-Heap
1. Each call to Max-Heapify costs O(log n) time, there are O(n)
such calls. Thus the complexity is O(n log n).
2. Tighter analysis results in the complexity of O(n).
The time required by Max-Heapify when called on a node of
height h is O(h), and so we can express the total cost of Build-MaxHeap as being bounded by
floor(lg n )

h 0
Next page
floor(lg n )
n
h
ceiling ( h 1 )O(h)  O(n  h )
2
2
h 0
Fuw-Yi Yang
14
Chapter 3 Greedy Method
Huffman code—Complexity of
Build-Max-Heap
floor(lg n )

h 0
floor(lg n )
n
h
ceiling ( h1 )O(h)  O(n 
)
h
2
2
h 0

1
x 
; for | x |  1, differentiating both sides

1 x
k 0

x
k
kx

; if x  1 / 2

2
(1  x)
k 0
k


k
2
k
k 0 2
 kx  
k
k 0
Fuw-Yi Yang
15
Chapter 3 Greedy Method
Huffman code—Complexity of
Build-Max-Heap
Thus,
floor(lg n )

h 0
floor(lg n )
n
h
ceiling ( h1 )O(h)  O(n 
)
h
2
2
h 0
= O(n)
end
Fuw-Yi Yang
16
Chapter 3 Greedy Method
Huffman code—Example
Example 3.20 Given that a = 20, b = 15, c = 12, d = 10, and e = 3,
find the Huffman codes.
Sol. 1. n = 5
2. initial array A = 20, 15, 12, 10, 3
After Build-Min-Heap(A): A = 3, 10, 12, 20, 15
3. i = 1, result: A = 3, 10, 15, 12, 20
A = 3, 10, 12, 15, 20
A = 3, 10, 12, 13, 20, 15
Fuw-Yi Yang
17
Chapter 3 Greedy Method
Huffman code—Example
3. i = 1, result: A = 3, 10, 15, 12, 20
A = 3, 10, 12, 15, 20
A = 3, 10, 12, 13, 20, 15
12, 13, 20, 15
3
10
i = 2, result: A = 3, 10, 12, 13, 15, 20
A = 3, 10, 12, 13, 15, 20
A = 3, 10, 12, 13, 15, 20, 25
Fuw-Yi Yang
18
Chapter 3 Greedy Method
Huffman code—Example
3. i = 2, result: A = 3, 10, 12, 13, 15, 20
A = 3, 10, 12, 13, 15, 20
A = 3, 10, 12, 13, 15, 20, 25
12, 13, 15, 20, 25
3
10
12
13
i = 3, result: A = 3, 10, 12, 13, 15, 20, 25
A = 3, 10, 12, 13, 15, 20, 25
A = 3, 10, 12, 13, 15, 20, 25, 35
Fuw-Yi Yang
19
Chapter 3 Greedy Method
Huffman code—Example
3. i = 3, result: A = 3, 10, 12, 13, 15, 20, 25
A = 3, 10, 12, 13, 15, 20, 25
A = 3, 10, 12, 13, 15, 20, 25, 35
12, 13, 15, 20, 25, 35
3
10
12
13
i = 4, result: A = 3, 10, 12, 13, 15, 20, 25, 35
A = 3, 10, 12, 13, 15, 20, 25, 35
A = 3, 10, 12, 13, 15, 20, 25, 35, 60
Fuw-Yi Yang
15
20
20
Chapter 3 Greedy Method
Huffman code—Example
3. i = 4, result: A = 3, 10, 12, 13, 15, 20, 25, 35
A = 3, 10, 12, 13, 15, 20, 25, 35
A = 3, 10, 12, 13, 15, 20, 25, 35, 60
60
0
1
25
35
00
01
10
12
13
15
010
011
3
10
End of example 3-20
Fuw-Yi Yang
11
20
21
Chapter 3 Greedy Method
Huffman code—Example
Example 3.20-1 Given that a = 45, b = 13, c = 12, d = 16, e = 9, and
f = 5, find the Huffman codes.
Sol. 1. n = 6
2. initial array A = 45, 13, 12, 16, 9, 5
After Build-Min-Heap(A): A = 5, 9, 12, 16, 13, 45
3. i = 1, result: A = 5, 9, 13, 12, 16, 45
A = 5, 9, 12, 13, 45, 16
A = 5, 9, 12, 13, 45, 16, 14
Fuw-Yi Yang
22
Chapter 3 Greedy Method
Huffman code—Example
A = 5, 9, 12, 13, 45, 16, 14
i = 2, result: A = 5, 9, 12, 13, 14, 45, 16
A = 5, 9, 12, 13, 14, 16, 45
A = 5, 9, 12, 13, 14, 16, 45, 25
i = 3, result: A = 5, 9, 12, 13, 14, 16, 25, 45
A = 5, 9, 12, 13, 14, 16, 25, 45
A = 5, 9, 12, 13, 14, 16, 25, 45, 30
Fuw-Yi Yang
23
Chapter 3 Greedy Method
Huffman code—Example
A = 5, 9, 12, 13, 14, 16, 25, 45, 30
i = 4, result: A = 5, 9, 12, 13, 14, 16, 25, 30, 45
A = 5, 9, 12, 13, 14, 16, 25, 30, 45
A = 5, 9, 12, 13, 14, 16, 25, 30, 45, 55
i = 5, result: A = 5, 9, 12, 13, 14, 16, 25, 30, 45, 55
A = 5, 9, 12, 13, 14, 16, 25, 30, 45, 55
A = 5, 9, 12, 13, 14, 16, 25, 30, 45, 55, 100
Fuw-Yi Yang
24
Chapter 3 Greedy Method
Huffman code—Example
100
0
1
45
55
10
11
25
100
12
30
101
110
13
111
14
1100
16
1101
5
9
Fuw-Yi Yang
end of example3.20-1
25
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
Dijkstra’s algorithm solves the single-source shortest-paths
problem on a weighted, directed graph G = (V, E) for the case in
which all edge weights are nonnegative.
That is, w(u, v)  0 for each edge (u, v)  E.
With a good implementation the running time of Dijkstra’s
algorithm is lower than that of the Bellman-Ford algorithm
(O(VE)).
Fuw-Yi Yang
26
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
Dijkstra’s algorithm maintains a set S of vertices whose final
shortest-path weights from the source s have already been
determined.
The algorithm repeatedly selects the vertex u  V – S with the
minimum shortest-path estimate, adds u to S, and relaxes all edges
leaving u. In the following implementation, we use a min-priority
queue Q of vertices, keyed by their d values.
symbols: v.d  the shortest-path estimate from source to v,
v.  the predecessor of v,
w(u, v)  weight of the edge (u, v),
adj[v]  the set of vertices adjacent to vertex v .
Fuw-Yi Yang
27
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
Relax (u, v, w)
1. If v.d > u.d + w(u, v)
2.
v.d = u.d + w(u, v)
3. v. = u
Initialize single source (G, s)
1. For each vertex v  G.V
2.
v.d = 
3.
v. = NIL
4. s.d = 0
Fuw-Yi Yang
28
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
Dijkstra (G, w, s)
// O((E + V) log V)
1. Initialize single source (G, s)
2. S = 
3. Q = G.V
// O(V)
4. While Q  
// O(V)
5.
u = Extract-min(Q) // O(log V)
6.
S = S  {u}
7.
For each vertex v  G.adj[u]
8.
Relax(u, v, w) // O(log V) maintain or heapify Q
Fuw-Yi Yang
29
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
Example Fig 3.5 V = {1, 2, 3, 4, 5, 6, 7}
E = {(1, 2, 15), (1, 3, 2),
(2, 4, 20), (2, 5, 30),
(3, 2, 10), (3, 4, 40), (3, 5, 12),
(4, 6, 4),
(5, 4, 14), (5, 6, 15),
(7, 6, 10)}
1. Initialize single source (G, s)
1.d = , …, 7.d = ; 1. = NIL,…, 7. = NIL
1.d = 0
Fuw-Yi Yang
30
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
2. S = 
1, 2 15
3. Q = G.V
1, 3 2
4. While Q  
5. u = Extract-min(Q) // u = 1 first iteration
6.
S = S  {u}
// S = {1} first iteration
7.
For each vertex v  G.adj[u] // G.adj[1] = {2, 3}
8.
Relax(u, v, w)
2.d =  > 1.d + w(1, 2) = 15; 2.d = 15; 2. = 1
3.d =  > 1.d + w(1, 3) = 2; 3.d = 2; 3. = 1
Note: the value 2.d and 3.d also heapify the Min-Heap Q.
Fuw-Yi Yang
31
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 3 second iteration
6.
S = S  {u}
// S = {1, 3}
7.
For each vertex v  G.adj[u] // G.adj[3] = {2, 4, 5}
8.
Relax(u, v, w)
2.d = 15 > 3.d + w(3, 2) = 12; 2.d = 12; 2. = 3
4.d =  > 3.d + w(3, 4) = 42 ; 4.d = 42; 4. = 3
5.d =  > 3.d + w(3, 5) = 12 ; 5.d = 14; 5. = 3
Note: the value 4.d and 5.d also heapify the Min-Heap Q.
Fuw-Yi Yang
32
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 2 third iteration
6.
S = S  {u}
// S = {1, 3, 2}
7.
For each vertex v  G.adj[u] // G.adj[2] = {4, 5}
8.
Relax(u, v, w)
4.d = 42 > 2.d + w(2, 4) = 32 ; 4.d = 32; 4. = 2
5.d = 14 < 2.d + w(2, 5) = 42 ; 5.d = 14; 5. = 3 unchanged
Note: the value 4.d and 5.d also heapify the Min-Heap Q.
Fuw-Yi Yang
33
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 5 fourth iteration
6.
S = S  {u}
// S = {1, 3, 2, 5}
7.
For each vertex v  G.adj[u] // G.adj[5] = {4, 6}
8.
Relax(u, v, w)
4.d = 32 > 5.d + w(5, 4) = 28 ; 4.d = 28; 4. = 5
6.d =  > 5.d + w(5, 6) = 29 ; 6.d = 29; 6. = 5
Note: the value 4.d and 6.d also heapify the Min-Heap Q.
Fuw-Yi Yang
34
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 4 fifth iteration
6.
S = S  {u}
// S = {1, 3, 2, 5, 4}
7.
For each vertex v  G.adj[u] // G.adj[4] = {6}
8.
Relax(u, v, w)
6.d = 29 < 4.d + w(4, 6) = 32; 6.d = 29; 6. = 5 unchanged
Note: No heapify to the Min-Heap Q.
Fuw-Yi Yang
35
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 6 sixth iteration
6.
S = S  {u}
// S = {1, 3, 2, 5, 4, 6}
7.
For each vertex v  G.adj[u] // G.adj[6] = {}
8.
Relax(u, v, w)
Note: No heapify to the Min-Heap Q.
Fuw-Yi Yang
36
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
4. While Q  
5.
u = Extract-min(Q) // u = 7 seventh iteration
6.
S = S  {u}
// S = {1, 3, 2, 5, 4, 6, 7}
7.
For each vertex v  G.adj[u] // G.adj[6] = {}
8.
Relax(u, v, w)
Note: Min-Heap Q empty.
Fuw-Yi Yang
37
Chapter 3 Greedy Method
Single-source shortest paths
Dijkstra’s algorithm
iteration Vertex
Distance of source(1) to vertex
selected 2
3
4
5
6
7
1
1
15
2




2
3
12
2
42
14


3
2
12
2
32
14


4
5
12
2
28
14
29

5
4
12
2
28
14
29

6
6
12
2
28
14
29

7
7
12
2
28
14
29

Fuw-Yi Yang
38
Chapter 4 Prune-and-Search
The Selection Problem—Algorithm 1
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
39
Chapter 4 Prune-and-Search
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 kth smallest element from S3 during the next iteration.
Fuw-Yi Yang
40
Chapter 4 Prune-and-Search
若將 5 個分為 1 群的策略, 改為 7 個分為 1 群的策略, 或 9 個
分為 1 群的策略, 則Algorithm 1 在最壞情況之下的時間複雜度為
何?
Case 1: each subgroup contains 7 elements
s
s
s
s
s
s
s
s
s
s
s
x
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
Fuw-Yi Yang
41
Chapter 4 Prune-and-Search
若將 5 個分為 1 群的策略, 改為 7 個分為 1 群的策略, 或 9 個
分為 1 群的策略, 則Algorithm 1 在最壞情況之下的時間複雜度為
何?
Case 1: each subgroup contains 7 elements
It follows that the number of elements greater than x is at least
4(1/2 n/7  - 2) ≧ 2n/7 – 8.
Similarly, at least 2n/7 – 8 elements are less than x.
Thus, in the worst case, step 6 calls algorithm recursively on at most
5n/7 + 8 elements.
Fuw-Yi Yang
42
Chapter 4 Prune-and-Search
We make the assumption that any input of fewer than 70 elements
requires O(1) time. We can therefore obtain the recurrence
T(n) ≦ O(1) if n < 70,
T(n) ≦ T(n/7 ) + T(5n/7 + 8 ) + O(n) if n >70.
T(n) ≦ c(n/7 ) + c(5n/7 + 8 ) + an
≦ cn/7 + c + 5cn/7 + 8c + an
= 6cn/7 + 9c + an
= cn + (-cn/7 + 9c + an)  O(n)
Which is at most cn if (-cn/10 + 9c + an) ≦0.
To satisfy the inequality above, n > 70, c ≧70a.
Fuw-Yi Yang
43
Chapter 4 Prune-and-Search
若將 5 個分為 1 群的策略, 改為 7 個分為 1 群的策略, 或 9 個
分為 1 群的策略, 則Algorithm 1 在最壞情況之下的時間複雜度為
何?
Case 2: each subgroup contains 9 elements
It follows that the number of elements greater than x is at least
5(1/2 n/9  - 2) ≧ 5n/18 – 10.
Similarly, at least 5n/18 – 10 elements are less than x.
Thus, in the worst case, step 6 calls algorithm recursively on at most
13n/18 + 10 elements.
Fuw-Yi Yang
44
Chapter 4 Prune-and-Search
We make the assumption that any input of fewer than 70 elements
requires O(1) time. We can therefore obtain the recurrence
T(n) ≦ O(1) if n < 67,
T(n) ≦ T(n/9 ) + T(13n/18 + 10 ) + O(n) if n > 67.
T(n) ≦ c(n/9 ) + c(13n/18 + 10 ) + an
≦ cn/9 + c + 13cn/18 + 10c + an
= 15cn/18 + 10c + an
= cn + (-3cn/18 + 10c + an)
Which is at most cn if (-3cn/18 + 10c + an) ≦0.
To satisfy the inequality above, n > 67, c ≧60a.
Fuw-Yi Yang
45
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
例題 Simple example 在平面上有點集合 S 包含:
(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7) 共 6 個點,
遵循演算法 5.3 一步一步地求出各極點之座標.
Fuw-Yi Yang
46
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
Algorithm 5.3 (S)
Step 1. Processing if n ≦ 3
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
2.2 並將平面點集合S分為 SL和SR
Algorithm 5.3 (S)
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點 Fuw-Yi Yang
47
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
Algorithm 5.3 (S)
Step 1. Processing if n ≦ 3 // skip, n = 6
Step 2. Divide
Algorithm 5.3 (S)
2.1 計算全部點 的 x 座標之中位數
// 第 n/2 = 3 大的 x 座標是 3
2.2 並將平面點集合S分為 SL和SR
SL: (1, 2), (2, 3), (3, 4)
SR: (4, 5), (5, 6), (6, 7)
Fuw-Yi Yang
48
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
SL: (1, 2), (2, 3), (3, 4)
SR: (4, 5), (5, 6), (6, 7)
Step 3. Recursive call Algorithm 5.3
Algorithm 5.3 (S)
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點
Fuw-Yi Yang
49
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
SL: (1, 2), (2, 3), (3, 4)
3.1 Algorithm 5.3 (SL)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SL)
SL的極點 (3, 4)
傳回
Fuw-Yi Yang
50
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
SR: (4, 5), (5, 6), (6, 7)
3.1 Algorithm 5.3 (SR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SR)
SR 的極點 (6, 7)
傳回
Fuw-Yi Yang
51
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Algorithm 5.3 (S)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax //ymax = 7
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點 (i.e. S) //(6, 7)
Fuw-Yi Yang
52
Chapter 5 Divide and Conquer
求取平面上的極點---例題 Simple example
Algorithm
5.3 (S)
Algorithm
5.3 (SL)
Algorithm
5.3 (SR)
End of solution (Simple example)
Fuw-Yi Yang
53
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
例題 5-19 在平面上有點集合 S 包含:
(5, 12), (12, 10), (2, 9), (5, 7), (10, 6),
(9, 4), (6, 3), (11, 4), (1, 3), (6, 1) 共 10 個點,
遵循演算法 5.3 一步一步地求出各極點之座標.
Fuw-Yi Yang
54
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Algorithm 5.3 (S)
Step 1. Processing if n ≦ 3 // skip, n = 10
Step 2. Divide
Algorithm 5.3 (S)
2.1 計算全部點 的 x 座標之中位數
// 第 n/2 = 5 大的 x 座標是 6
2.2 並將平面點集合S分為 SL和SR
SL: (5, 12), (2, 9), (5, 7), (6, 3), (1, 3), (6, 1)
SR: (12, 10), (10, 6), (9, 4), (11, 4)
Fuw-Yi Yang
55
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SL: (5, 12), (2, 9), (5, 7), (6, 3), (1, 3), (6, 1)
SR: (12, 10), (10, 6), (9, 4), (11, 4)
Step 3. Recursive call Algorithm 5.3
Algorithm 5.3 (S)
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點
Fuw-Yi Yang
56
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SL: (5, 12), (2, 9), (5, 7), (6, 3), (1, 3), (6, 1)
3.1 Algorithm 5.3 (SL)
Step 1. Processing if n ≦ 3 // skip, n = 6
Algorithm 5.3 (SL)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 6 / 2 = 3 大的 x 座標是 5
2.2 並將平面點集合SL分為 SLL和SLR
SLL: (5, 12), (2, 9), (5, 7), (1, 3)
SLR: (6, 3), (6, 1)
Fuw-Yi Yang
57
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLL)
3.2 Algorithm 5.3 (SLR)
Algorithm 5.3 (SL)
Step 4. Combine
4.1 求SLR 的極點中最大的 y 座標, ymax
4.2 如果 SLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLL 與 SLR 的極點
Fuw-Yi Yang
58
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SLL: (5, 12), (2, 9), (5, 7), (1, 3)
3.1 Algorithm 5.3 (SLL)
Step 1. Processing if n ≦ 3 // skip, n = 4
Algorithm 5.3 (SLL)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 4 / 2 = 2 大的 x 座標是 2
2.2 並將平面點集合SLL分為 SLLL和SLLR
SLLL: (2, 9), (1, 3)
SLLR: (5, 12), (5, 7)
Fuw-Yi Yang
59
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLLL)
3.2 Algorithm 5.3 (SLLR)
Algorithm 5.3 (SLL)
Step 4. Combine
4.1 求SLLR 的極點中最大的 y 座標, ymax
4.2 如果 SLLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLLL 與 SLLR 的極點
Fuw-Yi Yang
60
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SLLL: (2, 9), (1, 3)
3.1 Algorithm 5.3 (SLLL)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLLL)
SLLL的極點 (2,9)
傳回
Fuw-Yi Yang
61
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SLLR: (5, 12), (5, 7)
3.1 Algorithm 5.3 (SLLR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLLR)
SLLR的極點 (5, 12), (5, 7)
傳回
Fuw-Yi Yang
62
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLLL) //極點 (2,9)
3.2 Algorithm 5.3 (SLLR) //極點 (5, 12), (5, 7)
Step 4. Combine
Algorithm 5.3 (SLL)
4.1 求SLLR 的極點中最大的 y 座標, ymax //ymax = 12
4.2 如果 SLLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLLL 與 SLLR 的極點 (i.e SLL) //(5, 12), (5, 7)
Fuw-Yi Yang
63
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SLR: (6, 3), (6, 1)
3.1 Algorithm 5.3 (SLR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLR)
SLR 的極點 (6, 3), (6, 1)
傳回
Fuw-Yi Yang
64
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLL)
3.2 Algorithm 5.3 (SLR)
Algorithm 5.3 (SL)
Step 4. Combine
4.1 求SLR 的極點中最大的 y 座標, ymax //ymax = 3
4.2 如果 SLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLL 與 SLR 的極點 (i.e SL) //(6, 1), (6, 3), (5, 12), (5, 7),
Fuw-Yi Yang
65
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SR: (12, 10), (10, 6), (9, 4), (11, 4)
3.1 Algorithm 5.3 (SR)
Step 1. Processing if n ≦ 3 // skip, n = 4
Algorithm 5.3 (SR)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 4 / 2 = 2 大的 x 座標是 10
2.2 並將平面點集合SR分為 SRL和SRR
SRL: (10, 6), (9, 4)
SRR: (12, 10), (11, 4)
Fuw-Yi Yang
66
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SRL)
3.2 Algorithm 5.3 (SRR)
Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SRR 的極點中最大的 y 座標, ymax
4.2 如果 SRL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SRL 與 SRR 的極點
Fuw-Yi Yang
67
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SRL: (10, 6), (9, 4)
3.1 Algorithm 5.3 (SRL)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SRL)
SRL 的極點 (10, 6)
傳回
Fuw-Yi Yang
68
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
SRR: (12, 10), (11, 4)
3.1 Algorithm 5.3 (SRR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SRR)
SRR 的極點 (12, 10)
傳回
Fuw-Yi Yang
69
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SRL)
3.2 Algorithm 5.3 (SRR)
Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SRR 的極點中最大的 y 座標, ymax //ymax = 10
4.2 如果 SRL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SRL 與 SRR 的極點 (i.e. SR) //(12, 10)
Fuw-Yi Yang
70
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Algorithm 5.3 (S)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax //ymax = 10
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點 (i.e. S) //(12, 10), (5, 12)
Fuw-Yi Yang
71
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-19
Algorithm
5.3 (S)
Algorithm
5.3 (SL)
Algorithm
5.3 (SLL)
Algorithm
5.3 (SLLL)
Algorithm
5.3 (SLR)
Algorithm
5.3 (SLLR)
Algorithm
5.3 (SR)
Algorithm
5.3 (SRL)
Algorithm
5.3 (SRR)
End of solution (5-19)
Fuw-Yi Yang
72
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
例題 5-20 在平面上有點集合 S 包含:
( 6, 0), ( 11, 3), ( 10, 9), ( 8, 5),
( 9, 7), ( 6, 10), ( 4, 7), ( 5, 4),
( 3, 8), ( 2, 3), (2, 9), (9, 4) 共 12 個點,
遵循演算法 5.3 一步一步地求出各極點之座標.
Fuw-Yi Yang
73
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Algorithm 5.3 (S)
Step 1. Processing if n ≦ 3 // skip, n = 12
Step 2. Divide
Algorithm 5.3 (S)
2.1 計算全部點 的 x 座標之中位數
// 第 n/2 = 6 大的 x 座標是 6
2.2 並將平面點集合S分為 SL和SR
SL: (2,3), (2,9), (3,8), (4,7), (5,4), (6,0), (6,10)
SR: (8,5), (9,4), (9,7), (10,9), (11,3)
Fuw-Yi Yang
74
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SL: (2,3), (2,9), (3,8), (4,7), (5,4), (6,0), (6,10)
SR: (8,5), (9,4), (9,7), (10,9), (11,3)
Step 3. Recursive call Algorithm 5.3
Algorithm 5.3 (S)
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點
Fuw-Yi Yang
75
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SL: (2,3), (2,9), (3,8), (4,7), (5,4), (6,0), (6,10)
3.1 Algorithm 5.3 (SL)
Step 1. Processing if n ≦ 3 // skip, n = 7
Algorithm 5.3 (SL)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 7 / 2 = 4 大的 x 座標是 4
2.2 並將平面點集合SL分為 SLL和SLR
SLL: (2,3), (2,9), (3,8), (4,7)
SLR: (5,4), (6,0), (6,10)
Fuw-Yi Yang
76
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLL)
3.2 Algorithm 5.3 (SLR)
Algorithm 5.3 (SL)
Step 4. Combine
4.1 求SLR 的極點中最大的 y 座標, ymax
4.2 如果 SLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLL 與 SLR 的極點
Fuw-Yi Yang
77
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SLL: (2,3), (2,9), (3,8), (4,7)
3.1 Algorithm 5.3 (SLL)
Step 1. Processing if n ≦ 3 // skip, n = 4
Algorithm 5.3 (SLL)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 4 / 2 = 2 大的 x 座標是 2
2.2 並將平面點集合SLL分為 SLLL和SLLR
SLLL: (2,3), (2,9)
SLLR: (3,8), (4,7)
Fuw-Yi Yang
78
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLLL)
3.2 Algorithm 5.3 (SLLR)
Algorithm 5.3 (SLL)
Step 4. Combine
4.1 求SLLR 的極點中最大的 y 座標, ymax
4.2 如果 SLLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLLL 與 SLLR 的極點
Fuw-Yi Yang
79
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SLLL: (2,3), (2,9)
3.1 Algorithm 5.3 (SLLL)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLLL)
SLLL的極點 (2,3), (2,9)
傳回
Fuw-Yi Yang
80
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SLLR: (3,8), (4,7)
3.1 Algorithm 5.3 (SLLR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLLR)
SLLR的極點 (3,8), (4,7)
傳回
Fuw-Yi Yang
81
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLLL) //極點(2,3), (2,9)
3.2 Algorithm 5.3 (SLLR) //極點(3,8), (4,7)
Step 4. Combine
Algorithm 5.3 (SLL)
4.1 求SLLR 的極點中最大的 y 座標, ymax //ymax = 8
4.2 如果 SLLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLLL 與 SLLR 的極點 (i.e SLL) //(2,9), (3,8), (4,7)
Fuw-Yi Yang
82
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SLR: (5,4), (6,0), (6,10)
3.1 Algorithm 5.3 (SLR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SLR)
SLR 的極點 (6,10)
傳回
Fuw-Yi Yang
83
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SLL)
3.2 Algorithm 5.3 (SLR)
Algorithm 5.3 (SL)
Step 4. Combine
4.1 求SLR 的極點中最大的 y 座標, ymax //ymax = 10
4.2 如果 SLL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SLL 與 SLR 的極點 (i.e SL) //(6,10)
Fuw-Yi Yang
84
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SR: (8,5), (9,4), (9,7), (10,9), (11,3)
3.1 Algorithm 5.3 (SR)
Step 1. Processing if n ≦ 3 // skip, n = 5
Algorithm 5.3 (SR)
Step 2. Divide
2.1 計算全部點 的 x 座標之中位數
// 第 5 / 2 = 3 大的 x 座標是 9
2.2 並將平面點集合SR分為 SRL和SRR
SRL: (8,5), (9,4), (9,7)
SRR: (10,9), (11,3)
Fuw-Yi Yang
85
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SRL)
3.2 Algorithm 5.3 (SRR)
Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SRR 的極點中最大的 y 座標, ymax
4.2 如果 SRL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SRL 與 SRR 的極點
Fuw-Yi Yang
86
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SRL: (8,5), (9,4), (9,7)
3.1 Algorithm 5.3 (SRL)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SRL)
SRL 的極點 (9,4), (9,7)
傳回
Fuw-Yi Yang
87
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
SRR: (10,9), (11,3)
3.1 Algorithm 5.3 (SRR)
Step 1. Processing if n ≦ 3
Algorithm 5.3 (SRR)
SRR 的極點 (10,9), (11,3)
傳回
Fuw-Yi Yang
88
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SRL)
3.2 Algorithm 5.3 (SRR)
Algorithm 5.3 (SR)
Step 4. Combine
4.1 求SRR 的極點中最大的 y 座標, ymax //ymax = 9
4.2 如果 SRL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SRL 與 SRR 的極點 (i.e. SR) //(10,9), (11,3)
Fuw-Yi Yang
89
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Step 3. Recursive call Algorithm 5.3
3.1 Algorithm 5.3 (SL)
3.2 Algorithm 5.3 (SR)
Algorithm 5.3 (S)
Step 4. Combine
4.1 求SR 的極點中最大的 y 座標, ymax //ymax = 9
4.2 如果 SL 中的任何極點, y 座標小於 ymax, 刪除之
4.3 傳回 SL 與 SR 的極點 (i.e. S) //(6, 10), (10, 9), (11, 3)
Fuw-Yi Yang
90
Chapter 5 Divide and Conquer
求取平面上的極點---例題5-20
Algorithm
5.3 (S)
Algorithm
5.3 (SL)
Algorithm
5.3 (SLL)
Algorithm
5.3 (SLLL)
Algorithm
5.3 (SLR)
Algorithm
5.3 (SLLR)
Algorithm
5.3 (SR)
Algorithm
5.3 (SRL)
Algorithm
5.3 (SRR)
End of solution (5-20)
Fuw-Yi Yang
91
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
例題 Simple example 在平面上有點集合 S 包含:
(0, 1), (2, 3), (3, 4), (4, 5), (5, 6), (6, 8) 共 6 個點,
遵循演算法 5.4 一步一步地求出最接近兩點的座標.
Fuw-Yi Yang
92
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 1. Processing if n ≦ 3
Step 2. Pre-processing
2.1 根據 x 座標排序
2.2 根據 y 座標排序
Step 3. Divide
3.1 計算全部點 的 x 座標之中位數
3.2 並將平面點集合S分為 SL和SR
Fuw-Yi Yang
93
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 4. Recursively Call Algorithm 5.4(.)
4.1 Algorithm 5.4 (SL)
4.2 Algorithm 5.4 (SR)
4.3 最短距離  = min((SL), (SR))
Step 5. Combine
5.1 對SL中的每一點 p, 若 L -  < xp 成立則執行:
{ 對SR中的每一點 q,
若 L < xq < L +  and yp -  < yq < yp +  成立則執行:
計算 點 p 和 點 q 的距離, 若小於則更新之.}
5.2 傳回 最短距離 
Fuw-Yi Yang
94
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 1. Processing if n ≦ 3 // Skip, n = 6
Step 2. Pre-processing
Algorithm 5.4 (S)
2.1 根據 x 座標排序
(0, 1), (2, 3), (3, 4), (4, 5), (5, 6), (6, 8)
2.2 根據 y 座標排序
(0, 1), (2, 3), (3, 4), (4, 5), (5, 6), (6, 8)
Fuw-Yi Yang
95
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
2.2 根據 y 座標排序
(0, 1), (2, 3), (3, 4), (4, 5), (5, 6), (6, 8)
Step 3. Divide
3.1 計算全部點 的 x 座標之中位數
Algorithm 5.4 (S)
第 n / 2 = 3 大小的x 座標是 3
3.2 並將平面點集合S分為 SL和SR
SL: (0, 1), (2, 3), (3, 4)
SR: (4, 5), (5, 6), (6, 8)
Fuw-Yi Yang
96
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 4. Recursively Call Algorithm 5.4(.)
4.1 Algorithm 5.4 (SL)
Algorithm 5.4 (SL)
Algorithm 5.4 (S)
4.2 Algorithm 5.4 (SR)
Algorithm 5.4 (SR)
4.3 最短距離  = min((SL), (SR))
Fuw-Yi Yang
97
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 1. Processing if n ≦ 3 //SL: (0, 1), (2, 3), (3, 4)
distance((0, 1), (2, 3)) = 2.88
distance((0, 1), (3, 4)) = 4.2
distance((2, 3), (3, 4)) = 1.44
SL點集合中, 兩點的最短距離 dL = 1.44
傳回 dL
Algorithm 5.4 (SL)
Fuw-Yi Yang
98
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 1. Processing if n ≦ 3 //SR: (4, 5), (5, 6), (6, 8)
distance((4, 5), (5, 6)) = 1.44
distance((4, 5), (6, 8)) = 3.6
distance((5, 6), (6, 8)) = 2.24
SR點集合中, 兩點的最短距離 dR = 1.44
傳回 dR
Algorithm 5.4 (SR)
Fuw-Yi Yang
99
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Step 5. Combine
Algorithm 5.4 (S)
5.1 對SL中的每一點 p, 若 L -  < xp 成立則執行:
//L = 3,  = 1.44, 故滿足條件的點共有二點,
p1 = (2, 3) , p2 = (3, 4)
{ 對SR中的每一點 q,
若 L < xq < L +  and yp -  < yq < yp +  成立則執行:
計算 點 p 和 點 q 的距離, 若小於 則更新之.}
//對 p1 而言, 無滿足條件的點
//對 p2 而言,滿足條件的點共有一點, p3 = (4, 5)
Fuw-Yi Yang
100
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm 5.4 (S)
Algorithm 5.4 (S)
{ 對SR中的每一點 q,
若 L < xq < L +  and yp -  < yq < yp +  成立則執行:
計算 點 p 和 點 q 的距離, 若小於則更新之.}
//滿足條件的點共有一點, p3 = (4, 5),
distance(p2, p3) = 1.44,
最短距離 不需更新
5.2 傳回 最短距離 
 = 1.44
Fuw-Yi Yang
101
Chapter 5 Divide and Conquer
求取平面上最接近的兩點---例題 Simple example
Algorithm
5.4 (S)
Algorithm
5.4 (SL)
Algorithm
5.4 (SR)
End of solution (Simple example)
Fuw-Yi Yang
102