15
Algorithms – Divide and Conquer II Divide and Conquer II Design and Analysis of Algorithms Andrei Bulatov

Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Embed Size (px)

Citation preview

Page 1: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II

Divide and Conquer II

Design and Analysis of Algorithms

Andrei Bulatov

Page 2: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-2

Closest Pair: The Problem

The Closest Pair Problem

Instance:

n points in the plane

Objective:

Find a pair of points that are closest together

min

Page 3: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-3

Algorithm Idea

We have an algorithm that runs in O( ) time

Divide and conquer:

Split the set of points into two halves

Find closest pairs in the halves

Check if there is a closer pair crossing the border

min

2n

min

Page 4: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-4

Assumptions and Constructions

Assumption:

No two points have the same x-coordinate or the same y-coordinate

Let P be the set of points.

We create lists and of points sorted by the x- and

y-coordinate, respectively

This can be done using the same divide and conquer process, sorting,

and then merging the two halves.

Let Q and R be the two halves,

and the halves ordered with respect to the x- and

y-coordinates

Let and be the closest pairs from the two halves

xP yP

yx QQ , yx RR ,

*1

*0 ,qq *

1*0 ,rr

Page 5: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-5

Crossing the Border

Let δ be the minimum of and

Lemma

If there exist q ∈ Q and r ∈ R such that d(q,r) < δ, then each of

q and r lies within a distance δ of L

min

min

L

),( *1

*0 qqd ),( *

1*0 rrd

δδL is given by x = x*

Page 6: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-6

Crossing the Border (cntd)

Proof

Suppose q and r exist, say, and

We have

Then

and

so each of q and r has an x-coordinate within δ of x* and hence

lies within distance δ of the line L

QED

),( yx qqq =

δ<≤−≤− ),(* rqdqrqx xxx

),( yx rrr =

xx rxq ≤≤ *

δ<≤−≤− ),(* rqdqrxr xxx

Page 7: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-7

Crossing the Border (cntd)

Let S denote the set of points belonging to the band of width δ

around L

Let also be the set S sorted by increasing y-coordinate

Lemma

There exist q ∈ Q and r ∈ R for which d(q,r) < δ if and only if there

exist s,s’ ∈ S for which d(s,s’) < δ

yS

Page 8: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-8

Crossing the Border (cntd)

Lemma

If s,s’ ∈ S have the property that d(s,s’) < δ, then s and s’ are

within 15 positions of each other in the sorted list yS

Lδ/2

δ/2

boxes

Page 9: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-9

Crossing the Border (cntd)

Proof

Denote by Z the band of width δ around L

We partition Z into boxes: squares with side δ/2

No two points belong to the same box, as it contradicts the

assumption that minimal distance between two points on the same

side of L is δ

Since the distance between s, s’ is less then δ they also cannot

be more than 2 boxes apart vertically

QED

Page 10: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-10

Algorithm

Closest-Pair(P)

construct and /*In O(n log n) time

set :=Closest-Pair-Rec( )xP yP

yx PP ,),( *1

*0 pp

Page 11: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-11

Algorithm

Closest-Pair-Rec( )

if |P|≤3 then use brute force

construct /*O(n) time

:=Closest-Pair-Rec( )

:=Closest-Pair-Rec( )

set δ:=

set x*:=max x-coord. of points in Q, L:={x=x*}

set S:={points in P within distance δ from L}

construct

for each s∈S compute dist. to next 15 points in

if s,s’ is the pair achieving the minimum and d(s,s’)<δ return (s,s’)

else if then return

else return

yxyx RRQQ ,,,

yx PP ,

),( *1

*0 qq

),( *1

*0 rr

yx QQ ,

yx RR ,

),(),( *1

*0

*1

*0 rrdqqd < ),( *

1*0 qq

),( *1

*0 rr

)},(),,(min{ *1

*0

*1

*0 rrdqqd

yS

yS

Page 12: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-12

Closest Pair: Analysis

Theorem

The Closest-Pair algorithm outputs a closest pair of points in P

Theorem

The Closest-Pair algorithm runs in O(n log n) time for a list of n

elements

Page 13: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-13

Integer Multiplication

How much time do we really need to multiply two numbers?

Standard algorithm

1101

1011

1101

1101

1101

10001111

takes O( ) time2n

Page 14: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-14

Divide-and-Conquer Algorithm

Let x and y be given numbers, n digits each.

Represent them as:

Then

We need to compute 4 smaller products

Does it help? Let T(n) be the running time

By the Master Theorem

02/

102/

1 2 ,2 yyyxxxnn

+⋅=+⋅=

002/

100111

02/

102/

1

2)(2

)2)(2(

yxyxyxyx

yyxxxy

nn

nn

+⋅++⋅=

+⋅+⋅=

CnnTnT +≤ )2/(4)(

)()()( 24lognOnOnT =≤

Page 15: Divide and Conquer II - cs.sfu.ca · PDF fileAlgorithms – Divide and Conquer II 6-3 Algorithm Idea We have an algorithm that runs in O( ) time Divide and conquer: Split the set of

Algorithms – Divide and Conquer II 6-15

Divide-and-Conquer Algorithm (cntd)

Reduce the number of recursive calls

Compute and then

Does it help?

Now we need 3 recursive calls:

Thus

By the Master Theorem

002/

100111 2)(2 yxyxyxyxxynn

+⋅++⋅=

CnnTnT +≤ )2/(3)(

)()()( 59.13lognOnOnT =≤

))(( 0101 yyxx ++

001101011001 ))(( yxyxyyxxyxyx −−++=+

00110101 , ),)(( yxyxyyxx ++