The document discusses greedy algorithms, divide and conquer algorithms, and binary search algorithms. It provides examples of problems that can be solved using each type of algorithm. For greedy algorithms, it describes the characteristics of greedy approaches. For divide and conquer, it outlines the general approach of dividing a problem into subproblems, solving the subproblems, and combining the solutions. It also provides an example of the closest pair of points problem solved using divide and conquer.
Divide and Conquer Algorithms - D&C forms a distinct algorithm design technique in computer science, wherein a problem is solved by repeatedly invoking the algorithm on smaller occurrences of the same problem. Binary search, merge sort, Euclid's algorithm can all be formulated as examples of divide and conquer algorithms. Strassen's algorithm and Nearest Neighbor algorithm are two other examples.
A unique sorting algorithm with linear time & space complexityeSAT Journals
Â
Abstract Sorting a list means selection of the particular permutation of the members of that list in which the final permutation contains members in increasing or in decreasing order. Sorted list is prerequisite of some optimized operations such as searching an element from a list, locating or removing an element to/ from a list and merging two sorted list in a database etc. As volume of information is growing up day by day in the world around us and these data are unavoidable to manage for real life situations, the efficient and cost effective sorting algorithms are required. There are several numbers of fundamental and problem oriented sorting algorithms but still now sorting a problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently and effectively despite of its simple and familiar statements. Algorithms having same efficiency to do a same work using different mechanisms must differ their required time and space. For that reason an algorithm is chosen according to oneâs need with respect to space complexity and time complexity. Now a day, space (Memory) is available in market comparatively in cheap cost. So, time complexity is a major issue for an algorithm. Here, the presented approach is to sort a list with linear time and space complexity using divide and conquer rule by partitioning a problem into n (input size) number of sub problems then these sub problems are solved recursively. Required time and space for the algorithm is optimized through reducing the height of the recursive tree and reduced height is too small (as compared to the problem size) to evaluate. So, asymptotic efficiency of this algorithm is very high with respect to time and space. Keywords: sorting, searching, permutation, divide and conquer algorithm, asymptotic efficiency, space complexity, time complexity, and recursion.
Divide and Conquer Algorithms - D&C forms a distinct algorithm design technique in computer science, wherein a problem is solved by repeatedly invoking the algorithm on smaller occurrences of the same problem. Binary search, merge sort, Euclid's algorithm can all be formulated as examples of divide and conquer algorithms. Strassen's algorithm and Nearest Neighbor algorithm are two other examples.
A unique sorting algorithm with linear time & space complexityeSAT Journals
Â
Abstract Sorting a list means selection of the particular permutation of the members of that list in which the final permutation contains members in increasing or in decreasing order. Sorted list is prerequisite of some optimized operations such as searching an element from a list, locating or removing an element to/ from a list and merging two sorted list in a database etc. As volume of information is growing up day by day in the world around us and these data are unavoidable to manage for real life situations, the efficient and cost effective sorting algorithms are required. There are several numbers of fundamental and problem oriented sorting algorithms but still now sorting a problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently and effectively despite of its simple and familiar statements. Algorithms having same efficiency to do a same work using different mechanisms must differ their required time and space. For that reason an algorithm is chosen according to oneâs need with respect to space complexity and time complexity. Now a day, space (Memory) is available in market comparatively in cheap cost. So, time complexity is a major issue for an algorithm. Here, the presented approach is to sort a list with linear time and space complexity using divide and conquer rule by partitioning a problem into n (input size) number of sub problems then these sub problems are solved recursively. Required time and space for the algorithm is optimized through reducing the height of the recursive tree and reduced height is too small (as compared to the problem size) to evaluate. So, asymptotic efficiency of this algorithm is very high with respect to time and space. Keywords: sorting, searching, permutation, divide and conquer algorithm, asymptotic efficiency, space complexity, time complexity, and recursion.
JavaScript is the scripting language of the Web.
JavaScript is used in web pages to add functionality, validate forms, communicate with the server, and much more.
The local area technologies as 1-WLAN(Wireless Local Area Network) with moderate bandwidth. And WiMax
2.The large area technologies as GSM, GPRSor UMTS, LTEwhich have much higher bandwidth.
In divide and conquer, we will see
1.- Why Divide and Conquer?
2.- The Gauss Trick
3.- Recursion is the base of Divide and Conquer
4.- Induction to prove the correctness of algorithms
5.- The use of the Asymptotic notation
6.- Why the worst case?
7.- Some tricks to calculate upper and lower bounds for recursions:
- The substitution method
- The tree method
- The Master Theorem
This slide explain complexity of an algorithm. Explain from theory perspective. At the end of slide, I also show the test result to prove the theory. Pleas, read this slide to improve your code quality .
This slide is exported from Ms. Power
Point to PDF.
JavaScript is the scripting language of the Web.
JavaScript is used in web pages to add functionality, validate forms, communicate with the server, and much more.
The local area technologies as 1-WLAN(Wireless Local Area Network) with moderate bandwidth. And WiMax
2.The large area technologies as GSM, GPRSor UMTS, LTEwhich have much higher bandwidth.
In divide and conquer, we will see
1.- Why Divide and Conquer?
2.- The Gauss Trick
3.- Recursion is the base of Divide and Conquer
4.- Induction to prove the correctness of algorithms
5.- The use of the Asymptotic notation
6.- Why the worst case?
7.- Some tricks to calculate upper and lower bounds for recursions:
- The substitution method
- The tree method
- The Master Theorem
This slide explain complexity of an algorithm. Explain from theory perspective. At the end of slide, I also show the test result to prove the theory. Pleas, read this slide to improve your code quality .
This slide is exported from Ms. Power
Point to PDF.
In questa sessione a quattro mani introdurremo alcuni dei refactorings piÚ comuni e piÚ facilmente applicabili nell'utilizzo quotidiano, e vedremo come risolverli in maniera facile, veloce ed indolore utilizzando ReSharper e pochi colpi di tastiera.
Incidentalmente, inseriremo nel mentre un pÃ˛ di patterns e di Test-Driven Development, perchè "se non è testato, allora non funziona"
Slides from my session on GPars at Gr8Conf EU 2012. It was a manifesto speech for using actors, dataflow, CSP and data parallelism and avoiding all explicit locking and indeed explicit shared-memory multi-threading.
Boost Your Base Bootcamp - [Online & Offline] In BanglaStack Learner
Â
Boost Your Base Bootcamp
Stack School:
https://courses.stackschool.co/courses/boost-your-base-bootcamp
"Boost Your Base Bootcamp[ Online + Offline ]", In this long course we will introduce you to C Programming Language, Java, Data Structures and Algorithms, Design Patterns and Problem Solving. At the end of the Bootcamp, you will find yourself in a place where you can engage yourself in any field of the IT world.
50+ Weeks, 100+ Classes - A Long Journey to Become A Programmer
āĻ āĻŦāĻā§āĻā§āĻ āĻ āĻ°āĻŋā§ā§āĻ¨ā§āĻā§āĻĄ āĻĒā§āĻ°ā§āĻā§āĻ°āĻžāĻŽāĻŋāĻ, āĻĄāĻžāĻāĻž āĻ¸ā§āĻā§āĻ°āĻžāĻāĻāĻžāĻ°āĻ¸ āĻāĻŦāĻ āĻ ā§āĻ¯āĻžāĻ˛āĻā§āĻ°āĻŋāĻĻāĻŽ
āĻāĻāĻāĻŋ āĻāĻāĻ¤ā§ āĻ¨āĻŋāĻā§āĻā§ āĻ¯ā§āĻā§āĻ¯ āĻāĻ°ā§ āĻā§ā§ āĻ¤ā§āĻ˛āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻĒāĻ¨āĻžāĻ° āĻĻāĻ°āĻāĻžāĻ° āĻĒā§āĻ°ā§āĻā§āĻ°āĻžāĻŽāĻŋāĻ āĻāĻŦāĻ āĻāĻŽā§āĻĒāĻŋāĻāĻāĻžāĻ° āĻ¸āĻžāĻāĻ¨ā§āĻ¸ā§āĻ° āĻĻāĻā§āĻˇāĻ¤āĻžāĨ¤ āĻāĻ āĻĻā§āĻ°ā§āĻ āĻā§āĻ°ā§āĻ¸ā§ āĻāĻŽāĻ°āĻž āĻāĻĒāĻ¨āĻžāĻā§ āĻ¸āĻŋ āĻĒā§āĻ°ā§āĻā§āĻ°āĻžāĻŽāĻŋāĻ āĻ˛ā§āĻ¯āĻžāĻāĻā§ā§ā§āĻ, āĻāĻžāĻāĻž, āĻĄāĻžāĻāĻž āĻ¸ā§āĻā§āĻ°āĻžāĻāĻāĻžāĻ° āĻāĻŦāĻ āĻ ā§āĻ¯āĻžāĻ˛āĻā§āĻ°āĻŋāĻĻāĻŽ, āĻĄāĻŋāĻāĻžāĻāĻ¨ āĻĒā§āĻ¯āĻžāĻāĻžāĻ°ā§āĻ¨ āĻāĻŦāĻ āĻĒā§āĻ°āĻŦā§āĻ˛ā§āĻŽ āĻ¸āĻ˛ā§āĻāĻŋāĻ āĻāĻ° āĻ¸āĻžāĻĨā§ āĻĒāĻ°āĻŋāĻā§ āĻāĻ°āĻžāĻŦā§āĨ¤ āĻšāĻžāĻ¤ā§ āĻāĻ˛āĻŽā§ āĻļā§āĻāĻžāĻ¨ā§āĻ° āĻ¸āĻžāĻĨā§ āĻ¸āĻžāĻĨā§ āĻāĻ¨ā§āĻĄāĻŋāĻāĻŋāĻā§ā§āĻžāĻ˛ āĻāĻŦāĻ āĻā§āĻ°ā§āĻĒ āĻĒā§āĻ°ā§āĻā§āĻā§āĻā§āĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ āĻāĻĒāĻ¨āĻžāĻā§ āĻĻāĻā§āĻˇ āĻāĻ°ā§ āĻā§ā§ā§ āĻ¤ā§āĻ˛āĻžāĻ° āĻā§āĻˇā§āĻāĻž āĻāĻ°āĻž āĻšāĻŦā§ āĻāĻ āĻ¸ā§āĻŦāĻŋāĻļāĻžāĻ˛ āĻā§āĻ°ā§āĻ¸ā§āĨ¤ āĻāĻ āĻā§āĻ°ā§āĻ¸ āĻļā§āĻˇā§ āĻāĻĒāĻ¨āĻŋ āĻ¨āĻŋāĻā§āĻā§ āĻāĻŽāĻ¨ āĻāĻāĻāĻŋ āĻāĻžā§āĻāĻžā§ āĻāĻŦāĻŋāĻˇā§āĻāĻžāĻ° āĻāĻ°āĻŦā§āĻ¨ āĻ¯ā§āĻāĻžāĻ¨ āĻĨā§āĻā§ āĻāĻĒāĻ¨āĻŋ āĻāĻāĻāĻŋ āĻāĻāĻ¤ā§āĻ° āĻ¯ā§āĻā§āĻ¨ā§ āĻĢāĻŋāĻ˛ā§āĻĄā§ āĻ¨āĻŋāĻā§āĻā§ āĻā§āĻŋāĻ¤ āĻāĻ°āĻ¤ā§ āĻĒāĻžāĻ°āĻŦā§āĻ¨āĨ¤ āĻāĻĒāĻ¨āĻžāĻ° āĻĒā§āĻ°ā§āĻā§āĻ°āĻžāĻŽāĻŋāĻ āĻāĻ° āĻāĻŋāĻ¤ā§āĻ¤āĻŋ āĻ¤ā§āĻ°āĻŋāĻ° āĻāĻžāĻ āĻāĻ°āĻŦā§ āĻāĻ āĻŦā§āĻāĻā§āĻ¯āĻžāĻŽā§āĻĒāĻāĻŋāĨ¤
This is the slide deck I used for the keynote at the Agile Open Holland Conference 2011, held November 3 & 4 in Dieren. Using this slide deck I explain about being dogmatic and religious about agile approaches, and how a lot of the lightweight approaches are not sufficient as such for most larger, more complex or enterprise projects.
1. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Greedy, Divide and Conquer
League of Programmers
ACA, IIT Kanpur
October 22, 2012
League of Programmers Greedy, Divide and Conquer
2. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Outline
1 Greedy Algorithms
2 Divide and Conquer
3 Binary Search
4 Problems
League of Programmers Greedy, Divide and Conquer
3. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Greedy Algorithms
Greedy algorithms are generally used in optimization problems
League of Programmers Greedy, Divide and Conquer
4. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Greedy 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Greedy 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 all
points. In other words, we do at each step what seems best
without planning ahead
League of Programmers Greedy, Divide and Conquer
7. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Outline
1 Greedy Algorithms
2 Divide and Conquer
3 Binary Search
4 Problems
League of Programmers Greedy, Divide and Conquer
19. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide and Conquer
League of Programmers Greedy, Divide and Conquer
20. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide and Conquer
Suppose P(n) is the problem we are trying to solve, of size n
League of Programmers Greedy, Divide and Conquer
21. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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 for
merge-sort slightly.
League of Programmers Greedy, Divide and Conquer
25. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
League of Programmers Greedy, Divide and Conquer
27. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Figure: 1
League of Programmers Greedy, Divide and Conquer
28. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Figure: 1
League of Programmers Greedy, Divide and Conquer
29. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
First sort the points in
increasing order of X
coordinate.
Figure: 1
League of Programmers Greedy, Divide and Conquer
30. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Now, region of interest is the band of 2d around the dividing
line.
League of Programmers Greedy, Divide and Conquer
36. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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 wouldn't save us anything in terms of eciency.
League of Programmers Greedy, Divide and Conquer
37. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Figure: 2 League of Programmers Greedy, Divide and Conquer
38. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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 don't 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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 don't 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Summary
League of Programmers Greedy, Divide and Conquer
43. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair Problem
Improving the Algorithm
Hence the revised algorithm involves making the following changes:
League of Programmers Greedy, Divide and Conquer
59. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
ClosestPair 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide and Conquer
When to use divide and conquer
League of Programmers Greedy, Divide and Conquer
62. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Divide and Conquer
When to use divide and conquer
Divide and conquer works well when:
League of Programmers Greedy, Divide and Conquer
63. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
64. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
65. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
66. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
67. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
68. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
69. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
70. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Outline
1 Greedy Algorithms
2 Divide and Conquer
3 Binary Search
4 Problems
League of Programmers Greedy, Divide and Conquer
71. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
72. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
73. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
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
74. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Binary Search
Code
League of Programmers Greedy, Divide and Conquer
75. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
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
(there's 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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Generalization
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problem
Problem:
CodeJam
League of Programmers Greedy, Divide and Conquer
78. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Outline
1 Greedy Algorithms
2 Divide and Conquer
3 Binary Search
4 Problems
League of Programmers Greedy, Divide and Conquer
79. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Problems
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. Greedy Algorithms
Divide and Conquer
Binary Search
Problems
Acknowledgements
Image Credits:
http://www.cs.mcgill.ca/ cs251/ClosestPair/ClosestPairDQ.html
League of Programmers Greedy, Divide and Conquer