02 greedy, d&c, binary search

80

description

 

Transcript of 02 greedy, d&c, binary search

Page 1: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy, Divide and Conquer

League of Programmers

ACA, IIT Kanpur

October 22, 2012

League of Programmers Greedy, Divide and Conquer

Page 2: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Outline

1 Greedy Algorithms

2 Divide and Conquer

3 Binary Search

4 Problems

League of Programmers Greedy, Divide and Conquer

Page 3: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 4: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 5: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 6: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning ahead

ignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 7: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)

never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 8: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 9: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 10: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Greedy Algorithms

Greedy algorithms are generally used in optimization problems

There is an optimal substructure to the problem

Greedy Algorithms are algorithms that

try to maximize the function by making greedy choice at allpoints. In other words, we do at each step what seems bestwithout planning aheadignores future possibilities (no planning ahead)never changes choice (no backtracking)

Usually O(n) or O(n lg n) Solutions.

But, We must be able to prove the correctness of a greedyalgorithm if we are to be sure that it works

League of Programmers Greedy, Divide and Conquer

Page 11: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Problem 1:

A family goes for picnic along a straight road. There are k hotels intheir way situated at distances d1,d2 . . . dk from the start. The lasthotel is their destination. The can travel at most L length in day.However, they need to stay at a hotel at night. Find a strategy forthe family so that they can reach hotel k in minimum number ofdays.

League of Programmers Greedy, Divide and Conquer

Page 12: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Solution:

The �rst solution that comes to mind is to travel on a particularday as long as possible, i.e. do not stop at a hotel i� the next hotelcan be reached on the same day.

League of Programmers Greedy, Divide and Conquer

Page 13: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Proof:

Proof strategy 1 for greedy algorithms:

Greedy solution stays ahead

Suppose there is a better solution which stops at hotels i1 < i2. . . ir and greedy solution stops at j1 < j2 . . . js and r < s.

Consider the �rst place where the two solutions di�er and letthat index be p. Clearly ip < jp

On a given day when non greedy solution starts from hoteliq<jq, non greedy solution cannot end up ahead of greedysolution.

Therefore Greedy solution stays ahead

League of Programmers Greedy, Divide and Conquer

Page 14: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Problem 2:

There are n processes that need to be executed. Process i takes apreprocessing time pi and time ti to execute afterwards. Allpreprocessing has to be done on a supercomputer and executioncan be done on normal computers. We have only onesupercomputer and n normal computers. Find a sequence in whichto execute the processes such that the time taken to �nish all theprocesses is minimum.

League of Programmers Greedy, Divide and Conquer

Page 15: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Solution:

Sort in descending order of ti and execute them. At least this is thesolution for which no counterexample is available.

League of Programmers Greedy, Divide and Conquer

Page 16: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Proof:

Let there be any other ordering σ(1),σ(2). . .σ(n) such thattσ(i) < tσ(i+1) for some i.

Prove that by executing σ(i+1) before σ(i), we will do noworse.

Keep applying this transformation until there are no inversionsw.r.t. to t. This is our greedy solution.

League of Programmers Greedy, Divide and Conquer

Page 17: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Problem 3:

A thief breaks into a shop only to �nd that the stitches of his baghave become loose. There are N items in the shop that he canchoose with weight W[i] and value V[i]. The total weight that thebag can carry is W. How should he choose the items to maximizethe total value of the things he steals?

League of Programmers Greedy, Divide and Conquer

Page 18: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Outline

1 Greedy Algorithms

2 Divide and Conquer

3 Binary Search

4 Problems

League of Programmers Greedy, Divide and Conquer

Page 19: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

Suppose P(n) is the problem we are trying to solve, of size n

We can solve P(n) directly, for su�ciently small n

Divides the problem P(n) into subproblems

P1(n1); P2(n2); : : : ; Pk(nk)

for some constant k

Combine the solutions for Pi (ni ) (1 ≤ i ≤ k) to solve P(n)

League of Programmers Greedy, Divide and Conquer

