1 Introduction

Since \(\mathcal{P}\) is believed to be not equal to \(\mathcal{NP}\), it is accepted that algorithms solving \(\mathcal{NP}\)-hard problems use super-polynomial, or even exponential, time. This justifies the study of moderately exponential time algorithms: algorithms that solve \(\mathcal{NP}\)-hard problems exactly, but run in exponential time in the worst case. Naturally the challenge still is to get the worst-case complexity as low as possible, and this question already has given rise to many non-trivial algorithms. See for example the survey by Woeginger [20].

Many of these algorithms also use exponential space. In fact, the time/space-usage ratio is polynomial in the input (i.e. the running time is bounded by a polynomial function of the space usage). It is expected that these algorithms are not practical even for small problem-instances, since they tend to run out of memory, require lot of (slow) disk access, and hardly allow parallel execution. Therefore polynomial-space exact algorithms have already been studied for several \(\mathcal{NP}\)-hard problems [3, 7, 11, 14, 15, 20]. In this paper we focus on space usage and give improved algorithms for several problems.

In 2009, Björklund et al. [3] drew new attention to the well-known principle of Inclusion-Exclusion: They gave \(\mathcal {O}^{*}(2^{n})\)-time algorithms for several set partition problems, the most prominent one being k-Coloring. They also mention a simple adjustment to their algorithm to achieve an \(\mathcal {O}^{*}(2.24^{n})\)-time algorithm with polynomial space for k-Coloring. Also related to this are the \(\mathcal{O}^{*}(2^{n})\)-time polynomial-space algorithms for #Hamiltonian path by Karp [14] and (implicitly) Kohn et al. [15], and for #Perfect Matching by Björklund and Husfeldt [2].

Our algorithms heavily rely on the work of Björklund et al. [26]. The results can be read from Table 1.

Table 1 The results of this work compared to the relevant previous results. The number of vertices on the input graph is denoted by n. The running times of the algorithms of this work are given in the last column. These new algorithms use polynomial space, except the ones indicated with the , which use \(\mathcal{O}^{\star}(c)\) space

Steiner Tree is one of the most well-studied \(\mathcal{NP}\)-complete problems. For this problem, the Dreyfus-Wagner [9] dynamic programming algorithm has been the fastest exact algorithm for over 30 years. However, recently Björklund et al. [4] gave an \(\mathcal{O}^{\star}(2^{k})\)-time algorithm for the variant with bounded integer weights with k terminals, and Fuchs et al. [12] gave an \(\mathcal{O}^{\star}(c^{k})\)-time algorithm for the general case, for any c>2. Both algorithms use Ω(2k) space. In 2008, Fomin et al. [11] initiated the study of polynomial space algorithms for Steiner Tree. They gave polynomial space algorithms with running times bounded by \(5.96^{k}n^{\mathcal{O}(\log k)}\) and \(\mathcal{O}^{\star}(1.60^{n})\) where n is the number of vertices in the graph. They pose the question whether Steiner Tree is fixed parameter tractable with respect to k when there is a polynomial space restriction. We answer this question affirmatively by providing an algorithm that runs in \(\mathcal{O}^{*}(2^{k})\) time and meets the restriction. Using the techniques of [11], this also leads to a polynomial-space \(\mathcal{O}^{*}(1.36^{n})\)-time algorithm.

The Max Internal Spanning Tree (MIST) and Degree Constrained Spanning Tree (DCST; also called Min-Max Degree Spanning Tree) problems are natural generalizations of Hamiltonian path. In [10], Fernau et al. ask if there exists an \(\mathcal{O}^{\star}(2^{n})\)-time algorithm to solve MIST. In [13], Gaspers et al. ask if there exists an \(\mathcal{O}^{\star}(2^{n})\)-time algorithm solving DCST. We answer both questions by giving polynomial-space algorithms with this running time.

The Cover polynomial of a directed graph, introduced by Graham and Chung [8], generalizes all problems that can be solved using two operations named deletion and contraction of edges, and is designed to be the directed analogue of the Tutte polynomial. We improve the \(\mathcal{O}^{\star}(3^{n})\)-time polynomial-space algorithm of Björklund et al. [5] to an \(\mathcal{O}^{\star}(2^{n})\)-time polynomial-space algorithm. We also give the same improvement for #Spanning forests, which is one particular case of the Tutte polynomial. For more information about the Tutte polynomial we refer to [5].

Finally, we give two new algorithms for Convex Tree Coloring and #Perfect matching (counting the number of perfect matchings of a graph). (A special case of) the Convex Tree Coloring problem was studied in [19], where a \(\mathcal {O}^{\star}(2^{k}c)\) time and \(\mathcal{O}^{\star}(c)\) space algorithm was given. We will continue this study by emphasizing the space usage aspect. Finally, we show that # Perfect matching can be solved in \(\mathcal{O}^{\star }(1.95^{n})\) time and polynomial space, improving over the previous \(\mathcal{O}^{\star }(2^{n})\) time polynomial space algorithm of Björklund and Husfeldt [2]. It is worth to mention that with exponential space, one can count perfect matchings of general graphs even in \(\mathcal{O}^{\star}(1.62^{n})\) time due to Koivisto [16].

