The comments already mentioned that this problem is equivalent to the traveling salesman problem. I would like to dwell on this:
Each person is equivalent to a vertex, and the ribs are between the vertices, which represent people who can sit with each other. Now finding the possible seat arrangement is equivalent to finding the Hamiltonian trajectory on the graph.
So this problem is NPC. The most naive solution would be to try all possible permutations leading to O(n!) Time. There are many well-known approaches that work better than O(n!) And are freely available on the Internet. I would like to mention Held-Karp , which works in O(n^2*2^n) and is pretty directly transcoded to the code, here in python:
Disclaimer: This code is not properly verified, but I hope you can get the gist of it.
source share