I have $n$ people seated around a circular table, initially in arbitrary order. At each step, I choose two people and switch their seats. What is the minimum number of steps required such that every person has sat either to the right or to the left of everyone else?

To be specific, we consider two different cases:

  1. You can only switch people who are sitting next to each other.
  2. You can switch any two people, no matter where they are on the table.

The small cases are relatively simple: if we denote the answer in case 1 and 2 for a given value of $n$ as $f(n)$ and $g(n)$ respectively, then we have $f(x)=g(x)=0$ for $x=1, 2, 3$, $f(4)=g(4)=1$. I'm not sure how I would generalize to larger values, though.

(I initially claimed that $f(5)=g(5)=2$, but corrected it based on @Ryan's comment).

If you're interested, this question came up in a conversation with my friends when we were trying to figure out the best way for a large party of people during dinner to all get to know each other.

Edit: The table below compares the current best known value for case 2, $g(n)$, to the theoretical lower bound $\lceil{\frac{1}{8}n(n-3)}\rceil$ for a range of values of $n$. Solutions up to $n=14$ are known to be optimal, in large part due to the work of Andrew Szymczak and PeterKošinár.

\begin{array} {|r|r|l|} \hline n & \text{Best known value of g(n)} & \left\lceil{\frac{1}{8}n(n-3)}\right\rceil & \text{Comments}\\ \hline 4 & 1 & 1 & \\ \hline 5 & 3 & 2 & \\ \hline 6 & 4 & 3 & \\ \hline 7 & 4 & 4 & \\ \hline 8 & 6 & 5 & \\ \hline 9 & 8 & 7 & \\ \hline 10 & 10 & 9 & \\ \hline 11 & 12 & 11 & \\ \hline 12 & 14 & 14 & \\ \hline 13 & 17 & 17 & \\ \hline 14 & 20 & 20 & \\ \hline 15 & 24 & 23 & \\ \hline 16 & 28 & 26 & \\ \hline 17 & 32 & 30 & \\ \hline 18 & 37 & 34 & \\ \hline 20 & 47 & 43 & \text{Loose upper bound}\\ \hline 25 & 77 & 69 & \text{Loose upper bound}\\ \hline 30 & 114 & 102 & \text{Loose upper bound}\\ \hline \end{array}

The moves corresponding to the current best value are found below. Each ordered pair $(i, j)$ indicates that we switch the people in seats $(i, j)$ with each other, with the seats being labeled from $1 \ldots n$ consecutively around the table.

4  - ((2,1))
5  - ((2,5),(1,5),(1,3))
6  - ((5,3),(1,5),(2,5),(3,6))
7  - ((4,7),(3,7),(1,5),(2,5))
8  - ((1,2),(4,7),(1,5),(3,7),(1,6),(2,5)) (h.t. PeterKošinár)
9  - ((3,8),(1,4),(6,9),(4,8),(1,6),(5,8),(2,8),(2,9))
10 - ((3,8),(4,8),(7,10),(1,7),(3,6),(1,5),(2,9),(3,7),(1,4),(3,9))
11 - ((4,8),(2,9),(5,8),(1,7),(3,9),(7,11),(5,10),(1,4),(5,9),(2,7),(2,6),(5,10))
12 - ((1,2),(5,10),(1,6),(4,10),(1,7),(8,11),(4,12),(3,12),(1,9),(1,5),(7,11) (1,8),(5,10),(2,6))
13 - ((1,2),(1,7),(3,9),(6,12),(8,11),(8,12),(1,11),(4,12),(9,12),(6,10),(7,10),(1,6),(2,8),(5,9),(3,8),(8,12),(9,12))
14 - ((1,4),(1,5),(1,8),(1,12),(4,11),(4,12),(9,13),(1,12),(9,12),(6,10),(6,9),(1,9),(4,7),(4,13),(3,13),(3,10),(2,13),(2,7),(3,13),(4,12))
15 - ((0,3),(0,10),(4,7),(2,8),(1,8),(5,9),(3,14),(5,13),(2,11),(4,9),(5,14),(4,12),(2,6),(7,14),(0,3),(2,9),(6,10),(8,11),(0,12),(0,4),(0,7),(3,7),(3,10),(2,13))
16 - ((10,14),(10,13),(0,10),(5,9),(5,8),(2,12),(2,5),(7,12),(2,12),(3,14),(5,11),(0,5),(4,14),(4,7),(3,11),(3,10),(0,8),(0,9),(0,6),(3,6),(1,14),(11,15),(1,5),(6,14),(3,11),(11,14),(0,12),(1,4))
17 - ((9,15),(5,13),(13,16),(0,13),(2,10),(10,16),(5,16),(5,13),(2,6),(2,10),(10,16),(7,10),(4,15),(1,8),(4,9),(5,12),(4,10),(3,13),(5,14),(1,4),(5,15),(1,6),(5,12),(8,12),(7,12),(4,12),(0,12),(8,11),(8,14),(7,16),(2,3),(1,8))
18 - ((4,7),(4,14),(6,10),(7,13),(4,7),(8,16),(8,13),(7,13),(3,8),(0,8),(4,8),(6,16),(1,12),(1,5),(5,11),(0,5),(14,17),(1,13),(8,13),(3,13),(0,4),(11,16),(2,10),(11,17),(9,15),(10,15),(1,9),(2,13),(1,4),(5,12),(6,14),(7,16),(13,17),(0,15),(1,15),(6,10),(5,15))
# Note that some solutions are zero-indexed and some are one-indexed.

The code I used to generate my the results can be found on Github. Unless otherwise specified, the switches above were found by my code, using a randomized greedy approach. As demonstrated by PeterKošinár, since the total number of possibilities is large, this approach may not find the best result even after many trials.

Vincent Tjeng
  • 3,264
  • 1
  • 19
  • 34
  • 5
    Can you provide the 'switches' that result in $f(5)=2$? The best I can get using your rules is $f(5)=3$. – Ryan Jun 14 '14 at 04:15
  • 4
    Well, $f(n)\ge\tfrac14n(n-3)$ and $g(n)\ge\tfrac18n(n-3)$, so $f(5)$ is definitely $3$, and it's not hard to show $g(5)=3$ also. – Andrew Woods Jun 14 '14 at 11:46
  • It looks like $f(6)=5$ and $f(7)=9$. Example solutions: $0,3,5,1,3$ and $0,2,0,4,3,2,5,0,3$. – Andrew Woods Jun 14 '14 at 15:21
  • @Ryan My mistake, I messed up my calculations. – Vincent Tjeng Jun 15 '14 at 15:43
  • 4
    Also consider that "in random order" is really a red herring up to relabeling. That should make computer experiments a lot easier... – chubakueno Jun 15 '14 at 16:00
  • @chubakueno you're right. did you make any progress with it at all? – Vincent Tjeng Jun 22 '14 at 19:19
  • Apart from a bruteforce search, I can't think of something better. @AndrewWoods may you explain your reasoning to me? – chubakueno Jul 01 '14 at 06:31
  • 12
    @chubakueno We need a total of ${n\choose 2}$ pairs of people to sit next to each other. With regard to $f$, each time we perform a switch we add at most $2$ pairs. With regard to $g$, each time we perform a switch we add at most $4$ pairs. Since we already have $n$ pairs from the initial arrangement we have $2f(n)\ge{n\choose2}-n$ and $4g(n)\ge{n\choose2}-n$, which reduces to $f(n)\ge\frac{1}{4}n(n-3)$ and $g(n)\ge\frac{1}{8}n(n-3)$. – Peter Woolfitt Jul 01 '14 at 17:38
  • @PeterWoolfitt cool! I am currently trying to come up with some algotithm or upper bound so that a computer search doesn't run forever even for small values of $n$, but no luck... – chubakueno Jul 03 '14 at 03:18
  • 6
    @chubakueno, one upper bound for $f(n)$ is $\frac{1}{2}(n^2 - 3n + 2)$. Imagine $1...n$ in a line; $(1--n)$ is the circle connection. Move 1 all the way to between $n-1$ and $n$. That's $n-2$ moves, and $1$ has all its neighbors, plus $(2--n)$ from the circle connection. Bubble $2$ up until it is between $n-1$ and $1$ -- $n - 3$ moves. $2$ has all connections and $(3--n)$ is the circle connection. Keep going until you hit $(n-2)(n-1)(n-3)(n-4)...(2)(1)(n)$. You should get $(n-2) + (n-3) + \dotsb + 1$. – Charles Baker Jul 03 '14 at 22:34
  • 3
    The sequence of the $f(n)$ seems to be A200919 of the OEIS. No general formula is indicated there. – Pierre-Guy Plamondon Jul 07 '14 at 12:56
  • @chubakueno edited the problem above, if you're still interested. I'm particularly interested in the pattern for $n=4, 5, 6, 7, 8$; I'm confident that my best value is the actual value for those (unless I've written my exhaustive search wrongly). Tell me if you have any ideas! – Vincent Tjeng Jul 28 '14 at 02:48
  • @PeterWoolfitt pinging you just in case you're still interested! – Vincent Tjeng Jul 28 '14 at 02:49
  • Probably $f(n)=f(n-1)+ \left \lceil \dfrac{n}{2} \right \rceil$ and $g(n)=g(n-1)+\left \lceil \dfrac{n}{2} \right \rceil$. –  Nov 01 '14 at 13:21
  • @user170039 do you mind elaborating how you got to that bound, or was it just a conjecture? – Vincent Tjeng Nov 04 '14 at 00:29
  • 1
    @VincentTjeng: I haven't rigorously proved it so there may be some flaw in my argument. But suppose you know the value of $f(n)$. Then for $f(n+1)$ the problem is to satisfy the condition of sitting only for the $(n+1)$-th person. The minimization of the places in which $(n+1)$-th person can sit lead me to the formula. Similar argument applies to $g(n)$. –  Nov 04 '14 at 05:20
  • Have you thought about my "solution"? –  Nov 24 '14 at 13:45
  • 2
    @user170039 sorry for the delay. If I understand your solution correctly, you're suggesting that you can think of $n$ people making friends with each other in $g(n)$ time, and then inserting the $(n+1)$-th person who can get to know everyone in $\lceil (n+1)/2 \rceil$. I don't think that this is correct, since you cannot simply ignore the $(n+1)$-th person: even if he sat on the table and did not move, he would affect the friendships that are formed on the table (since you only make friends with people you sit directly next to). – Vincent Tjeng Nov 25 '14 at 16:23
  • 1
    What would be the answer if we could permute the people any way we like (instead of just switch)? (This time, the lower bound will be $\frac{n-3}{2}$). Not sure if it helps, but switches could be viewed as the conjugation action of transpositions on $n$-cycles, and the different scenarios in the problem are paths in the coset graph $S_n/<(1,...,n)>$ with transpositions as generators (adjacent transpositions in $f$ and any in $g$). – Daugmented Dec 20 '14 at 19:16
  • @Daugmented by permuting the people, do you mean to select a subset of the people and to rotate their position on the table while keeping the rest of the people fixed (and thus switching is a special case of permutation with sets of 2?) – Vincent Tjeng Dec 26 '14 at 19:02
  • 1
    By permutation of the people, I meant a bijection $\pi$ on the set of positions around the table moving the person at position $i$ to position $\pi(i)$. The set of such permutation on $n$ positions is called the symmetric group, denoted $S_n$. It should be easier to solve now, with something close to $n/2$. – Daugmented Dec 27 '14 at 09:48
  • Maybe the strategy used for all permutations would give some insight for your problem? The comment about the coset graph, meant there was an algebraic representation of such permutations as an action of $S_n$ by conjugation on $n$-cycles (where $n$-cycles correspond with an arrangement of the people around the table and conjugation by a permutation $\pi$ is exactly moving the person at position $i$ to position $\pi(i)$. With an analog for the switch action using transpositions $(i,j)\in S_n$ which lead to the coset graph). – Daugmented Dec 27 '14 at 09:49
  • I still don't understand why we have two different functions. What are $f$ and $g$ and there meaning? – hlapointe Oct 25 '15 at 14:44
  • @hlapointe $f(n)$ refers to the number of switches you need when you can only switch people who are sitting next to each other, and $g(n)$ to the number of switches you need when you can switch any pair of people. For a given value of $n$, we would expect $f(n) \ge g(n)$ – Vincent Tjeng Oct 25 '15 at 15:35
  • 3
    A related problem is that of "acquaintance time" on graphs. The difference is that for acquaintance time, multiple disjoint pairs of people are allowed to change seats at once in a single time step (which might actually fit your dinner party model better). In "Acquaintance Time of a Graph" (http://arxiv.org/pdf/1302.2787v4.pdf ) Benjamini, Shinkar, and Tsur show that on the path (and therefore on the cycle) everyone can be acquainted with everyone else in time $O(n)$. – Kevin P. Costello Oct 26 '15 at 19:27
  • 3
    A toy to play with https://jsfiddle.net/zn1f30mv/1/embedded/result/ – leonbloy Oct 31 '15 at 01:55
  • 2
    13 - 18, [(4,8)(3,6)(3,10)(8,13)(5,8)(2,8)(9,13)(3,11)(7,12)(4,8)(2,6)(2,12)(4,9)(6,10)(1,7)(6,11)(3,6)(1,6)] 14 - 21, [(9,12)(1,6)(1,10)(3,9)(8,14)(5,13)(8,13)(3,7)(4,11)(5,9)(1,4)(3,8)(2,13)(10,13)(6,12)(1,11)(5,8)(2,11)(7,12)(4,8)(8,14)] 15 - 25, [(8,12)(9,14)(3,15)(3,9)(1,7)(11,15)(3,15)(2,9)(6,14)(1,5)(1,11)(3,14)(9,12)(3,13)(4,7)(8,12)(4,10)(4,11)(10,13)(1,13)(7,10)(1,9)(3,14)(5,13)(5,8)] 16 - 29, [(3,10)(7,15)(10,16)(3,14)(5,13)(2,14)(6,11)(2,9)(2,12)(7,14)(2,8)(4,13)(1,4)(5,15)(8,11)(10,14)(2,14)(6,13)(6,12)(5,10)(10,16)(2,13)(7,14)(8,11)(4,10)(4,14)(2,10)(2,11)(6,14)] – Andrew Szymczak Jan 23 '16 at 00:03
  • 1
    Ran out of characters in the last comment. To summarize, for n=13..16 I found solutions with one less swap than what you have listed. – Andrew Szymczak Feb 05 '16 at 05:25
  • I think that the swaps I have up there are far from optimal - thanks for showing that the bound can be pushed down slightly. – Vincent Tjeng Feb 06 '16 at 22:32
  • This looks like it could be related to [the Futurama Theorem](https://en.wikipedia.org/wiki/The_Prisoner_of_Benda#The_theorem). – SQB Feb 11 '16 at 09:15
  • 3
    I think you only really *truly* know somebody once you've sat on both their left *and* their right. Then the minimum number of moves is much simpler to calculate! – samerivertwice May 19 '16 at 13:48
  • 3
    I would like to point out that for case two, any people who have sat next to everyone else could be considered an empty seat, since no one else needs to sit by them. You can then use this idea of empty seats to possibly generalize the case to $n$ people and $m$ chairs, where $m\geq n$. This is just an idea that might help. – AlgorithmsX Sep 04 '16 at 20:46
  • A few observations: It seems that there is always an optimal sequence for the second problem even if we require that one specific person never moves (verified for $4\leq n\leq 12$, sans possible coding mistakes). Also, my quick search suggests that the value of $g(8)$ in the table is incorrect and it should be $g(8)=6$ (e.g. $[(1,2), (4,7), (2,5), (3,4), (5,6), (1,2)]$). Similarly for $n=12$, my search suggests $g(12)=14$. The full sequence of values of $g(n)$ for $4\leq n\leq 12$ would then be $1,3,4,4,6,8,10,12,14$. – Peter Košinár Jan 13 '17 at 16:14
  • (and I forgot to mention -- the pairs in my $g(8)$ example are identifiers of _people_ who swap, rather than _seats_) – Peter Košinár Jan 13 '17 at 17:31
  • @PeterKošinár Bravo. I checked the value for $g(8)$ by hand, and you were correct. 1) How did you run the search? 2) Could you link to the code supporting your conjecture (which I believe to be correct) that you can fix an individual's position? (I'll update the main table soon). 3) It appears from this example that a greedy approach is not always best - in this case we start with a switch that only generates 2 new friends. – Vincent Tjeng Jan 15 '17 at 20:53
  • @AndrewSzymczak - would you care to share the code you used to find your swaps? My code took a relatively long time to match your performance, so if you have something faster that might be helpful. – Vincent Tjeng Jan 17 '17 at 17:07
  • 1
    @VincentTjeng my code is pretty slow too, and written in Python to boot. I'm just doing a randomized-greedy method. To choose the next swap I score each possible swap by the number of new friends, and out of all the highest scored I pick one at random. Pretty sure I ran my code overnight to get the values. I'm going to play around with this tonight and see if I can do better, but here is the current code: https://gist.github.com/AndrewSzymczak/6c9613b0dd4021ddfad71dbb529fe851 – Andrew Szymczak Jan 18 '17 at 00:42
  • @VincentTjeng it turns out that my greedy algorithm isn't even guaranteed to find the optimal solution. Doing an exhaustive search of the full greedy space for n=8 only finds the 7-swap solution. – Andrew Szymczak Jan 18 '17 at 01:43
  • 1
    A little further improvement (using the standard "seat" notation now): $g(13)=17$ (= the lower bound); as demonstrated by $[(1,2) (1,7) (3,9) (6,12) (8,11) (8,12) (1,11) (4,12) (9,12) (6,10) (7,10) (1,6) (2,8) (5,9) (3,8) (8,12) (9,12)]$. As for $g(12)=14$, one such sequence is $[(1,4) (1,9) (4,7) (7,3) (7,11) (1,10) (4,1) (1,8) (4,7) (6,1) (10,4) (5,1) (2,11) (9,4)]$. Both sequences satisfy the one-person-doesn't-move property too. My approach was just pretty straight-foward exhaustive search (I am currently playing with a few heuristics for speeding it up, but without much success yet). – Peter Košinár Jan 19 '17 at 06:00
  • 1
    @PeterKošinár It would be great if you could share your code when you have some time. If understand correctly, you carry out an exhaustive search where you try *every* possible combination with one person remaining in place? If you've run the exhaustive search for $n=9, 10, 11$ and not found something better, that's really interesting - I would have expected (without much evidence) the gap between the lower bound and the actual optimal value to be monotonically increasing. – Vincent Tjeng Jan 19 '17 at 15:05
  • $g(14)=20$: $(1,4) (1,5) (1,8) (1,12) (4,11) (4,12) (9,13) (1,12) (9,12) (6,10) (6,9) (1,9) (4,7) (4,13) (3,13) (3,10) (2,13) (2,7) (3,13) (4,12)$ (feel free to verify independently); found by greedy search with one-fixed-seat extra condition. – Peter Košinár Jan 26 '17 at 09:50
  • 1
    Haven't checked yours, but I obtained $g(14)=20$ with $(7, 10), (5, 11), (1, 6), (4, 11), (1, 7), (10, 13), (7, 10), (4, 8), (2, 12), (6, 12), (3, 6), (0, 7), (2, 8), (7, 10), (0, 6), (1, 12), (4, 9), (4, 10), (1, 9), (1, 10)$, so it seems likely to be correct. – Vincent Tjeng Jan 26 '17 at 13:30
  • Have one for $g(18)\leq 36$: $(1,4) (1,5) (1,8) (1,7) (1,10) (1,6) (1,12) (1,9) (1,13) (1,14) (1,11) (1,15) (2,17) (2,7) (2,6) (2,13) (4,14) (7,11) (11,16) (4,15) (11,14) (2,10) (4,9) (3,13) (7,15) (4,15) (5,15) (2,8) (1,14) (5,13) (1,5) (5,11) (9,15) (5,15) (11,16) (12,15)$. – Peter Košinár Jan 26 '17 at 18:18
  • 1
    And further one $g(18)\leq 35$: $(1,4) (1,5) (1,8) (1,7) (1,10) (1,6) (1,12) (1,9) (1,13) (1,14) (1,11) (3,17) (2,17) (3,14) (3,15) (3,9) (7,15) (6,10) (2,9) (2,16) (2,5) (4,11) (5,9) (10,13) (1,9) (3,14) (10,15) (5,11) (4,9) (3,7) (3,9) (7,13) (12,15) (1,16) (2,11)$. – Peter Košinár Jan 31 '17 at 12:10