The paper is organized as follows: After the preliminaries in Sect. 2, we revisit the principle of Inclusion-Exclusion and the well-known Hamiltonian path algorithm in Sect. 3. After this we provide in Sect. 4 a natural extension by introducing the concept of branching walks and give the resulting algorithms. In the remaining sections we prove the remaining results that are not primarily based on branching walks.

2 Preliminaries and Notation

We will use the \(\mathcal{O}^{\star}\) notation that suppresses any factor that is polynomial in the input size. For an integer i, we use [i] to denote {1,…,i}. We also use Iverson’s bracket notation: for a proposition p, [p] denotes 1 if p is true and 0 otherwise. Let G=(V,E) be a graph. We will use V(G) and E(G) for the vertices V of G and the edges E of G, respectively. The number of vertices of the input graphs is denoted by n. The graph G[X] induced by X is (i) (X,(X×X)∩E) where XV, and (ii) (⋃ eX e,X) if XE. For vV(G), N G (v) are all vertices adjacent to v in G and d G (v)=|N G (v)|; The subscript G is omitted if the graph is clear from the context. A walk of length k in G is a tuple W=(v 1,…,v k+1)∈V k+1 such that (v i ,v i+1)∈E for each 0<ik. We say W is from v if v 1=v, and W is cyclic if v 1=v k+1. Furthermore, vV is said to be visited by W if v i =v for some 1≤ik. For a rooted tree T we will use rt(T) to denote the root of T. For a vertex vV(T) we will also use pa(v) to denote the parent of v.

Also given a graph G 1=(V 1,E 1), a homomorphism from G 1 to G is a function φ:V 1V such that (u,v)∈E 1 implies (φ(u),φ(v))∈E. Note that φ directly corresponds to a walk in the case that G 1 is a path. We will use the notation φ(X)={φ(u)∣uX} and φ(Y)={(φ(u),φ(v))∣(u,v)∈Y} for XV 1 and YE 1. If G 2=(V 2,E 2) is a third graph with V 1V 2=∅ and φ 1:V 1V and φ 2:V 2V are homomorphisms, we will use φ 1φ 2 to denote the homomorphism V 1V 2V defined by φ(v)=φ 1(v) and φ(w)=φ 2(w) with vV 1 and wV 2.

2.1 Model

We assume integers in the input are given in binary, and we will prove our results for the RAM computation model where any arithmetic operation and storing any integer is assumed to take constant time and space. However, all our results also hold in the (more realistic) RAM computation model where arithmetic operations and storing integers only take constant time and space if they are constant-sized. To see the significance, note for example that in the first model two n-bits integers can be added in constant time, while in the second it takes linear time.

3 Inclusion-Exclusion

Theorem 1

(Folklore)

Let U and R be sets and for every vR, let P v be a subset of U. Use \(\overline{P_{v}}\) to denote UP v . With the convention \(\bigcap_{i \in\emptyset}\overline{P_{i}}=U\), the following holds:

$$ \biggl|\,\bigcap_{v \in R}P_v\biggr| = \sum_{F \subseteq R} {(-1)}^{|F|}\biggl|\,\bigcap _{v \in F} \overline{P_v}\biggr|.$$
(1)

In this work, we call any application of the above theorem an Inclusion-Exclusion-formulation (IE-formulation). In the context of this paper it is convenient to use the following terminology: We refer to the set U as the universe, to R as the requirement space and to P v as a property. Moreover, given a set FR, we call the task of computing \(|\bigcap_{v \in F}\overline{P_{v}}|\), the simplified problem. Note that if the simplified problem can be solved in \(\mathcal{O}^{\star}(t(n))\) time and \(\mathcal{O}^{\star} (s(n))\) space, the left-hand side of (1) can be determined in \(\mathcal{O}^{\star}(2^{n}t(n))\)-time and \(\mathcal {O}^{\star}(s(n))\) space in the straightforward manner. All algorithms in this paper will exploit this observation.

3.1 Hamiltonian Paths

To illustrate Theorem 1, we will first recall the following IE-formulation due to Karp [14] for counting Hamiltonian paths. Given a (possibly directed) graph G=(V,E), a Hamiltonian path is a walk that contains each vertex of G exactly once.Footnote 1

Theorem 2

([14])

Hamiltonian paths of a graph of n vertices can be counted in \(\mathcal{O}^{\star}(2^{n})\) time and polynomial space.

Proof

Let G=(V,E). In the context of Theorem 1, define the universe U as all walks of length n−1 in G, the requirement space R=V, and P v as all walks of length n−1 that visit vertex v, for every vV. With these definitions, the left-hand side of (1), |⋂ vV P v |, is the number of Hamiltonian paths in G. Now it remains to show how to solve the simplified problem. Given FV and xVF, let w F (x,k) be the number of walks from x of length k in G[VF]. Then w F (x,k) admits the following recurrence:

(2)

Also, notice that

$$\biggl|\,\bigcap_{v \in F}\overline{P_v}\biggr| = \sum_{s \in V \setminus F} w_{F}(s,n-1),$$

and hence the simplified problem can be solved in polynomial time using dynamic programming on (2) (the parameter F is fixed but is added for clarity). Thus it takes \(\mathcal{O}^{\star}(2^{n})\) time and polynomial space to evaluate (1), and the theorem follows. □

4 Branching Walks

Definition 1

