---
abstract: 'Given a symmetric $D\times D$ matrix $M$ over $\{0,1,*\}$, a list $M$-partition of a graph $G$ is a partition of the vertices of $G$ into $D$ parts which are associated with the rows of $M$. The part of each vertex is chosen from a given list in such a way that no edge of $G$ is mapped to a $0$ in $M$ and no non-edge of $G$ is mapped to a $1$ in $M$. Many important graph-theoretic structures can be represented as list $M$-partitions including graph colourings, split graphs and homogeneous sets and pairs, which arise in the proofs of the weak and strong perfect graph conjectures. Thus, there has been quite a bit of work on determining for which matrices $M$ computations involving list $M$-partitions are tractable. This paper focuses on the problem of counting list $M$-partitions, given a graph $G$ and given a list for each vertex of $G$. We identify a certain set of “tractable” matrices $M$. We give an algorithm that counts list $M$-partitions in polynomial time for every (fixed) matrix $M$ in this set. The algorithm relies on data structures such as sparse-dense partitions and subcube decompositions to reduce each problem instance to a sequence of problem instances in which the lists have a certain useful structure that restricts access to portions of $M$ in which the interactions of $0$s and $1$s is controlled. We show how to solve the resulting restricted instances by converting them into particular counting constraint satisfaction problems (${\ensuremath{\mathrm{\#CSP}}}$s) which we show how to solve using a constraint satisfaction technique known as “arc-consistency”. For every matrix $M$ for which our algorithm fails, we show that the problem of counting list $M$-partitions is [$\mathrm{\#P}$]{}-complete. Furthermore, we give an explicit characterisation of the dichotomy theorem — counting list $M$-partitions is tractable (in [$\mathrm{FP}$]{}) if the matrix $M$ has a structure called a derectangularising sequence. If $M$ has no derectangularising sequence, we show that counting list $M$-partitions is [$\mathrm{\#P}$]{}-hard. We show that the meta-problem of determining whether a given matrix has a derectangularising sequence is [$\mathrm{NP}$]{}-complete. Finally, we show that list $M$-partitions can be used to encode cardinality restrictions in $M$-partitions problems and we use this to give a polynomial-time algorithm for counting homogeneous pairs in graphs.'
author:
- 'Andreas Göbel[^1]'
- Leslie Ann Goldberg
- 'Colin McQuillan[^2]'
- David Richerby
- 'Tomoyuki Yamakami[^3]'
bibliography:
- '\\jobname.bib'
title: 'Counting list matrix partitions of graphs[^4]'
---

Introduction
============

A matrix partition of an undirected graph is a partition of its vertices according to a matrix which specifies adjacency and non-adjacency conditions on the vertices, depending on the parts to which they are assigned. For finite sets $D$ and $D'$, the set $\{0,1,*\}^{D\times D'}$ is the set of matrices with rows indexed by $D$ and columns indexed by $D'$ where each $M_{i,j} \in \{0,1,*\}$. For any symmetric matrix $M\in\{0,1,*\}^{D\times D}$, an [*$M$-partition*]{} of an undirected graph $G=(V,E)$ is a function $\sigma\colon V\to D$ such that, for distinct vertices $u$ and $v$,

-   $M_{\sigma(u),\sigma(v)}\neq 0$ if $(u,v)\in E$ and

-   $M_{\sigma(u),\sigma(v)}\neq 1$ if $(u,v)\not\in E$.

Thus, $M_{i,j}=0$ means that no edges are allowed between vertices in parts $i$ and $j$, $M_{i,j}=1$ means that there must be an edge between every pair of vertices in the two parts and $M_{i,j}=*$ means that any set of edges is allowed between the parts. For entries $M_{i,i}$ on the diagonal of $M$, the conditions only apply to distinct vertices in part $i$. Thus, $M_{i,i}=1$ requires that the vertices in part $i$ form a clique in $G$ and $M_{i,i}=0$ requires that they form an independent set.

For example, if $D=\{i,c\}$, $M_{i,i} = 0$, $M_{c,c}=1$ and $M_{c,i} =
M_{i,c} = *$, i.e., $M=\left(\begin{smallmatrix}0 & *\\ * &
1\end{smallmatrix}\right)$, then an $M$-partition of a graph is a partition of its vertices into an independent set (whose vertices are mapped to $i$) and a clique (whose vertices are mapped to $c$). The independent set and the clique may have arbitrary edges between them. A graph that has such an $M$-partition is known as a split graph [@Golumbic].

As Feder, Hell, Klein and Motwani describe [@FHKM], many important graph-theoretic structures can be represented as $M$-partitions, including graph colourings, split graphs, $(a,b)$-graphs [@Bra96], clique-cross partitions [@EKR], and their generalisations. $M$-partitions also arise as “type partitions” in extremal graph theory [@BT00]. In the special case where $M$ is a $\{0,*\}$-matrix (that is, it has no 1 entries), $M$-partitions of $G$ correspond to homomorphisms from $G$ to the (potentially looped) graph $H$ whose adjacency matrix is obtained from $M$ by turning every $*$ into a 1. Thus, proper $|D|$-colourings of $G$ are exactly $M$-partitions for the matrix $M$ which has 0s on the diagonal and $*$s elsewhere.

To represent more complicated graph-theoretic structures, such as homogeneous sets and their generalisations, which arise in the proofs of the weak and strong perfect graph conjectures [@lovasz; @CRST], it is necessary to generalise $M$-partitions by introducing lists. Details of these applications are given by Feder et al. [@FHKM], who define the notion of a list $M$-partition.

A [*list $M$-partition*]{} is an $M$-partition $\sigma$ that is also required to satisfy constraints on the values of each $\sigma(v)$. Let ${{\mathcal{P}(D)}}$ denote the powerset of $D$. We say that $\sigma$ [*respects*]{} a function $L\colon V(G)\to {{\mathcal{P}(D)}}$ if $\sigma(v)\in
L(v)$ for all $v\in V(G)$. Thus, for each vertex $v$, $L(v)$ serves as a list of allowable parts for $v$ and a *list $M$-partition* of $G$ is an $M$-partition that respects the given list function. We allow empty lists for technical convenience, although there are no $M$-partitions that respect any list function $L$ where $L(v)=\emptyset$ for some vertex $v$.

Feder et al.[@FHKM] study the computational complexity of the following decision problem, which is parameterised by a symmetric matrix $M\in\{0,1,*\}^{D\times D}\!$.

[[List-$M$-partitions]{}]{}.

A pair $(G,L)$ in which $G$ is a graph and $L$ is a function $V(G)\to{{\mathcal{P}(D)}}$.

“Yes”, if $G$ has an $M$-partition that respects $L$; “no”, otherwise.

Note that $M$ is a parameter of the problem rather than an input of the problem. Thus, its size is a constant which does not vary with the input.

A series of papers [@FH; @FHHList; @FHH] described in [@FHKM] presents a complete dichotomy for the special case of homomorphism problems, which are [[List-$M$-partitions]{}]{} problems in which $M$ is a $\{0,*\}$-matrix. In particular, Feder, Hell and Huang [@FHH] show that, for every $\{0,*\}$-matrix $M$ (and symmetrically, for every $\{1,*\}$-matrix $M$), the problem [[List-$M$-partitions]{}]{} is either polynomial-time solvable or [$\mathrm{NP}$]{}-complete.

It is important to note that both of these special cases of [[List-$M$-partitions]{}]{} are constraint satisfaction problems (CSPs) and a famous conjecture of Feder and Vardi [@FV] is that a P versus [$\mathrm{NP}$]{}-complete dichotomy also exists for every CSP. Although general [[List-$M$-partitions]{}]{} problems can also be coded as CSPs with restrictions on the input,[^5] it is not known how to code them without such restrictions. Since the Feder–Vardi conjecture applies only to CSPs with unrestricted inputs, even if proved, it would not necessarily apply to [[List-$M$-partitions]{}]{}.

Given the many applications of [[List-$M$-partitions]{}]{}, it is important to know whether there is a dichotomy for this problem. This is part of a major ongoing research effort which has the goal of understanding the boundaries of tractability by identifying classes of problems, as wide as possible, where dichotomy theorems arise and where the precise boundary between tractability and intractability can be specified.

Significant progress has been made on identifying dichotomies for [[List-$M$-partitions]{}]{}. Feder et al. [@FHKM Theorem 6.1] give a complete dichotomy for the special case in which $M$ is at most $3\times 3$, by showing that [[List-$M$-partitions]{}]{} is polynomial-time solvable or [$\mathrm{NP}$]{}-complete for each such matrix. Later, Feder and Hell studied the [[List-$M$-partitions]{}]{} problem under the name CSP$^*_{1,2}(H)$ and showed [@FHFull Corollary 3.4] that, for every $M$, [[List-$M$-partitions]{}]{} is either [$\mathrm{NP}$]{}-complete, or is solvable in quasi-polynomial time. In the latter case, they showed that [[List-$M$-partitions]{}]{} is solvable in $n^{O(\log n)}$ time, given an $n$-vertex graph. Feder and Hell refer to this result as a “quasi-dichotomy”.

