You are on page 1of 7

Lecture 2 Divide and Conquer 6.

046J Spring 2015

Lecture 2: Divide and Conquer


• Paradigm ✓
• Convex Hull

• Median finding ?
1 to d!v!de
>
b Should be d!l!de
If / + no
!t smaller problems .

Paradigm Ff <1 -1
b!ggerproblqsjlog!c.es !o

Given a problem of size n divide it into subproblems of size nb , a ≥ 1, b > 1. Solve each
subproblem recursively. Combine solutions of subproblems to get overall solution.
n format of the
T (n) = aT ( ) + [work for merge]
b & Conquer
runn!ng t!merecwrencewo!eforomb.me d!v!de
dışbükey örtü ? for problem Can
a

Convex Hull • e- s!ze n


problems ,
we

use Masters
Given n points in plane

S = {(xi , yi )|i = 1, 2, . . . , n}
Theo-II
assume no two have same x coordinate, no two have same y coordinate, and no
three in a line for convenience.
Convex Hull ( CH(S) ): smallest polygon containing all points in S.

q bü"üs"n" Kenar olarak



Ç!zd!ğ!m!z şekl"n dışında çünkü en
olmadık
h!çb!r nokta olmayacak .

v r
p u Küçük Polygon
'u !st!yoruz .

→ Heps! ya !ç!nde yada


kenarı olacak
Şekl"n b!r .

t s

CH(S) represented by the sequence of points on the boundary in order clockwise


as doubly linked list.

1 \
Lecture 2 Divide and Conquer 6.046J Spring 2015

representat"on
p q r s t
of Convex
nağmes"
Brute force for Convex Hull
bul

Test each line segment to see if it makes up an edge of the convex hull

• If the rest of the points are on one side of the segment, the segment is on the
convex hull. ntane nokta Herb!r!
^
ç!zg! çek!nce
.

A- 1) tane edge oluşturur


• else the segment is not.
n.tn 1) !!. nwn!ch
-

=
Kalan noktalar
O(n2 ) edges, O(n) tests ⇒ O(n3 ) complexity
Olr!edpes .

d!ğer yarıdaysa

.

Can we do better?
Segment!son
-

Herb!r egde'!n

Divide and Conquer Convex Hull convexhull olup



theconuelhdl
olmadığına bakmak
Sort points by x coord (once and for all, O(n log n))
edge sayısı kadar
!şlem gerekt!r .

For input set S of points: 'e denk gelmekted!r


Bu da OCNI
.

:( A- 1) + ( n -4tl
3) ± -

edgesay.!s
.

Çünkü
• Divide into left half A and right half B by x coords ocn )
= an -
e =

• Compute CH(A) and CH(B)

• Combine CH’s of two halves (merge step)

car"#edockw"se
How to Merge?

v7
A B

a4 b2
,bD 'e baktık .ve eled!k
^
a5 laı çekt!ğ!n!zde
çünkü ç!zg!
-
a1
- -
- -
_

lyarıda
-
-

a2 b1 tüm noktalar
a3 | L
| b3
toplanmadı .

h!ghest lowest
✗ value ✗ vek!l
max"mum
• Find upper tangent (ai , bj ). In example, (a4 , b2 ) is U.T. yc" ,
• Find lower tangent (ak , bm ). In example, (a3 , b3 ) is L.T.
ylıçm) m!n!mum

2
Lecture 2 Divide and Conquer 6.046J Spring 2015

• Cut and paste in time Θ(n).

First link ai to bj , go down b ilst till you see bm and link bm to ak , continue along
the a list until you return to ai . In the example, this gives (a4 , b2 , b3 , a3 ).

Finding Tangents
Assume ai maximizes x within CH(A) (a1 , a2 , . . . , ap ). b1 minimizes x within CH(B)
-

(b1 , b2 , . . . , bq )
L is the vertical line separating A and B. Define y(i, j) as y-coordinate of inter-
section between L and segment (ai , bj ).
Claim: (ai , bj ) is uppertangent iff it maximizes y(i, j).
If y(i, j) is not maximum, there will be points on both sides of (ai , bj ) and it Ç!zg! çek!p
cannot be a tangent. - test
2
Algorithm: Obvious O(n ) algorithm looks at all ai , bj pairs. T (n) = 2T (n/2) + ederek
-
Θ(n2 ) = Θ(n2 ). Hera! varolan tumbler !le kontrol ed!l!r .