A branching walk B in G=(V,E) is a pair (T,φ) where T is an ordered rooted tree and φ:V(T)→V is a homomorphism from T to G. For a vertex xV, B is from x if φ(rt(T))=x. B visits a vertex vV if vφ(V(T)). The length of B is |E(T)|.

A branching walk is a natural generalization of a walk: Notice that a branching walk (T,φ) is a walk in the special case that T is a path rooted at an endpoint. Since we will count distinct branching walks, we emphasize that two branching walks (T 1,φ 1) and (T 2,φ 2) are distinct if there is no isomorphism ψ:T 1T 2 such that φ 1(v)=φ 2(ψ(v)) for every vV(T 1).

4.1 Steiner Tree

In this section we will give an extension of the technique in the previous section to obtain a new IE-formulation for the Steiner Tree problem, which is defined as follows:

Steiner Tree

Input :

G=(V,E), c∈ℤ+, weight function w:E→[c]∖0 and terminals KV.

Question :

Is there a subtree (V′,E′) of G such that KV′ and ∑ eE w(e)≤c?

Given a branching walk (T,φ), the quantity ∑ eE(T) w(φ(e)) is said to be its weight.

Lemma 1

Let s 0K. There exists a subtree S=(V′,E′) of G such that KVand w(E′)≤c if and only if there exists a branching walk B=(T,φ) from s 0 of weight at most c such that Kφ(V(T)).

Proof

For the forward direction, assume S to be ordered by fixing an arbitrary order. Then define B=(S,φ), with φ:V′→V′ the identity function, and let rt(S)=s 0 (this is possible since s 0KV(S)). Then, clearly w(φ(E(S)))≤c. For the backward direction, notice that (φ(V(T)),φ(E(T))) is connected and if we let S be a spanning tree of (φ(V(T)),φ(E(T))), it has the required properties. □

Consider the following IE-formulation: Let s 0K be an arbitrarily chosen terminal, and define the universe U to be the set of all branching walks (T,φ) from s 0 of weight at most c. Let the requirement space R be K. For every vK, define a property P v U that consists of all branching walks in U that visit v. It follows that the left-hand side of (1), |⋂ vK P v |, is the number of branching walks of weight at most c that contain all terminals. By Lemma 1, this quantity is larger than 0 if and only if the instance of Steiner Tree is a yes-instance. Hence we can restrict our goal to determining |⋂ vK P v |. For this we use Theorem 1.

Before we proceed, first let us recall a basic combinatorial problem: Let \(\mathcal{T}_{n}\) be the set of all distinct ordered rooted trees on n edges (also called the Catalan numbers). We will give a recurrence for \(|\mathcal{T}_{n}|\). Let

$$ \gamma: \bigcup_{i,j} (\mathcal{T}_i \times\mathcal{T}_j)\leftrightarrow\mathcal{T}_{i+j+1}$$
(3)

be the gluing operation such that T=γ(T 1,T 2) is obtained by connecting rt(T 1) and rt(T 2), setting rt(T)=rt(T 2), letting the first child of rt(T) be rt(T 1), being followed by the children of rt(T) in T 2. Clearly γ is a bijection so \(|\mathcal{T}_{n}| = \sum_{i=0}^{n-i-1} |\mathcal{T}_{i}||\mathcal{T}_{n-i-1}|\).

We now continue applying Theorem 1 by showing how the simplified problem can be solved in \(\mathcal{O}^{\star}(c)\) time and space. For FK, define \(\mathcal{B}_{F}(x,W)\) to be all branching walks (T,φ) of weight at most W from x in G[VF], where xVF. Hence \(U=\mathcal{B}_{\emptyset}(x,c)\). Let \(b_{F}(x,W) = |\mathcal{B}_{F}(x,W)|\). First, note that the simplified problem is to compute

$$\biggl|\,\bigcap_{v \in F}\overline{P_v}\biggr| = b_F(s_0,W)$$

for a given set FK of terminals. Now b F (s 0,W) can be computed in polynomial time using the following lemma in combination with dynamic programming:

Lemma 2

Let FK and xVF, then

figure a

Proof

There is one branching walk B of weight 0, B=(T,φ), from x with T being a single vertex and φ mapping this single vertex to x, hence Case 4a. For the second case, define a function

$$\gamma': \bigcup_{t \in N(x) \setminus F} \ \bigcup _{W_1,W_2} \bigl(\mathcal{B}_F(t,W_1) \times\mathcal{B}_F(x,W_2)\bigr)\leftrightarrow\mathcal{B}_F\bigl(x,W_1+W_2+w(x,t)\bigr)$$

by γ′((T 1,φ 1),(T 2,φ 2))=(γ(T 1,T 2),φ 1φ 2). Now γ′ is a bijection since γ is a bijection as stated in (3) and for the branching walk (γ(T 1,T 2),φ) it must hold that φ(rt(T 1))∈N(φ(rt(T 2))). Hence Case 4b follows. □

Theorem 3

The Steiner tree problem can be solved in \(\mathcal {O}^{\star}(2^{k}c)\) time and \(\mathcal{O}^{\star}(c)\) space.

Proof

Due to Lemma 1 the considered IE-formulation solves Steiner Tree, and we can use dynamic programming on (4b) to compute the simplified problem in \(\mathcal{O}^{\star }(c^{2})\) time. This can be further reduced to \(\mathcal{O}^{\star}(c)\) time in a standard manner with the Fast Fourier Transform. Then the theorem follows from Theorem 1. □

