# Hafnian

In mathematics, the hafnian is a scalar function of a symmetric matrix that generalizes the permanent.
The hafnian was named by Eduardo R. Caianiello "to mark the fruitful period of stay in Copenhagen (Hafnia in Latin)."

## Definition

The hafnian of a $2n\times 2n$ symmetric matrix $A$ is defined as
    $\operatorname {haf} (A)=\sum _{\rho \in P_{2n}^{2}}\prod _{\{i,j\}\in \rho }A_{i,j},$
where $P_{2n}^{2}$ is the set of all partitions of the set $\{1,2,\dots ,2n\}$ into subsets of size $2$.
This definition is similar to that of the Pfaffian, but differs in that the signatures of the permutations are not taken into account. Thus the relationship of the hafnian to the Pfaffian is the same as relationship of the permanent to the determinant.

## Basic properties

The hafnian may also be defined as
    $\operatorname {haf} (A)={\frac {1}{n!2^{n}}}\sum _{\sigma \in S_{2n}}\prod _{i=1}^{n}A_{\sigma (2i-1),\sigma (2i)},$
where $S_{2n}$ is the symmetric group on $\{1,2,...,2n\}$. The two definitions are equivalent because if $\sigma \in S_{2n}$, then $\{\{\sigma (2i-1),\sigma (2i)\}:i\in \{1,...,n\}\}$ is a partition of $\{1,2,\dots ,2n\}$ into subsets of size 2, and as $\sigma$ ranges over $S_{2n}$, each such partition is counted exactly $n!2^{n}$ times. Note that this argument relies on the symmetry of $A$, without which the original definition is not well-defined.
The hafnian of an adjacency matrix of a graph is the number of perfect matchings (also known as 1-factors) in the graph. This is because a partition of $\{1,2,\dots ,2n\}$ into subsets of size 2 can also be thought of as a perfect matching in the complete graph $K_{2n}$.
The hafnian can also be thought of as a generalization of the permanent, since the permanent can be expressed as
    $\operatorname {per} (C)=\operatorname {haf} {\begin{pmatrix}0&C\\C^{\mathsf {T}}&0\end{pmatrix}}$.
Just as the hafnian counts the number of perfect matchings in a graph given its adjacency matrix, the permanent counts the number of matchings in a bipartite graph given its biadjacency matrix.
The hafnian is also related to moments of multivariate Gaussian distributions. By Wick's probability theorem, the hafnian of a real $2n\times 2n$ symmetric matrix may expressed as
    $\operatorname {haf} (A)=\mathbb {E} _{\left(X_{1},\dots ,X_{2n}\right)\sim {\mathcal {N}}\left(0,A+\lambda I\right)}\left[X_{1}\dots X_{2n}\right],$
where $\lambda$ is any number large enough to make $A+\lambda I$ positive semi-definite. Note that the hafnian does not depend on the diagonal entries of the matrix, and the expectation on the right-hand side does not depend on $\lambda$.

## Generating function

Let $S={\begin{pmatrix}A&C\\C^{\mathsf {T}}&B\end{pmatrix}}=S^{\mathsf {T}}$ be an arbitrary complex symmetric $2m\times 2m$ matrix composed of four $m\times m$ blocks $A=A^{\mathsf {T}}$, $B=B^{\mathsf {T}}$, $C$ and $C^{\mathsf {T}}$. Let $z_{1},\ldots ,z_{m}$ be a set of $m$ independent variables, and let $Z={\begin{pmatrix}0&{\textrm {diag}}(z_{1},z_{2},\ldots ,z_{m})\\{\textrm {diag}}(z_{1},z_{2},\ldots ,z_{m})&0\end{pmatrix}}$ be an antidiagonal block matrix composed of entries $z_{j}$ (each one is presented twice, one time per nonzero block). Let $I$ denote the identity matrix. Then the following identity holds:
    ${\frac {1}{\sqrt {\det {\bigI-ZS{\big )}}}}=\sum _{\{n_{k}\}}\operatorname {haf} {\tilde {S}}(\{n_{k}\})\prod _{k=1}^{m}{\frac {z_{k}^{n_{k}}}{n_{k}!}}$