Page 20: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

Suppose P(n) is the problem we are trying to solve, of size n

We can solve P(n) directly, for su�ciently small n

Divides the problem P(n) into subproblems

P1(n1); P2(n2); : : : ; Pk(nk)

for some constant k

Combine the solutions for Pi (ni ) (1 ≤ i ≤ k) to solve P(n)

League of Programmers Greedy, Divide and Conquer

Page 21: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

Suppose P(n) is the problem we are trying to solve, of size n

We can solve P(n) directly, for su�ciently small n

Divides the problem P(n) into subproblems

P1(n1); P2(n2); : : : ; Pk(nk)

for some constant k

Combine the solutions for Pi (ni ) (1 ≤ i ≤ k) to solve P(n)

League of Programmers Greedy, Divide and Conquer

Page 22: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

Suppose P(n) is the problem we are trying to solve, of size n

We can solve P(n) directly, for su�ciently small n

Divides the problem P(n) into subproblems

P1(n1); P2(n2); : : : ; Pk(nk)

for some constant k

Combine the solutions for Pi (ni ) (1 ≤ i ≤ k) to solve P(n)

League of Programmers Greedy, Divide and Conquer

Page 23: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

Suppose P(n) is the problem we are trying to solve, of size n

We can solve P(n) directly, for su�ciently small n

Divides the problem P(n) into subproblems

P1(n1); P2(n2); : : : ; Pk(nk)

for some constant k

Combine the solutions for Pi (ni ) (1 ≤ i ≤ k) to solve P(n)

League of Programmers Greedy, Divide and Conquer

Page 24: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Problem 1:

Merge-sort is divide-n-conquer.

Analysis of merge-sort.

T (n) ≤ 2T (n/2) + n

Problem- Find number of inversions by modifying code formerge-sort slightly.

League of Programmers Greedy, Divide and Conquer

Page 25: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Closest Pair of points

Given N points in a plane, �nd the pair of points that are closest toeach other.

League of Programmers Greedy, Divide and Conquer

Page 26: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 27: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 28: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 29: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 30: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 31: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 32: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 33: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.

League of Programmers Greedy, Divide and Conquer

Page 34: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 1

First sort the points inincreasing order of Xcoordinate.

Divide the plane into twohalves with equal number ofpoints P1 and P2.

Let d1 be the minimumdistance between closest pairin the P1 and similarly d2 inP2.

d1: minimum distance of P1

d2: minimum distance of P2

d ≤ min{d1, d2}.League of Programmers Greedy, Divide and Conquer

Page 35: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Now, region of interest is the band of 2d around the dividingline.

But, In fact, all of the points could be in the strip! This isdisastrous, because we would have to compare n2 pairs ofpoints to merge the set, and hence our divideandconqueralgorithm wouldn't save us anything in terms of e�ciency.

League of Programmers Greedy, Divide and Conquer

Page 36: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Now, region of interest is the band of 2d around the dividingline.

But, In fact, all of the points could be in the strip! This isdisastrous, because we would have to compare n2 pairs ofpoints to merge the set, and hence our divideandconqueralgorithm wouldn't save us anything in terms of e�ciency.

League of Programmers Greedy, Divide and Conquer

Page 37: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 2 League of Programmers Greedy, Divide and Conquer

Page 38: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 2

Thankfully, we can make another life savingobservation at this point. For any particularpoint p in one strip, only points that meet thefollowing constraints in the other strip need tobe checked:

1 Those points within d of p in the directionof the other strip

2 Those within d of p in the positive andnegative y directions

because points outside of this bounding boxcannot be less than d units from p.

League of Programmers Greedy, Divide and Conquer

Page 39: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 2

It just so happens that because everypoint in this box is at least d apart, therecan be at most six points within it.

League of Programmers Greedy, Divide and Conquer

Page 40: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 2

It just so happens that because everypoint in this box is at least d apart, therecan be at most six points within it.