2 Answers2


I didn't want to keep adding more and more comments, so I'll just post all my thoughts here. @PeterKošinár @VincentTjeng

States are only different if their underlying friend-graphs are non-isomorphic -- permuting the people-labels (or seat-labels) does not change the state you're in. You can drastically reduce your search space this way. For n=8 there are only ~3000 states within 6 swaps of the starting position. As an example, consider the very first swap. There are really only $\lfloor \tfrac{n}{2} \rfloor$ different swaps to make.

The problem is coming up with a canonical-representation for graphs. Fortunately, to reduce your state space you don't need a truly canonical representation. You only need to make sure you don't equate two non-isomorphic graphs. It's okay if isomorphic $A \sim B$ have different representations - it just means your search space has a few redundancies. In any case, I've written my own method in Python because I couldn't get any packages to work (I'll post it once I clean it up). A port to C with nauty would speed up the algorithm by a factor of 200+.

One naive canonicalization method is to permute the graph in all $n!$ ways and take the one that results in the lexicographically smallest adjacency list (using the binary sequence '001100....' as your representation). I use the same method, but I have a way to limit the number of valid permutations (down from $n!$ to ~$n^2$ on average). For example, since isomorphic graphs have the same degree sequence, we can enforce that the nodes are labelled by increasing order of degree. This by itself leads to a fairly drastic reduction of valid permutations. My idea iterates on that and is similar to finding the coarsest equitable partition that is described in this paper. The paper's algorithm (implemented by nauty) then goes on to do other stuff that I don't do, namely creating a search tree.

