4

I have an assignment which compares 2 different algorithms for a problem. Here's the problem:

Suppose I have a series of xy coords like this :

A(2,3), B(5,6), C(7,8), D(6,2), E(5,5), etc..

And I want to find 2 coords which have the shortest distance between them. One of the solution is using brute force (matching them one by one), but there is another solution using "divide and conquer" method..

Can you help me with the "divide and conquer" method?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Jason
  • 452
  • 2
  • 10
  • 27

2 Answers2

5

The recursive divide-and-conquer approach works as follows:

1) Sort points according to their x-coordinates.
2) Split the set of points into two equal-sized subsets by a vertical line x=xmid.

3) Solve the problem recursively in the left and right subsets. This
yields the left-side and right-side minimum distances dLmin and dRmin, respectively.

4) Find the minimal distance dLRmin among the pair of points in which one point lies on the left of the dividing vertical and the second point lies to the right.

5) The final answer is the minimum among dLmin, dRmin, and dLRmin. (source)

It has a complexity of O(n log n). There is also a pseudocode implementation here and a visual description of the method here.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

Think about what the "divide" and "merge" parts mean. Obviously "divide" means to divide the problem into 2 smaller separate ones. How? Then, given that you solved the 2 smaller problems how do you merge them together? What is the time complexity of both methods? If you need more clarification leave a comment.

jonathanasdf
  • 2,844
  • 2
  • 23
  • 26
  • I guess the point of the question is _how_ to divide and _how_ to conquer. – John Dvorak Dec 02 '12 at 03:57
  • 1
    Yes, but it is also a homework question, so instead of just giving the answer, I'd personally prefer a more interactive discussion. Sometimes, a really small thing like restating the question like what I'm doing here ends up actually enough for the person to solve it by themselves. – jonathanasdf Dec 02 '12 at 04:01
  • I understand the concept, but not the technical details. How to divide the coordinates to sub problems which can be easily solved and how to merge it all into a solution... From what base should I divide it?... – Jason Dec 02 '12 at 04:11
  • Well, you want to divide the points into two parts that don't intersect. Generally, you can draw any random line through the set of points that divides the points into two equal sized parts. However, that is difficult to implement. But, if you divide it with a vertical line (or a horizontal line), then it's easy to both find the line that divides it into equal halves, and also to actually figure out which point is on which side (exercise left to you). – jonathanasdf Dec 02 '12 at 04:14
  • Once you have the two halves, then obviously there are 2 possible cases: either the closes pair lies completely inside one of the halves, or one lies on one half, and the other on the other half. Recursively calling your function for each of the two halves (let's call them "left" and "right"), you can find the closest pair in left and the closest pair in right. Try figuring out how you can now merge the two parts. Take care to keep in mind what the base case is! (Whenever you have anything recursive, always figure out the base case first.) Try working it out on paper first, instead of code. – jonathanasdf Dec 02 '12 at 04:18