Abstract 1 Introduction 2 Preliminaries 3 Common framework 4 Self reduction for hafnian 5 Data structure for hafnian 6 Data structure for Hamiltonian cycles 7 The algorithms References

Counting Perfect Matchings and Hamiltonian Cycles Faster

Baitian Li ORCID Columbia University, New York, NY, USA
Abstract

We show that the hafnian of a symmetric 2n×2n matrix of poly(n)-bit integers (which counts the number of perfect matchings of a 2n-vertex graph) and the number of Hamiltonian cycles of an n-vertex directed graph can be computed in time 2nΩ(n), improving and generalizing an earlier algorithm of Björklund, Kaski, and Williams (Algorithmica 2019) that runs in time 2nΩ(n/loglogn).

A key tool of our approach is the design of a data structure that supports fast evaluation of high-order derivatives of hafnian and Hamiltonian cycles, which integrates with the new approach on multivariate multipoint evaluation by Bhargava, Ghosh, Guo, Kumar, and Umans (FOCS 2022, JACM 2024).

Keywords and phrases:
permanent, hafnian, Hamiltonian cycle, Kakeya sets
Category:
Track A: Algorithms, Complexity and Games
Funding:
Baitian Li: Supported in part by NSF Grant CCF-2238221, a Packard Foundation Fellowship, and a Columbia SEAS Presidential Fellowship.
Copyright and License:
[Uncaptioned image] © Baitian Li; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Design and analysis of algorithms
Acknowledgements:
The work was done when the author was an undergraduate student at Tsinghua University. The author would like to thank Josh Alman and anonymous referees for helpful comments on earlier drafts.
Editors:
Sayan Bhattacharya, Danupon Nanongkai, Michael Benedikt, and Gabriele Puppis

1 Introduction

Given an n×n matrix A over a commutative ring R, the R-Permanent is defined by

perA=σSni=1nAi,σ(i),

where Sn denotes the symmetric group on [n], i.e., permutations of {1,,n}. Similarly, R-HamCycles is defined as

hcA=σSnc(σ)=1i=1nAi,σ(i),

where c(σ) denotes the number of cycles in σ.

The permanent and Hamiltonian cycles are two fundamental problems in computer science. The problem of deciding whether a given graph has a Hamiltonian cycle is one of Karp’s 21 𝖭𝖯-complete problems [19]. Valiant proved that over the integers, computing the permanent is #𝖯-complete, even if the entries of the matrix are restricted to 0 and 1 [24], and counting Hamiltonian cycles is also #𝖯-complete [25].

Ryser’s formula [23] shows that the permanent can be computed with O(n2n) arithmetic operations. It remains a prominent open problem whether the permanent can be computed with arithmetic circuits of size less than 2n, as mentioned by Knuth [21, Exercise 4.6.4.11].

Indeed, beyond the confines of arithmetic operations, faster algorithms for computing the permanent have emerged. Bax and Franklin [1] gave an algorithm that computes the 01-permanent in 2nΩ(n1/3/logn) expected time. For dense instances over finite fields and integers, Björklund [7] introduced a framework based on self-reduction and tabulation, achieving a running time of 2nΩ(n/logn). Björklund, Kaski, and Williams [11] refined the tabulation step via Kakeya sets, obtaining an improved running time 2nΩ(n/loglogn).

1.1 Our results

In this paper, we further improve the algorithm of Björklund, Kaski, and Williams [11], removing the loglogn term in the exponent. We also show how to extend the complexity bound for permanent to a natural extension called hafnian.

For a 2n×2n symmetric matrix A over a commutative ring R, the R-Hafnian of A is defined as

haf(A)=σP2n(i,j)σAi,j,

where P2n is the family of partitions of [2n] into n pairs. The permanent of an n×n matrix can be reduced to the hafnian of a 2n×2n matrix via the following basic relation:

per(A)=haf(0AA𝖳0).

Previously the best known algorithm for hafnian ran in time O~(2n) (for a 2n×2n matrix), first developed by Björklund [5] and alternatively by Cygan and Pilipczuk [15].

Theorem 1.

There is an algorithm that computes the permanent per(A) of a given matrix A𝔽qn×n in time 2nΩ(n)qO(1). The same bound holds for hafnian haf(A) of a given symmetric matrix A𝔽q2n×2n, and for computing Hamiltonian cycles hc(A) of a given matrix A𝔽qn×n.

The Chinese remainder theorem and a simple estimate of prime products yield the following corollary for integer-valued matrices.

Corollary 2.

Given a 2n×2n symmetric matrix with integer entries having absolute values bounded by M, we can compute haf(A) (or per(A)) in time 2nΩ(n)(logM)O(1). The same type of bound holds for computing Hamiltonian cycles hc(A) of a given n×n integer matrix.

1.2 Related works

Multivariate Multipoint Evaluation. Our algorithm is inspired by progress in multivariate multipoint evaluation. Kedlaya and Umans [20] introduced a tabulation-based approach (combined with the Chinese remainder theorem) that later became a key ingredient in fast polynomial composition and factorization. More recently, a sequence of works [4, 2, 3] developed the use of Hasse derivatives and Hermite interpolation to extract more information per evaluation point. We adapt these ideas to our sparse tabulation framework.

