the divide-and- conquer strategy 5-1 the 2-dimensional maxima finding problem 5-2 the closest pair...

35
The Divide-and- The Divide-and- Conquer Strategy Conquer Strategy 5-1 The 2-Dimensional maxima 5-1 The 2-Dimensional maxima finding problem finding problem 5-2 The Closest pair problem 5-2 The Closest pair problem 5-3 The Convex hull problem 5-3 The Convex hull problem 指指指指指指指指徐徐徐 徐徐徐 指指 指指 指指指 指指 指指指 指指

Upload: vanessa-elinor-wilcox

Post on 16-Dec-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

The Divide-and-Conquer The Divide-and-Conquer StrategyStrategy

5-1 The 2-Dimensional maxima finding problem5-1 The 2-Dimensional maxima finding problem5-2 The Closest pair problem5-2 The Closest pair problem5-3 The Convex hull problem5-3 The Convex hull problem

指導教授:指導教授:徐熊健徐熊健 教授 教授報告者:陳琨報告者:陳琨

Page 2: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

22

Divide-and-ConquerDivide-and-Conquer

精神 : 分裂並各個擊破

什麼樣的問題適用 ? 一個問題如果能切割成兩個獨立的小問題,且解小問題與解

原始問題是一樣一樣的,差別只在於 size 的大小時

Page 3: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

33

A General Divide-and-Conquer A General Divide-and-Conquer AlgorithmAlgorithm

Step 1Step 1: If the problem size is small, solve this : If the problem size is small, solve this problem directly; otherwise, split the problem directly; otherwise, split the original problem into 2 sub-problems original problem into 2 sub-problems with equal sizes.with equal sizes.

Step 2Step 2: Recursively solve these 2 sub-problems : Recursively solve these 2 sub-problems by applying this algorithm.by applying this algorithm.

Step 3Step 3: Merge the solutions of the 2 sub-: Merge the solutions of the 2 sub-problems into a solution of the original problems into a solution of the original problem.problem.

Page 4: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

44

A General Divide-and-Conquer A General Divide-and-Conquer IIllustration - 1llustration - 1

Step 1Step 1

直接解

If sma

ll

else

Page 5: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

55

A General Divide-and-Conquer A General Divide-and-Conquer IIllustration - 2llustration - 2

Step 2Step 2

Page 6: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

66

Step 3Step 3

A General Divide-and-Conquer A General Divide-and-Conquer IIllustration - 3llustration - 3

Page 7: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

77

Time Complexity of the General Time Complexity of the General AlgorithmAlgorithm

Time Complexity

2 ( ) ( ) ( ) ,( ) 2

,

nT S n M n n c

T nb n c

time of splitting

time of merging

constant

Page 8: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

88

A Simple ExampleA Simple Example

29 14 15 01 06 10 32 12

Finding the maximum

29 14 15 01 06 10 32 12

29 14 15 01 06 10 32 12

Page 9: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

99

A Simple Example A Simple Example (cont.)(cont.)

29 32

Finding the maximum

29

32

29 14

15

15 01

10

06

32

32 1210

Page 10: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1010

Time ComplexityTime Complexity

Time Complexity 2 ( ) 1 , 2

( ) 21 , 2

nT n

T nn

T(n) = 2T(n/2)+1

Assume n = 2k

= 2(2T(n/4)+1)+1

= 2k-1 = n-1

= 4T(n/4)+2+1

= 2k-1T(2)+2k-2+…+4+2+1= 2k-1+2k-2+…+4+2+1

T(n/2) = 2T[(n/2)/2]+1

22

kn

x x

12kx

Page 11: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1111

5-1 The 2-Dimesional Maxima 5-1 The 2-Dimesional Maxima Finding ProblemFinding Problem

何謂 Dominates ? A point (x1, y1) dominates (x2, y2) if x1 > x2 and

y1 > y2.

X

Y

B

A

A dominates B

Page 12: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1212

A point is called a maxima if no other point dominates it.

何謂 Maxima ?

5-1 The 2-Dimesional Maxima 5-1 The 2-Dimesional Maxima Finding Problem Finding Problem (cont.)(cont.)

X

Y兩兩比較

Time Complexity : O(n2)

Page 13: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1313

Maxima Finding AlgorithmMaxima Finding Algorithm

Input:Input: A set S of n planar points.A set S of n planar points.

Output:Output: The maximal points of S.The maximal points of S.

