Algorithm for accommodating people with most common hobbies

My data on people and hobbies, with many-to-many relationships. Each person has at least one hobby.

I need to find a way to put all the people around N seating tables so that there is a maximum number of common hobbies between the people in each table. It is not necessary that each table has at least one joint hobby between all the people sitting around it. In addition, the tables need not be completely populated.

Any ideas would be highly appreciated.

+4
source share
1 answer

First of all, this is an NP-hard problem - for example, any algorithm that can solve your problem can solve the longest path of a weighted graph problem as follows:

  • Each vertex of the graph is human
  • Any edge of the graph can be encoded by creating a new hobby shared only by two vertices / faces of the edge

(Perhaps a simpler problem with an NP-complete problem, but I think it will be done).

Skiena will offer a simulated annealing , start with any arrangement and make a random choice. The trick gradually reduces the likelihood that you will agree to a deteriorating change (thus, at first you allow it a little freedom to avoid local optima, but it gradually โ€œcoolsโ€ and stabilizes in a certain area and makes small refinements). You can also run it several times and support the best solution.

Perhaps you can put together something less complicated in the same vein. I assume that your instances of problems will be small, so you will surely find good solutions if you try many of them (and try to clarify them randomly).

Of course, if your problems are very small, you can simply list all the permutations and find the best location. (If you have n people, make sure you fix someoneโ€™s position and only generate permutations for n - 1 people).

+2
source

All Articles