Permanents. There exist faster algorithms for computing the permanent in other settings. For sparse matrices, Cygan and Pilipczuk [15] gave a 2nΩ(n/d) time algorithm, where d is the average degree of non-zero entries per row. Björklund and Williams [12] gave a 2nΩ(n/d3/4) time algorithm for d-regular bipartite graphs, and a 2nΩ(n/r) time algorithm that runs over a finite ring with r elements. Björklund, Husfeldt, and Lyckberg [9] gave a 2nΩ(n/(plogp)) time algorithm for computing the permanent modulo a prime power pλn/p, for any constant λ<1.

Hamiltonian cycles. There exist faster algorithms for counting Hamiltonian cycles in other settings as well. Björklund, Kaski, and Koutis [10] gave an O((2δ)n)-time algorithm for counting Hamiltonian cycles modulo moderate prime powers. In the general setting, it is somewhat surprising that our counting algorithm also yields the fastest known algorithm for deciding Hamiltonicity. This differs from the case of permanents: the support of the permanent corresponds to perfect matchings in bipartite graphs, whose existence can be decided in polynomial time. Faster decision algorithms are known in special cases, including Björklund’s O(1.66n)-time algorithm for undirected graphs [6] and the O(1.888n)-time algorithm of Cygan, Kratsch, and Nederlof for directed bipartite graphs [14].

1.3 Technical overview

For simplicity, we first sketch the case of computing the permanent.

Our improvement comes from combining three ideas:

  1. 1.

    Reduce to smaller instances. We reduce the computation on an n×n matrix to many instances on k×k matrices. Taking k around n is what creates room for an improvement in the exponent, provided we can answer the reduced instances fast. This kind of reduction (“self-reduction”) for permanents and Hamiltonian cycles was introduced by Björklund [7]. More concretely, for a parameter k, the reduction produces about 2nkpoly(n) instances on k×k matrices.

  2. 2.

    Tabulate only on a sparse set of points. A direct lookup table for all smaller matrices would be far too large. Instead, we employ the fact that permanent is a low-degree polynomial – we tabulate only on a carefully chosen sparse subset of points with the following key property: for every query point, there exists a low-degree univariate curve passing through it whose other points all lie inside the tabulated subset. Then we can recover the value at the query point by interpolating along that curve. (Over finite fields, such subsets are called Kakeya sets. Björklund, Kaski, and Williams [11] were the first to leverage this idea for multivariate polynomial multipoint evaluation in our setting, and we build on their approach.)

  3. 3.

    Make each tabulated point more informative. Interpolating from plain point evaluations is limited by how many points we see on a curve. We use the recent idea from Bhargava, Ghosh, Guo, Kumar, and Umans [2, 3] to enrich each tabulated point with additional local information (captured via suitable high-order derivatives), and then use Hermite interpolation to recover higher-degree information along the curve. This is what allows the sparse tabulation approach to only require smaller Kakeya sets, thus working at the larger subproblem sizes we need. (We refer to this task as high-order derivative evaluation.)

Our main technical contribution is a dynamic programming algorithm that makes the required derivative access efficient for the permanent, and we develop analogous data structures for hafnian and Hamiltonian cycles. More concretely, for any constant ϵ>0 and a parameter k, our data structure tabulates over a Kakeya set of size 2Oϵ(k2)111Here Oϵ() means that the hidden constant factor depends on ϵ. and takes O(2ϵk) time to evaluate one k×k permanent, so the total time complexity is O(2nΩ(k)+2O(k2)). Balancing the savings from self-reduction with the costs of tabulation yields the final running time 2nΩ(n).

The same high-level framework extends to hafnian and Hamiltonian cycles. For hafnian, we derive a suitable self-reduction by modifying components of Björklund’s algorithm [5]. For Hamiltonian cycles, we design an efficient derivative-evaluation data structure based on a determinant characterization [13].

1.4 Discussion

With the tabulation of information on Kakeya sets in k×k dimensional space, our algorithm essentially computes the hafnian and Hamiltonian cycles in 2nΩ(k)nO(1) time. It seems that a better construction of a smaller Kakeya set of size 2o(k2) would lead to a faster algorithm. However, the resolution of the finite field Kakeya conjecture [16, 17] rules out such possibilities, showing that the size of a Kakeya set is at least Ω(δk2) when the degree of the curve is not greater than q/δ, which corresponds to the regime of our application. Thus, the current construction is essentially optimal for our purposes.

It seems that we have reached a limit with the current approach of self-reduction and Kakeya sets. It remains open whether the techniques on perturbing Ryser’s formula, which work well for sparse permanents [12] and modulo pk permanents [9], can be adapted to dense permanents to achieve a faster algorithm.

2 Preliminaries

2.1 Notation

We use O~(f(n)) to denote O(f(n)polylog(f(n))).

Bold symbols like 𝒙 denote vectors 𝒙=(x1,,xn).

For any positive integer n, we use [n] to denote the set {1,2,,n}.

We use Iverson’s bracket notation. Let P be a logical proposition, we let P be 1 if P is true and 0 otherwise.

With AB, we denote the disjoint union of two sets A and B.

For an n×m matrix A, for subsets S[n] and T[m], we use AS,T to denote the submatrix of A with rows indexed by S and columns indexed by T.

Let (nm) denote the partial sum of binomial coefficients, i.e.,

(nm)=0im(ni).

2.2 Inequality for binomials

We need the estimate of the partial sum of binomials, see [18, Lemma 3.13] for a proof.

Lemma 3.

Consider 0<α<1/2. Then we have

