Spanning paths in a tournament on n nodes The goal of this challenge is to extend the On-Line Encyclopedia of Integer Sequences (OEIS) sequence A038375.


  Maximal number of spanning paths in tournament on n nodes.


A tournament on \$n\$ vertices is a directed graph where for each pair of vertices \$(v_i, v_j)\$ there is either a directed edge \$v_i \rightarrow v_j\$ or a directed edge \$v_j \rightarrow v_i\$.

A spanning path or Hamiltonian path is an ordering of all of the vertices of a graph, \$(v_1, v_2, ..., v_n)\$ such that there is an edge \$v_i \rightarrow v_{i+1}\$ for all \$i&lt;n\$.

Example



In the above tournament, there are five Hamiltonian paths:


\$(1,2,4,3)\$
\$(1,4,3,2)\$
\$(2,4,3,1)\$
\$(3,1,2,4)\$
\$(4,3,1,2)\$


Moreover, it is not possible to construct a tournament on four vertices with more than five Hamiltonian paths, so \$A038375(4) = 5\$.



The way to win this fastest-code challenge is to write a program that can compute the largest value of A038375 deterministically in under 10 minutes on my machine, a 2017 MacBook Pro with 8GB RAM and a 2.3 GHz i5 processor.

In the case of a tie at the end of ten minutes, the winning code will be the one that got its final value fastest.
C++ (gcc), \$N=10\$



