We investigate how efficiently a well-studied family of domination-type problems can be solved on bounded-treewidth graphs. For sets σ, ρ of non-negative integers, a (σ, ρ)-set of a graph *G* is a set *S* of vertices such that | *N* (*u*) ∩ *S*| ∈ σ for every *u* ∈ *S*, and | *N* (*v*) ∩ *S*| ∈ ρ for every *v ∉ S*. The problem of finding a (σ, ρ)-set (of a certain size) unifies standard problems such as INDEPENDENT SET, DOMINATING SET, INDEPENDENT DOMINATING SET, and many others.

For all pairs of finite or cofinite sets (σ, ρ), we determine (under standard complexity assumptions) the best possible value *c*_{σ,ρ} such that there is an algorithm that counts (σ, ρ)-sets in time *c*^{tw}_{σ,ρ} · *n*^{O(1)} (if a tree decomposition of width tw is given in the input). Let *s*_{top} denote the largest element of σ if σ is finite, or the largest missing integer +1 if σ is cofinite; *r*_{top} is defined analogously for ρ. Surprisingly, *c*_{σ,ρ} is often significantly smaller than the natural bound *s*_{top} + *r*_{top} + 2 achieved by existing algorithms [van Rooij, 2020]. Toward defining *c*_{σ,ρ}, we say that (σ,ρ) is *m*-structured if there is a pair (α,β) such that every integer in σ equals α mod *m*, and every integer in ρ equals β mod *m*. Then, setting

• *c*_{σ,ρ} = *s*_{top} + *r*_{top} +2 if (σ, ρ) is not m-structured for any m ≥ 2

• *c*_{σ,ρ} = max{*s*_{top},*r*_{top}} + 2 if (σ,ρ) is 2-structured, but not m-structured for any m ≥ 3, and *s*_{top} = *r*_{top} is even, and

• *c*_{σ,ρ} = max{*s*_{top}, *r*_{top}} + 1, otherwise

we provide algorithms counting (σ, ρ)-sets in time *c*^{tw}_{σ,ρ} · *n*^{O(1)}. For example, for the EXACT INDEPENDENT DOMINATING SET problem (also known as PERFECT CODE) corresponding to σ = {0} and ρ = {1}, this improves the 3^{tw} · *n*^{O(1)} algorithm of van Rooij to 2^{tw}· *n*^{O(1)}.

Despite the unusually delicate definition of *c*_{σ,ρ}, we show that our algorithms are most likely optimal, i.e., for any pair (σ, ρ) of finite or cofinite sets where the problem is non-trivial, and any ε > 0, a (*c*_{σ,ρ} — ε)^{tw} · *n*^{O(1)}- algorithm counting the number of (σ, ρ)-sets would violate the COUNTING STRONG EXPONENTIAL-TIME HYPOTHESIS (#SETH). For finite sets σ and ρ, our lower bounds also extend to the decision version, showing that our algorithms are optimal in this setting as well. In contrast, for many cofinite sets, we show that further significant improvements for the decision and optimization versions are possible using the technique of representative sets.

^{*} The full version of this work can be accessed at https://arxiv.org/abs/2211.04278. Research supported by the European Research Council (ERC) consolidator grant No. 725978 SYSTEMATICGRAPH.