(nαn)2nH(α),

where H(α)=log2(αα(1α)1α) is the binary entropy function.

2.3 Hermite interpolation

We need the following lemma for Hermite interpolation, see [26, Section 5.6] for a proof.

Lemma 4.

Let f(t)𝔽[t] be a polynomial of degree less than d, and m distinct points τ1,,τm in 𝔽, with multiplicities e1,,em positive integers such that e1++em=d. Given the remainder polynomials ri=fmod(tτi)ei for each i[m], then

  • f is uniquely determined by these ri,

  • moreover, the coefficients of f can be recovered in poly(d) many 𝔽-operations, given the coefficients of ri as input.

In particular, our algorithm uses the case where those distinct points are the entire finite field 𝔽q, and ei=r for all i.

Corollary 5.

Let f(t) be a polynomial of degree less than qr. Given the coefficients of fmod(tα)r for all α𝔽q, then the coefficients of f can be recovered in poly(qr) 𝔽q-operations.

2.4 Multimodular reduction

Our algorithm uses the Chinese remainder theorem to reduce the problem to small finite fields.

Theorem 6.

Let p1,,pn be distinct primes, and a1,,an be integers such that 0ai<pi. Let M=p1pn. Then there exists a unique integer a in the range 0a<M such that aai(modpi) for every i[n]. Moreover, a can be computed in time poly(logM).

See [26, Section 10.3] for a proof.

We also need an estimate on the product of primes.

Lemma 7.

For an integer N2, we have

primepp16logNp>N.

See [20, Lemma 2.4] for a proof.

3 Common framework

In this section, we set up the common framework for computing hafnians and counting Hamiltonian cycles.

3.1 Self reduction

We borrow the self-reduction lemma of Hamiltonian cycles from [7, Lemma 4].

Lemma 8.

Suppose |𝔽|k2+1, given a matrix A𝔽n×n, one can compute m=2nknO(1) instances ai𝔽,Fi𝔽k×k such that

hc(A)=i=1maihc(Fi).

Furthermore, the computation of these instances takes 2nknO(1) 𝔽-operations.

3.2 Kakeya set

We borrow the definition and construction of Kakeya sets mentioned in [11].

Definition 9.

A set K𝔽qm is said to be a Kakeya set of degree u, if for every a1,,am𝔽q, there exists degree-u polynomials g1,,gm, such that the degree u coefficient of gi is ai, and the set

{(g1(τ),,gm(τ)):τ𝔽q}

is a subset of K.

Theorem 10.

Let u be a positive integer such that u+1 divides q1. Then there is a Kakeya set K of degree u in 𝔽qm of size at most

(q1u+1+1)m+1.

Such K can be constructed in time |K|poly(q) and for each point 𝐚=(a1,,am)𝔽qm, the coefficients of the corresponding polynomials g1,,gm can be computed in time poly(u,m).

For the convenience of the reader, we provide the construction below, which is originally from [22].

Proof.

Since u+1 divides q1, we have q1(modu+1), thus u+1 is coprime with q, so u+1 is invertible in 𝔽q. For each point 𝒂=(a1,,am)𝔽qm, we consider the polynomials given by

gi(τ) =(aiu+1+τ)u+1τu+1
=k=0u(u+1k)(aiu+1)uk+1τk

The u+1-th degree coefficient of gi cancels out, and the u-th degree coefficient is

(u+11)aiu+1=ai.

So these polynomials satisfy the leading monomial condition of Definition 9, and one can easily compute the coefficients of gi in polynomial time, through the explicit expression given above.

Since u+1 divides q1, from basic finite field theory, the set T={xu+1:x𝔽q} consists of 0𝔽q and roots of unity of order d=(q1)/(u+1), so |T|=d+1. Let K be the set of points

(u1v,,umv):uiT,vT.

Since each ui and v can take d+1 values, we have |K|(d+1)m+1. Moreover, for each τ𝔽q, we have gi(τ)=uiv for ui=(ai/(u+1)+τ)u+1 and v=τu+1. This shows that K is indeed a Kakeya set of degree u, and satisfies the size bound. It is also straightforward to see that the construction can be done in time |K|poly(q).

3.3 High-order derivative evaluation

Definition 11.

Let P be a polynomial over m indeterminates. We call the following operation a derivative evaluation of P at 𝐚𝔽qm up to order r (r-order evaluation): Given a polynomial vector 𝐟(t)=(f1(t),,fm(t)), where each fi(t)𝔽q[t] is a polynomial with degree less than r, and 𝐟(0)=𝐚. Compute the coefficients of the polynomial P(𝐟(t))modtr.

This terminology comes from the intuition in characteristic zero. In that case, computing P(𝒇(t))modtr is equivalent to computing all the derivatives of P(𝒇(t)) up to order r.

We rephrase the idea of [2, 3] to reveal information from derivatives.

Theorem 12.

Let P be a homogeneous degree k polynomial over m indeterminates, b be a positive integer such that q1(modb). Let u=(q1)/b1 and r=k/b. Let K be a Kakeya set of degree u, with an oracle that supports r-order evaluation query at any point of K.

Then given any point 𝐚 and the associated curve 𝐂𝐚(t)=(g1(t),,gm(t)), we can compute P(𝐚) with q oracle queries, and poly(k,q) arithmetic operations over 𝔽q.

Proof.