Step 1:Step 1: If S contains only one point, return it as the maxiIf S contains only one point, return it as the maxima. Otherwise, find a line L perpendicular to the ma. Otherwise, find a line L perpendicular to the X-axis which separates S into SX-axis which separates S into SLLand Sand SRR, with equ, with equ

al sizes.al sizes.

Step 2:Step 2: Recursively find the maximal points of SRecursively find the maximal points of SLL and S and SRR . .

Step 3:Step 3: Find the largest y-value of SR, denoted as yFind the largest y-value of SR, denoted as yRR. Dis. Dis

card each of the maximal points of SL if its y-valucard each of the maximal points of SL if its y-value is less than ye is less than yRR..

Page 14: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1414

Maxima Finding Algorithm Maxima Finding Algorithm Illustration - 1Illustration - 1

Step 1Step 1

maxima

If only one point

else

X

Y Y

X

L

SL SR

Time Complexity : O(n)

Page 15: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1515

Maxima Finding Algorithm Maxima Finding Algorithm Illustration - 2Illustration - 2

Step 2Step 2

Y

X

SL SR

L L

Time Complexity : 2T(n/2)

Page 16: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1616

Maxima Finding Algorithm Maxima Finding Algorithm Illustration - 3Illustration - 3

Step 3Step 3Time Complexity :

X

Y LSL SR => O(n) (presorting)

=> O(nlogn)

Page 17: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1717

Maxima Finding Time complexityMaxima Finding Time complexity

Step 1: O(n)

2 ( ) ( ) ( log ) , 1( ) 2

1 , 1

nT O n O n n n

T nn

Step 2: 2T(n/2)

Step 3: O(nlogn)

Assume n = 2k

2

2

( ) ( log ) ( log )

( log )

T n O n n O n n

O n n

Time complexity: T(n)

Page 18: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1818

Maxima Finding Time complexity Maxima Finding Time complexity (cont.)(cont.)

Step 1: O(n)

2 ( ) ( ) ( ) , 1( ) 2

1 , 1

nT O n O n n

T nn

Step 2: 2T(n/2)

Step 3: O(n)

Assume n = 2k

( ) ( log )T n O n n

After presorting Time complexity: O(nlogn)+T(n)

Time complexity = O(nlogn)+T(n) = O(nlogn)+O(nlogn) = O(nlogn)

Page 19: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

1919

5-2 The Closest Pair Problem5-2 The Closest Pair Problem

何謂 Closest Pair ? Given a set S of n points, find a pair of points which are cl

osest together( 在一個有 n 個點的集合 S 中,找最靠近的兩個點 )

1-D version:1-D version:

X

Time Complexity : O(nlogn)

2-D version:2-D version:

X

Y

Page 20: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2020

Closest Pair AlgorithmClosest Pair Algorithm

Input:Input: A set S of n planar points.A set S of n planar points.

Output:Output: The distance between two closest points. The distance between two closest points.

Step 1:Step 1: Sort points in S according to their y-values.Sort points in S according to their y-values.

Step 2:Step 2: If S contains only one point, return infinity as its distIf S contains only one point, return infinity as its distance.ance.

Step 3:Step 3: Find a median line L perpendicular to the X-axis to Find a median line L perpendicular to the X-axis to divide S into Sdivide S into SLL and S and SRR, with equal sizes., with equal sizes.

Step 4:Step 4: Recursively apply Steps 2 and 3 to solve the closesRecursively apply Steps 2 and 3 to solve the closest pair problems of St pair problems of SLL and S and SRR. Let d. Let dLL(d(dRR) denote the ) denote the

distance between the closest pair in Sdistance between the closest pair in SLL (S (SRR). Let d ). Let d

= min(d= min(dLL, d, dRR).).

Page 21: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2121

Y

X

Closest Pair Algorithm Closest Pair Algorithm Illustration - 1Illustration - 1

Step 2Step 2

d = ∞

If only one point

X

Y L

SL SR

Time Complexity : O(n)

Step 1Step 1 先針對每個點 X 軸的值和 Y 軸的值做排序

Step 3Step 3

Page 22: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2222

Closet Pair Algorithm Closet Pair Algorithm Illustration - 2Illustration - 2

Step 4Step 4

SL SR

L L

Time Complexity : 2T(n/2)

Y

X

Page 23: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2323

dR

Closet Pair Algorithm Closet Pair Algorithm Illustration - 3Illustration - 3

Step 5Step 5

SL SR

