Faster algorithms for counting subgraphs in sparse graphs

Given a k-node pattern graph H and an n-node host graph G, the subgraph counting problem asks to compute the number of copies of H in G. In this work we address the following question: can we count the copies of H faster if G is sparse? We answer in the affirmative by introducing a novel tree-like decomposition for directed acyclic graphs, inspired by the classic tree decomposition for undirected graphs. This decomposition gives a dynamic program for counting the homomorphisms of H in G by exploiting the degeneracy of G, which allows us to beat the state-of-the-art subgraph counting algorithms when G is sparse enough. For example, we can count the induced copies of any k-node pattern H in time \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$2^{O(k^2)} O(n^{0.25k + 2} \log n)$$\end{document}2O(k2)O(n0.25k+2logn) if G has bounded degeneracy, and in time \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$2^{O(k^2)} O(n^{0.625k + 2} \log n)$$\end{document}2O(k2)O(n0.625k+2logn) if G has bounded average degree. These bounds are instantiations of a more general result, parameterized by the degeneracy of G and the structure of H, which generalizes classic bounds on counting cliques and complete bipartite graphs. We also give lower bounds based on the Exponential Time Hypothesis, showing that our results are actually a characterization of the complexity of subgraph counting in bounded-degeneracy graphs.

This bound has a polynomial dependence on n better than Nešetřil-Poljak whenever r = O(n 0.221 ) , and in any case (that is, even if = 2 ) whenever r = O(n 0.056 ) . In particular, we have a 2 O(k 2 ) ⋅ O(n 0.625k+1 log n)-time algorithm when r = O (1) . These are the first improvements over the Nešetřil-Poljak algorithm for graphs with small degeneracy or small average degree.
As a second result, we give improved bounds for some classes of patterns. The first is the class of quasi-cliques, a typical target pattern for social networks [5][6][7][31][32][33]. We prove: This generalizes the classic O(d k−1 n) bound for counting cliques by Chiba and Nishizeki [10], at the price of an extra factor 2 O( +k log k) O(log n) . Next, we consider complete quasi-multipartite graphs: This generalizes an existing O(d 3 2 2d n) bound for counting the non-induced copies of complete (maximal) bi-partite graphs [14], again at the price of an extra factor 2 O(k log k) log n. Table 1 summarizes our upper bounds. We remark that our algorithms work for the colored versions of the problem (count only copies of H with prescribed vertex and/or edge colors) as well as the weighted versions of the problem (compute the total node or edge weight of copies of H in G). This can be obtained by a straightforward adaptation of our homomorphism counting algorithms.

Techniques
The bounds of Sect. 1.1.1 are instantiations of a single, more general result. This result is based on a novel notion of width, the dag treewidth (H) of H, which captures the relevant structure of H when counting its copies in a d-degenerate graph. In a simplified form, the bound is the following:  Let us briefly explain this result. The heart of the problem is computing hom(H, G) ; once we know how to do this, we can obtain sub(H, G) and ind(H, G) via inclusion-exclusion arguments at the price of an extra multiplicative factor f(k), like in [3,11]. To compute hom(H, G) , we give G an acyclic orientation with maximum outdegree d. Then, we take every possible acyclic orientation P of H, and compute hom(P, G) where by hom(P, G) we mean the number of homomorphisms from P to G that respect the orientations of the arcs. Note that the number of such homomorphisms can be n Ω(k) even if G has bounded degeneracy (for example, if P is an independent set), so we cannot list them explicitly. At this point we introduce our technical tool, the dag tree decomposition of P. This is a tree T that captures the relevant reachability relations between the nodes of P. Given T, one can compute hom(P, G) via dynamic programming in time f (k) ⋅ O(d k− (T) n (T) log n) , where (T) ∈ {1, … , k} is the width of T. The dynamic program computes hom(P, G) by combining carefully the homomorphism counts of certain subgraphs of P. The dagtreewidth (H) , which is the parameter appearing in the bound of Theorem 5, is the maximum width of the optimal dag tree decomposition of any acyclic orientation P of any graph obtainable by identifying nodes of and/or adding edges to H (this arises from the inclusion-exclusion arguments). With this, our technical machinery is complete. To obtain the bounds of the previous paragraph, we show how to compute efficiently dag tree decompositions of low width, and apply a more technical version of Theorem 5.
We conclude by complementing Theorem 5 with a lower bound based on the Exponential Time Hypothesis. This lower bound shows that in the worst case the dag-treewidth (H) cannot be beaten, and therefore our decomposition captures, at least in part, the complexity of counting subgraphs in d-degenerate graphs.