By the definition of Kakeya sets, it is guaranteed that 𝑪𝒂(τ)K for all τ𝔽q. The polynomial P(𝑪𝒂(t)) is of degree ku. Write P(x1,,xm) with

P(x1,,xm)=i1,,imi1++im=kpi1,,imx1i1xmim,

since gi(t)=aitu+O(tu1), we have

P(𝑪𝒂(t)) =i1,,imi1++im=kpi1,,img1(t)i1gm(t)im
=i1,,imi1++im=kpi1,,im(a1tu+O(tu1))i1(amtu+O(tu1))im
=i1,,imi1++im=kpi1,,im(a1i1amimtku+O(tku1))
=P(𝒂)tku+O(tku1),

from which we have that the coefficient of tku in P(𝑪𝒂(t)) is P(𝒂).

By the choice of u, we have ku=k((q1)/b1)<qk/bqr. Let Q(t)=P(𝑪𝒂(t)). If we are given Q(t)mod(tτ)r for each τ𝔽q, by Hermite interpolation (Lemma 4), we can recover Q in poly(qr) operations. So the problem reduces to computing P(𝑪𝒂(t))mod(tτ)r for each τ𝔽q.

In order to compute Q(t)mod(tτ)r, one can write Q(t)=R(t)+(tτ)rD(t) where degR<r, then R(t) is the desired result. Thus we have Q(t+τ)=R(t+τ)+trD(t+τ), so we can compute Q(t+τ)modtr, and then reveal Q(t)mod(tτ)r by substituting ttτ. The conversion of coefficients only takes poly(r) arithmetic operations over 𝔽q. Thus we only need to compute P(𝑪𝒂(t+τ))modtr for each τ𝔽q. This is exactly an r-order evaluation of P at 𝑪𝒂(τ).

4 Self reduction for hafnian

In this section, we show that it is enough to take a truncation of Björklund’s algorithm [5] to obtain a self-reduction algorithm for the hafnian.

4.1 Technical ingredients from Björklund’s algorithm

First, we list the technical ingredients we borrow from Björklund.

We first recap a basic concept introduced in [5, Section 3.1]. For a commutative ring R, the set-partition algebra R[Um] is defined as follows. Intuitively, Um may be viewed as a partial semigroup whose elements are the subsets of [m], with the operation given by disjoint union. Thus, the product of two subsets U,V[m] is defined precisely when U and V are disjoint. The algebra R[Um] is then the R-algebra associated with this partial semigroup, analogous to the group algebra R[G] associated with a group G.

More explicitly, every element rR[Um] can be written uniquely as a formal sum

r=X[m]rX[X].

where rXR. Addition in R[Um] is defined componentwise, while multiplication is given by

rs=X[m](YZ=XrYsZ)[X],

where the inner sum ranges over all ordered decompositions of X as a disjoint union YZ.

We identify R=R[U0] and the inclusion R[Um]R[Um+1] via the inclusion [m][m+1]. Computationally, an element rR[Um] can be stored as the 2m coefficients rXR where X goes through all subsets of [m]. The multiplication in R[Um] is known as the subset convolution, which can be done in O~(2m) R-operations [8].

Then, Björklund [5, Section 3.2] introduced a sequence of matrices B(i)R[Ui](2n2i)×(2n2i) (for 0in) starting with B(0)=A, together with a sequence called squeeze factors β(i)R[Ui] (for 1in). We will not need the precise definition of these matrices and factors, but we invoke the following two facts.

Lemma 13 (Björklund [5, Lemma 4]).

For every X[i1] where i1, the following relation holds:

(hafB(i1))X=(β(i)hafB(i))X{i}.

The above lemma has an efficient algorithmic counterpart.222Lemma 14 is implicit in Björklund’s original paper. In the original text Björklund merely stated how to sequentially compute B(i) and β(i) for all 1in and gave a time complexity analysis, however, in the last paragraph of [5, Section 3.4], the analysis of a single squeeze step is given.

Lemma 14 (Björklund [5, Section 3.4]).

There is an algorithm that given B(i1), computes B(i) and β(i) in 2inO(1) R-operations.

4.2 Self reduction via inclusion-exclusion

We take several steps to obtain a self-reduction for hafnian. For 1n, we denote b() to be the prefix product b()=β(1)β(), by repeatedly applying Lemma 13, we obtain

hafA=(hafB(0))=(b()hafB())[]. (1)

Then the next step involves extracting the ideas of ranked Möbius transform and inversion from the subset convolution algorithm [8, Section 2].

We consider the ranked Möbius transform, for which for every X[], we introduce the polynomial b^X()R[T] defined as

b^X()=YXbY()T|Y|.

Similarly, we define B^X()R[T](2n2)×(2n2) as

B^X()=YXBY()T|Y|.

For a polynomial fR[T], let [Td]f denote the coefficient of Td in f. We first prove a general statement and then apply it to equation (1).

Lemma 15.

For a polynomial FR[Y1,,Ym] and y1,,ymR[U], for every X[], let

y^i,X=YXyi,YT|Y|,

and f^X=F(y^1,X,,y^m,X), let its Möbius inversion be

fX=YX(1)|X||Y|f^Y,

then we have

F(y1,,ym)X=[T|X|]fX.

Proof.

By linearity, it suffices to prove the case when F is a monomial. Furthermore, we can without loss of generality, assume that F is a monomial of the form

F(Y1,,Ym)=Y1Ym.

In this case, we have