Now we don't need to check all n2 points.All we have to do is sort the points in thestrip by their y-coordinates and scan thepoints in order, checking each pointagainst a maximum of 6 of its neighbors.

League of Programmers Greedy, Divide and Conquer

Page 41: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Figure: 2

It just so happens that because everypoint in this box is at least d apart, therecan be at most six points within it.

Now we don't need to check all n2 points.All we have to do is sort the points in thestrip by their y-coordinates and scan thepoints in order, checking each pointagainst a maximum of 6 of its neighbors.

This means at most 6 ∗ n comparisons arerequired to check all candidate pairs.

League of Programmers Greedy, Divide and Conquer

Page 42: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 43: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 44: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 45: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 46: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 47: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 48: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Summary

1 Divide the set into two equal sized parts by the line l, andrecursively compute the minimal distance in each part.

2 Let d be the minimal of the two minimal distances.

3 Eliminate points that lie farther than d apart from l

4 Sort the remaining points according to their y-coordinates

5 Scan the remaining points in the y order and compute thedistances of each point to its �ve neighbors.

6 If any of these distances is less than d then update d.

League of Programmers Greedy, Divide and Conquer

Page 49: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 50: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 51: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 52: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 53: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 54: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

League of Programmers Greedy, Divide and Conquer

Page 55: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

Hence the merging of the sub-solutions is dominated by the sortingat step 4, and hence takes O(nlogn) time.

League of Programmers Greedy, Divide and Conquer

Page 56: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Analysis

Steps 2-6 de�ne the merging process which must be repeatedlog(n) times because this is a divide and conquer algortithm:

Step 2 takes O(1) time

Step 3 takes O(n) time

Step 4 is a sort that takes O(nlogn) time

Step 5 takes O(n) time (as we saw in the previous section)

Step 6 takes O(1) time

Hence the merging of the sub-solutions is dominated by the sortingat step 4, and hence takes O(nlogn) time.This must be repeated once for each level of recursion in thedivide-and-conquer algorithm, hence the whole of algorithmClosestPair takes O(logn*nlogn) = O(nlog2n) time.

League of Programmers Greedy, Divide and Conquer

Page 57: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Improving the Algorithm

We can improve on this algorithm slightly by reducing the time ittakes to achieve the y-coordinate sorting in Step 4. This is done byasking that the recursive solution computed in Step 1 returns thepoints in sorted order by their y coordinates. This will yield twosorted lists of points which need only be merged in Step 4 in orderto yield a complete sorted list.

League of Programmers Greedy, Divide and Conquer

Page 58: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Improving the Algorithm

Hence the revised algorithm involves making the following changes:

League of Programmers Greedy, Divide and Conquer

Page 59: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Improving the Algorithm

Hence the revised algorithm involves making the following changes:

Step 1: Divide the set into. . . , and recursively compute thedistance in each part, returning the points in each set in sortedorder by y-coordinate.

League of Programmers Greedy, Divide and Conquer

Page 60: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

ClosestPair Problem

Improving the Algorithm

Hence the revised algorithm involves making the following changes:

Step 1: Divide the set into. . . , and recursively compute thedistance in each part, returning the points in each set in sortedorder by y-coordinate.

Step 4: Merge the two sorted lists into one sorted list in O(n)time. Hence the merging process is now dominated by thelinear time steps thereby yielding an O(nlogn) algorithm for�nding the closest pair of a set of points in the plane.

League of Programmers Greedy, Divide and Conquer

Page 61: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

League of Programmers Greedy, Divide and Conquer

Page 62: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

League of Programmers Greedy, Divide and Conquer

Page 63: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

League of Programmers Greedy, Divide and Conquer

Page 64: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

League of Programmers Greedy, Divide and Conquer

Page 65: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

The size of each subproblem is much smaller than the original

League of Programmers Greedy, Divide and Conquer

Page 66: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

The size of each subproblem is much smaller than the original

Divide and Conquer is a bad choice when:

League of Programmers Greedy, Divide and Conquer

Page 67: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