→ çünkü problemde

ğü¥ÎİÜ
,

tüma! → yaklaşık n / 2

t!mb!-uauos.vn/znokta!ar!d!u!te1atkuaklaflkyarlS7
1 i=1
2 j=1
olacak Şek"lde
3 while (y(i, j + 1) > y(i, j) or y(i − 1, j) > y(i, j))
4 if (y(i, j + 1) > y(i, j)) ! move right finger clockwise

}
:÷÷÷÷¥÷→
5 j = j + 1( mod q) *
6 else
¥ 7 i = i − 1( mod p) ! move left finger anti-clockwise
8 return (ai , bj ) as upper tangent

Similarly for lower tangent.


juob-problemsngg!auwmasters-heoronk-n-a.IT?)+nC
n
T (n) = 2T ( ) + Θ(n) = Θ(n log n) =L
2 -
-2
a- Iogpa
--
1%1=1
dünür MEE b
Intuition for why Merge works b- 2

Ün"te )=O(n%gn)
Her ← G- 1 tn

Cöpü • •
ap-1 b4 Tınzoç!ıagnl -0GBP
-

% b3
b2


ap

b1
*
a1 bq bq-1
a2
o

3
Lecture 2 Divide and Conquer 6.046J Spring 2015

kodun açıklaması
a1 , b1 are right most and left most points. We move anti clockwise from a1 ,
clockwise from b1 . a1 , a2 , . . . , aq is a convex hull, as is b1 , b2 , . . . , bq . If ai , bj is such
that moving from either ai or bj decreases y(i, j) there are no points above the (ai , bj )
line. Olnlogn)
The formal proof is quite involved and won’t be covered.

Median Finding
Given set of n numbers, define rank(x) as number of numbers in the set that are ≤ x.
Find element of rank % n+1
2
& (lower median) and ' n+1
2
( (upper median).
Clearly, sorting works in time Θ(n log n).
Can we do better?

B x C
k ! 1 elements h ! k elements

Select(S, i)
*
*
B × L

1
2
Pick x ∈ S ! cleverly
Compute k = rank(x)
0123 4 ⑤67891011
3 B = {y ∈ S|y < x} sub Problems
4 C = {y ∈ S|y > x} >
5 if k = i !. 5
6 return x
7 else if k > i IEU
8 return Select(B, i)
9 else if k < i İndexk
10 return Select(C, i − k) 5- seçerken C 'n!n İ" oluyor ✗ 4.

!ç!n
Picking x Cleverly
olduğu
Need to pick x so rank(x) is not extreme.

• Arrange S into columns of size 5 (' n5 ( cols)

• Sort each column (bigger elements on top) (linear time)

• Find “median of medians” as x

4
Lecture 2 Divide and Conquer 6.046J Spring 2015

>x
larger

medians x
smaller

<x
notcomect
!bel!ve
:)
How many elements are guaranteed to be > x? →
n
Half of the ' 5 ( groups contribute at least 3 elements > x except for 1 group with
less than 5 elements and 1 group that contains x.
n n
At lease 3(' 10 ( − 2) elements are > x, and at least 3(' 10 ( − 2) elements are < x
Recurrence: d!scard
! somerandom
O(1), ?_? -6 for n ≤ 140 →
T (n) = =
large (1)
T (' n5 () + T ( 710n + 6), Θ(n), for n > 140 number

Solving the Recurrence t-!nd!ngmed!anofmed!onssortn.gr ofatcdomns


Master theorem does not apply. Intuition n5 + 710n < n.
Prove T (n) ≤ cn by induction, for some large enough c.
True for n ≤ 140 by choosing large c

n 7n
T (n) ≤ c' ( + c( + 6) + an (2)
5 10
cn 7nc
≤ +c+ + 6c + an (3)
5 10
cn
= cn + (− + 7c + an) (4)
10
70c
If c ≥ n
+ 10a, we are done. This is true for n ≥ 140 and c ≥ 20a.

5
Lecture 2 Divide and Conquer 6.046J Spring 2015

Appendix 1
Example

b1 b2

a4 b4 b3
a3

a2 a1 L

a3 , b1 is upper tangent. a4 > a3 , b2 > b1 in terms of Y coordinates.


a1 , b3 is lower tangent, a2 < a1 , b4 < b3 in terms of Y coordinates.
ai , bj is an upper tangent. Does not mean that ai or bj is the highest point.
Similarly, for lower tangent.

You might also like