02 greedy, d&c, binary search

2,429 views
2,332 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,429
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

02 greedy, d&c, binary search

  1. 1. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy, Divide and Conquer League of Programmers ACA, IIT Kanpur October 22, 2012League of Programmers Greedy, Divide and Conquer
  2. 2. Greedy Algorithms Divide and Conquer Binary Search ProblemsOutline 1 Greedy Algorithms 2 Divide and Conquer 3 Binary Search 4 Problems League of Programmers Greedy, Divide and Conquer
  3. 3. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy Algorithms Greedy algorithms are generally used in optimization problems League of Programmers Greedy, Divide and Conquer
  4. 4. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy Algorithms Greedy algorithms are generally used in optimization problems There is an optimal substructure to the problem League of Programmers Greedy, Divide and Conquer
  5. 5. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy Algorithms Greedy algorithms are generally used in optimization problems There is an optimal substructure to the problem Greedy Algorithms are algorithms that League of Programmers Greedy, Divide and Conquer
  6. 6. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy 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 all points. In other words, we do at each step what seems best without planning ahead League of Programmers Greedy, Divide and Conquer
  7. 7. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy 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 all points. In other words, we do at each step what seems best without planning ahead ignores future possibilities (no planning ahead) League of Programmers Greedy, Divide and Conquer
  8. 8. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy 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 all points. In other words, we do at each step what seems best without planning ahead ignores future possibilities (no planning ahead) never changes choice (no backtracking) League of Programmers Greedy, Divide and Conquer
  9. 9. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy 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 all points. In other words, we do at each step what seems best without planning ahead ignores future possibilities (no planning ahead) never changes choice (no backtracking) Usually O(n) or O(n lg n) Solutions. League of Programmers Greedy, Divide and Conquer
  10. 10. Greedy Algorithms Divide and Conquer Binary Search ProblemsGreedy 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 all points. In other words, we do at each step what seems best without 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 greedy algorithm if we are to be sure that it works League of Programmers Greedy, Divide and Conquer
  11. 11. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Problem 1: A family goes for picnic along a straight road. There are k hotels in their way situated at distances d ,d . . . d from the start. The last 1 2 k hotel 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 for the family so that they can reach hotel k in minimum number of days. League of Programmers Greedy, Divide and Conquer
  12. 12. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Solution: The rst solution that comes to mind is to travel on a particular day as long as possible, i.e. do not stop at a hotel i the next hotel can be reached on the same day. League of Programmers Greedy, Divide and Conquer
  13. 13. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Proof: Proof strategy 1 for greedy algorithms: Greedy solution stays ahead Suppose there is a better solution which stops at hotels i i 1 2 . . . i and greedy solution stops at j j . . . j and r s . r 1 2 s Consider the rst place where the two solutions dier and let that index be p. Clearly i jp p On a given day when non greedy solution starts from hotel i j , non greedy solution cannot end up ahead of greedy q q solution. Therefore Greedy solution stays ahead League of Programmers Greedy, Divide and Conquer
  14. 14. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Problem 2: There are n processes that need to be executed. Process i takes a preprocessing time p and time t to execute afterwards. All i i preprocessing has to be done on a supercomputer and execution can be done on normal computers. We have only one supercomputer and n normal computers. Find a sequence in which to execute the processes such that the time taken to nish all the processes is minimum. League of Programmers Greedy, Divide and Conquer
  15. 15. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Solution: Sort in descending order of t and execute them. At least this is the i solution for which no counterexample is available. League of Programmers Greedy, Divide and Conquer
  16. 16. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Proof: Let there be any other ordering σ (1),σ (2). . . σ (n) such that t σ(i) tσ(i+1) for some i. Prove that by executing σ (i+1) before σ (i), we will do no worse. Keep applying this transformation until there are no inversions w.r.t. to t. This is our greedy solution. League of Programmers Greedy, Divide and Conquer
  17. 17. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Problem 3: A thief breaks into a shop only to nd that the stitches of his bag have become loose. There are N items in the shop that he can choose with weight W[i] and value V[i]. The total weight that the bag can carry is W. How should he choose the items to maximize the total value of the things he steals? League of Programmers Greedy, Divide and Conquer
  18. 18. Greedy Algorithms Divide and Conquer Binary Search ProblemsOutline 1 Greedy Algorithms 2 Divide and Conquer 3 Binary Search 4 Problems League of Programmers Greedy, Divide and Conquer
  19. 19. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer League of Programmers Greedy, Divide and Conquer
  20. 20. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer Suppose P(n) is the problem we are trying to solve, of size n League of Programmers Greedy, Divide and Conquer
  21. 21. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer Suppose P(n) is the problem we are trying to solve, of size n We can solve P(n) directly, for suciently small n League of Programmers Greedy, Divide and Conquer
  22. 22. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer Suppose P(n) is the problem we are trying to solve, of size n We can solve P(n) directly, for suciently small n Divides the problem P(n) into subproblems P (n ); P (n ); : : : ; P (n ) 1 1 2 2 k k for some constant k League of Programmers Greedy, Divide and Conquer
  23. 23. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer Suppose P(n) is the problem we are trying to solve, of size n We can solve P(n) directly, for suciently small n Divides the problem P(n) into subproblems P (n ); P (n ); : : : ; P (n ) 1 1 2 2 k k for some constant k Combine the solutions for P (n ) (1 ≤ i ≤ k ) to solve P(n) i i League of Programmers Greedy, Divide and Conquer
  24. 24. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem 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 for merge-sort slightly. League of Programmers Greedy, Divide and Conquer
  25. 25. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Closest Pair of points Given N points in a plane, nd the pair of points that are closest to each other. League of Programmers Greedy, Divide and Conquer
  26. 26. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem League of Programmers Greedy, Divide and Conquer
  27. 27. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Figure: 1 League of Programmers Greedy, Divide and Conquer
  28. 28. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Figure: 1 League of Programmers Greedy, Divide and Conquer
  29. 29. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Figure: 1 League of Programmers Greedy, Divide and Conquer
  30. 30. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Divide the plane into two halves with equal number of points P1 and P2. Figure: 1 League of Programmers Greedy, Divide and Conquer
  31. 31. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Divide the plane into two halves with equal number of points P1 and P2. Let d1 be the minimum distance between closest pair in the P1 and similarly d2 in P2. Figure: 1 League of Programmers Greedy, Divide and Conquer
  32. 32. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Divide the plane into two halves with equal number of points P1 and P2. Let d1 be the minimum distance between closest pair in the P1 and similarly d2 in P2. d1: minimum distance of P1 Figure: 1 League of Programmers Greedy, Divide and Conquer
  33. 33. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Divide the plane into two halves with equal number of points P1 and P2. Let d1 be the minimum distance between closest pair in the P1 and similarly d2 in P2. d1: minimum distance of P1 Figure: 1 d2: minimum distance of P2 League of Programmers Greedy, Divide and Conquer
  34. 34. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem First sort the points in increasing order of X coordinate. Divide the plane into two halves with equal number of points P1 and P2. Let d1 be the minimum distance between closest pair in the P1 and similarly d2 in P2. d1: minimum distance of P1 Figure: 1 d2: minimum distance of P2 d ≤ min{d1, d2}. League of Programmers Greedy, Divide and Conquer
  35. 35. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Now, region of interest is the band of 2d around the dividing line. League of Programmers Greedy, Divide and Conquer
  36. 36. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Now, region of interest is the band of 2d around the dividing line. But, In fact, all of the points could be in the strip! This is disastrous, because we would have to compare n pairs of 2 points to merge the set, and hence our divideandconquer algorithm wouldnt save us anything in terms of eciency. League of Programmers Greedy, Divide and Conquer
  37. 37. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Figure: 2 League of Programmers Greedy, Divide and Conquer
  38. 38. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Thankfully, we can make another life saving observation at this point. For any particular point p in one strip, only points that meet the following constraints in the other strip need to be checked: 1 Those points within d of p in the direction of the other strip 2 Those within d of p in the positive and negative y directions because points outside of this bounding box cannot be less than d units from p. Figure: 2 League of Programmers Greedy, Divide and Conquer
  39. 39. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem It just so happens that because every point in this box is at least d apart, there can be at most six points within it. Figure: 2 League of Programmers Greedy, Divide and Conquer
  40. 40. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem It just so happens that because every point in this box is at least d apart, there can be at most six points within it. Now we dont need to check all n points. 2 All we have to do is sort the points in the strip by their y-coordinates and scan the points in order, checking each point against a maximum of 6 of its neighbors. Figure: 2 League of Programmers Greedy, Divide and Conquer
  41. 41. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem It just so happens that because every point in this box is at least d apart, there can be at most six points within it. Now we dont need to check all n points. 2 All we have to do is sort the points in the strip by their y-coordinates and scan the points in order, checking each point against a maximum of 6 of its neighbors. This means at most 6 ∗ n comparisons are required to check all candidate pairs. Figure: 2 League of Programmers Greedy, Divide and Conquer
  42. 42. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary League of Programmers Greedy, Divide and Conquer
  43. 43. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. League of Programmers Greedy, Divide and Conquer
  44. 44. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2Let d be the minimal of the two minimal distances. League of Programmers Greedy, Divide and Conquer
  45. 45. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2Let d be the minimal of the two minimal distances. 3Eliminate points that lie farther than d apart from l League of Programmers Greedy, Divide and Conquer
  46. 46. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2Let d be the minimal of the two minimal distances. 3Eliminate points that lie farther than d apart from l 4Sort the remaining points according to their y-coordinates League of Programmers Greedy, Divide and Conquer
  47. 47. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2Let d be the minimal of the two minimal distances. 3Eliminate points that lie farther than d apart from l 4Sort the remaining points according to their y-coordinates 5Scan the remaining points in the y order and compute the distances of each point to its ve neighbors. League of Programmers Greedy, Divide and Conquer
  48. 48. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Summary 1Divide the set into two equal sized parts by the line l, and recursively compute the minimal distance in each part. 2Let d be the minimal of the two minimal distances. 3Eliminate points that lie farther than d apart from l 4Sort the remaining points according to their y-coordinates 5Scan the remaining points in the y order and compute the distances of each point to its ve neighbors. 6If any of these distances is less than d then update d. League of Programmers Greedy, Divide and Conquer
  49. 49. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(n) times because this is a divide and conquer algortithm: League of Programmers Greedy, Divide and Conquer
  50. 50. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(n) times because this is a divide and conquer algortithm: Step 2 takes O(1) time League of Programmers Greedy, Divide and Conquer
  51. 51. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(n) times because this is a divide and conquer algortithm: Step 2 takes O(1) time Step 3 takes O(n) time League of Programmers Greedy, Divide and Conquer
  52. 52. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(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 League of Programmers Greedy, Divide and Conquer
  53. 53. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(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) League of Programmers Greedy, Divide and Conquer
  54. 54. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(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
  55. 55. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(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 sorting at step 4, and hence takes O(nlogn) time. League of Programmers Greedy, Divide and Conquer
  56. 56. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Analysis Steps 2-6 dene the merging process which must be repeated log(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 sorting at step 4, and hence takes O(nlogn) time. This must be repeated once for each level of recursion in the divide-and-conquer algorithm, hence the whole of algorithm ClosestPair takes O(logn*nlogn) = O(nlog n) time. 2 League of Programmers Greedy, Divide and Conquer
  57. 57. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Improving the Algorithm We can improve on this algorithm slightly by reducing the time it takes to achieve the y-coordinate sorting in Step 4. This is done by asking that the recursive solution computed in Step 1 returns the points in sorted order by their y coordinates. This will yield two sorted lists of points which need only be merged in Step 4 in order to yield a complete sorted list. League of Programmers Greedy, Divide and Conquer
  58. 58. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Improving the Algorithm Hence the revised algorithm involves making the following changes: League of Programmers Greedy, Divide and Conquer
  59. 59. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Improving the Algorithm Hence the revised algorithm involves making the following changes: Step 1: Divide the set into. . . , and recursively compute the distance in each part, returning the points in each set in sorted order by y-coordinate. League of Programmers Greedy, Divide and Conquer
  60. 60. Greedy Algorithms Divide and Conquer Binary Search ProblemsClosestPair Problem Improving the Algorithm Hence the revised algorithm involves making the following changes: Step 1: Divide the set into. . . , and recursively compute the distance in each part, returning the points in each set in sorted order 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 the linear 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
  61. 61. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer When to use divide and conquer League of Programmers Greedy, Divide and Conquer
  62. 62. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide and Conquer When to use divide and conquer Divide and conquer works well when: League of Programmers Greedy, Divide and Conquer
  63. 63. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  64. 64. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  65. 65. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  66. 66. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  67. 67. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  68. 68. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  69. 69. Greedy Algorithms Divide and Conquer Binary Search ProblemsDivide 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
  70. 70. Greedy Algorithms Divide and Conquer Binary Search ProblemsOutline 1 Greedy Algorithms 2 Divide and Conquer 3 Binary Search 4 Problems League of Programmers Greedy, Divide and Conquer
  71. 71. Greedy Algorithms Divide and Conquer Binary Search ProblemsBinary 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
  72. 72. Greedy Algorithms Divide and Conquer Binary Search ProblemsBinary 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
  73. 73. Greedy Algorithms Divide and Conquer Binary Search ProblemsBinary 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
  74. 74. Greedy Algorithms Divide and Conquer Binary Search ProblemsBinary Search Code League of Programmers Greedy, Divide and Conquer
  75. 75. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem In a building with 100 (=n) storeys a person is required to nd starting from which oor an egg if thrown to the ground will be broken. He has been given just 2(=m) eggs for this task. All he can do is to go to certain storeys and throw his egg and record whether the egg broke or not. Assume that for each storey, an egg will either always break or will never break. Since, climbing stairs (theres no lift!), throwing eggs and going downstairs again to check whether the egg broke or not and to take the egg again to the next oor being checked can be a tiring process, devise a strategy following which the person will nd the required storey having after minimum number of throws League of Programmers Greedy, Divide and Conquer
  76. 76. Greedy Algorithms Divide and Conquer Binary Search ProblemsGeneralization Extending the idea of Problem 1 The same algorithm described above on any monotonic function f whose domain is the set of integers. The only dierence is that we replace an array lookup with a function evaluation: we are now looking for some x such that f(x) is equal to the target value League of Programmers Greedy, Divide and Conquer
  77. 77. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblem Problem: CodeJam League of Programmers Greedy, Divide and Conquer
  78. 78. Greedy Algorithms Divide and Conquer Binary Search ProblemsOutline 1 Greedy Algorithms 2 Divide and Conquer 3 Binary Search 4 Problems League of Programmers Greedy, Divide and Conquer
  79. 79. Greedy Algorithms Divide and Conquer Binary Search ProblemsProblems Added on the contest on VOC http://ahmed-aly.com/voc/ Contest ID: 2594 Name: ACA, IITK LOP 02 Author: pnkjjindal Links: 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
  80. 80. Greedy Algorithms Divide and Conquer Binary Search ProblemsAcknowledgements Image Credits: http://www.cs.mcgill.ca/ cs251/ClosestPair/ClosestPairDQ.html League of Programmers Greedy, Divide and Conquer

×