My exhaustive search algorithm begins in the starting position and runs a breadth-first-search of the state-space. I process a single state by making all possible swaps and then converting the resulting states to canonical form. The $i$th step of the algorithm takes $D_i \rightarrow D_{i+1}$ where $D_i$ is the set of all states that are at a distance $i$ from the starting position. The algorithm terminates once the final state has been found (complete-graph $K_n$). Of course, an exhaustive search will no longer be tractable for $n>12$, but you can easily turn it into a monte-carlo-type method.

For $n=10..12$ the search space becomes too large to store in memory. So I combine my BFS with @PeterKošinár's memoryless DFS method. I run the BFS for the first 4 swaps and then run a DFS starting from each state in $D_4$ (without keeping track of visited states). In the DFS we only make moves that add atleast 1 friend, and we prune states that can not possible beat the best solution found. We can make a max number of 8 friends per swap (counting duplicates (i,j) and (j,i)). So if we are in a state of $s$ swaps and $e$ friends, and we've found a solution with $s^*$ swaps, then we can prune if $s + \lceil (n^2 - e)/8 \rceil \geq s^*$.
$n=14$ distinct 4-swap sequences
$n=15$ distinct 4-swap sequences

I've run an exhaustive search for n=4..12 and can confirm the bounds we found as the true lower bounds. Here are the lexicographically smallest solutions (indices refer to seat-swaps).

  • 4 $\quad$ - $\quad$ 1.[(1,2)]
  • 5 $\quad$ - $\quad$ 3.[(1,2) (1,3) (1,4)]
  • 6 $\quad$ - $\quad$ 4.[(1,2) (1,3) (1,5) (1,4)]
  • 7 $\quad$ - $\quad$ 4.[(1,4) (1,5) (3,6) (2,6)]
  • 8 $\quad$ - $\quad$ 6.[(1,2) (4,7) (1,5) (3,7) (1,6) (2,5)]
  • 9 $\quad$ - $\quad$ 8.[(1,4) (1,5) (1,7) (1,6) (3,8) (2,8) (3,6) (1,5)]
  • 10 $\quad$ - $\quad$ 10.[(1,2) (1,5) (1,6) (3,8) (4,10) (3,7) (5,8) (3,9) (3,10) (1,6)]
  • 11 $\quad$ - $\quad$ 12.[(1,2) (1,4) (3,7) (6,9) (6,10) (1,5) (2,7) (3,11) (4,9) (4,8) (6,10) (4,11)]
  • 12 $\quad$ - $\quad$ 14.[(1,2) (5,10) (1,6) (4,10) (1,7) (8,11) (4,12) (3,12) (1,9) (1,5) (7,11) (1,8) (5,10) (2,6)]