LY

X

dL

d=min(dL,dR)L-d L+d

Page 24: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2424

Closet Pair Algorithm Closet Pair Algorithm Illustration - 4Illustration - 4

Y

X

P

d

2d

Page 25: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2525

Closest Pair Time complexityClosest Pair Time complexity

Step 1: O(nlogn)

2 ( ) ( ) ( ) , 1( ) 2

1 , 1

nT O n O n n

T nn

Step 2~5: T(n) = 2T(n/2)+S(n)+M(n)

( ) ( log )T n O n n

Time complexity:

Total time-complexity = O(nlogn)+T(n) = O(nlogn)+O(nlogn) = O(nlogn)

Page 26: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2626

5-3 The Convex Hull Problem5-3 The Convex Hull Problem

Concave polygon ( 凹多邊形 ):

Convex polygon :

何謂 Convex polygon ( 凸多邊形 ) ?

Page 27: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2727

5-3 The Convex Hull Problem 5-3 The Convex Hull Problem (Cont.)(Cont.)

何謂 Convex Hull ?

The Convex hull of a set of planar points is defined as the smallest convex polygon containing all of the points.( 在平面上的一組點,用最小面積的凸多邊形將所有點包起來 )

Page 28: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2828

Convex Hull AlgorithmConvex Hull Algorithm

Input Input :: A set S of planar pointsA set S of planar pointsOutputOutput : : A convex hull for SA convex hull for SStep 1:Step 1: If S contains no more than five points, use exhaustiIf S contains no more than five points, use exhausti

ve searching to find the convex hull and return.ve searching to find the convex hull and return.Step 2:Step 2: Find a median line perpendicular to the X-axis whicFind a median line perpendicular to the X-axis whic

h divides S into Sh divides S into SLL and S and SRR, with equal sizes., with equal sizes.

Step 3:Step 3: Recursively construct convex hulls for SRecursively construct convex hulls for SLL and S and SRR, d, denoted as Hull(Senoted as Hull(SLL) and Hull(S) and Hull(SRR), respectively.), respectively.

Step 4:Step 4: Apply the merging procedure to merge Hull(SApply the merging procedure to merge Hull(SLL) and ) and Hull(SHull(SRR) together to form a convex hull.) together to form a convex hull.

Page 29: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

2929

Use Divide-and-Conquer to Use Divide-and-Conquer to SolveSolve

Y

X

SLSR

L

Use Graham scan

Page 30: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3030

Graham Scan AlgorithmGraham Scan Algorithm

Step 1:Step 1: Select an interior point as the origin.Select an interior point as the origin.

Step 2:Step 2: Each other point forms a polar angle, Each other point forms a polar angle, and all points sorted by polar anglesand all points sorted by polar angles

Step 3:Step 3: Examines the points cause reflexive Examines the points cause reflexive anglesangles

Step 4:Step 4: The remaining points are convex hull The remaining points are convex hull verticesvertices

Page 31: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3131

The Graham scan (Illustration)The Graham scan (Illustration)

Y

X

P P0

P1

P2

P3

P4

P5

Page 32: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3232

Eliminates points (Illustration)Eliminates points (Illustration)

Y

X

P2

P3

P4

P5

P0

P1

令 P0,P1,P2 座標各為 (x0,y0) (x1,y1) (x2,y2)

0 0

1 1

2 2

1

det 1

1

x y

x y

x y

If det < 0 than 逆時針If det > 0 than 順時針If det = 0 than 三點共線

0 1 1 2 2 0 2 1 1 0 0 2det x y x y x y x y x y x y

Page 33: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3333

Use Divide-and-Conquer to Use Divide-and-Conquer to SolveSolve

Y

X

SLSR

L

jj

kk

gghh

aa bb

cc

dd

ee

ff

ii

pp

找小於 π/2 最大的

找大於 3π/2 最小的

Page 34: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3434

Use Divide-and-Conquer to Use Divide-and-Conquer to SolveSolve

Y

X

SL

L

jj

kk

gghh

aa bb

cc

dd

ee

ff

ii

SR

Time Complexity :

TT((nn) = 2) = 2TT((nn/2) + /2) + OO((nn))

= = OO((nnloglognn))

Page 35: The Divide-and- Conquer Strategy 5-1 The 2-Dimensional maxima finding problem 5-2 The Closest pair problem 5-3 The Convex hull problem 指導教授:徐熊健 教授 報告者:陳琨

3535

The End