The following result is a consequence of Theorem 3 and the considerations of Sect. 4.2 in [11]:

Theorem 4

The Steiner Tree problem with unit weights can be solved in \(\mathcal{O}^{\star}(1.36^{n})\) time using polynomial space.

Proof

Modify Algorithm steiner as described in Sect. 4.2 in [11], except that we replace Step 4 of steiner with the algorithm due to Theorem 3. This clearly does not change the worst-case running time as claimed in Theorem 5 of [11], and it is easy to see that the new algorithm uses polynomial space. □

4.2 Degree Constrained Spanning Tree

Degree Constrained Spanning Tree (DCST)

Input :

G=(V,E), 1≤cn.

Question :

Is there a spanning tree of G with maximum degree at most c?

Analogous to Lemma 1, we will use the following lemma to reduce our problem to computing some quantity involving branching walks:

Lemma 3

There exists a spanning tree of G of maximum degree at most c if and only if there exists a branching walk B=(T,φ) of length n−1 such that φ(V(T))=V and the maximum degree of T is at most c.

Proof

For the forward direction, assume S to be a spanning tree of G of maximum degree at most c and order it by fixing an arbitrary order. Then define B=(S,φ), with φ:V(S)→V(S) the identity function, and choose rt(S) arbitrarily. For the backward direction, notice that (φ(V(T)),φ(E(T))) is a tree of maximum degree at most c. □

Now we can use the following IE-formulation: Define the universe U as all branching walks (T,φ) of length n−1 such that the maximum degree of T is at most c. Define the requirement space R=V and P v to be all branching walks in U visiting v, for every vV. That is, let P v ={(T,φ)∈U:vφ(V(T))}. For FV, define \(\mathcal{D}_{F}(x,j,g)\) as all branching walks (T,φ)∈U from x of length j in G[VF] such that the degree of the root of T is at most g and the degree of every other vertex is at most c. Hence \(U=\bigcup_{x\in V\setminus F}\mathcal{D}_{\emptyset}(x,n-1,c)\). Let \(d_{F}(x,j,g) =|\mathcal{D}_{F}(x,j,g)|\). We apply Theorem 1, and conclude that the simplified problem is to compute the number of branching walks in the universe that avoid F, and hence

$$\biggl|\,\bigcap_{v\in F}\overline{P_v}\biggr|= \sum_{x\in V \setminus F}d_{F}(x,n-1,c).$$

This can be done in polynomial time with dynamic programming using the following lemma:

Lemma 4

figure b

Proof

To see that Case 5a holds, notice that d F (x,0,g)=0 if g is negative since the root has a non-negative degree, and otherwise there is one branching walk (T,φ) of length zero obtained by letting T be the tree on one vertex and letting φ be the function mapping this vertex to x.

For Case 5b, define γ′((T 1,φ 1),(T 2,φ 2)) to be the branching walk (γ(T 1,T 2),φ 1φ 2), where γ is the gluing operation from (3). Then we claim that γ′ is a bijection

$$\gamma': \bigcup_{t \in N(x) \setminus F} \bigcup_{j_1,j_2}\bigl(\mathcal{D}_F(t,j_1,c-1) \times\mathcal{D}_F(x,j_2,g)\bigr)\leftrightarrow\mathcal{D}_F(x,j_1+j_2+1,g+1).$$

To see this, define a (c,g)-tree as a tree of maximum degree c with the degree of its root at most g, and notice that γ(T 1,T 2) is a (c,g)-tree if and only if T 1 is a (c,c−1)-tree and T 2 is a (c,g−1)-tree. Then Case 5b follows by combining with the arguments in the proof of Lemma 2. □

Theorem 5

The Degree Constrained Spanning Tree problem can be solved in \(\mathcal{O}^{\star}(2^{n})\) time and polynomial space.

Proof

Due to Lemma 3 the considered IE-formulation solves DCST, and we can use dynamic programming on (4) to compute the simplified problem in polynomial time. Then the theorem follows from Theorem 1. □

4.3 Maximum Internal Spanning Tree

A vertex of a tree is called internal if its degree is at least 2.

Maximum internal Spanning Tree

Input :

G=(V,E), 1≤cn.

Question :

Is there a spanning tree of G with at least c internal vertices?

Lemma 5

There exists a spanning tree of G with at least c internal vertices if and only if there exists a branching walk B=(T,φ) of length n−1 such that φ(V(T))=V and T has at least c internal vertices.

Proof

For the forward direction, assume S to be a spanning tree of G with at least c internal vertices and order it by fixing an arbitrary order. Define B=(S,φ), with φ:V(S)→V(S) the identity function. It clearly has the required properties. For the backward direction, notice that (V,φ(E(T))) is a spanning tree with at least c internal vertices since ϕ(E(T))=n−1. □

Consider the following IE-formulation: Define the universe U to be all branching walks (T,φ) of length n−1 such that T has at least c internal vertices. For vV, let P v ={(T,φ)∈U:vφ(V(T))}. For FV and δ∈ℤ∪{0,1,2}, define \(\mathcal{M}^{\delta}_{F}(x,j,g)\) as all branching walks (T,φ) in G[VF] of length j from x such that |{vV(T)∖rt(T):v is internal}|+[d(rt(T))≥δ]=g. Let \(m^{\delta}_{F}(x,j,g)=|\mathcal{M}^{\delta}_{F}(x,j,g)|\), then