The size of each subproblem is much smaller than the original

Divide and Conquer is a bad choice when:

There are too many subproblems

League of Programmers Greedy, Divide and Conquer

Page 68: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

The size of each subproblem is much smaller than the original

Divide and Conquer is a bad choice when:

There are too many subproblems

The subproblems are not independent

League of Programmers Greedy, Divide and Conquer

Page 69: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Divide and Conquer

When to use divide and conquer

Divide and conquer works well when:

Divide step produces a constant number of subproblems

The subproblems may be solved independently

The size of each subproblem is much smaller than the original

Divide and Conquer is a bad choice when:

There are too many subproblems

The subproblems are not independent

The subproblems are too large

League of Programmers Greedy, Divide and Conquer

Page 70: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Outline

1 Greedy Algorithms

2 Divide and Conquer

3 Binary Search

4 Problems

League of Programmers Greedy, Divide and Conquer

Page 71: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Binary Search

Problem: Finding a value in a sorted sequence

For example, �nd 55 in the sequence

0, 5, 13, 15, 24, 43, 55, 65, 72, 85, 96

League of Programmers Greedy, Divide and Conquer

Page 72: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Binary Search

Problem: Finding a value in a sorted sequence

For example, �nd 55 in the sequence

0, 5, 13, 15, 24, 43, 55, 65, 72, 85, 96

What is the time complexity?

League of Programmers Greedy, Divide and Conquer

Page 73: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Binary Search

Problem: Finding a value in a sorted sequence

For example, �nd 55 in the sequence

0, 5, 13, 15, 24, 43, 55, 65, 72, 85, 96

What is the time complexity?At each step, we are discarding half of the array.

League of Programmers Greedy, Divide and Conquer

Page 74: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Binary Search

Code

League of Programmers Greedy, Divide and Conquer

Page 75: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

In a building with 100 (=n) storeys a person is required to �ndstarting from which �oor an egg if thrown to the ground will bebroken. He has been given just 2(=m) eggs for this task. All hecan do is to go to certain storeys and throw his egg and recordwhether the egg broke or not. Assume that for each storey, an eggwill either always break or will never break. Since, climbing stairs(there's no lift!), throwing eggs and going downstairs again tocheck whether the egg broke or not and to take the egg again tothe next �oor being checked can be a tiring process, devise astrategy following which the person will �nd the required storeyhaving after minimum number of throws

League of Programmers Greedy, Divide and Conquer

Page 76: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Generalization

Extending the idea of Problem 1

The same algorithm described above on any monotonic function fwhose domain is the set of integers. The only di�erence is that wereplace an array lookup with a function evaluation: we are nowlooking for some x such that f(x) is equal to the target value

League of Programmers Greedy, Divide and Conquer

Page 77: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problem

Problem:

CodeJam

League of Programmers Greedy, Divide and Conquer

Page 78: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Outline

1 Greedy Algorithms

2 Divide and Conquer

3 Binary Search

4 Problems

League of Programmers Greedy, Divide and Conquer

Page 79: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Problems

Added on the contest on VOC http://ahmed-aly.com/voc/

Contest ID: 2594Name: ACA, IITK LOP 02Author: pnkjjindalLinks:

1 http://spoj.pl/problems/BALIFE

2 http://www.spoj.pl/problems/KOPC12A

3 http://spoj.pl/problems/AGGRCOW

4 http://spoj.pl/problems/ARRANGE

5 http://www.spoj.pl/problems/BIASED

6 http://www.spoj.pl/problems/DONALDO

7 http://www.spoj.pl/problems/PIE

8 http://www.spoj.pl/problems/CISTFILL

League of Programmers Greedy, Divide and Conquer

Page 80: 02 greedy, d&c, binary search

Greedy AlgorithmsDivide and Conquer

Binary SearchProblems

Acknowledgements

Image Credits:http://www.cs.mcgill.ca/ cs251/ClosestPair/ClosestPairDQ.html

League of Programmers Greedy, Divide and Conquer