poscat seminar 5

28
POSCAT Seminar 5 : Recursion and Divide & Conquer yougatup @ POSCAT 1

Upload: hyungyu-shin

Post on 12-Jan-2017

50 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Poscat seminar 5

POSCAT Seminar 5 :Recursion and Divide & Conquer

yougatup @ POSCAT

1

Page 2: Poscat seminar 5

Topic

Topic today

−Recursion

• Basic Concept

−Divide & Conquer

• Basic Concept

• Finding Medians

• Closest Pair

• Implementation

POSCAT Seminar 1-22 July 2014

yougatup

By the way, do you solve all the problems ?

Page 3: Poscat seminar 5

You should already know what recursion is !

POSCAT Seminar 1-32 July 2014

yougatup

Page 4: Poscat seminar 5

Divide & Conquer

Problem Solving Paradigm

1. Break the problem into subproblems : smaller instances

2. Solve them recursively

3. Combine the solutions to get the answer to the problem.

Calculation of time complexity is not trivial

− We need Master theorem to calculate it

− Recurrence relation

− Not that important now

POSCAT Seminar 1-42 July 2014

yougatup

Page 5: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

POSCAT Seminar 1-52 July 2014

yougatup

Page 6: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

Naïve approach : Sort it ! O(𝑛 log 𝑛)

POSCAT Seminar 1-62 July 2014

yougatup

Page 7: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

Naïve approach : Sort it ! O(𝑛 log 𝑛)

Divide & Conquer : Choose 𝑣 and split S into 𝑆𝐿, 𝑆𝑣, 𝑆𝑅 !

POSCAT Seminar 1-72 July 2014

yougatup

Page 8: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

Naïve approach : Sort it ! O(𝑛 log 𝑛)

Divide & Conquer : Choose 𝑣 and split S into 𝑆𝐿, 𝑆𝑣, 𝑆𝑅 !

POSCAT Seminar 1-82 July 2014

yougatup

Page 9: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

Naïve approach : Sort it ! O(𝑛 log 𝑛)

Divide & Conquer : Choose 𝑣 and split S into 𝑆𝐿, 𝑆𝑣, 𝑆𝑅 !

We have to choose 𝑣 nicely ! … How ?

POSCAT Seminar 1-92 July 2014

yougatup

Page 10: Poscat seminar 5

Finding Medians and Selection

Problem

Find the k-th smallest element in S, where S is a set of numbers

Naïve approach : Sort it ! O(𝑛 log 𝑛)

Divide & Conquer : Choose 𝑣 and split S into 𝑆𝐿, 𝑆𝑣, 𝑆𝑅 !

We have to choose 𝑣 nicely ! … How ?

POSCAT Seminar 1-102 July 2014

yougatup

Randomly !

Page 11: Poscat seminar 5

Finding Medians and Selection

Problem

If we’re lucky, then the partition goes to small very fast. If not,

It will take O(𝑛2). Can we trust random choice of 𝑣 ?

POSCAT Seminar 1-112 July 2014

yougatup

Page 12: Poscat seminar 5

Finding Medians and Selection

Problem

If we’re lucky, then the partition goes to small very fast. If not,

It will take O(𝑛2). Can we trust random choice of 𝑣 ?

Yes! because the worst case is extremely unlike to occur !

There are 50% chance of being good, that is, the Sublists 𝑆𝐿 and

𝑆𝑅 have size at most ¾ of that of S.

POSCAT Seminar 1-122 July 2014

yougatup

Page 13: Poscat seminar 5

Finding Medians and Selection

Problem

If we’re lucky, then the partition goes to small very fast. If not,

It will take O(𝑛2). Can we trust random choice of 𝑣 ?

Therefore, the expected running time T(𝑛) is

T(𝑛) ≤ T(3𝑛/4) + O(𝑐 ⋅ 𝑛)

where 𝑐 is the average number of consecutive splits to find a

good split. Clearly 𝑐 = 2. So we can conclude that T(𝑛) = O(𝑛)

POSCAT Seminar 1-132 July 2014

yougatup

Page 14: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

POSCAT Seminar 1-142 July 2014

yougatup

Page 15: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Brute force approach : Check all pairs of points. O(𝑛2)

POSCAT Seminar 1-152 July 2014

yougatup

Page 16: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Divide & Conquer approach

1. Split points into two groups

2. Find closest pair of points for each group

3. Combine the solutions !

POSCAT Seminar 1-162 July 2014

yougatup

Page 17: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

1. Split points into two group

POSCAT Seminar 1-172 July 2014

yougatup

Page 18: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

1. Split points into two group

POSCAT Seminar 1-182 July 2014

yougatup

Page 19: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

2. Find closest pair of points for each group

POSCAT Seminar 1-192 July 2014

yougatup

Page 20: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

3. Combine the solutions ?

POSCAT Seminar 1-202 July 2014

yougatup

Page 21: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

3. Combine the solutions ?

POSCAT Seminar 1-212 July 2014

yougatup

Is it enough to choose small one between two solutions ?

Page 22: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

3. Combine the solutions ?

POSCAT Seminar 1-222 July 2014

yougatup

Is it enough to choose small one between two solutions ?

We didn’t consider it

Page 23: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

3. Combine the solutions ?

POSCAT Seminar 1-232 July 2014

yougatup

Then, do we have to consider all the caseswhen a pair consists of a point in the leftand a point in the right ?

Page 24: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

3. Combine the solutions ?

POSCAT Seminar 1-242 July 2014

yougatup

Then, do we have to consider all the caseswhen a pair consists of a point in the leftand a point in the right ?No! because we have partial solution !

Page 25: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Idea : If a closer pair is exist, then it must be in the grey region

POSCAT Seminar 1-252 July 2014

yougatup

𝛿

𝛿 𝛿

Page 26: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Idea : If a closer pair is exist, then it must be in the grey region

POSCAT Seminar 1-262 July 2014

yougatup

𝛿

𝛿 𝛿 Still, worst case can happen

Page 27: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Claim. If 𝑖 − 𝑗 ≥ 12, then the distance

between 𝑝𝑖 and 𝑝𝑗 is at least 𝛿

• No two points lie in same 𝛿/2-by-𝛿/2 box.

• Two points at least 2 rows apart

have distance ≥ 2(𝛿/2)

Therefore, it is sufficient to consider 12 points

above me !

POSCAT Seminar 1-272 July 2014

yougatup

Page 28: Poscat seminar 5

Closest pair of points

Problem

Given 𝑛 points in the plane, find a pair with smallest Euclidean

distance between them.

Claim. If 𝑖 − 𝑗 ≥ 12, then the distance

between 𝑝𝑖 and 𝑝𝑗 is at least 𝛿

• No two points lie in same 𝛿/2-by-𝛿/2 box.

• Two points at least 2 rows apart

have distance ≥ 2(𝛿/2)

Therefore, it is sufficient to consider 12 points

above me ! ∴ T(𝑛) = 2T(𝑛/2) + O(𝑛) = O(𝒏 𝐥𝐨𝐠𝒏)

POSCAT Seminar 1-282 July 2014

yougatup