where the right-hand side involves hafnians of ${\Big2\sum _{k}n_{k}{\Big )}\times {\Big2\sum _{k}n_{k}{\Big )}$ matrices ${\tilde {S}}(\{n_{k}\})={\begin{pmatrix}{\tilde {A}}(\{n_{k}\})&{\tilde {C}}(\{n_{k}\})\\{\tilde {C}}^{\mathsf {T}}(\{n_{k}\})&{\tilde {B}}(\{n_{k}\})\\\end{pmatrix}}$, whose blocks ${\tilde {A}}(\{n_{k}\})$, ${\tilde {B}}(\{n_{k}\})$, ${\tilde {C}}(\{n_{k}\})$ and ${\tilde {C}}^{\mathsf {T}}(\{n_{k}\})$ are built from the blocks $A$, $B$, $C$ and $C^{\mathsf {T}}$ respectively in the way introduced in MacMahon's Master theorem. In particular, ${\tilde {A}}(\{n_{k}\})$ is a matrix built by replacing each entry $A_{k,t}$ in the matrix $A$ with a $n_{k}\times n_{t}$ block filled with $A_{k,t}$; the same scheme is applied to $B$, $C$ and $C^{\mathsf {T}}$. The sum $\sum _{\{n_{k}\}}$ runs over all $k$-tuples of non-negative integers, and it is assumed that $\operatorname {haf} {\tilde {S}}(\{n_{k}=0|k=1\ldots m\})=1$.
The identity can be proved by means of multivariate Gaussian integrals and Wick's probability theorem.
The expression in the left-hand side, $1{\Big /}{\sqrt {\det {\bigI-ZS{\big )}}}{\Big .}$, is in fact a multivariate generating function for a series of hafnians, and the right-hand side constitutes its multivariable Taylor expansion in the vicinity of the point $z_{1}=\ldots =z_{m}=0.$ As a consequence of the given relation, the hafnian of a symmetric $2m\times 2m$ matrix $S$ can be represented as the following mixed derivative of the order $m$:
    $\operatorname {haf} S={\bigg\prod _{k=1}^{m}{\frac {\partial }{\partial z_{k}}}{\bigg )}{\Bigg .}{\frac {1}{\sqrt {\det {\bigI-ZS{\big )}}}}{\Bigg \vert }_{z_{j}=0}.$
The hafnian generating function identity written above can be considered as a hafnian generalization of MacMahon's Master theorem, which introduces the generating function for matrix permanents and has the following form in terms of the introduced notation:
    ${\frac {1}{\det {\bigI-{\textrm {diag}}(z_{1},z_{2},\ldots ,z_{m})C{\big )}}}=\sum _{\{n_{k}\}}\operatorname {per} {\tilde {C}}(\{n_{k}\})\prod _{k=1}^{m}{\frac {z_{k}^{n_{k}}}{n_{k}!}}$
Note that MacMahon's Master theorem comes as a simple corollary from the hafnian generating function identity due to the relation $\operatorname {per} (C)=\operatorname {haf} {\begin{pmatrix}0&C\\C^{\mathsf {T}}&0\end{pmatrix}}$.

## Non-negativity

If $C$ is a Hermitian positive semi-definite $n\times n$ matrix and $B$ is a complex symmetric $n\times n$ matrix, then
    $\operatorname {haf} {\begin{pmatrix}B&C\\{\overline {C}}&{\overline {B}}\end{pmatrix}}\geq 0,$
where ${\overline {C}}$ denotes the complex conjugate of $C$.
A simple way to see this when ${\begin{pmatrix}C&B\\{\overline {B}}&{\overline {C}}\\\end{pmatrix}}$ is positive semi-definite is to observe that, by Wick's probability theorem, $\operatorname {haf} {\begin{pmatrix}B&C\\{\overline {C}}&{\overline {B}}\\\end{pmatrix}}=\mathbb {E} \left[\left|X_{1}\dots X_{n}\right|^{2}\right]$ when $\left(X_{1},\dots ,X_{n}\right)$ is a complex normal random vector with mean $0$, covariance matrix $C$ and relation matrix $B$.
This result is a generalization of the fact that the permanent of a Hermitian positive semi-definite matrix is non-negative. This corresponds to the special case $B=0$ using the relation $\operatorname {per} (C)=\operatorname {haf} {\begin{pmatrix}0&C\\C^{\mathsf {T}}&0\end{pmatrix}}$.

## Loop hafnian

The loop hafnian of an $m\times m$ symmetric matrix is defined as
    $\operatorname {lhaf} (A)=\sum _{\rho \in {\mathcal {M}}}\prod _{(i,j)\in \rho }A_{i,j}$
where ${\mathcal {M}}$ is the set of all perfect matchings of the complete graph on $m$ vertices with loops, i.e., the set of all ways to partition the set $\{1,2,\dots ,m\}$ into pairs or singletons (treating a singleton $(i)$ as the pair $(i,i)$). Thus the loop hafnian depends on the diagonal entries of the matrix, unlike the hafnian. Furthermore, the loop hafnian can be non-zero when $m$ is odd.
The loop hafnian can be used to count the total number of matchings in a graph (perfect or non-perfect), also known as its Hosoya index. Specifically, if one takes the adjacency matrix of a graph and sets the diagonal elements to 1, then the loop hafnian of the resulting matrix is equal to the total number of matchings in the graph.
The loop hafnian can also be thought of as incorporating a mean into the interpretation of the hafnian as a multivariate Gaussian moment. Specifically, by Wick's probability theorem again, the loop hafnian of a real $m\times m$ symmetric matrix can be expressed as
    $\operatorname {lhaf} (A)=\mathbb {E} _{\left(X_{1},\dots ,X_{m}\right)\sim {\mathcal {N}}\left(\operatorname {diag} \left(A\right),A+\lambda I\right)}\left[X_{1}\dots X_{m}\right],$
where $\lambda$ is any number large enough to make $A+\lambda I$ positive semi-definite.

## Computation

Computing the hafnian of a (0,1)-matrix is #P-complete, because computing the permanent of a (0,1)-matrix is #P-complete.
The hafnian of a $2n\times 2n$ matrix can be computed in $O(n^{3}2^{n})$ time.
If the entries of a matrix are non-negative, then its hafnian can be approximated to within an exponential factor in polynomial time.