So this is an algorithm question. The problem statement is the following: given two lists of coordinates (or length n each) of bikes and people on a 2D grid (or a 2D grid that show the positions of each bike and person), calculate the optimal pairing of the bikes and people so that the total Manhattan distance of all the pairs are minimized. It is guaranteed that for each person, the distance to all bikes won't be the same and same applies for each bikes.
There is a solution in this question, which states that we can sort all the distances from low to high and pair the bike to the person if they are both unpaired. The complexity is obviously O(n^2 logn) time and O(n^2) space.
So my questions are
- Is this the optimal way and why? Can someone prove its optimality? If it's not optimal, what would be the optimal algorithm in minimizing the total distance?
- How is it related to the stable marriage problem?
Update:
The question linked is using a different criteria of priority. So what would be the algorithm that calculates the optimal pairing if the criteria is minimizing total Manhattan distance (except for the brute force DFS algorithm that is exponential in complexity)?
Update 2:
If the criteria is that no pair would prefer each other than their current partner, then it's a stable marriage problem. If the criteria is the total sum of distances, Hungarian algorithm should be used.