Andrew Szymczak
  • 1,460
  • 12
  • 19
  • Thanks for writing this up! Could you elaborate on your representation for the graphs (once you've cleaned up the code) - I think that'll really add to this answer. Also, when you say that an exhaustive search is no longer feasible for $n>13$, you do mean that it's possible but will just take much more time, right? – Vincent Tjeng Jan 21 '17 at 21:33
  • Post updated with a brief explanation of my canonicalization function. For n=13 there are 50,502,031,367,952 non-isomorphic graphs. By empirical results I'm guessing the state-space is ~1/5 that. So... yea even 13 is probably not tractable. – Andrew Szymczak Jan 21 '17 at 22:48
  • Thank you for the list of solutions -- it matches what my exhaustive search coded in C found (so either we both have the same mistake in our codes, or both work well ;-) ). My search was not using any special heuristics apart from the one which compared number of missing edges in the graph with the total number of steps remaining until the best known solution (since each steps adds at most four edges); did you employ something such to reduce the number of explored states? – Peter Košinár Jan 22 '17 at 10:47
  • Also, it would be nice to have the best results found for $n=10$ and $n=11$ confirmed by independent exhaustive search; although I trust my program, mistakes can always happen :-) Of course, $n=12$ and $n=13$ aren't necessary, since they already match the proven lower bound and beyond that the (exhaustive) search really looks intractable so far. – Peter Košinár Jan 22 '17 at 10:49
  • @PeterKošinár how exactly are you representing states in your search -- are you converting the graphs to canonical form as well? If you're not reducing the state space, then you must be running an A* search? I just sped up my canonicalization code and can run 10-11 in reasonable time, but I'm having memory issues now. I know I can use a DFS to reduce memory by a factor of $\mathcal{O}(n^2)$ for $n$ seats and perhaps even better using a topological iteration. But I think A* is probably my best bet for both runtime and memory. Implementing now. – Andrew Szymczak Jan 22 '17 at 19:06
  • In fact, I was just using pure backtracking with _no_ optimization when it comes to states -- both isomorphic and even _identical_ states were visited more than once. The only pruning applied was the one I mentioned (i.e. returning if I can be sure the remaining steps cannot produce sufficient number of edges) and only taking a swap if it produced at least one new edge. – Peter Košinár Jan 22 '17 at 19:42
  • Oh I see, so you are iterating depth-first over all swap sequences and pruning based on the current best solution found. I can actually combine that with my shortest-path algorithm and get something running right now. Good stuff. – Andrew Szymczak Jan 22 '17 at 21:52
  • @PeterKošinár I've confirmed your values for $n=10$ and $n=11$ as the true lower bounds (added to post). I've also composed a list of all 4-swap sequences for $n=14$ and $n=15$ pruned by your method (on values 21 and 24 resp.) and distinct up to isomorphism. Starting your search with these swaps will drastically reduce the search space. If your search ends without finding anything then that means your current solutions are optimal. Links at bottom of post. – Andrew Szymczak Jan 23 '17 at 08:56
  • As an update, my randomized algorithm found an example with 17 swaps with $n=13$. Here's the sequence: [(1, 5), (2, 12), (2, 8), (7, 12), (2, 12), (4, 10), (6, 9), (4, 11), (2, 6), (2, 5), (9, 12), (7, 10), (0, 4), (4, 12), (7, 11), (3, 9), (1, 8)] – Vincent Tjeng Jan 24 '17 at 12:02
  • @VincentTjeng My comment on your question shows one such as well (it also satisfies the one-fixed-seat condition). I am tempted to conjecture that the lower bound is actually quite close to being optimal; possibly differing at most by a constant difference only (admittedly, basing it only on the data seen so far, no deeper insight yet). Furthermore, the one-person-fixed rule has been proven preserve optimality for $n\leq 13$ now and it might even work for $n=14$ (I have a solution with $21$ steps which satisfies it) – Peter Košinár Jan 24 '17 at 14:51
  • Ah, sorry. I was confused why you did not have a solution listed above - I realize now that you were listing lexicographically smallest examples. – Vincent Tjeng Jan 24 '17 at 21:03
  • @AndrewSzymczak Thanks for the lists! I am running my DFS code on them now; but, predictably, even a single DFS run still takes quite some time to complete, so I wouldn't expect any results in near future. – Peter Košinár Jan 24 '17 at 22:54
  • 1
    @VincentTjeng Meanhwile, a DFS with two additional restrictions (one person doesn't move, plus each swap needs to be worse or equal to the previous one in terms of newly added edges to the friendship graph) has found a solution confirming $g(14)=20$ (I'll add it in a comment to the question). Of course, as we know from $n=8$, this approach is not guaranteed to find the optimal solution in all cases. – Peter Košinár Jan 26 '17 at 09:49
  • I made a mistake, it turns out graph-isomorphism isn't the whole story. The state space is actually (n-1)!/2 times larger because the table configurations come into play. Now I am completely out of ideas for an exhaustive search for $n \geq 14$. I'm having a hard time proving any of your restrictions. Consider even the restriction where we only allow swaps that increase the number of friends. Let $n=6$ and make the following seat-swaps (1,2),(1,3),(2,6). This is 2 swaps away at best. If we then make the swap (1,3) (giving us zero friends), we are only 1 swap away. – Andrew Szymczak Jan 26 '17 at 22:08
  • @AndrewSzymczak I'm not sure that that is a counter-example, since we're not trying to find *all* optimal sequences of swaps, but just one such sequence - we simply have to show that for each $n$ there exists some optimal sequence of swaps where each swap satisfies the restriction we're attempting to impose (e.g. swaps must increase the number of friends), right? – Vincent Tjeng Jan 31 '17 at 19:24
  • @PeterKošinár right, I was just trying to say that any exhaustive search we run can't utilize the restrictions until we come up with such a proof. Of course, we are at values of $n$ at which an exhaustive search is intractable anyway, so I guess it's a moot point. – Andrew Szymczak Jan 31 '17 at 22:03
  • @AndrewSzymczak You might have mis-directed your last comment ;-) My observations (the fixed seat / very-greedy / ...) are only heuristics that can be used to speed up the searches without any guarantees for success... so unless we hit the lower bounds, we cannot be sure about optimality of the solution (I haven't found a proof for any of them either, yet). That being said, they certainly allowed my searches to push the best known values a bit even in the area where exhaustive search is useless (as you can see in my comments on the question) – Peter Košinár Jan 31 '17 at 22:22

Not an answer, but a toy to play with https://jsfiddle.net/zn1f30mv/14/embedded/result/

  • 56,395
  • 9
  • 64
  • 139