Although the Feder–Vardi conjecture remains open, a complete dichotomy is now known for counting CSPs. In particular, Bulatov [@Bul08] (see also [@DRfull]) has shown that, for every constraint language $\Gamma$, the counting constraint satisfaction problem ${\ensuremath{\mathrm{\#CSP}}}(\Gamma)$ is either polynomial-time solvable, or [$\mathrm{\#P}$]{}-complete. It is natural to ask whether a similar situation arises for counting list $M$-partition problems. We study the following computational problem, which is parameterised by a finite symmetric matrix $M\in\{0,1,*\}^{D\times D}\!$.

[[\#List-$M$-partitions]{}]{}.

A pair $(G,L)$ in which $G$ is a graph and $L$ is a function $V(G)\to{{\mathcal{P}(D)}}$.

The number of $M$-partitions of $G$ that respect $L$.

Hell, Hermann and Nevisi [@HHN] have considered the related problem [[\#$M$-partitions]{}]{} without lists, which can be seen as [[\#List-$M$-partitions]{}]{} restricted to the case that $L(v)=D$ for every vertex $v$. This problem is defined as follows.

[[\#$M$-partitions]{}]{}.

A graph $G$.

The number of $M$-partitions of $G$.

In the problems [[List-$M$-partitions]{}]{}, [[\#List-$M$-partitions]{}]{} and [[\#$M$-partitions]{}]{}, the matrix $M$ is fixed and its size does not vary with the input.

Hell et al. gave a dichotomy for small matrices $M$ (of size at most $3\times 3$). In particular, [@HHN Theorem 10] together with the graph-homomorphism dichotomy of Dyer and Greenhill [@DG] shows that, for every such $M$, [[\#$M$-partitions]{}]{} is either polynomial-time solvable or ${\ensuremath{\mathrm{\#P}}}$-complete. An interesting feature of counting $M$-partitions, identified by Hell et al. is that, unlike the situation for homomorphism-counting problems, there are tractable $M$-partition problems with non-trivial counting algorithms. Indeed the main contribution of the present paper, as described below, is to identify a set of “tractable” matrices $M$ and to give a non-trivial algorithm which solves [[\#List-$M$-partitions]{}]{} for every such $M$. We combine this with a proof that [[\#List-$M$-partitions]{}]{} is ${\ensuremath{\mathrm{\#P}}}$-complete for every other $M$.

Dichotomy theorems for counting list $M$-partitions {#subsec:dichotomy}
---------------------------------------------------

Our main theorem is a general dichotomy for the counting list $M$-partition problem, for matrices $M$ of all sizes. As noted above, since there is no known coding of list $M$-partition problems as CSPs without input restrictions, our theorem is not known to be implied by the dichotomy for [$\mathrm{\#CSP}$]{}.

Recall that [$\mathrm{FP}$]{} is the class of functions computed by polynomial-time deterministic Turing machines. [$\mathrm{\#P}$]{} is the class of functions $f$ for which there is a nondeterministic polynomial-time Turing machine that has exactly $f(X)$ accepting paths for every input $X$; this class can be thought of as the natural analogue of [$\mathrm{NP}$]{} for counting problems. Our main theorem is the following.

\[thm:dichotomy\][ For any symmetric matrix $M\in\{0,1,*\}^{D\times D}\!$, [[\#List-$M$-partitions]{}]{} is either in ${\ensuremath{\mathrm{FP}}}$ or ${\ensuremath{\mathrm{\#P}}}$-complete.]{}

To prove Theorem \[thm:dichotomy\], we investigate the complexity of the more general counting problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}, which has two parameters — a matrix $M\in\{0,1,*\}^{D\times D}$ and a (not necessarily proper) subset [$\mathcal L$]{} of ${{\mathcal{P}(D)}}$. In this problem, we only allow sets in [$\mathcal L$]{} to be used as lists.

[[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}.

A pair $(G,L)$ where $G$ is a graph and $L$ is a function $V(G)\to {\ensuremath{\mathcal L}}$.

The number of $M$-partitions of $G$ that respect $L$.

Note that $M$ and [$\mathcal L$]{} are fixed parameters of [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} — they are not part of the input instance. The problem [[\#List-$M$-partitions]{}]{} is just the special case of [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} where ${\ensuremath{\mathcal L}}= {{\mathcal{P}(D)}}$.

We say that a set ${\ensuremath{\mathcal L}}\subseteq {{\mathcal{P}(D)}}$ is [*subset-closed*]{} if $A\in {\ensuremath{\mathcal L}}$ implies that every subset of $A$ is in [$\mathcal L$]{}. This closure property is referred to as the “inclusive” case in [@FHFull].

\[def:closure\] Given a set ${\ensuremath{\mathcal L}}\subseteq {{\mathcal{P}(D)}}$, we write ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$ for its subset-closure, which is the set $${{\mathscr{S}({\ensuremath{\mathcal L}})}}=\{X \mid \mbox{for some $Y\in {\ensuremath{\mathcal L}}$,  $X\subseteq Y$}\}.$$

We prove the following theorem, which immediately implies Theorem \[thm:dichotomy\].

\[thm:fulldichotomy\][Let $M$ be a symmetric matrix in $\{0,1,*\}^{D\times D}$ and let ${\ensuremath{\mathcal L}}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. The problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is either in ${\ensuremath{\mathrm{FP}}}$ or ${\ensuremath{\mathrm{\#P}}}$-complete.]{}

Note that this does not imply a dichotomy for the counting $M$-partitions problem without lists. The problem with no lists corresponds to the case where every vertex of the input graph $G$ is assigned the list $D$, allowing the vertex to be potentially placed in any part. Thus, the problem without lists is equivalent to the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} with ${\ensuremath{\mathcal L}}=\{D\}$, but Theorem \[thm:fulldichotomy\] applies only to the case where [$\mathcal L$]{} is subset-closed.

Polynomial-time algorithms and an explicit dichotomy
----------------------------------------------------

We now introduce the concepts needed to give an explicit criterion for the dichotomy in Theorem \[thm:fulldichotomy\] and to provide polynomial-time algorithms for all tractable cases. We use standard definitions of relations and their arities, compositions and inverses.

For any symmetric $M\in\{0,1,*\}^{D\times D}$ and any sets $X,Y\in{{\mathcal{P}(D)}}$, define the binary relation $$H^M_{X,Y}=\{(i,j)\in X\times Y\mid M_{i,j}=*\}.$$

The intractability condition for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} begins with the following notion of rectangularity, which was introduced by Bulatov and Dalmau [@BD].

A relation $R\subseteq D\times D'$ is [*rectangular*]{} if, for all $i,j\in D$, and $i'\!,j'\in D'\!$, $$(i,i'),(i,j'),(j,i')\in R\implies (j,j')\in R\,.$$

Note that the intersection of two rectangular relations is itself rectangular. However, the composition of two rectangular relations is not necessarily rectangular: for example, $\{(1,1), (1,2),
(3,3)\}\circ \{(1,1), (2,3), (3,1)\} = \{(1,1), (1,3), (3,1)\}$.

Our dichotomy criterion will be based on what we call [$\mathcal L$]{}-$M$-derectangularising sequences. In order to define these, we introduce the notions of pure matrices and $M$-purifying sets.

Given index sets $X$ and $Y$, a matrix $M\in\{0,1,*\}^{X\times Y}$ is [*pure*]{} if it has no $0$s or has no $1$s.

Pure matrices correspond to ordinary graph homomorphism problems. As we noted above, $M$-partitions of $G$ correspond to homomorphisms of $G$ when $G$ is a $\{0,*\}$-matrix. The same is true (by complementation) when $G$ is a $\{1,*\}$-matrix.

For any $M\in\{0,1,*\}^{D\times D}$, a set ${\ensuremath{\mathcal L}}\subseteq {{\mathcal{P}(D)}}$ is [*$M$-purifying*]{} if, for all $X,Y\in{\ensuremath{\mathcal L}}$, the $X$-by-$Y$ submatrix $M|_{X\times Y}$ is pure.

For example, consider the matrix $$M = \left(\begin{matrix}
                  1 & * & 0 \\
                  * & 1 & * \\
                  0 & * & 1
              \end{matrix}\right)$$ with rows and columns indexed by $\{0,1,2\}$ in the obvious way. The matrix $M$ is not pure but for ${\ensuremath{\mathcal L}}=  \{\{0,1\},
\{2\}\}$, the set ${\ensuremath{\mathcal L}}$ is $M$-purifying and so is the closure ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$.

\[def:derect\] An [*[$\mathcal L$]{}-$M$-derectangularising sequence*]{} of length $k$ is a sequence $D_1,\dots,D_k$ with each $D_i \in{\ensuremath{\mathcal L}}$ such that:

-   $\{D_1,\ldots,D_k\}$ is $M$-purifying and

-   the relation $H^M_{D_1,D_2} \circ H^M_{D_2, D_3} \circ \dots \circ
    H^M_{D_{k-1}, D_k}$ is not rectangular.

If there is an $i\in \{1,\ldots,k\}$ such that $D_i$ is the empty set then the relation $H=H^M_{D_1,D_2} \circ H^M_{D_2, D_3} \circ \dots \circ
H^M_{D_{k-1}, D_k}$ is the empty relation, which is trivially rectangular. If there is an $i$ such that $|D_i|=1$ then $H$ is a Cartesian product, and is therefore rectangular. It follows that $|D_i|\geq 2$ for each $i$ in a derectangularising sequence.

We can now state our explicit dichotomy theorem, which implies Theorem \[thm:fulldichotomy\] and, hence, Theorem \[thm:dichotomy\].

\[thm:explicitdichotomy\][ Let $M$ be a symmetric matrix in $\{0,1,*\}^{D\times D}$ and let ${\ensuremath{\mathcal L}}{}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. If there is an [$\mathcal L$]{}-$M$-derectangularising sequence then the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is ${\ensuremath{\mathrm{\#P}}}$-complete. Otherwise, it is in ${\ensuremath{\mathrm{FP}}}$. ]{}

Sections \[sec:purifiedcsp\], \[sec:arc\] and \[sec:dichotomy\] develop a polynomial-time algorithm which solves the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} whenever there is no [$\mathcal L$]{}-$M$-derectangularising sequence. The algorithm involves several steps.

First, consider the case in which ${\ensuremath{\mathcal L}}$ is subset-closed and $M$-purifying. In this case, Proposition \[prop:purifiediscsp\] presents a polynomial-time transformation from an instance of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} to an instance of a related counting CSP. Algorithm \[alg:AC\] exploits special properties of the constructed CSP instance so that it can be solved in polynomial time using a CSP technique called arc-consistency. (This is proved in Lemma \[lem:quickarc\].) This provides a solution to the original [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} problem for the $M$-purifying case.

The case in which ${\ensuremath{\mathcal L}}$ is not $M$-purifying is tackled in Section \[sec:dichotomy\]. Section \[sec:DS\] gives algorithms for constructing the relevant data structures, which include a special case of sparse-dense partitions and also subcube decompositions. Algorithm \[alg:purify\] uses these data structures (via Algorithms \[alg:purifystep\], \[alg:Case1\], \[alg:Case2\], \[alg:Case3\] and \[alg:purifytriv\]) to reduce the [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} problem to a sequence of problems [[\#${\ensuremath{\mathcal L}}_i$-$M$-partitions]{}]{} where ${\ensuremath{\mathcal L}}_i$ is $M$-purifying. Finally, the polynomial-time algorithm is presented in Algorithms \[alg:mainpurifying\] and \[alg:main\]. For every ${\ensuremath{\mathcal L}}$ and $M$ where there is no ${\ensuremath{\mathcal L}}$-$M$-derectangularising sequence, either Algorithm \[alg:mainpurifying\] or Algorithm \[alg:main\] defines a polynomial-time function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} for solving the [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} problem, given an input $(G,L)$. The function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is not recursive. However, its *definition* is recursive in the sense that the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} defined in Algorithm \[alg:main\] calls a function [[\#${\ensuremath{\mathcal L}}_i$-$M$-partitions]{}]{} where ${\ensuremath{\mathcal L}}_i$ is a subset of ${{\mathcal{P}(D)}}$ whose cardinality is smaller than ${\ensuremath{\mathcal L}}$. The function [[\#${\ensuremath{\mathcal L}}_i$-$M$-partitions]{}]{} is, in turn, defined either in Algorithm \[alg:mainpurifying\] or in \[alg:main\].

The proof of Theorem \[thm:explicitdichotomy\] shows that, when Algorithms \[alg:mainpurifying\] and \[alg:main\] fail to solve the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}, the problem is ${\ensuremath{\mathrm{\#P}}}$-complete.

Complexity of the dichotomy criterion
-------------------------------------

Theorem \[thm:explicitdichotomy\] gives a precise criterion under which the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is in ${\ensuremath{\mathrm{FP}}}$ or ${\ensuremath{\mathrm{\#P}}}$-complete, where [$\mathcal L$]{} and $M$ are considered to be fixed parameters. In Section \[sec:meta\], we address the computational problem of determining which is the case, now treating [$\mathcal L$]{} and $M$ as inputs to this “meta-problem”. Dyer and Richerby [@DRfull] studied the corresponding problem for the [$\mathrm{\#CSP}$]{} dichotomy, showing that determining whether a constraint language $\Gamma$ satisfies the criterion for their ${\ensuremath{\mathrm{\#CSP}}}(\Gamma)$ dichotomy is reducible to the graph automorphism problem, which is in [$\mathrm{NP}$]{}. We are interested in the following computational problem, which we show to be [$\mathrm{NP}$]{}-complete.

[[ExistsDerectSeq]{}]{}.

An index set $D$, a symmetric matrix $M$ in $\{0,1,*\}^{D\times D}$ (represented as an array) and a set ${\ensuremath{\mathcal L}}{}\subseteq{{\mathcal{P}(D)}}$ (represented as a list of lists).

“Yes”, if there is an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence; “no”, otherwise.

\[thm:meta\][ [[ExistsDerectSeq]{}]{} is [$\mathrm{NP}$]{}-complete under polynomial-time many-one reductions.]{}

Note that, in the definition of the problem [[ExistsDerectSeq]{}]{}, the input ${\ensuremath{\mathcal L}}$ is not necessarily subset-closed. Subset-closedness allows a concise representation of some inputs: for example, ${{\mathcal{P}(D)}}$ has exponential size but it can be represented as ${{\mathscr{S}(\{D\})}}$, so the corresponding input is just ${\ensuremath{\mathcal L}}=\{D\}$. In fact, our proof of Theorem \[thm:meta\] uses a set of lists [$\mathcal L$]{} where $|X|\leq 3$ for all $X\in{\ensuremath{\mathcal L}}$. Since there are at most $|D|^3+1$ such sets, our [$\mathrm{NP}$]{}-completeness proof would still hold if we insisted that the input [$\mathcal L$]{} to [[ExistsDerectSeq]{}]{} must be subset-closed.

Let us return to the original problem [[\#List-$M$-partitions]{}]{}, which is the special case of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} where ${\ensuremath{\mathcal L}}={{\mathcal{P}(D)}}$. This leads us to be interested in the following computational problem.

[[MatrixHasDerectSeq]{}]{}.

An index set $D$ and a symmetric matrix $M$ in $\{0,1,*\}^{D\times D}$ (represented as an array).

“Yes”, if there is a ${{\mathcal{P}(D)}}$-$M$-derectangularising sequence; “no”, otherwise.

Theorem \[thm:meta\] does not quantify the complexity of [[MatrixHasDerectSeq]{}]{} because its proof relies on a specific choice of [$\mathcal L$]{} which, as we have noted, is not ${{\mathcal{P}(D)}}$. Nevertheless, the proof of Theorem \[thm:meta\] has the following corollary.

\[cor:meta\][ [[MatrixHasDerectSeq]{}]{} is in [$\mathrm{NP}$]{}.]{}

Cardinality constraints
-----------------------

Many combinatorial structures can be represented as $M$-partitions with the addition of cardinality constraints on the parts. For example, it might be required that certain parts be non-empty or, more generally, that they contain at least $k$ vertices for some fixed $k$.

Feder et al. [@FHKM] showed that the problem of determining whether such a structure exists in a given graph can be reduced to a [[List-$M$-partitions]{}]{} problem in which the cardinality constraints are expressed using lists. In Section \[sec:card\], we extend this to counting. We show that any [[\#$M$-partitions]{}]{} problem with additional cardinality constraints of the form, “part $d$ must contain at least $k_d$ vertices” is polynomial-time Turing reducible to [[\#List-$M$-partitions]{}]{}. As a corollary, we show that the “homogeneous pairs” introduced by Chvátal and Sbihi [@CS1987:Bull-free] can be counted in polynomial time. Homogeneous pairs can be expressed as an $M$-partitions problem for a certain $6\times 6$ matrix, with cardinality constraints on the parts.

Preliminaries {#sec:prelim}
=============

For a positive integer $k$, we write $[k]$ to denote the set $\{1,\dots,k\}$. If $\mathcal{S}$ is a set of sets then we use $\bigcap \mathcal{S}$ to denote the intersection of all sets in $\mathcal{S}$. The vertex set of a graph $G$ is denoted $V(G)$ and its edge set is $E(G)$. We write $\{0,1,*\}^{D}$ for the set of all functions $\sigma\colon D\to\{0,1,*\}$ and $\{0,1,*\}^{D\times D'}$ for the set of all matrices $M=(M_{i,j})_{i\in
  D,j\in D'}$, where each $M_{i,j}\in\{0,1,*\}$.

We always use the term “$M$-partition” when talking about a partition of the vertices of a graph according to a $\{0,1,*\}$-matrix $M$. When we use the term “partition” without referring to a matrix, we mean it in the conventional sense of partitioning a set $X$ into disjoint subsets $X_1, \dots, X_k$ with $X_1\cup
\dots \cup X_k = X$.

We view computational counting problems as functions mapping strings over input alphabets to natural numbers. Our model of computation is the standard multi-tape Turing machine. We say that a counting problem $P$ is polynomial-time Turing-reducible to another counting problem $Q$ if there is a polynomial-time deterministic oracle Turing machine $M$ such that, on every instance $x$ of $P$, $M$ outputs $P(x)$ by making queries to oracle $Q$. We say that $P$ is polynomial-time Turing-equivalent to $Q$ if each is polynomial-time Turing-reducible to the other. For decision problems (languages), we use the standard many-one reducibility: language $A$ is many-one reducible to language $B$ if there exists a function $f$ that is computable in polynomial time such that $x\in A$ if and only if $f(x)\in B$.

Counting list $M$-partition problems and counting CSPs {#sec:purifiedcsp}
======================================================

Toward the development of our algorithms and the proof of our dichotomy, we study a special case of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}, in which [$\mathcal L$]{} is $M$-purifying and subset-closed. For such [$\mathcal L$]{} and $M$, we show that the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is polynomial-time Turing-equivalent to a counting constraint satisfaction problem ([$\mathrm{\#CSP}$]{}). To give the equivalence, we introduce the notation needed to specify \#CSPs.

A *constraint language* is a finite set $\Gamma$ of named relations over some set $D$. For such a language, we define the counting problem ${\ensuremath{\mathrm{\#CSP}}}(\Gamma)$ as follows.

${\ensuremath{\mathrm{\#CSP}}}(\Gamma)$.

A set $V$ of variables and a set $C$ of constraints of the form ${\langle (v_1,\dots,v_k),R \rangle}$, where $(v_1,\dots,v_k)\in V^k$ and $R$ is an arity-$k$ relation in $\Gamma$.

The number of assignments $\sigma\colon V\to D$ such that $$\label{eq:satisfying}(\sigma(v_1),\dots,\sigma(v_k))\in
  R\text{ for all }{\langle (v_1,\dots,v_k),R \rangle}\in C\,.$$

The tuple of variables $v_1, \dots, v_k$ in a constraint is referred to as the constraint’s *scope*. The assignments $\sigma\colon V\to D$ for which holds are called the [*satisfying assignments*]{} of the instance $(V,C)$. Note that a unary constraint ${\langle v,R \rangle}$ has the same effect as a list: it directly restricts the possible values of the variable $v$. As before, we allow the possibility that $\emptyset\in\Gamma$; any instance that includes a constraint ${\langle (v_1, \dots, v_k), \emptyset \rangle}$ has no satisfying assignments.

\[defgammaprime\] Let $M$ be a symmetric matrix in $\{0,1,*\}^{D\times D}$ and let [$\mathcal L$]{} be a subset-closed $M$-purifying set. Define the constraint language $$\Gamma'_{\!{\ensuremath{\mathcal L}},M} = \{H^M_{X,Y}\mid X,Y\in{\ensuremath{\mathcal L}}\}$$ and let ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}= \Gamma'_{\!{\ensuremath{\mathcal L}},M} \cup {{\mathcal{P}(D)}}$, where ${{\mathcal{P}(D)}}$ represents the set of all unary relations on $D$.

The unary constraints in ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$ will be useful in our study of the complexity of the dichotomy criterion, in Section \[sec:meta\]. First, we define a convenient restriction on instances of ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$.

\[def:simple\] An instance of ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ is *simple* if:

-   there is exactly one unary constraint ${\langle v,X_v \rangle}$ for each variable $v\in V\!$,

-   there are no binary constraints ${\langle (v,v),R \rangle}$, and

-   each pair $u$, $v$ of distinct variables appears in at most one constraint of the form ${\langle (u,v),R \rangle}$ or ${\langle (v,u),R \rangle}$.

\[lemma:simple\] For every instance $(V,C)$ of ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$, there is a simple instance $(V,C')$ such that an assignment $\sigma\colon V\to D$ satisfies $(V,C)$ if and only if it satisfies $(V,C')$. Further, such an instance can be computed in polynomial time.

Observe that the set of binary relations in ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$ is closed under intersections: $H^M_{X,Y} \cap H^M_{X'\!,Y'} =
    H^M_{X\cap X'\!,Y\cap Y'}$ and this relation is in ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$ because [$\mathcal L$]{} is subset-closed. The binary part of ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$ is also closed under relational inverse because $M$ is symmetric, so $$\left(H^M_{X,Y}\right)^{-1} = \{(b,a) \mid (a,b)\in H^M_{X,Y}\}
                                              = H^M_{Y,X}\in {\Gamma_{\!{\ensuremath{\mathcal L}}, M}}\,.$$ Since ${{\mathcal{P}(D)}}\subseteq {\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$, the set of unary relations is also closed under intersections.

We construct $C'$ as follows, starting with $C$. Any binary constraint ${\langle (v,v), R \rangle}$ can be replaced by the unary constraint ${\langle v, \{d\mid (d,d)\in R\} \rangle}$. All the binary constraints between distinct variables $u$ and $v$ can be replaced by the single constraint $$\left\langle
            (u,v), \bigcap 
                \{R \mid {\langle (u,v), R \rangle}\in C
                    \text{ or } {\langle (v,u), R^{-1} \rangle}\in C\}
        \right\rangle\,.$$ Let the set of constraints produced so far be $C''\!$. For each variable $v$ in turn, if there are no unary constraints applied to $v$ in $C''\!$, add the constraint ${\langle v, D \rangle}$; otherwise, replace all the unary constraints involving $v$ in $C''$ with the single constraint $$\left\langle
            v, \bigcap \{R \mid {\langle v, R \rangle}\in C''\}
        \right\rangle\,.$$ $C'$ is the resulting constraint set. The closure properties established above guarantee that $(V,C')$ is a ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ instance. It is clear that it has the same satisfying assignments as $(V,C)$ and that it can be produced in polynomial time.

Our main result connecting the counting list $M$-partitions problem with counting CSPs is the following.

\[prop:purifiediscsp\][For any symmetric $M\in\{0,1,*\}^{D\times
    D}$ and any subset-closed, $M$-purifying set [$\mathcal L$]{}, the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is polynomial-time Turing-equivalent to ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$.]{}

Because of its length, we split the proof of the proposition into two lemmas.

For any symmetric $M\in\{0,1,*\}^{D\times D}$ and any subset-closed, $M$-purifying set [$\mathcal L$]{}, ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ is polynomial-time Turing-reducible to [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}.

Consider an input $(V,C)$ to ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$, which we may assume to be simple. Each variable appears in exactly one unary constraint, ${\langle v,X_v \rangle}\in C$. Any variable $v$ that is not used in a binary constraint can take any value in $X_v$ so just introduces a multiplicative factor of $|X_v|$ to the output of the counting CSP. Thus, we will assume without loss of generality that every variable is used in at least one constraint with a relation from $\Gamma'_{\!{\ensuremath{\mathcal L}},M}$ and, by simplicity, there are no constraints of the form ${\langle (v,v),R \rangle}$.

We now define a corresponding instance $(G,L)$ of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}. The vertices of $G$ are the variables $V$ of the [$\mathrm{\#CSP}$]{} instance. For each variable $v\in V\!$, set $$L(v) = X_v \cap \bigcap\left\{
               X \mid 
               \mbox{for some $u$ and $Y$,
              $ {\langle (v,u),H^M_{X,Y} \rangle}\in C$  or
                     $ {\langle (u,v),H^M_{Y,X} \rangle}\in C$}
           \right\}.$$ The edges $E(G)$ of our instance are the unordered pairs $\{u,v\}$ that satisfy one of the following conditions:

-   there is a constraint between $u$ and $v$ in $C$ and $M|_{L(u)\times L(v)}$ has a $0$ entry, or

-   there is no constraint between $u$ and $v$ in $C$ and $M|_{L(u)\times L(v)}$ has a $1$ entry.

Since every vertex $v$ is used in at least one constraint with a relation $H^M_{X,Y}$ where, by definition, $X$ and $Y$ are in ${\ensuremath{\mathcal L}}$, every set $L(v)$ is a subset of some set $W\in{\ensuremath{\mathcal L}}$. [$\mathcal L$]{} is subset-closed so $L(v)\in{\ensuremath{\mathcal L}}$ for all $v\in V$, as required.

We claim that a function $\sigma\colon V\to D$ is a satisfying assignment of $(V,C)$ if and only if it is an $M$-partition of $G$ that respects $L$. Note that, since [$\mathcal L$]{} is $M$-purifying, no submatrix $M|_{X\times Y}$ ($X,Y\in{\ensuremath{\mathcal L}})$ contains both 0s and 1s.

First, suppose that $\sigma$ is a satisfying assignment of $(V,C)$. For each variable $v$, $\sigma$ satisfies all the constraints ${\langle v,X_v \rangle}$, ${\langle (v,u),H^M_{X,Y} \rangle}$ and ${\langle (u,v),H^M_{Y,X} \rangle}$ containing $v$. Therefore, $\sigma(v)\in X_v$ and $\sigma(v)\in X$ for each binary constraint ${\langle (v,u),H^M_{X,Y} \rangle}$ or ${\langle (u,v),H^M_{Y,X} \rangle}$, so $\sigma$ satisfies all the list requirements.

To show that $\sigma$ is an $M$-partition of $G$, consider any pair of distinct vertices $u,v\in V$. If there is a constraint ${\langle (u,v), H^M_{X,Y} \rangle}\in C$, then $\sigma$ satisfies this constraint so $M_{\sigma(u),\sigma(v)}=*$ and $u$ and $v$ cannot stop $\sigma$ being an $M$-partition. Conversely, suppose there is no constraint between $u$ and $v$ in $C$. If $M|_{L(u)\times L(v)}$ contains a 0, there is no edge $(u,v)\in E(G)$ by construction; otherwise, if $M|_{L(u)\times L(v)}$ contains a 1, there is an edge $(u,v)\in E(G)$ by construction; otherwise, $M_{x,y}=*$ for all $x\in L(u)$, $y\in L(v)$. In all three cases, the assignment to $u$ and $v$ is consistent with $\sigma$ being an $M$-partition.

Conversely, suppose that $\sigma$ is not a satisfying assignment of $(V,C)$. If $\sigma$ does not satisfy some unary constraint ${\langle v,X \rangle}$ then $\sigma(v)\notin L(v)$ so $\sigma$ does not respect [$\mathcal L$]{}. If $\sigma$ does not satisfy some binary constraint ${\langle (u,v), H^M_{X,Y} \rangle}$ where $u$ and $v$ are distinct then, by definition of the relation $H^M_{X,Y}$, $M_{\sigma(u),\sigma(v)}\neq
*$. If $M_{\sigma(u),\sigma(v)}=0$, there is an edge $(u,v)\in E(G)$ by construction, which is forbidden in $M$-partitions; if $M_{\sigma(u),\sigma(v)}=1$, there is no edge $(u,v)\in E(G)$ but this edge is required in $M$-partitions. Hence, $\sigma$ is not an $M$-partition.

For any symmetric $M\in\{0,1,*\}^{D\times D}$ and any subset-closed, $M$-purifying set [$\mathcal L$]{}, the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is polynomial-time Turing-reducible to ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$.

We now essentially reverse the construction of the previous lemma to give a reduction from [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} to ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$. For any instance ($G,L)$ of [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}, we construct a corresponding instance $(V,C)$ of ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ as follows. The set of variables $V$ is $V(G)$. The set of constraints $C$ consists of a constraint ${\langle v,
L(v) \rangle}$ for each vertex $v\in V(G)$ and a constraint ${\langle (u,v),
H^M_{L(u),L(v)} \rangle}$ for every pair of distinct vertices $u$, $v$ such that:

-   $(u,v)\in E(G)$ and $M|_{L(u)\times L(v)}$ has a 0 entry, or

-   $(u,v)\not\in E(G)$ and $M|_{L(u)\times L(v)}$ has a 1 entry.

We show that a function $\sigma\colon V\to D$ is a satisfying assignment of $(V,C)$ if and only if it is an $M$-partition of $G$ that respects $L$. It is clear that $\sigma$ satisfies the unary constraints if and only if it respects $L$.

If $\sigma$ satisfies $(V,C)$ then consider any pair of distinct vertices $u,v\in V$. If there is a binary constraint involving $u$ and $v$, then $M_{\sigma(u),\sigma(v)} = M_{\sigma(v),\sigma(u)} = *$ so the existence or non-existence of the edge $(u,v)$ of $G$ does not affect whether $\sigma$ is an $M$-partition. If there is no binary constraint involving $u$ and $v$, then either there is an edge $(u,v)\in
E(G)$ and $M_{\sigma(u),\sigma(v)}\neq 0$ or there is no edge $(u,v)$ and $M_{\sigma(u),\sigma(v)}\neq 1$. In all three cases, $\sigma$ maps $u$ and $v$ consistently with it being an $M$-partition.

Conversely, if $\sigma$ does not satisfy $(V,C)$, either it fails to satisfy a unary constraint, in which case it does not respect $L$, or it satisfies all unary constraints (so it respects $L$), but it fails to satisfy a binary constraint ${\langle (u,v),H^M_{L(u),L(v)} \rangle}$. In the latter case, by construction, $M_{\sigma(u),\sigma(v)}\neq *$ so either $M_{\sigma(u),\sigma(v)}=0$ but there is an edge $(u,v)\in E(G)$, or $M_{\sigma(u),\sigma(v)}=1$ and there is no edge $(u,v)\in E(G)$. In either case, $\sigma$ is not an $M$-partition of $G$.

An arc-consistency based algorithm for ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ {#sec:arc}
=================================================================================================================

In the previous section, we showed that a class of [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} problems is equivalent to a certain class of counting CSPs, where the constraint language consists of binary relations and all unary relations over the domain $D$. We now investigate the complexity of such [$\mathrm{\#CSP}$]{}s.

Arc-consistency is a standard solution technique for constraint satisfaction problems [@CSPbook]. It is, essentially, a local search method which initially assumes that each variable may take any value in the domain and iteratively reduces the range of values that can be assigned to each variable, based on the constraints applied to it and the values that can be taken by other variables in the scopes of those constraints.

For any simple ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ instance $(V,C)$, define the vector of [*arc-consistent domains*]{} $(D_v)_{v\in V}$ by the procedure in Algorithm \[alg:ACComp\].

At no point in the execution of the algorithm can any domain $D_v$ increase in size so, for fixed $D$, the running time of the algorithm is at most a polynomial in $|V|+|C|$.

It is clear that, if $(D_v)_{v\in V}$ is the vector of arc-consistent domains for a simple ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ instance $(V,C)$, then every satisfying assignment $\sigma$ for that instance must have $\sigma(v)\in D_v$ for each variable $v$. In particular, if some $D_v=\emptyset$, then the instance is unsatisfiable. (Note, though, that the converse does not hold. If $D=\{0,1\}$ and $R=\{(0,1),(1,0)\}$, the instance with constraints ${\langle x,D \rangle}$, ${\langle y,D \rangle}$, ${\langle z,D \rangle}$, ${\langle (x,y),R \rangle}$, ${\langle (y,z),R \rangle}$ and ${\langle (z,x),R \rangle}$ is unsatisfiable but arc-consistency assigns $D_x = D_y = D_z =
\{0,1\}$.)

The arc-consistent domains computed for a simple instance $(V,C)$ can yield further simplification of the constraint structure, which we refer to as [*factoring*]{}. The factoring applies when the arc-consistent domains restrict a binary relation to a Cartesian product. In this case, the binary relation can be replaced with corresponding unary relations. Algorithm \[alg:factor\] factors a simple instance with respect to a vector $(D_v)_{v\in V}$ of arc-consistent domains, producing a set $F$ of factored constraints.

Recall that there is at most one constraint in $C$ between distinct variables and there are no binary constraints ${\langle (v,v), R \rangle}$ because the instance is simple. Note also that, if $|D_u|\leq 1$ or $|D_v|\leq 1$, then $R\cap (D_u\times D_v)$ is necessarily a Cartesian product. It is easy to see that the result of factoring a simple instance is simple, that Algorithm \[alg:factor\] runs in polynomial time and that the instance $(V,F)$ has the same satisfying assignments as $(V,C)$.

The *constraint graph* of a [$\mathrm{CSP}$]{} instance $(V,C)$ (in any constraint language) is the undirected graph with vertex set $V$ that contains an edge between every pair of distinct variables that appear together in the scope of some constraint.

\[line:choice\]

Algorithm \[alg:AC\] uses arc-consistency to count the satisfying assignments of simple ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ instances. It is straightforward to see that the algorithm terminates, since each recursive call is either on an instance with strictly fewer variables or on one in which at least one variable has had its unary constraint reduced to a singleton and no variable’s unary constraint has increased. For general inputs, the algorithm may take exponential time to run but, in Lemma \[lem:quickarc\] we show that the running time is polynomial for the inputs we are interested in.

We first argue that the algorithm is correct. By Lemma \[lemma:simple\], we may assume that the given instance $(V,C)$ is simple. Every satisfying assignment $\sigma\colon V\to D$ satisfies $\sigma(v)\in D_v$ for all $v\in V$ so restricting our attention to arc-consistent domains does not alter the output. Factoring the constraints also does not change the number of satisfying assignments: it merely replaces some binary constraints with equivalent unary ones. The constraints are factored, so any variable $v$ with $|D_v|=1$ must, in fact, be an isolated vertex in the constraint graph because, as noted above, any binary constraint involving it has been replaced by unary constraints. Therefore, if a component $H_i$ contains a variable $v$ with $|D_v|=1$, that component is the single vertex $v$, which is constrained to take a single value, so the number of satisfying assignments for this component, which we denote $Z_i$, is equal to $1$. (So we have now shown that the if branch in the for loop is correct.) For components that contain more than one variable, it is clear that we can choose one of those variables, $w_i$, and group the set of $M$-partitions $\sigma$ according to the value of $\sigma(w_i)$. (So we have now shown that the else branch is correct.) Because there are no constraints between variables in different components of the constraint graph, the number of satisfying assignments factorises as $\prod_{i=1}^\kappa Z_i$.

For a binary relation $R$, we write $$\begin{aligned}
    \pi_1(R) &= \{a \mid (a,b)\in R \text{ for some }b\} \\
    \pi_2(R) &= \{b \mid (a,b)\in R \text{ for some }a\}\,.\end{aligned}$$

For the following proof, we will also need the observation of Dyer and Richerby [@DRfull Lemma 1] that any rectangular relation $R\subseteq
\pi_1(R)\times \pi_2(R)$ can be written as $(A_1\times B_1) \cup \dots
\cup (A_\lambda \times B_\lambda)$, where the $A_i$ and $B_i$ partition $\pi_1(R)$ and $\pi_2(R)$, respectively. The subrelations $A_i\times B_i$ are referred to as [*blocks*]{}. A rectangular relation $R\neq \pi_1(R)\times \pi_2(R)$ must have at least two blocks.

\[lem:quickarc\] Suppose that ${\ensuremath{\mathcal L}}$ is subset-closed and $M$-purifying. If there is no [$\mathcal L$]{}-$M$-derectangularising sequence, then Algorithm \[alg:AC\] runs in polynomial time.

We will argue that the number of recursive calls made by the function AC in Algorithm \[alg:AC\] is bounded above by a polynomial in $|V|$. This suffices, since every other step of the procedure is obviously polynomial.

Consider a run of the algorithm on instance $(V,C)$ which, by Lemma \[lemma:simple\], we may assume to be simple. Suppose the run makes a recursive call with input $(V_i,F'_{i,d})$. For each $v\in V_i$, let $D'_{v}$ denote the arc-consistent domain for $v$ that is computed during the recursive call. We will show below that $D'_v\subset D_v$ for every variable $v\in V_i$. This implies that the recursion depth is at most $|D|$. As a crude bound, it follows that the number of recursive calls is at most ${(|V|\cdot |D|)}^{|D|},$ since each recursive call that is made is nested below a sequence of at most $|D|$ previous calls, each of which chose a vertex $v\in V$ and “pinned” it to a domain element $d\in D$ (i.e., introduced the constraint ${\langle v,\{d\} \rangle}$).

Towards showing that the domains of all variables decrease at each recursive call, suppose that we are computing $\mathrm{AC}(V,C)$ and the arc-consistent domains are $(D_v)_{v\in V}$. As observed above, for any component $H_i$ of the constraint graph on which a recursive call is made, we must have $|D_v|>1$ for every $v\in V_i$. Fix such a component and, for each $v\in V_i$, let $D'_v$ be the arc-consistent domain calculated for $v$ in the recursive call on $H_i$. It is clear that $D'_v\subseteq D_v$; we will show that $D'_v
\subset D_v$.

Consider a path $v_1\dots v_\ell$ in $H_i$, where $v_1=w_i$ and $v_\ell=v$. For each $j\in[\ell-1]$, there is exactly one binary constraint in $F_i$ involving $v_j$ and $v_{j+1}$. This is either ${\langle (v_j, v_{j+1}), R_j \rangle}$ or ${\langle (v_{j+1}, v_j), R_j^{-1} \rangle}$ and, without loss of generality, we may assume that it is the former. For $j\in[\ell-1]$, let $R'_j = R_j \cap (D_{v_j} \times D_{v_{j+1}}) =
H^M_{D_{v_j},D_{v_{j+1}}}$. The relation $R'_j$ is pure because $D_{v_j}$ and $D_{v_{j+1}}$ are in the subset-closed set ${\ensuremath{\mathcal L}}$ and, since ${\ensuremath{\mathcal L}}$ is $M$-purifying, so is $\{D_{v_j},D_{v_{j+1}}\}$. These two domains do not form a derectangularising sequence by the hypothesis of the lemma, so $H^M_{D_{v_j},D_{v_{j+1}}}$ is rectangular. If some $R_j=\emptyset$ then $D_{v_j} = D_{v_{j+1}} = \emptyset$ by arc-consistency, contradicting the fact that $|D_v|>1$ for all $v\in
V_i$. If some $R'_j$ has just one block, $R_j\cap (D_{v_j}\times
D_{v_{j+1}})$ is a Cartesian product, contradicting the fact that $F$ is a factored set of constraints. Thus, every $R'_j$ has at least two blocks.

For $j\in[\ell-1]$, let $\Phi_j = R'_1 \circ \dots \circ R'_j$. As above, note that $\{D_{v_1}, \ldots, D_{v_{j+1}}\}$ is $M$-purifying and the sequence $D_{v_1}, \dots, D_{v_{j+1}}$ is not derectangularising, so $\Phi_j$ is rectangular. We will show by induction on $j$ that $\pi_1(\Phi_j) = D_{v_1}$, $\pi_2(\Phi_j) = D_{v_{j+1}}$ and $\Phi_j$ has at least two blocks. Therefore, since the recursive call constrains $\sigma(w_i)$ to be $d$ and $d\in A$ for some block $A\times B\subset \Phi_\ell$, we have $D'_v\subseteq B\subset
D_v$, which is what we set out to prove.

For the base case of the induction, take $j=1$ so $\Phi_1=R'_1$. We showed above that $R'_1$ has at least two blocks and that $R'_1= H^M_{D_{v_1},D_{v_2}}$. By arc-consistency, $\pi_1(R'_1) = D_{v_1}$ and $\pi_2(R'_1) = D_{v_2}$.

For the inductive step, take $j\in [\ell-2]$. Suppose that $\pi_1(\Phi_j)=D_{v_1}$, $\pi_2(\Phi_j)=D_{v_{j+1}}$ and $\Phi_j = \bigcup_{s=1}^\lambda (A_s\times
A'_s)$ has at least two blocks. We have $\Phi_{j+1} = \Phi_j\circ R'_{j+1}$ and $R'_{j+1} = \bigcup_{t=1}^\mu (B_t\times B'_t)$ for some $\mu \geq 2$.

For every $d\in D_{v_1}$, there is a $d'\in D_{v_{j+1}}$ such that $(d,d')\in \Phi_j$ by the inductive hypothesis, and a $d''\in
D_{v_{j+1}}$ such that $(d'\!, d'')\in D_{v_{j+2}}$, by arc-consistency. Therefore, $\pi_1(\Phi_{j+1}) = D_{v_1}$; a similar argument shows that $\pi_2(\Phi_{j+1}) = D_{v_{j+2}}$.

Suppose, towards a contradiction, that $\Phi_{j+1} = D_{v_1}\times
D_{v_{j+2}}$. For this to be the case, we must have $A'_s\cap B_t\neq\emptyset$ for every $s\in\{1,2\}$ and $t\in[\mu]$. Now, let $D^*_{v_{j+1}}=D_{v_{j+1}}\setminus
                            (A'_2\cap B_2)$ and consider the relation $$R = \{(d_1, d_3) \mid 
    \mbox{for some $d_2\in D^*_{v_{j+1}} $, 
                            $(d_1, d_2)\in \Phi_j$  and $(d_2, d_3)\in R'_{j+1}$ 
                            }\}.$$ Since $A'_1 \subseteq D^*_{v_{j+1}}$ the non-empty sets $A'_1 \cap B_1$ and $A'_1 \cap B_2$ are both subsets of $D^*_{v_{j+1}}$ so $A_1\times B'_1\subseteq R$ and $A_1\times B'_2\subseteq
R$. Similarly, $B_1 \subseteq D^*_{v_{j+1}}$, so $A'_2 \cap B_1 \subseteq D^*_{v_{j+1}}$ so $A_2\times B'_1\subseteq R$. However, $(A_2\times B'_2)\cap R =
\emptyset$, so $R$ is not rectangular. We will now derive a contradiction by showing that $R$ is rectangular. Note that $$R = H^M_{D_{v_1},D_{v_2}} \circ \cdots \circ H^M_{D_{v_{j-1}},D_{v_j}}
\circ
H^M_{D_{v_j},D^*_{v_{j+1}}}
\circ
H^M_{D^*_{v_{j+1}},D_{v_{j+2}}}$$ but this relation is rectangular because the hypothesis of the lemma guarantees that the sequence $$D_{v_1},\ldots,D_{v_{j}},D^*_{v_{j+1}},D_{v_{j+2}}$$ is not an ${\ensuremath{\mathcal L}}$-$M$-derectangularising sequence and all of the elements of this sequence are in ${\ensuremath{\mathcal L}}$, and $\{D_{v_1},\ldots,D_{v_{j}},D^*_{v_{j+1}},D_{v_{j+2}}\}$ is $M$-purifying.

Polynomial-time algorithms and the dichotomy theorem {#sec:dichotomy}
====================================================

Bulatov [@Bul08] showed that every problem of the form ${\ensuremath{\mathrm{\#CSP}}}(\Gamma)$ is either in ${\ensuremath{\mathrm{FP}}}$ or $\nP$-complete. Together with Proposition \[prop:purifiediscsp\], his result immediately shows that a similar dichotomy exists for the special case of the problem [[\#$\mathcal L$-$M$-partitions]{}]{} in which $\mathcal L$ is $M$-purifying and is closed under subsets. Our algorithmic work in Section \[sec:arc\] can be combined with Dyer and Richerby’s explicit dichotomy for ${\ensuremath{\mathrm{\#CSP}}}$ to obtain an explicit dichotomy for this special case of [[\#$\mathcal L$-$M$-partitions]{}]{}. In particular, Lemma \[lem:quickarc\] gives a polynomial-time algorithm for the case in which there is no [$\mathcal L$]{}-$M$-derectangularising sequence. When there is such a sequence, ${\Gamma_{\!{\ensuremath{\mathcal L}}, M}}$ is not “strongly rectangular” in the sense of [@DRfull]. It follows immediately that ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ is [$\mathrm{\#P}$]{}-complete [@DRfull Lemma 24] so [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is also [$\mathrm{\#P}$]{}-complete by Proposition \[prop:purifiediscsp\]. In fact, the dichotomy for this special case does not require the full generality of Dyer and Richerby’s dichotomy. If there is an [$\mathcal L$]{}-$M$-derectangularising sequence then it follows immediately from work of Bulatov and Dalmau [@BD Theorem 2 and Corollary 3] that ${\ensuremath{\mathrm{\#CSP}}}({\Gamma_{\!{\ensuremath{\mathcal L}}, M}})$ is [$\mathrm{\#P}$]{}-complete.

In this section we will move beyond the case in which ${\ensuremath{\mathcal L}}$ is $M$-purifying to provide a full dichotomy for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}. We will use two data structures: *sparse-dense partitions* and a representation of the set of *splits* of a bipartite graph. Similar data structures were used by Hell et al. [@HHN] in their dichotomy for the [[\#$M$-partitions]{}]{} problem for matrices of size at most $3$-by-$3$.

Data Structures {#sec:DS}
---------------

We use two types of graph partition. The first is a special case of a sparse-dense partition [@FHKM] which is also called an $(a,b)$-graph with $a=b=2$.

\[def:bsd\] A bipartite–cobipartite partition of a graph $G$ is a partition $(B,C)$ of $V(G)$ such that $B$ induces a bipartite graph and $C$ induces the complement of a bipartite graph.

\[lem:sparsedense\][@FHKM Theorem 3.1; see also the remarks on $(a,b)$-graphs.] There is a polynomial-time algorithm for finding all bipartite–cobipartite partitions of a graph $G$.

The second decomposition is based on certain sub-hypercubes called subcubes. For any finite set $U\!$, a [*subcube*]{} of $\{0,1\}^U$ is a subset of $\{0,1\}^U$ that is a Cartesian product of the form $\prod_{u\in U} S_u$ where $S_u\in\{\{0\},\{1\},\{0,1\}\}$ for each $u\in U\!$. We can also associate a subcube $\prod_{u\in U} S_u$ with the set of assignments $\sigma\colon U\to \{0,1\}$ such that $\sigma(u)\in S_u$ for all $u\in
U\!$. Subcubes can be represented efficiently by listing the projections $S_u$.

Let $G=(U,U'\!,E)$ be a bipartite graph, where $U$ and $U'$ are disjoint vertex sets, and $E\subseteq U\times U'\!$. A [*subcube decomposition*]{} of $G$ is a list $U_1,\dots,U_k$ of subcubes of $\{0,1\}^U$ and a list $U'_1,\dots, U'_k$ of subcubes of $\{0,1\}^{U'}$ such that the following hold.

-   The union $(U_1\times U'_1)\cup \dots \cup (U_k\times U'_k)$ is the set of assignments $\sigma\colon
      U\cup U'\to\{0,1\}$ such that: $$\begin{aligned}
       & \label{todayone} \mbox{no edge $(u,u')\in E$ has $\sigma(u)=\sigma(u')=0$ and}\\
       & \label{todaytwo} \mbox{no pair $(u,u')\in (U\times U')\setminus E$ has $\sigma(u)=
        \sigma(u')=1$.}
      \end{aligned}$$

-   For distinct $i,j\in[k]$, $U_i\times U'_i$ and $U_j\times U'_j$ are disjoint.

-   For each $i\in[k]$, either $|U_i|=1$ or $|U'_i|=1$ (or both).

Note that, although we require $U_i\times U'_i$ and $U_j\times U'_j$ to be disjoint for distinct $i,j\in[k]$, we allow $U_i\cap
U_j\neq\emptyset$ as long as $U'_i$ and $U'_j$ are disjoint, and vice-versa. It is even possible that $U_i=U_j$, and indeed this will happen in our constructions below.

\[lem:splittocubes\] A subcube decomposition of a bipartite graph $G=(U,U'\!,E)$ can be computed in polynomial time, with the subcubes represented by their projections.

For a vertex $x$ in a bipartite graph, let $\Gamma(x)$ be its set of neighbours and let ${\overline{\Gamma}}(x)$ be its set of non-neighbours on the other side of the graph. Thus, for $x\in U\!$, ${\overline{\Gamma}}(x) =
U'\setminus \Gamma(x)$ and, for $x\in U'\!$, ${\overline{\Gamma}}(x) =
U\setminus \Gamma(x)$.

Observe that we can write $\{0,1\}^n\setminus
\{0\}^n$ as the disjoint union of $n$ subcubes $\{0\}^{k-1}\times
\{1\}^1\times \{0,1\}^{n-k}$ with $1\leq k\leq n$, and similarly for any other cube minus a single point.

We first deal with two base cases. If $G$ has no edges, then the set of assignments $\sigma\colon  U\cup U'\to\{0,1\}$ satisfying (\[todayone\]) and (\[todaytwo\]) is the disjoint union of $$\{0\}^U\times \{0\}^{U'}, \quad 
 (\{0,1\}^U\setminus\{0\}^U)\times \{0\}^{U'}, \quad \text{and} \quad
  \{0\}^U\times(\{0,1\}^{U'}\setminus\{0\}^{U'}).$$ The second and third terms can be decomposed into subcubes as described above to produce the output. Similarly, if $G$ is is a complete bipartite graph, then the set of assignments satisfying (\[todayone\]) and (\[todaytwo\]) is the disjoint union of $$\{1\}^U\times \{1\}^{U'}, \quad
  (\{0,1\}^U\setminus\{1\}^U)\times \{1\}^{U'}, \quad \text{and} \quad
  \{1\}^U\times(\{0,1\}^{U'}\setminus\{1\}^{U'}).$$

If neither of these cases occurs then there is a vertex $x$ such that neither $\Gamma(x)$ nor ${\overline{\Gamma}}(x)$ is empty. If possible, choose $x\in U$; otherwise, choose $x\in U'\!$. To simplify the description of the algorithm, we assume that $x\in U$; the other case is symmetric. We consider separately the assignments where $\sigma(x)=0$ and those where $\sigma(x)=1$. Note that, for any assignment, if $\sigma(y)=0$ for some vertex $y$, then $\sigma(z)=1$ for all $z\in\Gamma(y)$ and, if $\sigma(y)=1$, then $\sigma(z)=0$ for all $z\in{\overline{\Gamma}}(y)$. Applying this iteratively, setting $\sigma(x)=c$ for $c\in\{0,1\}$ also determines the value of $\sigma$ on some set $S_{x=c}\subseteq
U\cup U'$ of vertices.

Thus, we can compute a subcube decomposition for $G$ recursively. First, compute $S_{x=0}$ and $S_{x=1}$. Then, recursively compute subcube decompositions of $G-S_{x=0}$ (the graph formed from $G$ by deleting the vertices in $S_{x=0}$) and $G-S_{x=1}$. Translate these subcube decompositions into a subcube decomposition of $G$ by extending each subcube $(U_i\times U'_i)$ of $G-S_{x=c}$ to a subcube $(V_i\times V'_i)$ of $G$ whose restriction to $G-S_{x=c}$ is $(U_i\times U'_i)$ and whose restriction to $S_{x=c}$ is an assignment $\sigma$ with $\sigma(x)=c$ (in fact, all assignments that set $x$ to $c$ agree on the set $S_{x=c}$, by construction).

It remains to show that the algorithm runs in polynomial time. The base cases are clearly computable in polynomial time, as are the individual steps in the recursive cases, so we only need to show that the number of recursive calls is polynomially bounded. At the recursive step, we only choose $x\in U'$ when $E(G) = U''\times U'$ for some proper subset $\emptyset\subset U''\subset U$ and, in this case, the two recursive calls are to base cases. Since each recursive call when $x\in U$ splits $U'$ into disjoint subsets, there can be at most $|U'|-1$ such recursive calls, so the total number of recursive calls is linear in $|V(G)|$.

Reduction to a problem with $M$-purifying lists
-----------------------------------------------

Our algorithm for counting list $M$-partitions uses the data structures from Section \[sec:DS\] to reduce problems where [$\mathcal L$]{} is not $M$-purifying to problems where it is (which we already know how to solve from Sections \[sec:purifiedcsp\] and \[sec:arc\]). The algorithm is defined recursively on the set ${\ensuremath{\mathcal L}}$ of allowed lists. The algorithm for parameters ${\ensuremath{\mathcal L}}{}$ and $M$ calls the algorithm for ${\ensuremath{\mathcal L}}_i$ and $M$ where ${\ensuremath{\mathcal L}}_i$ is a subset of ${\ensuremath{\mathcal L}}$. The base case arises when ${\ensuremath{\mathcal L}}_i$ is $M$-purifying.

We will use the following computational problem to reduce [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} to a collection of problems [[\#${\ensuremath{\mathcal L}}'$-$M$-partitions]{}]{} that are, in a sense, disjoint.

[[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}.

A graph $G$ and a function $L\colon V(G)\to{\ensuremath{\mathcal L}}$.

Functions $L_1,\dots,L_t\colon V(G)\to{\ensuremath{\mathcal L}}$ such that

-   for each $i\in[t]$, the set $\{L_i(v) \mid  v\in V(G)\}$ is $M$-purifying,

-   for each $i\in [t]$ and $v \in V(G)$, $L_i(v) \subseteq L(v)$, and

-   each $M$-partition of $G$ that respects $L$ respects exactly one of $L_1,\dots,L_t$.

We will give an algorithm for solving the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} in polynomial time when there is no [$\mathcal L$]{}-$M$-derectangularising sequence of length exactly 2. The following computational problem will be central to the inductive step.

[[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{}.

A graph $G$ and a function $L\colon V(G)\to {\ensuremath{\mathcal L}}$.

Functions $L_1,\dots, L_k\colon
  V(G)\to {\ensuremath{\mathcal L}}$ such that

-   for each $i\in [k]$ and $v \in V(G)$, $L_i(v) \subseteq L(v)$,

-   every $M$-partition of $G$ that respects $L$ respects exactly one of $L_1,\dots,L_k$, and

-   for each $i\in[k]$, there is a $W\in{\ensuremath{\mathcal L}}{}$ which is inclusion-maximal in [$\mathcal L$]{} but does not occur in the image of $L_i$.

Note that we can trivially produce a solution to the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{} by letting $L_1, \dots, L_k$ be an enumeration of all possible functions such that all lists $L_i(v)$ have size $1$ and satisfy $L_i(v) \subseteq L(v)$. Such a function $L_i$ corresponds to an assignment of vertices to parts so there is either exactly one $L_i$-respecting $M$-partition or none, which means that every $L$-respecting $M$-partition is $L_i$-respecting for exactly one $i$. However, this solution is exponentially large in $|V(G)|$ and we are interested in solutions that can be produced in polynomial time. Also, if $L(v)=\emptyset$ for some vertex $v$, the algorithm is entitled to output an empty list, since no $M$-partition respects $L$.

The following definition extends rectangularity to $\{0,1,*\}$-matrices and is used in our proof.

A matrix $M\in\{0,1,*\}^{X\times Y}$ is [*$*$-rectangular*]{} if the relation $H^M_{X,Y}$ is rectangular.

Thus, $M$ is $*$-rectangular if and only if $M_{x,y}=M_{x'\!,y}=M_{x,y'}=*$ implies that $M_{x'\!,y'}=*$ for all $x,x'\in X'$ and all $y,y'\in Y''\!$.

We will show in Lemma \[lem:claim\] that the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{} from Algorithm \[alg:purifystep\] is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{} whenever [$\mathcal L$]{} is not $M$-purifying and there is no length-2 [$\mathcal L$]{}-$M$-derectangularising sequence. Note that a length-2 [$\mathcal L$]{}-$M$-derectangularising sequence is a pair $X,Y\in{\ensuremath{\mathcal L}}$ such that $M|_{X\times Y}$, $M|_{X\times X}$ and $M|_{Y\times Y}$ are pure and $M|_{X\times Y}$ is not $*$-rectangular. If ${\ensuremath{\mathcal L}}\neq
{{\mathcal{P}(D)}}$, it is possible that a matrix that is not $*$-rectangular has no length-2 [$\mathcal L$]{}-$M$-derectangularising sequence. For example, let $D=\{1,2,3\}$ and ${\ensuremath{\mathcal L}}=
{{\mathcal{P}(\{1,2\})}}$ and let $M_{3,3}=0$ and $M_{i,j}=*$ for every other pair $(i,j)\in D^2\!$. $M$ is not $*$-rectangular but this fact is not witnessed by any submatrix $M|_{X\times Y}$ for $X,Y\in{\ensuremath{\mathcal L}}$.

/\* $v_j\in C_i$\*/

$L_i(v_j) \gets X_0$ $L_i(v_j) \gets X_1$

$L_i(v_j) \gets Y_0$ $L_i(v_j) \gets Y_1$

\[lem:claim\] Let $M$ be a symmetric matrix in $\{0,1,*\}^{D\times D}$ and let ${\ensuremath{\mathcal L}}{}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. If [$\mathcal L$]{} is not $M$-purifying and there is no length-2 [$\mathcal L$]{}-$M$-derectangularising sequence, then Algorithm \[alg:purifystep\] is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{}.

We consider an instance $(G,L)$ of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{} with $V(G)=\{v_1,\ldots,v_n\}$. If there is a $v_i\in V(G)$ with $L(v_i)=\emptyset$ then no $M$-partition of $G$ respects $L$, so the output is correct. Otherwise, we consider the three cases that can occur in the execution of the algorithm.

#### Case 1.

In this case column $d$ of $M|_{X\times Y}$ contains both a zero and a one. Equivalently, row $d$ of $M|_{Y\times X}$ does. Algorithm \[alg:Case1\] groups the set of $M$-partitions of $G$ that respect $L$, based on the first vertex that is placed in part $d$. For $i\in[n]$, $L_i$ requires that $v_i$ is placed in part $d$ and $v_1, \dots, v_{i-1}$ are not in part $d$; $L_{n+1}$ requires that part $d$ is empty. Thus, no $M$-partition can respect more than one of the $L_i$. Now consider an $L$-respecting $M$-partition $\sigma\colon
V(G)\to D$ and suppose that $i$ is minimal such that $\sigma(v_i)=d$. We claim that $\sigma$ respects $L_i$. We have $\sigma(v_i)=d$, as required. For $j\neq i$, we must have $\sigma(v_j)\in L(v_j)$ since $\sigma$ respects $L$ and we must have $M_{d,\sigma(v_j)}\neq 1$ if $(v_i, v_j)\notin E(G)$ and $M_{d,\sigma(v_j)}\neq 0$ if $(v_i,v_j)\in
E(G)$, since $\sigma$ is an $M$-partition. In addition, by construction, $\sigma(v_j)\neq d$ if $j<i$. Therefore, $\sigma$ respects $L_i$. A similar argument shows that $\sigma$ respects $L_{n+1}$ if $\sigma(v)\neq d$ for all $v\in V(G)$. Hence, any $M$-partition that respects $L$ respects exactly one of the $L_i$.

Finally, we show that, for each $i\in[n+1]$, there is a set $W$ which is inclusion-maximal in [$\mathcal L$]{} and is not in the image of $L_i$. For $i\in [n]$, we cannot have both $a$ and $b$ in $L_i(v_j)$ for any $v_j$, so $X$ is not in the image of $L_i$. $Y$ contains $d$, so $Y$ is not in the image of $L_{n+1}$.

#### Case 2.

In this case, every row of $M|_{X_0\times X}$ contains a 0, while every row of $M|_{X_1\times X}$ fails to contain a zero. Since $M|_{X\times X}$ is not pure, but no row of $M|_{X\times X}$ contains both a zero and a one (since we are not in Case 1), $X_0$ and $X_1$ are non-empty. Note that $M|_{X_0\times X_0}$ and $M|_{X_1\times X_1}$ are both pure, while every entry of $M|_{X_0\times X_1}$ is a $*$.

If $V_X=\emptyset$ then $X$ is an inclusion-maximal member of [$\mathcal L$]{} that is not in the image of $L$, so the output of Algorithm \[alg:Case2\] is correct. Otherwise, $(B_1,C_1),\dots,(B_k,C_k)$ is the list containing all partitions $(B,C)$ of $V_X$ such that $B$ induces a bipartite graph in $G$ and $C$ induces the complement of a bipartite graph. The algorithm returns $L_1,\ldots, L_k$. $X$ is not in the image of any $L_i$ so, to show that $\{L_1, \dots, L_k\}$ is a correct output for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{}, we just need to show that every $M$-partition of $G$ that respects $L$ respects exactly one of $L_1,\dots,L_k$. For $i\neq i'$, $(B_i,C_i)\neq (B_{i'},C_{i'})$ so there is at least one vertex $v_j$ such that $L_i(v_j)=X_0$ and $L_{i'}(v_j)=X_1$ or vice-versa. Since $X_0$ and $X_1$ are disjoint, no $M$-partition can simultaneously respect $L_i$ and $L_{i'}$. It remains to show that every $M$-partition respects at least one of $L_1, \dots, L_k$. To do this, we deduce two structural properties of $M|_{X\times X}$.

First, we show that $M|_{X\times X}$ has no $*$ on its diagonal. Suppose towards a contradiction that $M_{d,d}=*$ for some $d\in X$. If $d\in X_0$, then, for each $d'\in X_1$, $M_{d,d'}=M_{d'\!,d}=*$ because, as noted above, every entry of $M|_{X_0\times X_1}$ is a $*$. Therefore, the $2\times 2$ matrix $M'=M|_{\{d,d'\}\times \{d,d'\}}$ contains at least three $*$s so it is pure. $\{d,d'\}
\subseteq
X\in
{\ensuremath{\mathcal L}}$ so, by the hypothesis of the lemma, the length-2 sequence $\{d,d'\},\{d,d'\}$ is not [$\mathcal L$]{}-$M$-derectangularising, so $M'$ must be $*$-rectangular, so $M_{d'\!,d'}=*$ for all $d'\in X_1$. Similarly, if $M_{d'\!,d'}=*$ for some $d'\in X_1$, then $M_{d,d}=*$ for all $d\in X_0$. Therefore, if $M|_{X\times X}$ has a $*$ on its diagonal, every entry on the diagonal is $*$. But $M$ contains a 0, say $M_{i,j}=0$ with $i,j\in X_0$. For any $k\in X_1$, $$M|_{\{i,j\}\times \{j,k\}} = \begin{pmatrix} 0 & * \\ * & * \end{pmatrix},$$ so the length-2 sequence $\{i,j\}, \{j,k\}$ is [$\mathcal L$]{}-$M$-derectangularising, contradicting the hypothesis of the lemma (note that $\{i,j\},\{j,k\}\subseteq X\in{\ensuremath{\mathcal L}}$).

Second, we show that there is no sequence $d_1,\dots,d_\ell\in X_0$ of odd length such that $$M_{d_1,d_2}=M_{d_2,d_3}=\dots=M_{d_{\ell-1},d_\ell}=M_{d_\ell,d_1}=*\,.$$ Suppose for a contradiction that such a sequence exists. Note that $M|_{X_0\times X_0}$ is $*$-rectangular since $X_0,X_0$ is not an [$\mathcal L$]{}-$M$-derectangularising sequence and $M|_{X_0\times X_0}$ is pure since Case 1 does not apply. We will show by induction that for every non-negative integer $\kappa \leq (\ell-3)/2$, $M_{d_1,d_{\ell-2\kappa-2}}=*$. This gives a contradiction by taking $\kappa=(\ell-3)/2$ since $M_{d_1,d_1}=*$ and we have already shown that $M|_{X_0\times X_0}$ has no $*$ on its diagonal. For every $\kappa$, the argument follows by considering the matrix $M_\kappa = M|_{\{d_1,d_{\ell-2\kappa-1}\} \times \{d_{\ell-2\kappa-2},d_{\ell-2\kappa}\}}$. The definition of the sequence $d_1,\ldots,d_\ell$ together with the symmetry of $M$ guarantees that both entries in row $d_{\ell-2\kappa-1}$ of $M_\kappa$ are equal to $*$. It is also true that $M_{d_1,d_{\ell-2\kappa}}=*$: If $\kappa=0$ then this follows from the definition of the sequence; otherwise it follows by induction. The fact that $M_{d_1,d_{\ell-2\kappa-2}}=*$ then follows by $*$-rectangularity.

This second structural property implies that, for any $M|_{X\times X}$-partition of $G[V_X]$, the graph induced by vertices assigned to $X_0$ has no odd cycles, and is therefore bipartite. Similarly, the vertices assigned to $X_1$ induce the complement of a bipartite graph. Therefore, any $M$-partition of $G$ that respects $L$ must respect at least one of the $L_1, \dots, L_k$, so it respects exactly one of them, as required.

#### Case 3.

Since Cases 1 and 2 do not apply and [$\mathcal L$]{} is not $M$-purifying, there are distinct $X,Y\in
{\ensuremath{\mathcal L}}$ such that $X$ and $Y$ are inclusion-maximal in [$\mathcal L$]{} and $M|_{X\times Y}$ is not pure. As in the previous case, the sets $X_0$, $X_1$, $Y_0$ and $Y_1$ are all non-empty.

If either $V_X$ or $V_Y$ is empty then either $X$ or $Y$ is an inclusion-maximal set in [$\mathcal L$]{} that is not in the image of $L$ so the output of Algorithm \[alg:Case3\] is correct. Otherwise, $(U_1,U'_1),\dots,(U_k,U'_k)$ is a subcube decomposition of the bipartite subgraph $(V_X,V_Y,E)$. The $U_i$s are subcubes of $\{0,1\}^{V_X}$ and the $U'_i$s are subcubes of $\{0,1\}^{V_Y}$. The algorithm returns $L_1,\ldots,L_k$.

Note that if $|U'_i|=1$ then $Y$ is not in the image of $L_i$. Similarly, if $|U'_i|>1$ but $|U_i|=1$ then $X$ is not in the image of $L_i$. The definition of subcube decompositions guarantees that, for every $i$, at least one of these is the case. To show this definition of $L_1,\ldots,L_k$ is a correct output for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{}, we must show that any $M$-partition of $G$ that respects $L$ also respects exactly one $L_i$. Since the sets in $\{U_i \times U'_i \mid  i\in[k]\}$ are disjoint subsets of $\{0,1\}^{V_X\cup V_Y}$, any $M$-partition of $G$ that respects $L$ respects at most one $L_i$ so it remains to show that every $M$-partition of $G$ respects at least one $L_i$. To do this, we deduce two structural properties of $M|_{X\times Y}$.

First, we show that every entry of $M|_{X_0\times Y_0}$ is $0$. The definition of $X_0$ guarantees that every row of $M|_{X_0\times Y_0}$ contains a $0$. Since Case 1 does not apply, and $M$ is symmetric, every entry of $M|_{X_0\times Y_0}$ is either $0$ or $*$. Suppose for a contradiction that $M_{i,j}=*$ for some $(i,j)\in
X_0\times Y_0$. Pick $i'\in X_1$. For any $j'\in Y_0\setminus\{j\}$ we have $M_{i,j}=M_{i'\!,j}=M_{i'\!,j'}=*$, so by $*$-rectangularity of $M|_{X\times Y_0}$ we have $M_{i,j'}=*$. Thus, every entry of $M|_{\{i\}\times Y_0}$ is $*$, so there is a $*$ in every $Y_0$-indexed column of $M$. By the same argument, swapping the roles of $X$ and $Y$, every entry in $M|_{X_0\times Y_0}$ is $*$, contradicting the fact that $M|_{X\times Y}$ contains a $0$ since $M|_{X\times Y}$ is not pure.

Second, a similar argument shows that every entry of $M|_{X_1\times Y_1}$ is $1$.

Thus for all $M$-partitions $\sigma$ of $G$ respecting $L$, for all $x\in V_X$ and $y\in V_Y$, if $(x,y)\in E$ then $(\sigma(x),\sigma(y))\notin X_0\times Y_0$ while if $(x,y)\notin E$ then $(\sigma(x),\sigma(y))\notin X_1\times Y_1$. Using the definition of subcube decompositions, this shows that any $M$-partition of $G$ respecting $L$ respects some $L_i$.

We can now give an algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}. The algorithm consists of the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}, which is defined in Algorithm \[alg:purifytriv\] for the trivial case in which ${\ensuremath{\mathcal L}}$ is $M$-purifying and in Algorithm \[alg:purify\] for the case in which it is not. Note that for any fixed ${\ensuremath{\mathcal L}}$ and $M$ the algorithm is defined either in Algorithm \[alg:purifytriv\] or in Algorithm \[alg:purify\] and the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} is not recursive. However, the *definition* is recursive, so the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} defined in Algorithm \[alg:purify\] does make a call to a function [[\#${\ensuremath{\mathcal L}}_i$-$M$-purify]{}]{} for some ${\ensuremath{\mathcal L}}_i$ which is smaller than ${\ensuremath{\mathcal L}}$. The function [[\#${\ensuremath{\mathcal L}}_i$-$M$-purify]{}]{} is in turn defined in Algorithm \[alg:purifytriv\] or Algorithm \[alg:purify\].

The correctness of the algorithm follows from the definition of the problem. The following lemma bounds the running time.

\[lem:reducetopure\] Let $M\in\{0,1,*\}^{D\times D}$ be a symmetric matrix and let ${\ensuremath{\mathcal L}}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. If there is no length-$2$ [$\mathcal L$]{}-$M$-derectangularising sequence, then the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} as defined in Algorithms \[alg:purifytriv\] and \[alg:purify\] is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}.

Note that ${\ensuremath{\mathcal L}}$ is a fixed parameter of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} — it is not part of the input. The proof is by induction on $|{\ensuremath{\mathcal L}}|$. If $|{\ensuremath{\mathcal L}}|=1$ then ${\ensuremath{\mathcal L}}=\{\emptyset\}$ so it is $M$-purifying. In this case, function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} is defined in Algorithm \[alg:purifytriv\]. It is clear that it is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}.

For the inductive step suppose that $|{\ensuremath{\mathcal L}}|>1$. If ${\ensuremath{\mathcal L}}$ is $M$-purifying then function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} is defined in Algorithm \[alg:purifytriv\] and again the result is trivial. Otherwise, function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} is defined in Algorithm \[alg:purify\]. Note that ${\ensuremath{\mathcal L}}\subseteq{{\mathcal{P}(D)}}$ is subset-closed and there is no length-$2$ [$\mathcal L$]{}-$M$-derectangularising sequence. From this, we can conclude that, for any subset-closed subset ${\ensuremath{\mathcal L}}'$ of ${\ensuremath{\mathcal L}}$, there is no length-$2$ ${\ensuremath{\mathcal L}}'$-$M$-derectangularising sequence. So we can assume by the inductive hypothesis that for all subset-closed ${\ensuremath{\mathcal L}}'\subset {\ensuremath{\mathcal L}}{}$, the function [[\#${\ensuremath{\mathcal L}}'$-$M$-purify]{}]{} runs in polynomial time.

The result now follows from the fact that the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{} runs in polynomial time (as guaranteed by Lemma \[lem:claim\]) and from the fact that each ${\ensuremath{\mathcal L}}_i$ is a strict subset of ${\ensuremath{\mathcal L}}$, which follows from the definition of problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify-step]{}]{}. Each $M$-partition that respects $L$ respects exactly one of $L_1,\dots,L_k$ and, hence, it respects exactly one of the list functions that is returned.

Algorithm for [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} and proof of the dichotomy
---------------------------------------------------------------------------------------------

We can now present our algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}. The algorithm consists of the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} which is defined in Algorithm \[alg:mainpurifying\] for the case in which ${\ensuremath{\mathcal L}}$ is $M$-purifying and in Algorithm \[alg:main\] when it is not.

AC$(V,C)$ where AC is the function from Algorithm \[alg:AC\]

\[lem:positive\] Let $M\in\{0,1,*\}^{D\times D}$ be a symmetric matrix and let ${\ensuremath{\mathcal L}}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. If there is no [$\mathcal L$]{}-$M$-derectangularising sequence, then the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} as defined in Algorithms \[alg:mainpurifying\] and \[alg:main\] is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}.

If ${\ensuremath{\mathcal L}}$ is $M$-purifying then the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is defined in Algorithm \[alg:mainpurifying\]. Proposition \[prop:purifiediscsp\] shows that the reduction in Algorithm \[alg:mainpurifying\] to a CSP instance is correct and takes polynomial time. The CSP instance can be solved by the function AC in Algorithm \[alg:AC\], whose running time is shown to be polynomial in Lemma \[lem:quickarc\].

If ${\ensuremath{\mathcal L}}$ is not $M$-purifying then the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is defined in Algorithm \[alg:main\]. Lemma \[lem:reducetopure\] guarantees that the function [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} is a polynomial-time algorithm for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{}. If the list $L_1,\ldots,L_t$ is empty then there is no $M$-partition of $G$ that respects $L$ so it is correct that the function [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} returns $0$. Otherwise, we know from the definition of the problem [[\#${\ensuremath{\mathcal L}}$-$M$-purify]{}]{} that

-   functions $L_1,\ldots,L_t$ are from $V(G)$ to ${\ensuremath{\mathcal L}}$,

-   for each $i\in [t]$, the set $\{L_i(v) \mid  v\in V(G)\}$ is $M$-purifying,

-   for each $i\in [t]$ and $v \in V(G)$, $L_i(v) \subseteq L(v)$, and

-   each $M$-partition of $G$ that respects $L$ respects exactly one of $L_1,\dots,L_t$.

The desired result is now the sum, over all $i\in[t]$, of the number of $M$-partitions of $G$ that respect $L_i$. Since the list $L_1, \dots, L_t$ is generated in polynomial time, $t$ is bounded by some polynomial in $|V(G)|$.

Now, for each $i\in[t]$, ${\ensuremath{\mathcal L}}_i$ is a subset-closed subset of ${\ensuremath{\mathcal L}}$. Since there is no ${\ensuremath{\mathcal L}}$-$M$-derectangularising sequence, there is also no ${\ensuremath{\mathcal L}}_i$-$M$-derectangularising sequence. Also, ${\ensuremath{\mathcal L}}_i$ is $M$-purifying. Thus, the argument that we gave for the purifying case shows that $Z_i$ is the desired quantity.

We can now combine our results to establish our dichotomy for the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{}.

[thm:explicitdichotomy]{}[ Let $M$ be a symmetric matrix in $\{0,1,*\}^{D\times D}$ and let ${\ensuremath{\mathcal L}}{}\subseteq{{\mathcal{P}(D)}}$ be subset-closed. If there is an [$\mathcal L$]{}-$M$-derectangularising sequence then the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is ${\ensuremath{\mathrm{\#P}}}$-complete. Otherwise, it is in ${\ensuremath{\mathrm{FP}}}$. ]{}

Suppose that there is an [$\mathcal L$]{}-$M$-derectangularising sequence $D_1,\dots,D_k$. Recall (from Definition \[def:closure\]) the definition of the subset-closure ${{\mathscr{S}({\ensuremath{\mathcal L}}'')}}$ of a set ${\ensuremath{\mathcal L}}'' \subseteq {{\mathcal{P}(D)}}$. Let $${\ensuremath{\mathcal L}}'= {{\mathscr{S}(\{D_1,\ldots,D_k\} )}}.$$ Since $\{D_1,\ldots,D_k\}$ is $M$-purifying, so is ${\ensuremath{\mathcal L}}'\!$, which is also subset-closed. It follows that $\Gamma_{\!{\ensuremath{\mathcal L}}'\!,M}$ is well defined (see Definition \[defgammaprime\]) and contains the relations $H_{D_1,D_2}^M, \ldots,H_{D_{k-1},D_k}^M$ (and possibly others). Since $H_{D_1,D_2}^M \circ H_{D_2,D_3}^M \circ \cdots \circ H_{D_{k-1},D_k}^M$ is not rectangular, ${\ensuremath{\mathrm{\#CSP}}}(\Gamma_{\!{\ensuremath{\mathcal L}}'\!,M})$ is ${\ensuremath{\mathrm{\#P}}}$-complete [@BD Theorem 2 and Corollary 3] (see also [@DRfull Lemma 24]). By Proposition \[prop:purifiediscsp\], the problem [[\#${\ensuremath{\mathcal L}}'$-$M$-partitions]{}]{} is ${\ensuremath{\mathrm{\#P}}}$-complete so the more general problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is also ${\ensuremath{\mathrm{\#P}}}$-complete. On the other hand, if there is no ${\ensuremath{\mathcal L}}$-$M$-derectangularising sequence, then the result follows from Lemma \[lem:positive\].

Complexity of the dichotomy criterion {#sec:meta}
=====================================

The dichotomy established in Theorem \[thm:explicitdichotomy\] is that, if there is an [$\mathcal L$]{}-$M$-derectangularising sequence, then the problem [[\#${\ensuremath{\mathcal L}}$-$M$-partitions]{}]{} is ${\ensuremath{\mathrm{\#P}}}$-complete; otherwise, it is in ${\ensuremath{\mathrm{FP}}}$. This section addresses the computational problem of determining which is the case, given [$\mathcal L$]{} and $M$.

The following lemma will allow us to show that the problem [[ExistsDerectSeq]{}]{}  (the problem of determining whether there is an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence, given [$\mathcal L$]{} and $M$) and the related problem [[MatrixHasDerectSeq]{}]{}  (the problem of determining whether there is a ${{\mathcal{P}(D)}}$-$M$-derectangularising sequence, given $M$) are both in [$\mathrm{NP}$]{}. Note that, for this “meta-problem”, [$\mathcal L$]{} and $M$ are the inputs whereas, previously, we have regarded them as fixed parameters.

\[lem:small\_derect\] Let $M\in\{0,1,*\}^{D\times D}$ be symmetric, and let ${\ensuremath{\mathcal L}}\subseteq  {{\mathcal{P}(D)}}$ be subset-closed. If there is an [$\mathcal L$]{}-$M$-derectangularising sequence, then there is one of length at most $512(|D|^3+1)$.

Pick an [$\mathcal L$]{}-$M$-derectangularising sequence $D_1,\dots,D_k$ with $k$ minimal; we will show that $k\leq 512(|D|^3+1)$. Define $$R=H^M_{D_1, D_2} \circ H^M_{D_2, D_3} \circ \dots \circ
H^M_{D_{k-1}, D_k}.$$ Note that $R\subseteq D_1\times D_k$. By the definition of derectangularising sequence, there are $a,a'\in D_1$ and $b,b'\in D_k$ such that $(a,b)$, $(a'\!,b)$ and $(a,b')$ are all in $R$ but $(a'\!,b')\not\in R$. So there exist $$(x_1,\dots,x_k),(y_1,\dots,y_k),(z_1,\dots,z_k)\in D_1\times \dots
    \times D_k$$ with $(x_1,x_k)=(a,b)$, $(y_1,y_k)=(a'\!,b)$ and $(z_1,z_k)=(a,b')$ such that $M_{x_i,x_{i+1}} = M_{y_i,y_{i+1}} =
M_{z_i,z_{i+1}}=*$ for every $i\in[k-1]$ but, for any $(w_1,\ldots,w_k)\in D_1 \times \dots \times D_k$ with $(w_1,w_k)=(a'\!,b')$, there is an $i\in[k-1]$ such that $M_{w_i,w_{i+1}}\neq *$.

Setting $D'_i=\{x_i,y_i,z_i\}$ for each $i$ gives an [$\mathcal L$]{}-$M$-derectangularising sequence $D'_1,\dots,D'_k$ with $|D'_i|\leq 3$ for each $1\leq i\leq k$. (Note that any submatrix of a pure matrix is pure.) For all $1\leq s <
t\leq k$ define $$R_{s,t}=H^M_{D'_s, D'_{s+1}} \circ H^M_{D'_{s+1}, D'_{s+2}} \circ \dots \circ
  H^M_{D'_{t-1}, D'_t}.$$ Since $D'_1,\ldots,D'_k$ is [$\mathcal L$]{}-$M$-derectangularising, $R_{1,k}$ is not rectangular but, by the minimality of $k$, every other $R_{s,t}$ is rectangular. Note also that no $R_{s,t}=\emptyset$ since, if that were the case, we would have $R_{1,k}=\emptyset$, which is rectangular.

Suppose for a contradiction that $k> 512(|D|^3+1)$. There are at most $|D|^3+1$ subsets of $D$ with size at most three, so there are indices $1\leq i_0<i_1<i_2<\dots<i_{512}\leq k$ such that $D'_{i_0}=\dots=D'_{i_{512}}$. There are at most $2^{|D'_{i_0}|^2}-1 \leq 2^9-1=511$ non-empty binary relations on $D'_{i_0}$, so $R_{i_0,i_m}=R_{i_0,i_n}$ for some $1\leq 
m<n\leq 512$. Since $R_{1,k}$ is not rectangular, $$R_{1,k}= R_{1,i_0} \circ R_{i_0,i_n} \circ R_{i_n,k}= R_{1,i_0} \circ R_{i_0,i_m} \circ R_{i_n,k}= R_{1,i_m} \circ R_{i_n,k}$$ is not rectangular. Therefore, $D'_1,D'_2,\dots,D'_{i_m},D'_{1+i_n},D'_{2+i_n},\dots,D'_k$ is an $\mathcal
L$-$M$-derectangularising sequence of length less than $k$, contradicting the minimality of $k$.

Now that we have membership in [$\mathrm{NP}$]{}, we can prove completeness.

[thm:meta]{}[ [[ExistsDerectSeq]{}]{} is [$\mathrm{NP}$]{}-complete under polynomial-time many-one reductions.]{}

We first show that [[ExistsDerectSeq]{}]{} is in [$\mathrm{NP}$]{}. Given $D$, $M\in \{0,1,*\}^{D\times D}$ and ${\ensuremath{\mathcal L}}\subseteq {{\mathcal{P}(D)}}$, a non-deterministic polynomial time algorithm for [[ExistsDerectSeq]{}]{}first “guesses” an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence $D_1,\ldots,D_k$ with $k\leq 512{(|D|^3+1)}$. Lemma \[lem:small\_derect\] guarantees that such a sequence exists if the output should be “yes”. The algorithm then verifies that each $D_i$ is a subset of a set in ${\ensuremath{\mathcal L}}$, that $\{D_1,\ldots,D_k\}$ is $M$-purifying, and that the relation $H^M_{D_1,D_2} \circ H^M_{D_2, D_3} \circ \dots \circ
H^M_{D_{k-1}, D_k}$ is not rectangular. All of these can be checked in polynomial time without explicitly constructing ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$.

To show that [[ExistsDerectSeq]{}]{} is [$\mathrm{NP}$]{}-hard, we give a polynomial-time reduction from the well-known [$\mathrm{NP}$]{}-hard problem of determining whether a graph $G$ has an independent set of size $k$.

Let $G$ and $k$ be an input to the independent set problem. Let $V(G)= [n]$ and assume without loss of generality that $k\in[n]$. Setting $D=[n]\times[k]\times[3]$, we construct a $D\times
D$ matrix $M$ and a set [$\mathcal L$]{} of lists such that there is an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence if and only if $G$ has an independent set of size $k$.

$M$ will be a block matrix, constructed using the following $3\times
3$ symmetric matrices. Note that each is pure, apart from ${\mathrm{Id}}$. $$\begin{gathered}
    {M_\mathrm{start}}= \begin{pmatrix}*&*&0 \\ *&*&0 \\ 0&0&*\end{pmatrix} \qquad
    {M_\mathrm{end}}= \begin{pmatrix}*&0&0 \\ 0&*&* \\ 0&*&*\end{pmatrix} \qquad
    {M_\mathrm{bij}}= \begin{pmatrix}*&0&0 \\ 0&*&0 \\ 0&0&*\end{pmatrix} \\
    {\mathbf{0}}= \begin{pmatrix}0&0&0 \\ 0&0&0 \\ 0&0&0\end{pmatrix} \qquad
    {\mathrm{Id}}= \begin{pmatrix}1&0&0 \\ 0&1&0 \\ 0&0&1\end{pmatrix}\,.\end{gathered}$$

For $v\in [n]$ and $j\in[k]$, let $D[v,j] = \{(v,j,c) \mid c\in[3]\}$. Below, when we say that $M|_{D[v,j]\times D[v',j']}= N$ for some $3\times 3$ matrix $N$, we mean more specifically that $M_{(v,j,c),(v'\!,j'\!,c')} = N_{c,c'}$ for all $c,c'\in[3]$. $M$ is constructed as follows.

-   For all $v\in[n]$, $M|_{D[v,1] \times D[v,1]}= {M_\mathrm{start}}$ and $M|_{D[v,k]\times D[v,k]} =
        {M_\mathrm{end}}$.

-   For all $v\in[n]$ and all $j\in\{2,\dots,k-1\}$, $M|_{D[{v,j}]\times D[{v,j}]} =
        {M_\mathrm{bij}}$.

-   If $v\neq v'\!$, $(v,v')\notin E(G)$ and $j<k$, then

    -   $M|_{D[{v,j}]\times D[{v',j+1}]}   = M|_{D[v',j+1]\times D[{v,j}]}  = {M_\mathrm{bij}}$ and

    -   $M|_{D[{v,j}] \times D[{v',j'}]}  = M|_{D[{v',j'}]\times D[{v,j}]}   = {\mathbf{0}}$ for all $j'>j+1$.

-   For all $v,v'\in[n]$ and $j,j'\in[k]$ not covered above, $M|_{D[v,j]\times D[v',j']}  = {\mathrm{Id}}$.

To complete the construction, let ${\ensuremath{\mathcal L}}=\{D[v,j]\mid v\in[n], j\in[k]\}$. We will show that $G$ has an independent set of size $k$ if and only if there is an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence.

For the forward direction of the proof, suppose that $G$ has an independent set $I = \{v_1, \dots, v_k\}$ of size $k$. We will show that $$D[v_1,1], D[v_1,1], D[v_2,2], D[v_3,3], \dots, D[v_{k-1}, k-1],
    D[v_k,k], D[v_k,k]$$ (where the first and last elements are repeated and the others are not) is ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising. Since there is no edge $(v_i,v_{i'})\in E(G)$ for $i,i'\in[k]$, the matrix $M|_{D[v_i,i]\times D[v_{i'},i']}  $ is always one of ${M_\mathrm{start}}$, ${M_\mathrm{end}}$, ${M_\mathrm{bij}}$ and ${\mathbf{0}}$, so it is always pure. Therefore, $\{D[v_1,1], \dots, D[v_k,k]\}$ is $M$-purifying. It remains to show that the relation $$R = H^M_{D[v_1,1],D[v_1,1]} \circ H^M_{D[v_1,1],D[v_2,2]} \circ \dots
            \circ H^M_{D[v_{k-1,k-1}],D[v_{k},k]} \circ H^M_{D[v_k,k],D[v_k,k]}$$ is not rectangular.

Consider $i\in[k-1]$. Since $(v_i,v_{i+1})\notin E(G)$, $M|_{ D[{v_i,i}] \times D[{ v_{i+1}, i+1 }]}  = {M_\mathrm{bij}}$ so $H^M_{D[v_i,i], D[v_{i+1},i+1]}$ is the bijection that associates $(v_i,i,c)$ with $(v_{i+1},i+1,c)$ for each $c\in[3]$. Therefore, $$H^M_{D[v_1,1],D[v_1,2]} \circ \dots \circ
H^M_{D[v_{k-1},k-1],D[v_k,k]}$$ is the bijection that associates $(v_1,1,c)$ with $(v_k,k,c)$ for each $c\in[3]$. We have $M|_{D[v_1,1]\times D[v_1,1]} 
= {M_\mathrm{start}}$ and $M|_{D[v_k,k] \times D[v_k,k]}  = {M_\mathrm{end}}$ so $$\begin{aligned}
    H^M_{D[v_1,1], D[v_1,1]} &= \{((v_1,1,c),(v_1,1,c')) \mid c,c'\in[2]\}
                        \cup \{((v_1,1,3),(v_1,1,3))\} \\
    H^M_{D[v_k,k], D[v_k,k]} &= \{((v_k,k,1),(v_k,k,1))\}
                        \cup \{((v_k,k,c),(v_k,k,c')) \mid c,c'\in\{2,3\}\}\,,\end{aligned}$$ and, therefore, $$\begin{aligned}
    R = \{((v_1,1,c),(v_k,k,c')) \mid c,c'\in[3]\}
         \setminus \{((v_1,1,3),(v_k,k,1))\}\,,\end{aligned}$$ which is not rectangular, as required.

For the reverse direction of the proof, suppose that there is an ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising sequence $D_1,\ldots,D_m$. The fact that the sequence is derectangularising implies that $|D_i|\geq 2$ for each $i\in[m]$ — see the remarks following Definition \[def:derect\]. Each set in the sequence is a subset of some $D[v,j]$ in ${\ensuremath{\mathcal L}}$ so for every $i\in[m]$ let $v_i$ denote the vertex in $[n]$ and let $j_i$ denote the index in $[k]$ such that $D_i \subseteq D[v_i,j_i]$. Clearly, it is possible to have $(v_i,j_i)=(v_{i'},j_{i'})$ for distinct $i$ and $i'$ in $[m]$.

We will finish the proof by showing that $G$ has a size-$k$ independent set. Let $$R = H^M_{D_1 ,  D_2} \circ \dots \circ H^M_{D_{m-1}, D_m  },$$ which is not rectangular because the sequence is ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$-$M$-derectangularising. Since $\{D_1,\ldots,D_m\}$ is $M$-purifying, and any submatrix of ${\mathrm{Id}}$ with at least two rows and at least two columns is impure, every pair $(i,i')\in [m]^2$ satisfies $M|_{D[v_i,j_i]\times D[v_{i'},j_{i'}]} \neq {\mathrm{Id}}$. This means that we cannot have $(v_i,v_{i'})\in E(G)$ for any pair $(i,i')\in [m]^2$ so the set $I=\{v_1, \dots, v_m\}$ is independent in $G$. It remains to show that $|I|\geq k$.

Observe that, if $v_i=v_{i'}$, we must have $j_i=j_{i'}$ since, otherwise, the construction ensures that $$M|_{D[v_i,j_i]\times D[v_{i'},j_{i'}]}
 = M|_{D[v_i,j_i]\times D[v_i,j_{i'}]}
  = {\mathrm{Id}},$$ which we already ruled out. Therefore, $|I| \geq |\{j_1, \dots, j_m\}|$.

We must have $|j_i-j_{i+1}|\leq 1$ for each $i\in[m-1]$ as, otherwise, $M|_{D[v_i,j_i]\times D[v_{i+1},j_{i+1}]}
 = {\mathbf{0}}$, which implies that $R=\emptyset$, which is rectangular. There must be at least one $i\in
[m-1]$ such that $v_i=v_{i+1}$ and $j_i=j_{i+1}=1$, so $M|_{D[v_i,j_i]\times D[v_{i+1},j_{i+1}]}
 = {M_\mathrm{start}}$. If not, $R$ is a composition of relations corresponding to ${M_\mathrm{bij}}$ and ${M_\mathrm{end}}$ and any such relation is either a bijection, or of the form of ${M_\mathrm{end}}$, so it is rectangular. Similarly, there must be at least one $i$ such that $v_i=v_{i+1}$ and $j_i=j_{i+1}=k$, giving $M|_{D[v_i,j_i]\times D[v_{i+1},j_{i+1}]}
 = {M_\mathrm{end}}$. Therefore, the sequence $j_1, \dots, j_m$ contains 1 and $k$. Since $|j_i-j_{i+1}|\leq 1$ for all $i\in[m-1]$, it follows that $[k] \subseteq \{j_1,\dots, j_m\}$, so $|I|\geq k$, as required. In fact, $\{j_1,\dots, j_m\} = [k]$ since each $j_i\in [k]$ by construction.

We defined the problem [[ExistsDerectSeq]{}]{} using a concise input representation: ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$ does not need to be written out in full. Instead, the instance is a subset ${\ensuremath{\mathcal L}}$ containing the maximal elements of ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$. For example, when the instance is ${\ensuremath{\mathcal L}}=\{D\}$, we have ${{\mathscr{S}({\ensuremath{\mathcal L}})}}= {{\mathcal{P}(D)}}$. It is important to note that the [$\mathrm{NP}$]{}-completeness of [[ExistsDerectSeq]{}]{} is not an artifact of this concise input coding. The elements of the list ${\ensuremath{\mathcal L}}$ constructed in the NP-hardness proof have length at most three, so the list ${{\mathscr{S}({\ensuremath{\mathcal L}})}}$ could also be constructed explicitly in polynomial time.

Lemma \[lem:small\_derect\] has the following immediate corollary for the complexity of the dichotomy criterion of the general [[\#List-$M$-partitions]{}]{} problem. Recall that, in this version of the meta-problem, the input is just the matrix $M$.

[cor:meta]{}[ [[MatrixHasDerectSeq]{}]{} is in [$\mathrm{NP}$]{}.]{}

Take ${\ensuremath{\mathcal L}}= \{D\}$ in Lemma \[lem:small\_derect\].

Cardinality constraints {#sec:card}
=======================

Finally, we show how lists can be used to implement cardinality constraints of the kind that often appear in counting problems in combinatorics.

Feder, Hell, Klein and Motwani [@FHKM] point out that lists can be used to determine whether there are $M$-partitions that obey simple cardinality constraints. For example, it is natural to require some or all of the parts to be non-empty or, more generally, to contain at least some constant number of vertices. Given a $D\times D$ matrix $M$, we represent such cardinality constraints as a function $C\colon D\to{\mathbb{Z}_{\geq 0}}$. We say that an $M$-partition $\sigma$ of a graph $G$ *satisfies* the constraint if, for each $d\in D$, $|\{v\in
V(G)\mid \sigma(v)=d\}| \geq C(d)$. Given a cardinality constraint $C$, we write $|C| = \sum_{d\in D} C(d)$.

We can determine whether there is an $M$-partition of $G=(V,E)$ that satisfies the cardinality constraint $C$ by making at most ${|V|}^{|C|}$ queries to an oracle for the list $M$-partitions problem, as follows. Let $L_C$ be the set of list functions $L\colon
V\to {{\mathcal{P}(D)}}$ such that:

-   for all $v\in V\!$, either $L(v) = D$ or $|L(v)| = 1$, and

-   for all $d\in D$, there are exactly $C(d)$ vertices $v$ with $L(v) = \{d\}$.

There are at most ${|V|}^{|C|}$ such list functions and it is clear that $G$ has an $M$-partition satisfying $C$ if, and only if, it has a list $M$-partition that respects at least one $L\in L_C$. The number of queries is polynomial in $|V|$ as long as the cardinality constraint $C$ is independent of $G$.

For counting, the situation is a little more complicated, as we must avoid double-counting. The solution is to count all $M$-partitions of the input graph and subtract off those that fail to satisfy the cardinality constraint. We formally define the problem [[\#$C$-$M$-partitions]{}]{} as follows, parameterized by a $D\times D$ matrix $M$ and a cardinality constraint function $C\colon D\to {\mathbb{Z}_{\geq 0}}$.

[[\#$C$-$M$-partitions]{}]{}.

A graph $G$.

The number of $M$-partitions of $G$ that satisfy $C$.

\[prop:cardinality\] [[\#$C$-$M$-partitions]{}]{} is polynomial-time Turing reducible to [[\#List-$M$-partitions]{}]{}.

Given the cardinality constraint function $C$, let $R = \{d\in
    D\mid C(d)>0\}$: that is, $R$ is the set of parts that have a non-trivial cardinality constraint. For any set $P\subseteq R$, say that an $M$-partition $\sigma$ of a graph $G=(V,E)$ *fails on $P$* if $|\{v\in V\mid \sigma(v) = d\}| < C(d)$ for all $d\in P$. That is, if $\sigma$ violates the cardinality constraints on all parts in $P$ (and possibly others, too). Let $\Sigma$ be the set of all $M$-partitions of our given input graph $G$. For $i\in R$, let $A_i = \{\sigma \in \Sigma \mid \mbox{$\sigma$ fails on $\{i\}$}\}$ and let $A=\bigcup_{i\in R} A_i$. By inclusion-exclusion, $$\begin{aligned}
|A| &= -\!\!\sum_{\emptyset \subset P \subseteq R} {(-1)}^{|P|} \left|\bigcap_{i\in P} A_i\right|\\
&= -\!\!\sum_{\emptyset\subset P \subseteq R} {(-1)}^{|P|} \big|\{\sigma \in \Sigma \mid
\mbox{$\sigma$ fails on $P$}\}\big|\,.\end{aligned}$$

We wish to compute $$\begin{aligned}
        \big|\{\sigma\in \Sigma\mid \text{$\sigma$ satisfies $C$}\}\big|
            &= \big|\Sigma\big| - |A| \\
            &= \big|\Sigma\big|
               + \sum_{\emptyset \subset P\subseteq R}
                     (-1)^{|P|}
                     \big|\{\sigma\in\Sigma \mid \text{$\sigma$
                           fails on $P$}\}\big|\,.
    \end{aligned}$$

Therefore, it suffices to show that we can use lists to count the $M$-partitions that fail on each non-empty $P\subseteq R$. For such a set $P$, let $L_P$ be the set of list functions $L$ such that

-   for all $v\in V$, either $L(v) = D\setminus P$ or $L(v) =
            \{p\}$ for some $p\in P$, and

-   for all $p\in P$, $\big|\big\{v\in V\mid L(v)=\{p\}\big\}\big| < C(p)$.

Thus, the set of $M$-partitions that respect some $L\in L_P$ is precisely the set of $M$-partitions that fail on $P$. Also, for distinct $L$ and $L'$ in $L_P$, the set of $M$-partitions that respect $L$ is disjoint from the set of $M$-partitions that respect $L'\!$. So we can compute $ \big|\{\sigma\in\Sigma \mid \text{$\sigma$
                           fails on $P$}\}\big|$ by making $|L_P|$ calls to [[\#List-$M$-partitions]{}]{}, noting that $|L_P|\leq |V|^{|C|}\!$.

As an example of a combinatorial structure that can be represented as an $M$-partition problem with cardinality constraints, consider the *homogeneous pairs* introduced by Chvátal and Sbihi [@CS1987:Bull-free]. A homogeneous pair in a graph $G=(V,E)$ is a partition of $V$ into sets $U$, $W_1$ and $W_2$ such that:

-   $|U|\geq 2$;

-   $|W_1|\geq 2$ or $|W_2|\geq 2$ (or both);

-   for every vertex $v\in U$, $v$ is either adjacent to every vertex in $W_1$ or to none of them; and

-   for every vertex $v\in U$, $v$ is either adjacent to every vertex in $W_2$ or to none of them.

Feder et al. [@FHKM] observe that the problem of determining whether a graph has a homogeneous pair can be represented as the problem of determining whether it has an [$M_{\mathrm{hp}}$]{}-partition satisfying certain constraints, where $D = \{1, \dots, 6\}$ and $${\ensuremath{M_{\mathrm{hp}}}}= \begin{pmatrix}
               * & * & 1 & 0 & 1 & 0 \\
               * & * & 1 & 1 & 0 & 0 \\
               1 & 1 & * & * & * & * \\
               0 & 1 & * & * & * & * \\
               1 & 0 & * & * & * & * \\
               0 & 0 & * & * & * & *
           \end{pmatrix}.$$

$W_1$ corresponds to the set of vertices mapped to part $1$ (row 1 of ${\ensuremath{M_{\mathrm{hp}}}}$), $W_2$ corresponds to the set of vertices mapped to part $2$ (row 2 of ${\ensuremath{M_{\mathrm{hp}}}}$), and $U$ corresponds to the set of vertices mapped to parts $3$–$6$.

In fact, there is a one-to-one correspondence between the homogeneous pairs of $G$ in which $W_1$ and $W_2$ are non-empty and the ${\ensuremath{M_{\mathrm{hp}}}}$-partitions $\sigma$ of $G$ that satisfy the following additional constraints. For $d\in D$, let $N_\sigma(d) = |\{v\in V(G)\mid \sigma(v)=d\}|$ be the number of vertices that $\sigma$ maps to part $d$. We require that

-   $N_\sigma(3) + N_\sigma(4) + N_\sigma(5) + N_\sigma(6)\geq 2$,

-   $N_\sigma(1) > 0$ and $N_\sigma(2) > 0$, and

-   at least one $N_\sigma(1)$ and $N_\sigma(2)$ is at least $2$.

To see this, consider a homogeneous pair $(U,W_1,W_2)$ in which $W_1$ and $W_2$ are non-empty. Note that there is exactly one ${\ensuremath{M_{\mathrm{hp}}}}$-partition of $G$ in which vertices in $W_1$ are mapped to part $1$ and vertices in $W_2$ are mapped to part $2$ and vertices in $U$ are mapped to parts $3$–$6$. There is exactly one part available to each $v\in U$ since $v$ has an edge or non-edge to $W_1$ (but not both!) ruling out exactly two parts and $v$ has an edge or non-edge to $W_2$ ruling out an additional part. Going the other way, an ${\ensuremath{M_{\mathrm{hp}}}}$-partition that satisfies the constraints includes a homogeneous pair.

Now let $${\ensuremath{M_{\mathrm{hs}}}}= \begin{pmatrix}
    * & 0 & 1\\
    0 & * & *\\
    1 & * & * 
           \end{pmatrix}.$$ There is a one-to-one correspondence between the homogeneous pairs of $G$ in which $W_2$ is empty and the ${\ensuremath{M_{\mathrm{hs}}}}$-partitions of $G$ that satisfy the following additional constraints.

-   At least two vertices are mapped to parts $2$–$3$ (vertices in these parts are in $U$).

-   At least two vertices are mapped to part $1$ (vertices in this part are in $W_1$).

Symmetrically, there is also a one-to-one correspondence between the homogeneous pairs of $G$ in which $W_1$ is empty and the ${\ensuremath{M_{\mathrm{hs}}}}$-partitions of $G$ that satisfy the above constraints. (Partitions according to ${\ensuremath{M_{\mathrm{hs}}}}$ correspond to so-called “homogeneous sets” but we do not need the details of these.)

It is known from [@EKR1997:Hom-pair] that, in deterministic polynomial time, it is possible to determine whether a graph contains a homogeneous pair and, if so, to find one. We show that the homogeneous pairs in a graph can also be counted in polynomial time. We start by considering the relevant list-partition counting problems.

\[thm:hompair\] There are polynomial-time algorithms for [[\#List-${\ensuremath{M_{\mathrm{hp}}}}$-partitions]{}]{} and [[\#List-${\ensuremath{M_{\mathrm{hs}}}}$-partitions]{}]{}.

We first show that there is a polynomial-time algorithm for [[\#List-${\ensuremath{M_{\mathrm{hp}}}}$-partitions]{}]{}. The most natural way to do this would be to show that there is no ${{\mathcal{P}(D)}}$-[$M_{\mathrm{hp}}$]{}-derectangularising sequence and then apply Theorem \[thm:explicitdichotomy\]. In theory, we could show that there is no ${{\mathcal{P}(D)}}$-[$M_{\mathrm{hp}}$]{}-derectangularising sequence by brute force since $|D|=6$, but the number of possibilities is too large to make this feasible. Instead, we argue non-constructively.

First, if there is no ${{\mathcal{P}(D)}}$-[$M_{\mathrm{hp}}$]{}-derectangularising sequence, the result follows from Theorem \[thm:explicitdichotomy\].

Conversely, suppose that $D_1, \dots, D_k$ is a ${{\mathcal{P}(D)}}$-[$M_{\mathrm{hp}}$]{}-derectangularising sequence. Let $M$ be the matrix such that $M_{i,j} = 0$ if $({\ensuremath{M_{\mathrm{hp}}}})_{i,j} = 1$ and $M_{i,j}
    = ({\ensuremath{M_{\mathrm{hp}}}})_{i,j}$, otherwise. $D_1, \dots, D_k$ is also a ${{\mathcal{P}(D)}}$-$M$-derectangularising sequence, since $H^M_{X,Y} =
    H^{{\ensuremath{M_{\mathrm{hp}}}}}_{X,Y}$ for any $X,Y\subseteq D$ and any sequence $D_1,
    \dots, D_k$ is $M$-purifying because $M$ is already pure. Therefore, by Theorem \[thm:explicitdichotomy\], counting list $M$-partitions is [$\mathrm{\#P}$]{}-complete.

However, counting the list $M$-partitions of a graph $G$ corresponds to counting list homomorphisms from $G$ to the $6$-vertex graph $H$ whose two components are an edge and a $4$-clique, and which has loops on all six vertices. There is a very straightforward polynomial-time algorithm for this problem (a simple modification of the version without lists in [@DG]). Thus, ${\ensuremath{\mathrm{\#P}}}={\ensuremath{\mathrm{FP}}}$ so, in particular, there is a polynomial-time algorithm for counting list [$M_{\mathrm{hp}}$]{}-partitions.

The proof that there is a polynomial-time algorithm for [[\#List-${\ensuremath{M_{\mathrm{hs}}}}$-partitions]{}]{} is similar.

\[cor:hompair\] There is a polynomial-time algorithm for counting the homogeneous pairs in a graph.

We are given a graph $G=(V,E)$ and we wish to compute the number of homogeneous pairs that it contains. By the one-to-one correspondence given earlier, it suffices to show how to count ${\ensuremath{M_{\mathrm{hp}}}}$-partitions and ${\ensuremath{M_{\mathrm{hs}}}}$-partitions of $G$ satisfying additional constraints. We start with the first of these. Recall the constraints on the ${\ensuremath{M_{\mathrm{hp}}}}$-partitions $\sigma$ that we wish to count:

-   $N_\sigma(3) + N_\sigma(4) + N_\sigma(5) + N_\sigma(6)\geq 2$,

-   $N_\sigma(1) > 0$ and $N_\sigma(2) > 0$, and

-   at least one $N_\sigma(1)$ and $N_\sigma(2)$ is at least $2$.

Define three subsets $\Sigma_1$, $\Sigma_2$ and $\Sigma_{1,2}$ of the set of [$M_{\mathrm{hp}}$]{}-partitions of $G$ that satisfy the constraints. In the definition of each of $\Sigma_1$, $\Sigma_2$ and $\Sigma_{1,2}$, we will require that parts $1$ and $2$ are non-empty and parts $3$–$6$ contain a total of at least two vertices. In $\Sigma_1$, part $1$ must contain at least two vertices; in $\Sigma_2$, part $2$ must contain at least two vertices; in $\Sigma_{1,2}$, both parts $1$ and $2$ must contain at least two vertices. The number of suitable ${\ensuremath{M_{\mathrm{hp}}}}$-partitions of $G$ is $|\Sigma_1| + |\Sigma_2| - |\Sigma_{1,2}|$.

Each of $|\Sigma_1|$, $|\Sigma_2|$ and $|\Sigma_{1,2}|$ can be computed by counting the ${\ensuremath{M_{\mathrm{hp}}}}$-partitions of $G$ that satisfy appropriate cardinality constraints. Parts $1$ and $2$ are trivially dealt with. The requirement that parts $3$–$6$ must contain at least two vertices between them is equivalent to saying that at least one of them must contain at least two vertices or at least two must contain at least one vertex. This can be expressed with a sequence of cardinality constraint functions and using inclusion–exclusion to eliminate double-counting.

Counting constrained ${\ensuremath{M_{\mathrm{hs}}}}$-partitions of $G$ is similar (but simpler).

[^1]: Department of Computer Science, University of Oxford, Wolfson Building, Parks Road, Oxford, OX1 3QD, United Kingdom.

[^2]: Department of Computer Science, Ashton Building, University of Liverpool, Liverpool, L69 3BX, United Kingdom.

[^3]: Department of Information Science, University of Fukui, 3-9-1 Bunkyo, Fukui City, Fukui 910-8507, Japan.

[^4]: A preliminary version of this paper appeared in the proceedings of CCC 2014. The research leading to these results has received funding from the MEXT Grants-in-Aid for Scientific Research and the EPSRC and the European Research Council under the European Union’s Seventh Framework Programme (FP7/2007–2013) ERC grant agreement no. 334828. The paper reflects only the authors’ views and not the views of the ERC or the European Commission. The European Union is not liable for any use that may be made of the information contained therein.

[^5]: For the reader who is familiar with CSPs, it might be useful to see how a [[List-$M$-partitions]{}]{} problem can be coded as a CSP with restrictions on the input. Given a symmetric $M \in \{0,1,*\}^{D\times D}$, let $M_0$ be the relation on $D\times D$ containing all pairs $(i,j) \in D\times D$ for which $M_{i,j} \neq 1$. Let $M_1$ be the relation on $D\times D$ containing all pairs $(i,j)\in D\times D$ for which $M_{i,j} \neq 0$. Then a [[List-$M$-partitions]{}]{} problem with input $G,L$ can be encoded as a CSP whose constraint language includes the binary relations $M_0$ and $M_1$ and also the unary relations corresponding to the sets in the image of $L$. Each vertex $v$ of $G$ is a variable in the CSP instance with the unary constraint $L(v)$. If $(u,v)$ is an edge of $G$ then it is constrained by $M_1$. If it is a non-edge of $G$, it is constrained by $M_0$. Note that the CSP instance satisfies the restriction that every pair of distinct variables has exactly one constraint, which is either $M_0$ or $M_1$. In a general CSP instance, a pair of variables could be constrained by $M_0$ and $M_1$ or one of them, or neither. It is not clear how to code such a general CSP instance as a list partitions problem.