F(y1,,ym)X=(y1ym)X=X1Xm=Xy1,X1ym,Xm.

On the other hand, since {y^i,X}X is the Möbius transform of {yi,XT|X|}X, and {fX}X is the Möbius inversion of {f^X}X, the basic property of the Möbius transform gives us

fX=X1Xm=Xy1,X1ym,XmT|X1|++|Xm|.

Since X1Xm=X, we have |X1|++|Xm||X| and the equality is attained when X1,,Xm form a partition of X. Thus, we have

[T|X|]fX=X1Xm=Xy1,X1ym,Xm.

In equation (1), treating bhafB() as a polynomial in b and all entries of B(), we have the following corollary.

Corollary 16.

For every X[] define h^X(),hX() by h^X()=b^X()haf(B^X()) and its Möbius inversion

hX()=YX(1)|X||Y|h^Y(),

then we have

hafA=[T]h[]().

Now we are ready to give the self-reduction algorithm for hafnian.

Theorem 17.

Let 𝔽q be a finite field with q(k+1)(nk)+1. There is an algorithm that takes a symmetric matrix A𝔽q2n×2n as input, outputs m=2nknO(1) instances, consisting of ai𝔽q and symmetric matrices Fi𝔽q2k×2k such that

haf(A)=i=1maihaf(Fi).

This algorithm also runs in 2nknO(1) 𝔽q-operations.

Proof.

Consider the following algorithm.

  1. 1.

    Iteratively compute B(i) and β(i) for 1ink via Lemma 14.

  2. 2.

    Compute b(nk)=β(1)β(nk) via fast subset convolution [8].

  3. 3.

    Compute b^X(nk) and entries of B^X(nk) for all X[nk], via fast Möbius transform [8, Section 2.2].

  4. 4.

    Let D=(k+1)(nk). Since qD+1, by Lagrange interpolation, it is possible to choose points α0,,αD𝔽q and coefficients γ0,,γD𝔽q such that [Tnk]f(T)=i=0Dγif(αi) hold for every polynomial f(T) of degree at most D. For every X[nk] and 0iD, compute the pair of a scalar and a matrix over 𝔽q:

    ((1)nk|X|γib^X(nk)(αi),B^X(nk)(αi))

    and they are the instances we need.

We now explain the correctness of the algorithm. By Corollary 16, we have

hafA =[Tnk]h[nk](nk)
=[Tnk]X[nk](1)nk|X|h^X(nk)
=X[nk](1)nk|X|[Tnk]h^X(nk)
=X[nk](1)nk|X|[Tnk]b^X(nk)haf(B^X(nk)).

Since haf(B^X(nk)) is a polynomial of degree k in the entries of B^X(nk), b^X(nk)haf(B^X(nk)) is a polynomial of degree D in T, so we have

X[nk]0iD(1)nk|X|γib^X(nk)(αi)haf(B^X(nk)(αi))
=X[nk](1)nk|X|[Tnk]b^X(nk)haf(B^X(nk))

correctly computes hafA. Each step of the algorithm requires 2nknO(1) 𝔽q-operations, and outputs 2nk(D+1)=2nknO(1) many instances, each is a tuple consisting of a scalar in 𝔽q and a matrix in 𝔽q2k×2k.

5 Data structure for hafnian

Lemma 18.

For a commutative ring R and symmetric matrices A,BR2n×2n, we have

haf(A+B)=S[2n]|S|0(mod2)haf(BS,S)haf(A[2n]S,[2n]S).

Proof.

We give a combinatorial proof. The hafnian haf(A+B) takes the summation over perfect matchings of the complete graph K2n with the product of edge weights. By expanding the product of (A+B)i,j, this is equivalent to coloring each selected edge with one of two colors A and B, and taking the product of the weights of edges with the selected color. Hence we can first determine the vertices whose matching edges have color A and B respectively, say vertices colored by B form the set S (whose cardinality must be even). Then the contribution of such a coloring is haf(BS,S)haf(A[2n]S,[2n]S).

Theorem 19.

Given a symmetric matrix A𝔽2k×2k and a positive integer r, we can precompute in time O~((2k2r)2k), and answer the r-order evaluation of hafnian at A in time O~((2k2r)). Both are measured in 𝔽-operations.

Proof.

We write F(t)=A+B(t), where B(t) has no constant term.

Note that when |S|2r, the term haf(BS,S) does not contribute to the result. Let f(S)=haf(BS,S), these f(S) can be computed via dynamic programming, described as follows.

For the base case, we have f()=1.

For 0<|S|<2r and |S|0(mod2), let s be a member of S, by enumerating the matching vertex v of s, we have

f(S)=vS{s}Bs,vf(S{s,v}).

After computing all f(S), we can compute

haf(A+B)=S[2n]|S|0(mod2)|S|<2rf(S)gS,

where gS=haf(A[2n]S) can be precomputed via Björklund’s algorithm in time O~(2k). The precomputation time is (2k2r)O~(2k)=O~((2k2r)2k), and each query takes time O~((2k2r)).

Note that when r=αk for some 0<α<1/2, by Lemma 3, precomputation takes time O~(2(1+2H(α))k), and each query takes time O~(22H(α)k).

6 Data structure for Hamiltonian cycles

In [13] they considered that Hamiltonian cycles can be counted as spanning trees with restricted degree and used it to count undirected Hamiltonian cycles in time exponential of treewidth. We give a directed version.

