问题
Problem Statement: Given a matrix of people(denoted by small alphabets) and bikes(denoted by capital alphabets), find the nearest bike for a given person. How will you change your solution if you have to find bikes for a set of people? (assuming multiple bikes can be at the same distance from 1 person)?
I know Dijkstra's algorithm and Bellman Ford's algorithm, but curious about the implementation here. Or can it be solved by BFS(Breadth First Search)?
回答1:
To assign a single bike to a single person, you can use BFS or you can have a per-processing step in which you caclculate all the distances from people to bikes, store them, and use the table to answer single assignment queries.
It gets more complicated if you want to assign a group of bikes to a group of people. Then, you need to have an objective. For example, if your objective is to minimize total distance traveled by all the people to reach their assigned bikes, you are effectively solving a minimum weight bipartite matching problem which can be formulated and solved as a linear programming problem.
These lecture notes explain the problem and the algorithm.
回答2:
Supplementing @jrook's answer, note that the "minimum weight full bipartite matching" ("full" or "perfect" because otherwise the optimal solution is the empty matching) also goes under the name of the linear assignment problem, just to give some more keywords to get you started.
While it's certainly true that phrasing the problem as a linear programming problem does the job, indeed there are plenty of algorithms that provide solutions for this particular case.
If you're familiar with shortest path and augmenting path search algorithms (such as those that can be used to maximum bipartite matching or maximum flow), then perhaps the simplest algorithm to understand is what you first get by realizing that the problem at hand is a special case of the minimum cost flow problem which can be solved by replacing the BFS step of the Edmonds--Karp algorithm by a shortest path search. More common in introductory texts is the Hungarian algorithm.
If you care about performance, there are more efficient algorithms out there; indeed it is easy to find solutions that are orders of magnitude faster than what you get by just throwing a commercial solver after the linear programming formulation.
来源:https://stackoverflow.com/questions/58240773/algorithm-for-locating-the-closest-destinations-to-sources-and-resolving-con