Theorem 6
Under the Exponential Time Hypothesis [23] , no algorithm can compute sub(H, G) or ind(H, G) in time f (d, k) ⋅ n o( (H)∕ log (H)) for all H.

Preliminaries and notation
Both G = (V, E) and H = (V H , E H ) are simple graphs, possibly disconnected. For any subset V ′ ⊆ V we denote by G[V � ] the subgraph of G induced by V ′ ; the same notation applies to any graph.
We write ∶ H → G to highlight the edges that preserves. When H and G are oriented, must preserve the direction of the arcs. If is injective then we have an injective homomorphism. We denote by hom(H, G) and inj(H, G) the number of homomorphisms and injective homomorphisms from H to G. To avoid confusion, we will use the symbol to denote maps that are not necessarily homomorphisms. The symbol ≃ denotes isomorphism.
is an induced copy. We denote by sub(H, G) and ind(H, G) the number of copies and induced copies of H in G; we may omit G if clear from the context. When we give an acyclic orientation to the edges of H, we denote the resulting dag by P. All the notation described above applies to directed graphs in the natural way.
The degeneracy of G is the smallest integer d such that there is an acyclic orientation of G with maximum outdegree bounded by d. Such an orientation can be found in time O(|E|) by repeatedly removing from G a minimum-degree node [27]. From now on we assume that G has this orientation. Equivalently, d is the smallest integer that bounds from above the minimum degree of every subgraph of G.
We assume the following operations take constant time: accessing the i-th arc of any node u ∈ V , and checking if (u, v) is an arc of G for any pair (u, v). Our upper bounds still hold if checking an arc takes time O(log n) , which can be achieved via binary search if we first sort the adjacency lists of G. The log n factor in our bounds appears since we assume logarithmic access time for our dictionaries, each of which holds O(n k ) entries. This factor can be removed by using dictionaries with worstcase O(1) access time (e.g., hash maps), at the price of obtaining probabilistic/amortized bounds rather than deterministic ones.
Finally, we recall the tree decomposition and treewidth of a graph. For any two nodes X, Y in a tree T, we denote by T(X, Y) the unique path between X and Y in T.
Definition 1 (see [13], Ch. 12.3) Given a graph G = (V, E) , a tree decomposition of G is a tree T = (V T , E T ) such that each node X ∈ V T is a subset X ⊆ V , and that: 2 The width of a tree decomposition T is t(T) = max X∈V T |X| − 1 . The treewidth t(G) of a graph G is the minimum of t(T) over all tree decompositions T of G.

Related work
As discussed above, the fastest algorithm known for computing ind(H, G) is the one by Nešetřil and Poljak [29] that runs in time O(n ⌊ k 3 ⌋+(k mod 3) ) where is the matrix multiplication exponent. With the current bound ≤ 2.373 , this running time is in O(n 0.791k+2 ) . Unfortunately, the algorithm is based on fast matrix multiplication, which makes it oblivious to the sparsity of G.
Under certain assumptions on G, faster algorithms are known. If G has bounded maximum degree, Δ = O(1) , then we can compute ind(H, G) in time c k ⋅ O(n) for some c = c(Δ) via multivariate graph polynomials [30]. If G has treewidth t(G) ≤ k , and we are given a tree decomposition of G of such width, then we can compute ind(H, G) in time 2 O(k log k) O(n) ; see Lemma 18.4 of [27]. When G is planar, we obtain an f (k) O(n) algorithm where f is exponential in k [15]. All these assumptions are stronger than bounded degeneracy, and the techniques cannot be extended easily. A more general class that captures all these cases is that of nowhere-dense graphs 1 3 [28], for which there exist fixed-parameter-tractable subgraph counting algorithms [22]. Nowhere dense graphs however do not include all bounded degeneracy graphs or all graphs with bounded average degree.
Even assuming G has bounded degeneracy, algorithms faster than Nešetřil-Poljak are known only when H belongs to special classes. The earliest result of this kind is the classic algorithm by Chiba and Nishizeki [10] to list all k-cliques in time O(d k−1 n) . Eppstein showed that one can list all maximal cliques in time O(d3 d∕3 n) [16] and all non-induced complete bipartite subgraphs in time O(d 3 2 2d n) [14]. These algorithms exploit the degeneracy ordering of G in a way similar to ours. In fact, our techniques can be seen as a generalization of [10] that takes into account the structure of H. We note that a fundamental limitation of [10,14,16] is that they list all the copies of H, which for a generic H might be Θ(n k ) even if G has bounded degeneracy (for example if H is the independent set). In contrast, we list the copies of subgraphs H, and combine them to infer the number of copies of H. To be more precise, we list the homomorphisms of H, which is another difference we have with [10,14,16] and a point we have in common with previous work [11].
Regarding our "dag tree decomposition", it is inspired by the standard notion of tree decomposition of a graph, and it yields a similar dynamic program. Yet, the similarity between the two decompositions is rather superficial; indeed, our dagtreewidth can be O(1) when the treewidth is Ω(k) , and vice versa. Our decomposition is unrelated to the several notions of tree decomposition for directed graphs already known [19]. Finally, our lower bounds are novel; no general lower bound in terms of d and of the structure of H was available before.