Let σSn be a permutation, let Pσ denote the permutation matrix associated with σ, such that (Pσ)i,j=j=σ(i).

Lemma 20.

For a permutation σSn,

det((IPσ)[n]{1},[n]{1})=c(σ)=1.

Proof.

Consider a directed graph G with directed edges (i,σ(i)), then L=IPσ is exactly the Laplacian of the graph G. By the directed version of matrix tree theorem, det(L[n]{1},[n]{1}) is the number of directed spanning trees rooted at vertex 1. When c(σ)=1, then clearly there is exactly one spanning tree, otherwise there is no spanning tree. Thus we can conclude the claimed equality.

Therefore, we use the above characterization of Hamiltonian cycles to help computing HamCycles.

Theorem 21.

Given a matrix A𝔽k×k and a positive integer r, we can precompute in O~((kr)4k), and answer the r-order evaluation of Hamiltonian cycles polynomial at A in time O~((kr)3). Both are measured in 𝔽-operations.

Proof.

By the definition of HamCycles and Lemma 20, we have

hc(A)=σSk(i=1kAi,σ(i))det((IPσ)[k]{1},[k]{1}).

We also expand the determinant by the Leibniz formula, i.e.,

det((IPσ)[k]{1},[k]{1})=τSkτ(1)=1sgn(τ)i=2k(IPσ)i,τ(i).

Combining the above two equations, and interpret sgn(τ) as (1)inv(τ), where inv(a) denotes the number of inversions for a sequence a, we have
det((IPσ)[k]{1},[k]{1}) =σ,τSkτ(1)=1sgn(τ)(i=1kAi,σ(i))(i=2k(IPσ)i,τ(i)) =σ,τSkτ(1)=1(1)inv(τ)(i=1kAi,σ(i))(i=2ki=τ(i)σ(i)=τ(i)).

Now consider dynamic programming. For S[k]{1},T[k] and say s=|S|=|T|, let f(S,T) only counts in the last s values of σ and τ, with domain {τ(ks+1),,τ(k)}=S and {σ(ks+1),,σ(k)}=T, and the inversions of τ in the last s values are counted, i.e.,

f(S,T)=σ,τ(1)inv(τ)(i=ks+1kAi,σ(i))(i=ks+1ki=τ(i)σ(i)=τ(i)). (2)

We let a+b denote aa+b for simplicity in describing the updating rules. The base case is simply f(,)=1, and for each s<k1, we use the computed values of f(S,T) with |S|=|T|=s to compute f(S,T) with |S|=|T|=s+1 by the following rules. Let i=ks. For each jT, we can choose σ(i) to be j, then there are two choices of τ(i):

  • If iS, update with

    f(S{i},T{j})+(1)inv(i,S)Ai,jf(S,T),

    denoting the choice that the contribution of term i=τ(i) in equation (2).

  • If jS, update with

    f(S{j},T{j})+(1)1+inv(j,S)Ai,jf(S,T),

    denoting the choice that the contribution of term σ(i)=τ(i) in equation (2).

Here inv(v,S) means the number of elements xS such that v>x.

Finally, we have the choice of σ(1), thus

hc(A)=i=1kA1,if([k]{1},[k]{i}).

This dynamic programming takes O~(4k), which is slower than the usual one, but its dependence on the rows of A is explicitly graded by s, so is useful for our purpose.

Now suppose the first j rows are left undetermined, we can first preprocess all the f(S,T) for skj in time O~(4k), since their value does not depend on the first j rows. Then for each query, i.e., given the first j rows, can be computed in time

O~(i=1j(ki)(ki1))=O~((kj)2).

Write F=A+B(t), where B(t) has no constant term, by the multilinearity on rows of hc(), we have

hc(F(t))=hc(A+B)=S[k]hc(repS(A,B)),

where repS(A,B) denote the matrix obtained by replacing the rows indexed in S of A by those rows of B. The terms |S|r do not contribute to the result. For each |S|<r, we can reorder the rows and columns simultaneously to make S be the first |S| rows, and use the above dynamic programming to do precomputation and handle queries.

There are (kr) ways to choose S, so the precomputation needs O~((kr)4k) time, and O~((kr)3) for each query.

Note that when r=αk for some 0<α<1/2, by Lemma 3, precomputation takes time O~(2(2+H(α))k), and each query takes time O~(23H(α)k).

7 The algorithms

We first prove Theorem 1 under some restrictions, and then remove the restrictions by bootstrapping the results.

Lemma 22.

Let q satisfy qn2+1 and q1(modb), where b10. There is an algorithm that computes the hafnian haf(A) of a given symmetric matrix A𝔽q2n×2n in time 2nδbnqO(1), for some δb>0.

Proof.

Let θ=log(1.9)/log(1+b) and k=θn, and consider the following algorithm.

  1. 1.

    First compute the Kakeya set by Theorem 10 over k2 variables of degree u=(q1)/b1.

  2. 2.

    Precompute the data structure for r-order evaluation for r=k/b at each point of K.

  3. 3.

    Use the self-reduction of hafnian (Theorem 17) to reduce the problem to m=2nknO(1) instances of size 2k×2k.

  4. 4.

    For each instance, use Theorem 12 to compute the hafnian.

Then we analyze the time complexity. In the precomputation phase, by Theorem 10, the size of Kakeya set is (q1u+1+1)k2+1(b+1)θ2n+1=O(1.9n), and by Theorem 19, each data structure takes 2O(k) time to precompute, so the total time of the first two steps is 1.9n+O(n)qO(1).