#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;cstring&gt;
#include &lt;ctime&gt;
//a rewrite for vector&lt;int&gt;
struct vi
{
&#9;int l,a[9];
&#9;const int&amp; operator[](int u) const {return a[u];}
&#9;int&amp; operator[](int u) {return a[u];}
};
bool operator &lt; (const vi&amp;a,const vi&amp;b)
{
&#9;for(int i=0;i&lt;a.l;++i) if(a[i]!=b[i]) return a[i]&lt;b[i];
&#9;return 0;
}
bool operator == (const vi&amp;a,const vi&amp;b)
{
&#9;for(int i=0;i&lt;a.l;++i) if(a[i]!=b[i]) return 0;
&#9;return 1;
}
std::vector&lt;vi&gt; g[13];
void dfs(int x,int u,const vi&amp;c,int*w)
{
&#9;++w[u];
&#9;if(c[x]&amp;u)
&#9;&#9;for(int o=c[x]&amp;u;o;o^=o&amp;-o)
&#9;&#9;&#9;dfs(__builtin_ctz(o),u^(o&amp;-o),c,w);
}
void dfsf(int x,int u,const vi&amp;c,int*w)
{
&#9;++w[u];
&#9;if((~c[x])&amp;u)
&#9;&#9;for(int o=(~c[x])&amp;u;o;o^=o&amp;-o)
&#9;&#9;&#9;dfsf(__builtin_ctz(o),u^(o&amp;-o),c,w);
}
int main()
{
&#9;g[0].push_back(vi());
&#9;for(int i=0;i&lt;9;++i)
&#9;{
&#9;&#9;for(auto t:g[i])
&#9;&#9;{
&#9;&#9;&#9;int m1=0,m=0;
&#9;&#9;&#9;for(int j=0,r;j&lt;i;++j)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;if((r=__builtin_popcount(t[j]))&gt;m1) m=0,m1=r;
&#9;&#9;&#9;&#9;if(r==m1) m|=1&lt;&lt;j;
&#9;&#9;&#9;}
&#9;&#9;&#9;for(int e=0;e&lt;(1&lt;&lt;i);++e) if(__builtin_popcount(e)&gt;m1
&#9;&#9;&#9;||(__builtin_popcount(e)==m1&amp;&amp;((e&amp;m)==m))) //ensure the node has the highest degree
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;vi u; u.l=i+1;
&#9;&#9;&#9;&#9;for(int p=0;p&lt;=i;++p) //enumerate position
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;bool s=(p==0); int x;
&#9;&#9;&#9;&#9;&#9;#define yield(l,c) do{if((x=(c))&gt;u[l]&amp;&amp;!s) goto skip; else s|=x&lt;u[l],u[l]=x;}while(0)
&#9;&#9;&#9;&#9;&#9;for(int a=0;a&lt;p;++a)
&#9;&#9;&#9;&#9;&#9;&#9;yield(a,((t[a]&gt;&gt;p)&lt;&lt;(p+1))|(t[a]&amp;((1&lt;&lt;p)-1))|((!((e&gt;&gt;a)&amp;1))&lt;&lt;p));
&#9;&#9;&#9;&#9;&#9;yield(p,((e&gt;&gt;p)&lt;&lt;(p+1))|(e&amp;((1&lt;&lt;p)-1)));
&#9;&#9;&#9;&#9;&#9;for(int a=p;a&lt;i;++a)
&#9;&#9;&#9;&#9;&#9;&#9;yield(a+1,((t[a]&gt;&gt;p)&lt;&lt;(p+1))|(t[a]&amp;((1&lt;&lt;p)-1))|((!((e&gt;&gt;a)&amp;1))&lt;&lt;p));
&#9;&#9;&#9;&#9;&#9;skip:;
&#9;&#9;&#9;&#9;&#9;#undef yield
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;g[i+1].push_back(u);
&#9;&#9;&#9;}
&#9;&#9;}
&#9;&#9;std::sort(g[i+1].begin(),g[i+1].end());
&#9;&#9;g[i+1].erase(std::unique(g[i+1].begin(),g[i+1].end()),g[i+1].end());
&#9;&#9;int sz=g[i+1].size();
&#9;&#9;std::vector&lt;vi&gt; h(sz);
&#9;&#9;#pragma omp parallel for
&#9;&#9;for(int w=0;w&lt;sz;++w)
&#9;&#9;{
&#9;&#9;&#9;vi u=g[i+1][w],v;
&#9;&#9;&#9;auto sw=[&amp;](int x,int y) {
&#9;&#9;&#9;&#9;std::swap(v[x],v[y]);
&#9;&#9;&#9;&#9;for(int s=0;s&lt;v.l;++s)
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;int&amp;o=v[s];
&#9;&#9;&#9;&#9;&#9;if(((o&gt;&gt;x)^(o&gt;&gt;y))&amp;1) o^=(1&lt;&lt;x)|(1&lt;&lt;y);
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;};
&#9;&#9;&#9;for(int x=0;x&lt;=i;++x)
&#9;&#9;&#9;&#9;for(int y=x+1;y&lt;=i;++y)
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,y);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;for(int z=y+1;z&lt;=i;++z)
&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,y);sw(y,z);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,y);sw(x,z);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(y,z);sw(x,z);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(y,z);sw(x,y);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,z);sw(x,y);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,z);sw(y,z);u=std::min(u,v);}
&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;&#9;{v=u;sw(x,y);u=std::min(u,v);}
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;h[w]=u;
&#9;&#9;}
&#9;&#9;std::sort(h.begin(),h.end());
&#9;&#9;h.erase(std::unique(h.begin(),h.end()),h.end());
&#9;&#9;g[i+1]=h;
&#9;}
&#9;std::cout&lt;&lt;"pre ("&lt;&lt;clock()*1./CLOCKS_PER_SEC&lt;&lt;"s)\n";
&#9;for(int o=0;o&lt;=9;++o)
&#9;{
&#9;&#9;int mx=0;
&#9;&#9;const int s=g[o].size();
&#9;&#9;#pragma omp parallel for reduction(max:mx)
&#9;&#9;for(int t=0;t&lt;s;++t)
&#9;&#9;{
&#9;&#9;&#9;int a[9&lt;&lt;9],b[9&lt;&lt;9];
&#9;&#9;&#9;memset(a,0,sizeof(int)*(o&lt;&lt;o));
&#9;&#9;&#9;memset(b,0,sizeof(int)*(o&lt;&lt;o));
&#9;&#9;&#9;for(int i=0;i&lt;o;++i)
&#9;&#9;&#9;&#9;dfs(i,((1&lt;&lt;o)-1)^(1&lt;&lt;i),g[o][t],a+(i&lt;&lt;o)),
&#9;&#9;&#9;&#9;dfsf(i,((1&lt;&lt;o)-1)^(1&lt;&lt;i),g[o][t],b+(i&lt;&lt;o));
&#9;&#9;&#9;int r[9][9],c=(1&lt;&lt;o)-1,vx[1&lt;&lt;9];
&#9;&#9;&#9;for(int p=0;p&lt;o;++p)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;int*A=a+(p&lt;&lt;o),vn=0;
&#9;&#9;&#9;&#9;for(int x=0;x&lt;=c;++x) if(A[x]) vx[vn++]=x;
&#9;&#9;&#9;&#9;for(int q=0;q&lt;o;++q) if(p!=q)
&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;int*B=b+(q&lt;&lt;o),su=0;
&#9;&#9;&#9;&#9;&#9;for(int u=0;u&lt;vn;++u)
&#9;&#9;&#9;&#9;&#9;&#9;su+=A[vx[u]]*B[c^vx[u]];
&#9;&#9;&#9;&#9;&#9;r[p][q]=su;
&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;}
&#9;&#9;&#9;int m1=0,m=0;
&#9;&#9;&#9;for(int j=0,pc;j&lt;o;++j)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;if((pc=__builtin_popcount(g[o][t][j]))&gt;m1) m=0,m1=pc;
&#9;&#9;&#9;&#9;if(pc==m1) m|=1&lt;&lt;j;
&#9;&#9;&#9;}
&#9;&#9;&#9;for(int i=0;i&lt;(1&lt;&lt;o);++i) if(__builtin_popcount(i)&gt;m1
&#9;&#9;&#9;||(__builtin_popcount(i)==m1&amp;&amp;((i&amp;m)==m)))
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;int tot=(o==0);
&#9;&#9;&#9;&#9;for(int j=0;j&lt;o;++j)
&#9;&#9;&#9;&#9;&#9;if(i&amp;(1&lt;&lt;j))
&#9;&#9;&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;&#9;&#9;tot+=a[j&lt;&lt;o];
&#9;&#9;&#9;&#9;&#9;&#9;for(int ii=c^i;ii;ii^=ii&amp;-ii)
&#9;&#9;&#9;&#9;&#9;&#9;&#9;tot+=r[j][__builtin_ctz(ii)];
&#9;&#9;&#9;&#9;&#9;}
&#9;&#9;&#9;&#9;&#9;else tot+=b[j&lt;&lt;o];
&#9;&#9;&#9;&#9;mx=std::max(mx,tot);
&#9;&#9;&#9;}
&#9;&#9;}
&#9;&#9;std::cout&lt;&lt;"a("&lt;&lt;o+1&lt;&lt;") = "&lt;&lt;mx&lt;&lt;" ("&lt;&lt;clock()*1./CLOCKS_PER_SEC&lt;&lt;"s)\n";
&#9;}
}