Lemma 6

figure c

Proof

To see that Case 6a holds, notice that there is only one branching walk of length 0 from x and its tree-part is a single vertex, so g must be equal to 1 if δ≤0 and equal to 0 otherwise by definition. For Case 6b, first define γ′((T 1,φ 1),(T 2,φ 2)) as the branching walk (γ(T 1,T 2),φ 1φ 2), where γ is the gluing operation from (3). Then we claim that γ′ is a bijection

To see this, note that γ(T 1,T 2) has g′ internal vertices not equal to the root if and only if T 1 has \(g'_{1}\) and T 2 has \(g'_{2}\) internal vertices not equal to the root with \(g'_{1}+g'_{2} = g'\) and

$$d_{\gamma(T_1,T_2)}\bigl(\mathtt{rt}\bigl(\gamma(T_1,T_2)\bigr)\bigr)= 1 +d_{T_2}\bigl(\mathtt{rt}(T_2)\bigr).$$

Hence the δ and g accumulators are modified correctly. Then Case 6b follows by combining with the arguments in the proof of Lemma 2. □

Theorem 6

The Maximum Internal Spanning Tree problem can be solved in \(\mathcal{O}^{\star}(2^{n})\) time and polynomial space.

Proof

Use the IE-formulation as described above. The simplified problem is to compute \(|\bigcap_{v \in F}\overline{P_{v}}|\), which is equal to \(\sum _{s \in V \setminus F}m^{2}_{F}(s,n-1,c)\) since the root needs at least two neighbors in order to be an internal vertex. It follows from Lemma 5 that there exists B∈⋂ vV P v if and only if there exists a spanning tree with at least c leaves. □

4.4 Counting Spanning Forests

Define a c-spanning forest to be an acyclic spanning subgraph with exactly c connected components. In this section we will address the following problem:

#Spanning Forests

Input :

G=(V,E), 1≤cn.

Question :

The number of acyclic spanning subgraphs of G with exactly c connected components.

Assume that a total ordering ≺ on the vertex set V is given. Given a subset XV, let minX be the minimum element of X with respect to ≺.

Definition 2

A sorted branching walk is a branching walk (T,φ) such that for every vV(T) with children c 1,…,c l , numbered with respect to the order of T, φ(c i )≺φ(c j ) for every i<j and φ(rt(v))=minφ(V(T)).

It can be noted that the definition implies no two children of vertex vV(T) can be mapped to same vertex in G.

Lemma 7

There is a bijection between the set of all subtrees S of G, and the set of all sorted branching walks B=(T,φ) of length |φ(E(T))| such that φ(E(T)) induces a subtree.

Proof

Note that it is sufficient to show that for every subtree S of G, there is exactly one a sorted branching walk B=(T,φ) of length |φ(E(T))| such that φ(E(T))=E(S).

Since φ(V(T))=V(S) and B is sorted, we know that φ(rt(T))=minV(T). Since |E(S)|=|E(T)|, φ is a bijection and hence S and T are isomorphic. Moreover φ(c i )≺φ(c j ) whenever c i occurs before c j as a child of a vertex in T since B is sorted. This implies that T and φ are uniquely determined by S. □

Define \(\mathcal{B}_{F}(l)\) to be all sorted branching walks (T,φ) of length l in G such that Fφ(V(T))=∅. Also, define \(b_{F}(l)=|\mathcal{B}_{F}(l)|\).

Lemma 8

The number of c-spanning forests is equal to

$$ \sum_{F \subseteq V} (-1)^{|F|} \frac{1}{c!} \sum_{l_1 + \cdots+l_c = n-c} \prod_{j=1}^{c} b_F(l_j).$$
(7)

Proof

We apply Theorem 1. Define U to be the set of all families f of sorted branching walks of total length nc (that is, the sum of the length of all members of f is nc). Let the requirement space be V, and for every vV define P v to be all elements fU such that there is (T,φ)∈f with vφ(V(T)). It is not hard to see that a term of the summation of (7) (ignoring (−1)|F|) is equal to \(|\bigcap_{v \in F}\overline{P_{v}}|\).

Applying Theorem 1, it remains to show that the number of c-spanning forests is |⋂ vV P v |. To see this, notice that for a family of sorted branching walks f={(T 1,φ 1),…,(T c ,φ c )}, \(\bigcup_{i=1}^{c}\varphi_{i}(V_{i}) = V\) if and only if \(\bigcup_{i=1}^{c}\varphi_{i}(E_{i})\) is a c-spanning forest. Then every c-spanning forest corresponds to exactly one set of branching walks of total length nc due to Lemma 7. The lemma follows. □

Define \(\mathcal{B}_{F}(x,j,g)\) as all sorted branching walks (T,φ) from x of length j such that φ(V(T))∩F=∅ and no child of the root of T is mapped to one of the first g−1 neighbors of x in G[VF] with respect to the ordering ≺. Define \(b_{F}(x,j,g)=|\mathcal {B}_{F}(x,j,g)|\). Use \(N_{F}^{q}(x)\) to denote the qth element of the set N(x)∖F with respect to the ordering ≺.

Lemma 9

figure d

Proof

For Case 8a, notice there is exactly one branching walk (T,φ) with T being a single vertex and with φ mapping this vertex to x.

To see Case 8b, notice that since g>|N(x)∩F|, in any branching walk in \(\mathcal{B}_{F}(x,j,g)\) the root can not have any children but since j>0 this must be the case, and hence such a branching walk does not exist.

For Case 8c, first notice that \(\mathcal{B}_{F}(x,j,g+1)\) are exactly all branching walks \((T,\varphi) \in\mathcal {B}_{F}(x,j_{1}+j_{2}+1,g)\) where no child of the root of T is mapped to \(N_{F}^{g}(x)\). Define γ′((T 1,φ 1),(T 2,φ 2)) as the branching walk (γ(T 1,T 2),φ 1φ 2), where γ is the gluing operation from (3). Then it is not too hard to see that γ′ is a bijection

since γ′(B 1,B 2) and \(\gamma'(B'_{1},B'_{2})\) are distinct whenever either B 1 and \(B^{'}_{1}\) or B 2 and \(B'_{2}\) are distinct. Then Case 8c follows by combining with the arguments in the proof of Lemma 2. □

Theorem 7

The #Spanning forests problem can be solved in \(\mathcal {O}^{\star}(2^{n})\) time and polynomial space.

Proof

Let \(\overline{F} = V \setminus F\) and \(b_{F}(l) = \sum_{x\in \overline{F}} b_{\overline{F}[x]}(x,l,1)\), where \(\overline{F}[x]\) stands for the set of all elements e in \(\overline{F}\) such that xe. Using dynamic programming in combination with Lemma 9, the values b F (l) can be computed for every 1≤ln for a fixed F. Also using standard dynamic programming, the simplified problem (that is, the summand of (8a) ignoring the (−1)|F|) can be computed in polynomial time. Hence (8a) can be evaluated within the claimed resource bounds and the theorem follows from Lemma 8. □

5 Cover Polynomial

We use \(x^{\underline{i}}\) for the falling factorial \(\frac {x!}{(x-i)!}\). The cover polynomial of a directed graph D=(V,A) can be defined as (see also [5, 8]):

$$\sum_{i,j}c(i,j) x^{\underline{i}}y^{\underline{j}}$$

where c(i,j) is defined as the number of ways to partition V into i directed paths and j directed cycles of D. In this section we will address the following problem:

Cover Polynomial

Input :

A graph G.

Question :

The coefficients c(i,j) for every 0≤i,jn.

Since paths and cycles with l edges contain l+1 and l vertices respectively, the sum of the lengths of the paths and cycles in such a partition will be ni. Moreover, if V is covered by i paths and j cycles with lengths summing up to n−1, the path and cycles are disjoint because of the size restriction. Recall from Sect. 3.1 that w F (s,j) is the number of walks starting from s of length j avoiding F. Similarly, define w F (j) as all walks of length j avoiding F and \(\hat{w}_{F}(j)\) as the number of cyclic walks of length j avoiding F.

Lemma 10

$$ c(i,j) = \sum_{F \subseteq V} (-1)^{|F|} \frac{1}{i!j!} \sum_{l_1 +\cdots +l_{i+j}=n-i} \Biggl(\,\prod_{k=1}^i w_F(l_k)\Biggr) \Biggl(\,\prod_{k=i+1}^{i+j} \hat{w}_{F}(l_k)\Biggr).$$
(9)

Proof

We apply Theorem 1. Define U as all combinations of i walks and j cyclic walks with lengths summing up to exactly ni. Define the requirement space to be V, and for every vV let P v be all combinations of walks and cyclic walks in U such that at least one of the (cyclic) walks visits v. It is easy to see that each summand (ignoring the (−1)|F|) equals \(|\bigcap_{v \in F}\overline {P_{v}}|\), and since |⋂ vF P v |=c(i,j) by the above discussion, the lemma follows from Theorem 1. □

Theorem 8

The Cover Polynomial problem can be solved in \(\mathcal {O}^{\star}(2^{n})\) time and polynomial space.

Proof

By using minor modifications of the dynamic programming algorithm using (2), for every FV and 0≤jn, both w F (j) and \(\hat{w}_{F}(j)\) can be computed in polynomial time. Using straightforward dynamic programming, the simplified problem can be obtained from the values w F (j) and \(\hat{w}_{F}(j)\). Using this, (9) can be evaluated within the claimed resource bounds. □

6 Convex Tree Coloring

In this section we solve a generalization of the Convex Recoloring problem studies in [19] and improve upon one of their results. Let T=(V,E) be a tree and w:E(T)×[k]×[k]→[c]. A k-coloring is a function γ:V→[k]. As before, for XV let φ(X) be ⋃ vX φ(v). The coloring γ is minimal if γ(V)=[k]. Define w(γ)=∑(u,v)∈E(T) w((u,v),γ(u),γ(v)). The coloring γ is convex if for every x,y,zV such that y is in the (unique) path from x to z it holds that γ(x)=γ(z) implies γ(x)=γ(y), or more informally: γ is convex if all its color classes induce a connected subtree. It is worth mentioning that in [19] the slightly different Convex Tree Recoloring (CTR) was studied, but that CTR is a special case of CTC.

Convex Tree Coloring (CTC)

Input :

A tree T=(V,E) with a weight function w:E×[k]×[k]→[c].

Question :

Is there a convex coloring γ:V→[k] with w(γ)≤c?

Let us first note that we can safely assume that T is in fact binary, since if it is not, we can insert vertices and force them to have the same color by using an appropriate weight function. Also note we can restrict ourselves to minimal colorings by k vertices of degree one to arbitrary vertices and setting w(⋅,⋅,⋅=)=0. Hence the colors assigned to the added vertices does not matter at all, but if a color is not minimal, it can be extended to a minimal one using the new added vertices. Thus we can restrict ourselves to solving the following variant:

Minimal Convex Binary Tree Coloring (MCBTC)

Input :

A binary tree T=(V,E) with a weight function w:E×[k]×[k]→[c].

Question :

Is there a minimal convex coloring γ:V→[k] with w(γ)≤c?

For a given coloring γ, use pce(γ) to denote the number of poly-chromatic edges, i.e. the number of edges (y,z)∈E(T) such that φ(y)≠φ(z).

Lemma 11

The instance of MCBTC is a yes-instance if and only if there exists a k-coloring γ such that pce(γ)=k−1, and w(γ)≤c.

Proof

In a tree, the fact that γ gives k monochromatic connected components is equivalent with pce(γ)=k−1 since after contracting all monochromatic edges, we again obtain a tree. □

Using Lemma 11, we can reduce our problem to determining the existence of a minimal k-coloring with k−1 polychromatic edges. We will use Theorem 1: For notational convenience add a vertex to T, make it adjacent to an arbitrarily chosen other vertex of T and let the added vertex be the root of T. For the added edge, set all corresponding weights to 0. Define the universe U={γ:V→[k]∣pce(γ)=k−1∧w(γ)≤c}. Define the requirement space to be [k] with for each 1≤vk a requirement P v being all elements γ of U with vγ(V). Then |⋂ v∈[l] P v |>0 if and only if the current instance is a yes-instance by Lemma 11. Define \(\mathcal {C}_{F}(s,W,g,p)\) as all k-colorings γ of T[s] with pce(γ)=g+[γ(rt(T))≠p], γ(V(T[s]))∩F=∅ and w(γ)≤W and let \(c_{F}(s,W,g,p)=|\mathcal{C}_{F}(s,W,g,p)|\). Also note that \(|\bigcap_{v \in F}\overline{P_{v}}|c_{F}(\mathtt{rt}(T),c,k-1,1)\) (the color of the root is not relevant here so we just set it to 1). It remains to show how to compute c F (rt(T),c,k−1,p):

Lemma 12

figure e

where

(11)
(12)

Proof

For Case 10a, the only possible valid coloring is the one where γ(s)=γ(pa(s)). For Case 10b, any coloring γ with γ(s)≠γ(pa(s)) and low enough cost is counted. For Case 10c, no such a coloring exists since a leaf is only adjacent to one edge. For Case 10d, s is not a child and hence has two children s 1 and s 2. It is not too hard to see that the term in (11) is the number of colorings in \(\mathcal{C}_{F}(s,W,g,p)\) where γ(s)=γ(pa(s)) and that the term in (12) is the number of colorings in \(\mathcal{C}_{F}(s,W,g,p)\) where γ(s)≠γ(pa(s)). □

Theorem 9

The Convex Tree Coloring problem can be solved in \(\mathcal {O}^{\star}(2^{k}c)\) time and \(\mathcal{O}^{\star}(c)\) space.

Proof

By Lemma 11 and the discussion before it we can reduce Convex Tree Coloring to finding a minimal k-coloring γ with k−1 polychromatic edges and w(γ)≤c. This can be solved using the IE-formulation as discussed above. The simplified problem can be solved in \(\mathcal{O}^{\star}(c^{2})\) time and \(\mathcal{O}^{\star}(c)\) using Lemma 1 and using standard Fast Fourier Transform techniques this can be reduced to \(\mathcal{O}^{\star}(c)\) time and space. The theorem then follows from Theorem 1. □

7 Counting Perfect Matchings

In this section we will count the number of perfect matchings in a graph G=(V,E). We let |V|=2n. First, we arbitrarily partition the vertex set V into A and B (thus, AB=V, and AB=∅), with |A|=|B|=n. Let an l-matching of G be a perfect matching ME such that |{eM:|eA|=1}|=l, i.e. a perfect matching containing exactly l edges with exactly one endpoint in A.

We will need the following simple lemma:

Lemma 13

([18], Lemma 4.10; [1], Theorem 4)

Given an independent set SV, the number of perfect matching of G can be computed in \(\mathcal{O}^{\star}(2^{2n-|S|})\).

We will first give two algorithms that we combine later. Both algorithms solve the same problem, but with different running times. Afterwards we show how to combine the two to obtain the main result of this section.

Lemma 14

l-matchings can be counted in \(\mathcal{O}^{*}\bigl(\binom{n}{ l}2^{n}\bigr)\) time and polynomial space.

Proof

Given a perfect matching M, define

$$L(M) = \bigl\{ a \in A : \exists(a,b) \in M \wedge b \in B \bigr\}.$$

That is, L(M) is the set of all vertices in A that are matched with a vertex in b in M. Then, if we define f(X) as the number of perfect matchings M such that L(M)=X, then we can compute the number of l-matchings according to

$$ \#l\hbox{-matchings} = \sum_{X \in {{A }\choose {l}}} f(X).$$
(13)

Define g(X,B) as the number of perfect matchings in the graph obtained from G[XB] by making X into an independent set. Then f(X)=pm(AX)g(X,B) where pm(AX) is the number of perfect matching in G[AX]. Using Lemma 13 both pm(AX) and g(X,B) can be computed in polynomial space and time \(\mathcal {O}^{\star}(2^{n -l})\) and \(\mathcal{O}^{\star}(2^{n})\) respectively. Hence the lemma follows. □

We proceed to the next algorithm:

Lemma 15

l-matchings can be counted in \(\mathcal{O}^{*}\bigl({n\choose\frac{n}{2} + \frac{l}{2}}2^{n}\bigr)\) time and polynomial space.

Proof

Arbitrarily choose a total ordering ≺ on A and use the shorthand \(k=\frac{n+l}{2}\). We will use Theorem 1: Define the universe U to be

$$U = \Bigl\{ \big( (u_i,v_i)\big)_{i \leq k} \in\big((A \times V)\cap E\big)^{k} \bigm| \forall i < j: u_i \prec u_j \Bigr\} \times E^{n-k}$$

and the requirement space to be V. Define P v to be

$$\Big\{ \big((u_1,v_1),\ldots,(u_n,v_n)\big) \in U \bigm| v \in\{u_1,\ldots,u_n\} \cup\{v_1,\ldots,v_n\} \Big\}$$

for each vV. We claim that |⋂ vV P v | is \(2^{n}(\frac {(n-l)}{2})!\) times the number of l-matchings. To see this, notice that for every l-matching there are exactly \(2^{n}(\frac{n-l}{2})!\) elements in \(|\bigcap_{v \in F}\overline{P_{v}}|\) obtained by all permutations of the \(\frac{n-l}{2}\) edges contained in G[B] and then flipping the order of the vertices of the edge. Moreover, every element in \(|\bigcap_{v \in F}\overline{P_{v}}|\) can obtained in this way from exactly one l-matching.

Thus, combining Theorem 1 with the above we obtain that the number of l-matchings is

$$ \frac{1}{2^{n}(\frac{n-l}{2})!} \sum_{F \subseteq V} (-1)^{|F|}\biggl|\,\bigcap_{v \in F}\overline{P_v}\biggr|.$$
(14)

We proceed by trimming (see also [6]): Observe that \(|\bigcap_{v \in F}\overline{P_{v}}|=0\) if |FA|>(nk) since u 1,…,u k A are distinct for any ((u 1,v 1),…,(u n ,v n ))∈U. Hence for evaluating (14), we can restrict ourselves to sum over FV such that |FA|≤(nk). The number of FV such that |FA|≤(nk) is \(\mathcal{O}^{\star }({n \choose n-k}2^{n})=\mathcal{O}^{\star}({n \choose k}2^{n})\). Hence to prove the lemma, it suffices to show that \(|\bigcap_{v \in F}\overline{P_{v}}|\) can be computed in polynomial time for a fixed F.

Let {a 1,…,a n } be obtained by sorting A with respect to the total ordering ≺. Define

$$p_F(r,m) = \Big\{ \big((u_i,v_i)\big)_{i \leq m} \in\big(\big((A\setminus F) \times(V \setminus F)\big) \cap E\big)^{m} \bigm|\forall i < j: u_i \prec u_j \prec a_r \Big\}.$$

Then we have \(|\bigcap_{v \in F}\overline{P_{v}}|=p_{F}(n,k)|E(G[B\setminus F])|^{n-k}\) and it is easy to see that p F (n,k) can be computed according to

figure f

hence p F (n,k) and \(|\bigcap_{v \in F}\overline{P_{v}}|\) can be computed in polynomial time and the lemma follows. □

Theorem 10

The number of perfect matchings of a graph G can be computed in \(\mathcal{O}^{\star}(1.95^{|V(G)|})\) time and polynomial space.

Proof

Sum over all 0≤ln over the number of l-matchings computed by the algorithm of minimum running time among the algorithms of Lemmas 14 and 15. The running time of this algorithm is

$$ \max_{0 \leq l \leq n} \min \left\{ \binom{n }{ l}2^{n},\binom{n}{\frac{1}{2}n + \frac{1}{2}l}2^{n} \right\} = \max_{0 \leq l \leq n}\min \left\{ \binom{n }{ l},\binom{n }{\frac{1}{2}n + \frac{1}{2}l}\right\} 2^{n}.$$
(16)

Since \(0 \leq p \leq p' \leq\frac{n}{2}\) implies \(\binom{n }{ p} \leq \binom{n }{p'}\), (16) is maximized if \(\min\{ l, \frac{n}{2} - \frac{l}{2} \}\) is maximized, which is at \(l=\frac {n}{3}\). Hence (16) is equal to \(\binom{n}{ \frac {n}{3}}2^{n} = \mathcal{O}^{\star}(1.89^{n}2^{n})\), where the latter is due to standard approximations (see for example Lemma 4 of [7]). □

8 Further Remarks

It is worth mentioning that, as the proofs in this paper might suggest, the results of this paper admit a generalization. The study of such a generalization was initiated in the conference version of this work and finally given in [17]. In the latter Theorem 3 is also improved by giving a \(\mathcal{O}^{\star }(2^{k}c)\) time and polynomial space algorithm.