The data structure can answer r-order evaluation in time O~(22H(α)k). Here we have α=1/b0.1, hence 2H(α)2H(0.1)<0.94, the total time in last two steps is

2nknO(1)O(20.94k)qO(1)=2n0.06kqO(1)=2n0.06θnqO(1).

In conclusion, we have δb=0.06θ satisfies the requirement.

Lemma 23.

Let q satisfy qn2+1 and q1(modb), where b17. There is an algorithm that computes Hamiltonian cycles hc(A) of a given matrix A𝔽qn×n in time 2nδbnqO(1), for some δb>0.

Proof.

The algorithm is similar to the proof of Lemma 22, with replacing the data structure for Hamiltonian cycles instead of hafnian.

By Theorem 21, the data structure can answer r-order evaluation of Hamiltonian cycles in time O~(23H(α)k), where 3H(α)3H(1/17)<0.97.

Then the total time in the last two steps is

2nknO(1)O(20.97k)qO(1)=2n0.03kqO(1)=2n0.03θnqO(1).

In conclusion, we have δb=0.03θ satisfies the requirement.

7.1 Proof of Theorem 1

To prove Theorem 1, we only need to remove the conditions of Lemma 22 and Lemma 23 on q that qn2+1 and q1(modb) for some fixed modulus b.

Note that for some integer , we can embed 𝔽q into a larger finite field 𝔽q. We only need to satisfy qn2+1 and q1(modb). When q is coprime with b, taking =φ(b) is enough to satisfy the second condition, where φ is the Euler totient function. Taking as the smallest multiple of φ(b) such that q>n2, we have qqφ(b)n2.

For the hafnian, since q is a prime power, it must be coprime with either b=10 or b=11. For Hamiltonian cycles, q must be coprime with either b=17 or b=18. Therefore, we have q=qO(1)n2 since we only consider finite possibilities for b.

Therefore, by invoking the algorithms in Lemma 22 and Lemma 23 through the finite field 𝔽q, we can compute the hafnian and Hamiltonian cycles in time 2nΩ(n)(q)O(1)=2nΩ(n)qO(1).

To actually support the computation in the finite field 𝔽q, we need to find an irreducible polynomial f and identify 𝔽q as 𝔽q[t]/(f). We can enumerate the polynomials of degree over 𝔽q and test whether they satisfy the conditions. By [26, Theorem 14.37], the time complexity of testing irreducibility is poly(,logq). The time required to find an irreducible polynomial is O(qpoly(logq)), so this is not a bottleneck.

7.2 Proof of Corollary 2

The absolute value of haf(A) and hc(A) is trivially bounded by C=(2n)!Mn. Let p1,,pr be distinct prime numbers such that D:=ipi>2C+1. Then if we can compute haf(A) and hc(A) modulo D, the values of haf(A) and hc(A) are uniquely determined.

By the Chinese remainder theorem, we only need to compute haf(A) and hc(A) modulo pi for each i, and then combine them to get the result modulo D.

By Lemma 7, the primes not greater than 16logD=O(nlogM) have their product greater than D. So we only need to compute haf(A) and hc(A) over finite fields 𝔽q with p=O(nlogM). By Theorem 1, we can compute them in time 2nΩ(n)pO(1)=2nΩ(n)(logM)O(1). There are O(nlogM) instances to compute. Since the product of the chosen primes has O(nlogM) bits, by Theorem 6, it takes poly(nlogM) time to combine them, which is not a bottleneck. So the total time is 2nΩ(n)(logM)O(1).