Try it online!

Compile with g++ and -Ofast -funroll-all-loops -ffast-math -fno-stack-protector -march=native -fopenmp. Note that OpenMP is used for parallelism.

Output on my modest computer:

pre (5.217s)
a(1) = 1 (5.218s)
a(2) = 1 (5.218s)
a(3) = 3 (5.219s)
a(4) = 5 (5.221s)
a(5) = 15 (5.221s)
a(6) = 45 (5.222s)
a(7) = 189 (5.223s)
a(8) = 661 (5.225s)
a(9) = 3357 (5.318s)
a(10) = 15745 (18.634s)


The run time may differ a lot because of the use of OpenMP parallelism.



Explanation for \$N=9\$ version:

This code is two-fold. First, it will try to generate all tournament graphs up to isomorphism. Then, it will count the number of hamilton paths on these generated graphs. Adjacency matrices of graphs are stored as arrays of bitmasks (i.e. arrays of uint) for performance reasons.

The second part is relatively easier. We can just use depth-first-search to search for all possible paths and use bitmasks to speed up. The time complexity is \$O(\text{no. of paths in all graphs})\$. It's also possible to use bitmask dynamic programming to do it, with \$O(2^NN^2)\$. The above code used the former while the latter one should be faster when N goes higher. OpenMP parallelization is used to speed it up.

The first part is harder. My approach is incremental: first generating all tournament graphs of \$t\$ nodes, then add one node to be tournament graphs of \$t+1\$ nodes. The trick here is to find an order for adding nodes. Here, instead of adding any node, we restrict the added node to have the greatest degree in the result graph. Therefore, a graph won't be generated too many times.

Graph-isomorphism isn't a easy thing to check too. Therefore we're not really trying to make every graph unique, but to have some degree of tolerance. I have two strategies used here. First, the newest \$t+1\$-th node won't be directly assigned number \$t\$. Instead, we try all the possible numbers from \$0\$ to \$t\$ and take the result graph with the smallest lexicographic order (you can use early-stopping when comparing lexicographic order, so not much overhead). After generating all \$t+1\$-node graphs in this way, we remove the found duplicates by sorting and making them unique. This alone can cut down the number of found graphs for \$N=9\$ to around \$3\times 10^6\$.

The second strategy is that, after found such candidates, we further try to remove duplicates by trying to swap every two nodes and see if we can get a graph with smaller lexicographic order. We then proceed by sorting and making them unique. These two strategies cut down the number of found graphs for \$N=9\$ to around \$10^6\$. (This strategy alone is more powerful than I thought, cutting down to around \$1.1\times 10^6\$)



Explanation for \$N=10\$ version:

Some improvements are made upon the \$N=9\$ version. First, instead of swapping two positions, three positions are swapped to achieve a better result. This successfully cut the number of graphs generated at \$N=9\$ to less than \$5 \times 10^5\$ (~50% improvement). OpenMP parallelization is also introduced here to speed up.

Generating all graphs for \$N=10\$ sounds unfeasible, so instead I try to expand the last step. We still try to add one node for graphs with \$9\$ nodes, but instead of actually generating these new graphs, we can actually count the hamilton paths in them directly!

Consider the relationship between a hamilton path and the new node. The new node can be the first end of the path so we need to find an edge from the new node and continue the path from that node. The new node can also be the second end so we can find an edge to the new node and trace back the path starting from that node.

Of course, the new node can be in the middle of the path. In this case, we can enumerate the two nodes in front and on the back of it. We can then enumerate the subset of nodes both halves of the path contain and then multiply them to get the result.

Notice that the contribution is the same for every pair of nodes adjacent to the new node regardless of other edges to the new node, so we can get that precalculated for every two nodes.

In order to get the number of paths starting and ending on each node covering a certain subset of nodes, we can use depth-first-search from each starting and ending point. (A bitmask dynamic programming could work too, but I guess it's slower)