Given 3 points in a grid, how would you find a point such that sum of distances of this point from all 3 points in minimized. An obvious answer to this problem is Fermat’s triangle. I am interested in knowing if we can locate Fermat’s point using a breadth first search algorithm in a graph.
struct node{
int Person1X,Person1Y,Person2X,Person2Y,Person3X,Person3Y; //X and Y coordinates of all 3 persons
int steps; //sum of distances covered by all 3 person to reach this state
}
While doing the BFS we could put a constraint,
if steps>min(sum of any two edges of the triangle with 3 persons as vertices) return;
if(Person1X=Person2X=Person2X)AND(Person1Y=Person2Y=Person3Y) return steps;
No search is necessary.
Given “triangle” ABC:
SumOfDistances( p ) = dist( A, p ) + dist( B, p ) + dist( C, p )
where dist( q, p ) = |qx-px| + |qy-py| (Manhattan distance)
you can see that SumOfDistances( p ) = SumOfDistancesx( p ) + SumOfDistancesy( p )
So, you can minimize by the distance on the x and y axis independently.
So, the Fermat point’s x-coordinate is the median of the 3 given x-coordinates.
The Fermat point’s y-coordinate is the median of the 3 given y-coordinates.