References

  • [1] Eric Bax and Joel Franklin. A finite-difference sieve to count paths and cycles by length. Inform. Process. Lett., 60(4):171–176, 1996. doi:10.1016/S0020-0190(96)00159-7.
  • [2] Vishwas Bhargava, Sumanta Ghosh, Zeyu Guo, Mrinal Kumar, and Chris Umans. Fast multivariate multipoint evaluation over all finite fields. In Proceedings of the 63rd annual IEEE symposium on foundations of computer science, FOCS 2022, Denver, CO, USA, October 31 – November 3, 2022, pages 221–232. Los Alamitos, CA: IEEE Computer Society, 2022. doi:10.1109/FOCS54457.2022.00028.
  • [3] Vishwas Bhargava, Sumanta Ghosh, Zeyu Guo, Mrinal Kumar, and Chris Umans. Fast multivariate multipoint evaluation over all finite fields. J. ACM, 71(3):Art. 22, 32, 2024. doi:10.1145/3652025.
  • [4] Vishwas Bhargava, Sumanta Ghosh, Mrinal Kumar, and Chandra Kanta Mohapatra. Fast, algebraic multivariate multipoint evaluation in small characteristic and applications. In Proceedings of the 54th annual ACM SIGACT symposium on theory of computing, STOC ’22, Rome, Italy June 20–24, 2022, pages 403–415. New York, NY: Association for Computing Machinery (ACM), 2022. doi:10.1145/3519935.3519968.
  • [5] Andreas Björklund. Counting perfect matchings as fast as Ryser. In Proceedings of the twenty-third annual acm-siam symposium on discrete algorithms, pages 914–921. SIAM, 2012. doi:10.1137/1.9781611973099.73.
  • [6] Andreas Björklund. Determinant sums for undirected Hamiltonicity. SIAM J. Comput., 43(1):280–299, 2014. doi:10.1137/110839229.
  • [7] Andreas Björklund. Below all subsets for some permutational counting problems. In 15th Scandinavian symposium and workshops on algorithm theory, SWAT 2016, Reykjavik, Iceland, June 22–24, 2016. Proceedings, page 11. Wadern: Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2016. Id/No 17. doi:10.4230/LIPIcs.SWAT.2016.17.
  • [8] Andreas Björklund, Thore Husfeldt, Petteri Kaski, and Mikko Koivisto. Fourier meets Möbius: fast subset convolution. In Proceedings of the thirty-ninth annual ACM symposium on Theory of computing, pages 67–74, 2007. doi:10.1145/1250790.1250801.
  • [9] Andreas Björklund, Thore Husfeldt, and Isak Lyckberg. Computing the permanent modulo a prime power. Inform. Process. Lett., 125:20–25, 2017. doi:10.1016/j.ipl.2017.04.015.
  • [10] Andreas Björklund, Petteri Kaski, and Ioannis Koutis. Directed Hamiltonicity and out-branchings via generalized Laplacians. In Ioannis Chatzigiannakis, Piotr Indyk, Fabian Kuhn, and Anca Muscholl, editors, 44th International Colloquium on Automata, Languages, and Programming (ICALP 2017), volume 80 of Leibniz International Proceedings in Informatics (LIPIcs), pages 91:1–91:14, Dagstuhl, Germany, 2017. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.ICALP.2017.91.
  • [11] Andreas Björklund, Petteri Kaski, and Ryan Williams. Generalized Kakeya sets for polynomial evaluation and faster computation of fermionants. Algorithmica, 81(10):4010–4028, 2019. doi:10.1007/s00453-018-0513-7.
  • [12] Andreas Björklund and Ryan Williams. Computing permanents and counting hamiltonian cycles by listing dissimilar vectors. In 46th International Colloquium on Automata, Languages, and Programming, volume 132 of LIPIcs. Leibniz Int. Proc. Inform., pages Art. No. 25, 14. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2019. doi:10.4230/LIPIcs.ICALP.2019.25.
  • [13] Hans L. Bodlaender, Marek Cygan, Stefan Kratsch, and Jesper Nederlof. Deterministic single exponential time algorithms for connectivity problems parameterized by treewidth. Inform. and Comput., 243:86–111, 2015. doi:10.1016/j.ic.2014.12.008.
  • [14] Marek Cygan, Stefan Kratsch, and Jesper Nederlof. Fast Hamiltonicity checking via bases of perfect matchings. J. ACM, 65(3):Art. 12, 46, 2018. doi:10.1145/3148227.
  • [15] Marek Cygan and Marcin Pilipczuk. Faster exponential-time algorithms in graphs of bounded average degree. Inform. and Comput., 243:75–85, 2015. doi:10.1016/j.ic.2014.12.007.
  • [16] Zeev Dvir. On the size of Kakeya sets in finite fields. J. Amer. Math. Soc., 22(4):1093–1097, 2009. doi:10.1090/S0894-0347-08-00607-3.
  • [17] Zeev Dvir, Swastik Kopparty, Shubhangi Saraf, and Madhu Sudan. Extensions to the method of multiplicities, with applications to Kakeya sets and mergers. SIAM J. Comput., 42(6):2305–2328, 2013. doi:10.1137/100783704.
  • [18] Fedor V. Fomin and Dieter Kratsch. Exact exponential algorithms. Texts Theor. Comput. Sci., EATCS Ser. Berlin: Springer, 2010. doi:10.1007/978-3-642-16533-7.
  • [19] Richard M. Karp. Reducibility among combinatorial problems. In Complexity of computer computations (Proc. Sympos., IBM Thomas J. Watson Res. Center, Yorktown Heights, N.Y., 1972), The IBM Research Symposia Series, pages 85–103. Plenum, New York-London, 1972. doi:10.1007/978-1-4684-2001-2_9.
  • [20] Kiran S. Kedlaya and Christopher Umans. Fast polynomial factorization and modular composition. SIAM J. Comput., 40(6):1767–1802, 2011. doi:10.1137/08073408X.
  • [21] Donald E. Knuth. The art of computer programming. Vol. 2: Seminumerical algorithms. Addison-Wesley Publishing Co., Reading, Mass.-London-Don Mills, Ont., 1969.
  • [22] Gerd Mockenhaupt and Terence Tao. Restriction and Kakeya phenomena for finite fields. Duke Math. J., 121(1):35–74, 2004. doi:10.1215/S0012-7094-04-12112-8.
  • [23] Herbert John Ryser. Combinatorial mathematics, volume No. 14 of The Carus Mathematical Monographs. Mathematical Association of America, distributed by John Wiley and Sons, Inc., New York, 1963. doi:10.5948/UPO9781614440147.
  • [24] L. G. Valiant. The complexity of computing the permanent. Theoret. Comput. Sci., 8(2):189–201, 1979. doi:10.1016/0304-3975(79)90044-6.
  • [25] Leslie G. Valiant. The complexity of enumeration and reliability problems. SIAM J. Comput., 8(3):410–421, 1979. doi:10.1137/0208032.
  • [26] Joachim von zur Gathen and Jürgen Gerhard. Modern computer algebra. Cambridge University Press, Cambridge, third edition, 2013. doi:10.1017/CBO9781139856065.