Manuscript organisation
In Sect. 2 we build the intuition with a gentle introduction to our approach. In Sect. 3 we give our dag tree decomposition and the dynamic program for counting homomorphisms. In Sect. 4 we show how to compute good dag tree decompositions. Finally, in Sect. 5 we prove the lower bounds.

Exploiting degeneracy orientations
We build the intuition behind our approach, starting from the classic algorithm for counting cliques by Chiba and Nishizeki [10]. The algorithm begins by orienting G acyclically so that max v∈G d out (v) ≤ d , which takes time O(|E|). With G oriented acyclically, we take each v ∈ G in turn, enumerate every subset of (k − 1) out-neighbors of v, and check its edges. In this way we can explicitly find all k-cliques of G in time O(k 2 d k−1 n) . Observe that the crucial fact here is that an acyclically oriented clique has exactly one source, that is, a node with no incoming arcs. We would like to extend this approach to an arbitrary pattern H. Since every copy of H in G appears with exactly one acyclic orientation, we take every possible acyclic orientation P of H, count the copies of P in G, and sum all the counts. Thus, the problem reduces to counting the copies of an arbitrary dag P in our acyclic orientation of G.
Let us start in the naive way. Suppose P has s sources. Fix a directed spanning forest F of P. This is a collection of s directed disjoint trees rooted at the sources of P (arcs pointing away from the roots). Clearly, each copy of P in G contains a copy of F. Hence, we can enumerate the copies of F in G, and for each one check if it is a copy of P. To this end, first we enumerate the O(n s ) possible s-tuples of V to which the sources of P can be mapped. For each such s-uple, we enumerate the possible mappings of the remaining k − s nodes of the forest. This can be done in time O(d k−s ) by a straightforward extension of the out-neighbor listing algorithm above. Finally, for each mapping we check if its nodes induce P in G, in time O(k 2 ) . The total running time is O(k 2 d k−s n s ) . Unfortunately, if P is an independent set then s = k and the running time is O(k 2 n k ) , so we have made no progress over the naive algorithm.
At this point we introduce our first idea. For reference we use the toy pattern P in Fig. 1. Instead of enumerating the copies of P in G, we decompose P into two pieces, P(1) and P (3,5). Here, P(1) denotes the subgraph of P reachable from 1 (that is, the transitive closure of 1 in P). The same for P(3) and P(5), and we let P(3, 5) = P(3) ∪ P (5) . Now we count the copies of P(1), and then the copies of P (3,5), hoping to combine the result in some way to obtain the count of P. To simplify the task, we focus on counting homomorphisms rather than copies (see below). Thus, we want to compute hom(P, G) by combining hom(P(1), G) and hom(P(3, 5), G). Now, clearly, knowing hom(P(1), G) and hom(P (3,5), G) is not sufficient to infer hom(P, G) . Thus, we need to solve a slightly more complex problem. For every pair x, y ∈ V(G) , let ∶ {2, 6} ↦ V(G) be the map given by (2) = x and (6) = y . We let hom(P, G, (x, y)) be the number of homomorphisms of P in G whose restriction to {2, 6} is . By a counting argument one can immediately see that: Thus, to compute hom(P, G) we only need to compute hom(P, G, ) for all possible . Now, define hom(P(1), G, ) and hom(P (3,5), G, ) with the same meaning as above. A crucial observation is that {2, 6} , the domain of , is precisely the set of nodes in P(1) ∩ P (3,5) . It is not difficult to see that this implies: Thus, now our goal is to compute hom(P(1), G, ) and hom(P (3,5) (2) hom(P, G, ) = hom(P(1), G, ) ⋅ hom(P(3, 5), G, ) Fig. 1 Toy example: an acyclic orientation P of H = C 6 , decomposed into two pieces the technique above, and for each such P(1) we increment a counter associated to ( P(1) (2), P(1) (6)) in a dictionary with default value 0. Thus, we obtain hom(P(1), G, ) for all ∶ {2, 6} → V . Since P(1) has one source, we enumerate O(k 2 d k−1 n) maps. If the dictionary takes time O(log n) to access an entry, the total running time is O(k 2 d k−1 n log n) . The same technique applied to P(3, 5) yields a running time of O(k 2 d k−2 n 2 log n) , since P(3, 5) has two sources. Finally, we apply Eq. 1 by running over all entries in the first dictionary and retrieving the corresponding value from the second dictionary. The total running time is O(k 2 d k−2 n 2 log n) , while enumerating the homomorphisms of P would have required time O(k 2 d k−3 n 3 ).
Let us abstract the general approach from this toy example. We want to decompose P into a set of pieces P 1 , P 2 , … with the following properties: (i) Each piece P i has a small number of sources s(P i ) , and (ii) we can obtain hom(P, G, ) by combining the homomorphism counts of the P i . This is achieved by the dag tree decomposition, which we introduce in Sect. 3. Like the tree decomposition for undirected graphs, the dag tree decomposition leads to a dynamic program to compute hom(P, G).

DAG tree decompositions
Let P = (V P , A P ) be a directed acyclic graph. We denote by S P , or simply S , the set of nodes of P having no incoming arc. These are the sources of P. We denote by V P (u) the transitive closure of u in P, i.e. the set of nodes of P reachable from u, and we let P(u) = P[V P (u)] be the corresponding subgraph of P. For a subset of sources B ⊆ S we let V P (B) = ∪ u∈B V P (u) and P(B) = P[V P (B)] . Thus, P(B) is the subgraph of P induced by all nodes reachable from B. We call B a bag of sources. We can now formally introduce our decomposition. Definition 2 (Dag tree decomposition) Let P = (V P , A P ) be a dag. A dag tree decomposition (d.t.d.) of P is a (rooted) tree T = (B, E) with the following properties: One can see the similarity with the tree decomposition of an undirected graph (Definition 1). However, our dag tree decomposition differs crucially in two aspects. First, the bags are subsets of S rather than subsets of V P . This is because the time needed to list the homomorphisms between P(B i ) and G is driven by n |B i | . Second, the path-intersection property (3) concerns the pieces reachable from the bags rather than the bags themselves. The reason is that, to combine the counts of two pieces together, their intersection must form a separator in P (similarly to the tree decomposition of an undirected graph). The dag tree decomposition induces the following notions of width, used throughout the rest of the article.

Definition 3
The width of T is (T) = max B∈B |B| . The dag treewidth (P) of P is the minimum of (T) over all dag tree decompositions T of P.
Clearly (P) ∈ {1, … , k} for any k-node dag P. Figure 2 shows a pattern P together with a d.t.d. of width 1. We observe that (P) has no obvious relation to the treewidth t(H) of H; see the discussion in Sect. 3.2.

Counting homomorphisms via dag tree decompositions
For any B ∈ B let T(B) be the subtree of T rooted at B. We let Γ[B] be the downclosure of B in T, that is, the union of all bags in T(B). Consider P(Γ[B]) , the subgraph of P induced by the nodes reachable from Γ[B] (note the difference with P(B), which contains only the nodes reachable from B). We compute hom(P(Γ[B]), G) in a bottom-up fashion over all B, starting with the leaves of T and moving towards the root. This is similar to the dynamic program given by the standard tree decomposition (see [18]).
As anticipated, we actually compute hom(P(Γ[B]), ) , the number of homomorphisms that extend a fixed mapping . We need the following concept: be two subgraphs of P, and let 1 ∶ P 1 → G and 2 ∶ P 2 → G be two homomorphisms. We say 1 and 2 respect each other if 1 Given some , we denote by hom(P 1 , G, 2 ) the number of homomorphisms from P 1 to G that respect 2 . We can now present our main algorithmic result.  The proof of Theorem 7 is given in the next subsection. Before continuing, let f T (k) be an upper bound on the time needed to compute a d.t.d. of minimum width with at most 2 k bags for a pattern on k nodes. We can show that such a d.t.d. always exists: Lemma 1 Any k-node dag P has a minimum-width d.t.d. on at most 2 k bags.
Proof We show that, if a d.t.d. T = (B, E) has two bags containing exactly the same sources, then one of the two bags can be removed. This implies that there exists a minimum-width d.t.d. where every bag contains a distinct source set, which therefore has at most 2 k bags. Suppose indeed T contains two bags X and X ′ formed by the same subset of sources. Let B be the neighbor of X on the unique path T(X, X � ) . Let T * = (B * , E * ) be the tree obtained from T by replacing the edge {B � , X} with {B � , B} for every neighbor B ′ ≠ B of X and then deleting X. Clearly |B * | = |B| − 1 , and properties (1) and (2) of Definition 2 are satisfied. Let us then check property (3). Consider a generic path T * (B 1 , B 2 ) and look at the corresponding path In this case the property holds for any bag in Then, as an immediate corollary of Theorem 7, we have: Theorem 8 will be used in Sect. 3.2 to prove the bounds for our original problem of counting the copies of H via inclusion-exclusion arguments.

Proof of Theorem 7
The algorithm behind Theorem 7 is similar to the one for counting homomorphisms using a tree decomposition. To start, we prove that our dag tree decomposition enjoys a separator property similar to the one enjoyed by tree decompositions.
. Thus, by point (3) of Definition 2, we have u ∈ V P (B) . This contradicts the third inclusion, In both cases however we have u ∈ V P (B) : in the first case this holds since u is reachable from u ′ , and in the second case since B ∈ T(B i , B � j ) and by point (3)  Next, we show that each homomorphism of P(Γ [B]) is the juxtaposition (definition below) of some homomorphism B of B and some homomorphisms Note that the juxtaposition is always well-defined and unique, since the i respect each other and the image of every u is determined by at least one among 1 , … , .  Proof We prove the bound on the enumeration time; the proof gives immediately also the bound on |Φ| .

Lemma 3 Let T be a d.t.d. and let B 1 , … , B l be the children of B in T. Fix any
is a directed tree rooted at u i (arcs pointing away from the root). Consider any ∈ Φ , and let i be its restriction to V i .
. Together with the check on the arcs, this gives a total running time of O(k 2 d k−b n b ) for enumerating Φ , as desired. To enumerate Φ T i , we take each v ∈ G and enumerate all i ∈ Φ T i such that i (s i ) = v . To this end note that, once we have fixed i (x) , for each arc (x, y) ∈ T i we have at most d choices for (y) . Thus we can enumerate all i ∈ Φ T i that map s i to v in time d |V i |−1 . The total time to enumerate Φ T i is therefore O(d |V i |−1 n) , as claimed. ◻ We can now describe our dynamic programming algorithm, HomCount, to compute hom(P (Γ[B]), G) . Given a d.t.d. T of P, the algorithm goes bottom-up from the leaves of T towards the root, combining the counts using Lemma 3. For readability, we write the algorithm in a recursive fashion. We prove:

Inclusion-exclusion arguments and the dag-treewidth of undirected graphs
We turn to computing hom(H, G) , sub(H, G) and ind(H, G) . We do so via standard inclusion-exclusion arguments, using our algorithm for computing hom(P, G) as a primitive. To this end we shall define appropriate notions of width for undirected pattern graphs. Let Σ(H) be the set of all dags P that can be obtained by orienting H acyclically. Let Θ(H) be the set of all equivalence relations on V H (that is, all the partitions of V H ), and for ∈ Θ(H) let H∕ be the pattern obtained from H by identifying equivalent nodes according to and removing loops and multiple edges. Let D(H) be the set of all supergraphs of H on the node set V H , including H.

The claim still holds if we replace 1 , 2 , with upper bounds, and f T (k) with the time needed to compute a d.t.d. on 2 O(k log k) bags that satisfies those upper bounds.
Proof We prove the three bounds in three separated steps. The last claim follows straightforwardly.
From dags to undirected patterns. Let H be any undirected pattern. First, note that: Let indeed Φ(H) = { ∶ H → G} be the set of homomorphisms from H to G. Similarly, for any P ∈ Σ(H) define Φ(P) = { P ∶ P → G} (note that P must preserve the direction of the arcs). Then, there is a bijection between Φ(H) and ∪ P∈Σ(H) Φ(P) . Consider indeed any ∈ Φ(H) . Let be the orientation of H that assigns to {u, v} ∈ E H the orientation of { (u), (v)} in G, and let P = H . Then is a homomorphism of  [26]. This proves the second bound of the theorem. To compute ind(H, G) , we take every H � ∈ D(H) , compute inj(H � , G) , and apply Eq. 11. Since |D(H)| ≤ 2 k 2 , the third bound of the theorem follows. ◻ The algorithmic part of our work is complete. We shall now focus on computing good dag tree decompositions, so to instantiate Theorem 9 and obtain the upper bounds of Sect. 1.1.

Computing good dag tree decompositions
In this section we show how to compute dag tree decompositions of low width. First, we show that for every k-node dag P we can compute in time 2 O(k) a dag tree decomposition T that satisfies (T) ≤ ⌊ k 4 ⌋ + 2 . This result requires a nontrivial proof. As a corollary, we prove Theorems 1 and 2. Second, we give improved bounds for cliques minus edges; as a corollary, we prove Theorem 3. Third, we give improved bounds for complete multipartite graphs plus edges; as a corollary, we prove Theorem 4. number of H, which is of independent interest. This implies that the trivial decomposition on one bag has width that is asymptotically optimal, since in any orientation of H, the set of sources is an independent set. To proceed, we need some additional notation. For a dag P, we say v ∈ V P is a joint if it is reachable from at least two sources, i.e., if v ∈ V P (u) ∩ V P (u � ) for some u, u � ∈ S with u ≠ u ′ . Let J be the set of joints of P. We write J(u) for the set of joints reachable from u, and for any X ⊆ V P we let J(X) = ∪ u∈X J(u) . Similarly, we denote by S(y) the sources from which y is reachable, and we let S(X) = ∪ u∈X S(u).

A bound for all patterns
This subsection is devoted to prove: By combining Theorem 10 with Definition 6 and Theorem 9, we obtain as a corollary Theorem 1. The proof of Theorem 10 is divided in four steps, as follows. First (step 1), we remove the "easy" pieces of P; this can break P into several connected components, and we show that their d.t.d.'s can be composed into a d.t.d. for P. Next, we show that if the i-th component has k i nodes, then it admits a d.t.d. of width k i 4 + 2 . This requires to "peel" the component to remove its tree-like parts (step 2) and decomposing the remainder using a reduction to standard tree decompositions (step 3). Finally, we wrap up our results and conclude the proof (step 4).
Throughout the proof, the relevant structure of P is encoded by a graph that we call the skeleton of P, defined as follows. Figure 3 gives an example. Note that Λ(P) does not contain nodes that are neither sources nor joints, as they are irrelevant to the d.t.d.. Note also that computing Λ(P) takes time O( poly (k)).
Let us now delve into the proof. For any node x, we denote by d x the current degree of x in the skeleton.
, where k = |V P | and e = |A P |.
Proof For the first term of the min() , note that at each step we remove at least 4 nodes from Λ (j) and add one node to B (j) . Hence For the second term of the min() , we show that the set of nodes V (j) Λ (u) removed at step j identifies at least 4 unique arcs of P. To this end, consider the sub-pattern P (j) = P ⧵ P(B (j) ) containing all nodes not reachable from B (j) . Note that Λ (j) is the skeleton of P (j) . Indeed, if v ∈ J (j) then v is not reachable from any source in B (j) , since otherwise v would have been removed before step j. Thus, at step j we are removing at least 3 joint nodes of P (j) (u) . Therefore, P (j) (u) contains at least three arcs pointing to its joints. In addition, by definition, the joints of P (j) (u) must be reachable from some node in P (j) ⧵ P (j) (u) . Thus there is an arc from P (j) ⧵ P (j) (u) to a node of P (j) (u) , and this node is therefore a joint itself. Thus, P (j) contains at least 4 arcs pointing to the joints of P (j) (u) . Since the joints of P (j) (u) are then removed from P (j) , these arcs are counted only once. Hence Now, if B * = S , then T = ({B * }, �) is a d.t.d. of P whose width is (T) = |B * | . By Lemma 6, |B * | ≤ min( k 4 , e 4 ) , so Theorem 10 is proven. If instead B * ⊂ S , then Λ * has ≥ 1 nonempty connected components. For each i = 1, … , let Λ i = (S i ∪ J i , E i ) be the i-th component of Λ * . Let P * = P ⧵ P(B * ) , and let P i = P * (S i ) . Then, Λ i is the skeleton of P i ; this follows from the same argument used in the proof of Lemma 6. We shall now see that we can obtain a d.t.d. for P by arranging the d.t.d.'s of the P i into a tree, and adding B * to all bags.

Lemma 7 For each
Consider the tree T obtained as follows. The root of T is the bag B * , and the subtrees below B * are Proof The claims on (T) and |B| are straightforward. Let us check that Step 2: Peeling Λ i . We now remove the tree-like parts of Λ i . These include, for instance, sources that have only one reachable joint. For each such source, we create a dedicated bag which becomes the child of another bag that reaches the same joint. This removes a source without increasing the width of the decomposition.
The construction is recursive. Let P (0) i = P i and Λ (0) i . We will show that the tree T (0) returned by our recursive construction is a d.t.d. for P (0) i = P i . The base case is |S i . Consider the first one of these three cases that applies (if none of them does, then we stop): Then choose any such u, and we choose any Then choose any such v, let u be the unique source such that v ∈ J (j) i (u) , and let u ′ ≠ u be any source with J Then, we define T  (3), since B u is a leaf, we only need to check that J To this end note that, by the choice of u and u ′ , for any u �� ∈ S (j) . We repeat the entire procedure until we reach the base case, or until |S (j) i | > 1 and none of the three cases above holds, in which case we move to the next phase.
Before continuing, we make sure that the procedure above is well defined; we must guarantee that, in each of the three cases, the node u ′ exists. One can see that u ′ exists whenever |S (j) i | > 1 (which is true by hypothesis) and Λ (j) i is connected. To see that Λ (j) i is connected, note that if this was not the case then in some step h < j we removed a source u with d (h) u = 2 such that no other source u ′ has . However, this cannot happen by construction of the procedure.
Step 3: Decomposing the core Suppose the peeling phase stops at j = j * . Let i . We say P • i is the core of P i ; this is the part that determines the dag treewidth. Now, since Λ • i violates all three conditions of the peeling step, we have d • u = 2 for every source u and d • v ≥ 2 for every joint v. Thus Λ • i can be encoded as a simple graph. Formally, let To ease the discussion, for the edges we use u and e u interchangeably. Figure 4 gives an example. Note that Λ • i is the skeleton of P • i , since S • i are the sources of P • i and the degree bound above implies that each v ∈ J c is reachable from at least two sources of P • i . In what follows we let First, we show that four our purposes it is sufficient to find a d.t.d. of width at most Proof First, suppose that |V C i | ≤ 4 . Since all nodes of C i have degree at least 2, then C i contains a 4-cycle, and thus an edge cover B cov of size 2. We then build T • i by setting B cov as root, and B u = {u} for every u ∈ E C i ⧵ B cov as child of B cov . This is clearly a d.t.d. for P • i of width 2 < k i 4 + 2 , and thus (P • i ) < k i 4 + 2. Suppose instead that |V C i | ≥ 5 . Note that |E C i | ≥ |V C i | by construction of C i . One can check that these conditions imply Proof By Theorem 2 of [24], the treewidth of a graph G = (V, E) is at most |E| 5 + 2 . By Theorems 5.23-5.24 of [18], we can compute a minimum-width tree decomposition of an n-node graph in time O(1.7549 n ) . By Lemma 5.16 of [18], in time O(n) we can transform such a decomposition into one that contains at most 4n bags, leaving its width unchanged. Therefore, in time O(1.7549 k i ) we can build a tree decomposition D for C i with O(k i ) bags that satisfies t(D) ≤ Proof To simplify the notation let us write T, We first show how to build the tree T. The tedious part is proving it is a valid d.t.d. for P • i . The intuition is that D covers the edges of C i , which correspond to the sources of P • i . This gives a way to "convert" the bags of D into bags for . Note that, by construction, we must have put some u ′ with e u � = {v, z � } in B ′ and some u ′′ with e u �� = {v, z �� } in B ′′ , for some z � , z �� ∈ V i . Moreover, Y � ∩ {v, z � } ≠ � and Y �� ∩ {v, z �� } ≠ � , else we could not have u � ∈ B � and u �� ∈ B �� . Finally, bear in mind that v ∉ Y and u � , u �� ∉ B , otherwise v ∈ J(B) , contradicting the hypothesis. Now we consider three cases. We use repeatedly properties (2) and (3) of Definition 1.
Then z �� ∈ Y �� and u ′′ with e u �� = {v, z �� } is the edge chosen to cover z ′′ , else we would not put u �� ∈ B Y �� . Moreover there must be Ŷ ∈ D such that e u �� = {v, z �� } ⊆Ŷ . For the sake of the proof root D at Y, so Y ′ and Y ′′ are in distinct subtrees. If Ŷ and Y ′′ are in the same subtree then Y ∈ D(Y � ,Ŷ) , but v ∈ Y � ∩Ŷ and thus v ∈ Y , a contradiction. Otherwise Y ∈ D(Y �� ,Ŷ) , and since z �� ∈ Y �� ∩Ŷ then z �� ∈ Y and then u �� ∈ B(Y) , a contradiction.
Step 4: Assembling the tree Recall the sub-patterns P i obtained after the greedy bag construction (step 1). Let T i = (B i , E i ) be the d.t.d. for P i as returned by the recursive peeling and the core decomposition. Since the peeling phase only adds bags of size 1, then (T i ) = (T • i ) . Therefore, by Lemma 9, (T i ) ≤ ⌊ k i 4 ⌋ + 2 . Moreover, since each bag added in the peeling phase corresponds to a unique source, then |B i | = O(k i + |V(P i )|) = O(|V(P i )|).
Let now T = (B, E) be the d.t.d. for P obtained by assembling the trees T 1 , … , T as in Lemma 7. By Lemma 7 itself, (T) ≤ |B * | + max i=1,…, (T i ) , thus: Now, by Lemma 6 we know that P(B * ) has at least 4|B * | nodes and 4|B * | arcs. Similarly, since each Λ i has at least k i nodes and k i arcs, then P ⧵ P(B * ) has at least ∑ i=1 k i nodes and ∑ i=1 k i arcs. Then (T) ≤ ⌊ k 4 ⌋ + 2 and (T) ≤ ⌊ e 4 ⌋ + 2 , so (T) ≤ min(⌊ k 4 ⌋, ⌊ e 4 ⌋) + 2 . Moreover, . Finally, by Lemma 9 the time to build T i is O(1.7549 k i poly (k i )) , since the peeling phase clearly takes time poly (k i ) . The total time to build T is therefore O(1.7549 k poly (k)) . This concludes the proof of Theorem 10.

Lemma 12
If a k-node dag P has k 2 − edges, then in time O( poly (k)) one can compute a d.t.d. T for P on two bags such that (T) ≤ ⌈ 1 ∕2 + √ ∕2 ⌉.

Lower bounds
We prove Theorem 6, in a more technical form. Note that, since (H) = Θ( (H)) by Lemma  Let us now state the lower bound. Proof We reduce counting cycles in an arbitrary graph to counting a gadget pattern on k nodes and dag treewidth O(a(k)) in a d-degenerate graph.
First, fix a function d ∶ ℕ → ℕ such that d(k) ∈ Ω( k a(k) ) . Now consider a simple cycle on k 0 ≥ 3 nodes and any arbitrarily large k ≥ 3 . Our gadget pattern on k nodes is the following. For each edge e = uv of the cycle create a clique C e on d(k) − 1 nodes; delete e and connect both u and v to every node of C e . The resulting pattern H has k = k 0 d(k) nodes. Let us prove that (H) ≤ k 0 ; since k 0 = k d(k) ∈ O(a(k)) , this implies (H) = O(a(k)) as desired. Consider again the generic edge e = uv . In any acyclic orientation P of H, the set C e ∪ u induces a clique, and thus can contain at most one source. Applying the argument to all e shows that |S(P)| ≤ k 0 , hence (P) ≤ k 0 . This holds also if we add edges and/or identify nodes of P, hence (H) ≤ k 0 . Now consider a simple graph G 0 on n 0 nodes and m 0 edges. We replace each edge of G 0 as described above, which takes time O( poly (n 0 )) . The resulting graph G has n = m 0 (d − 1) + n 0 = O(dn 2 0 ) nodes and degeneracy d. Every k 0 -cycle of G 0 is univocally associated to a copy of H in G (note that every non-induced copy of H in G is induced too). Suppose we have an algorithm that computes ind(H, G) or sub(H, G) in time f (d, k) ⋅ n o( (H)∕ log (H)) . Since (H) ≤ k 0 , n = O(dn 2 0 ) , k = f 1 (d, k 0 ) , and d = f 2 (k 0 ) , for some functions f 1 , f 2 , then the running is time f (k 0 ) ⋅ (n 0 ) o(k 0 ∕ log k 0 ) . Invoking Theorem 11 concludes the proof. ◻

Conclusions
We have shown how, by introducing a novel tree-like decomposition for directed acyclic graphs, one can improve on the decades-old state-of-the-art subgraph counting algorithms when the host graph is sufficiently sparse. This decomposition seems to capture the structure of the problem, and may therefore be of independent interest. We leave open the (important) problem of finding a tight characterization of the class of patterns that are fixed-parameter tractable with respect to homomorphism, subgraph, and induced subgraph counting, when the complexity is parameterized by the degeneracy of the host graph. Our results represent one step, outlining properties (the boundedness of our notions of width) that are sufficient, but maybe not necessary. Indeed, our lower bounds only give evidence that such properties are necessary for counting induced or non-induced occurrences, and only for some classes of patterns. Finding a complete characterization, and therefore establishing a class pattern dichotomy for all three problems, would be an exciting development.