2

My apologies if question is not in correct format. I don't post here often. I don't know what tags would be appropriate for this question, or even if this is an appropriate question.

Looking for algorithm for closest available resource.

n people P1-Pn, n taxis T1-Tn, are in a 2 dimensional coordinate system eg x,y

Determine Person Taxi pairs if each person walks to the closest taxi that will be available to them, and not taken by another person. Ties of two (or more) people to same taxi can go to the person of lower index. Ties of two (or more) taxis as closest taxi can go to taxi of lowest index.

Does this go by a common problem name? What algorithms exist?

As for what algorithms exist, I can think of 2 right off but I doubt they be considered efficient. 1. use recursion, eg find closest person taxi pair, remove both from available list, and run again. 2. enumerate (n squared) all possible P-T pairs into binary tree sorted by distance value, and then traverse binary tree starting with lowest value until each P has unique T. 3. or can someone offer better algorithms?
Thank you

Cris
  • 121
  • 2

2 Answers2

1

I would call this problem as closest matching between two equal sets with priority.

Here is an $O(n\log n)$ algorithm, assuming the distance is the 2-dimensional Euclidean distance. It might work with the Manhattan distance as well; however, I have not double checked that.

Build a balanced 2-d tree for all taxis in $O(n\log n)$ time. Attach to each node an inventory number, the number of all points in the rectangle or half plane represented by that node, which can be done in an $O(n)$-time tree traversal.

Now iterate over $P_1, P_2, \cdots, P_n$ in that order. For each person $P$, do the following.

Find all nearest taxis of $P$, using the inventory number and ignore the unavailable taxis, which takes (amortized) $O(\log n)$ time. Assign the taxi with the lowest index among them to $P$. Mark that taxi as unavailable. Subtract one from the inventory number attached to each node that contain $P$, which takes $O(\log n)$ time.

John L.
  • 39,205
  • 4
  • 34
  • 93
0

I would compute all taxi-people pair distances (O(n²)).

Then I would solve the Stable marriage problem with Gale–Shapley algorithm (O(n²)). People looking for the closest taxi and taxis looking for the closest person.

An interesting point of your problem is that it is indeed symmetric on people and taxis. If it is solved considering taxis looking for people, the solution would be the same.

It makes an overall O(n²) solution quite easy to implement.

Optidad
  • 1,788
  • 1
  • 10
  • 12