• Aucun résultat trouvé

Counting Subgraph Isomorphisms

j=0

n n−j

O(1.2461j) =O(2.2461n).

Thus the chromatic number can be computed in timeO(2.2461n).

u t Exercise 4.15.Find a polynomial space algorithm to compute the domatic number of a given graph.

4.4 Counting Subgraph Isomorphisms

The HAMILTONIANPATHproblem studied in previous sections is a special case of a more general problem, namely the SUBGRAPHISOMORPHISMproblem.

In the SUBGRAPHISOMORPHISMproblem we are given two (directed or undi-rected) graphs F = (VF,EF) andG= (VG,EG). The question is whether G con-tains a copy ofF, or in other words, whetherGhas a subgraph isomorphic toF.

More formally, we say that a graphGhas a subgraphH= (VH,EH)isomorphic to F= (VF,EF), if there is a bijection f:VF→VHsuch that for every pair of vertices u,v∈VF, the vertices f(u)andf(v)are adjacent inHif and only if{u,v}is an edge ofF.

Fig. 4.2 GraphGhas two subgraphs isomorphic toF: the graph formed by the edges{a,b},{b,e}, {b,f}, and the graph formed by edges{c,e},{b,e},{e,f}.

To give an example, ifF is a (chordless) cycle onn=|VG| vertices, then the question whetherGhas a subgraph isomorphic toF is equivalent to the question if Gcontains a cycle passing through all its vertices, i.e. whetherGhas a Hamiltonian cycle. Another example is given in Fig.4.2.

While COLORINGis not a subgraph isomorphism problem, it can be reduced to SUBGRAPHISOMORPHISM. LetGbe the complement ofG, i.e. the graph on the same vertex set asG, and for every pairu6=vof vertices,{u,v}is an edge inGif

2 4

1 3

a

c e

b

f

F: G:

4.4 Counting Subgraph Isomorphisms 69 and only if{u,v}is not an edge ofG. ThenGcan be colored inkcolors if and only if the vertices ofGcan be covered bykdisjoint cliques. In other words,χ(G)≤k if and only ifGcontains as subgraph a graphFwhich is a disjoint union of cliques Kt1,Kt2,· · ·,Ktk, such thatt1+t2+· · ·+tk=n. See Fig.4.3.

Fig. 4.3 The complementGofGcontains a subgraph isomorphic toF, which is a disjoint union of cliquesK1,K1, andK2. Moreover, the number of vertices inFis equal to 1+1+2, the number of vertices inG. ThusGcan be colored with 3 colors.

In this section we discuss algorithms for counting the number of copies of a given graphFin a graphG, or in other words, counting the subgraphs isomorphic toF inG. This counting problem becomes hard even for very simple graphs F.

For example, when the number of vertices ofGis 2n, and the graphFis a disjoint union ofnedges, then the number of subgraphs ofGisomorphic toFis exactly the number of perfect matchings inG. Counting perfect matchings in a graph is a well known #P-complete problem.

To count perfect matching, Hamiltonian paths, and colorings in this chapter, we counted objects that were easier to deal with, and then used inclusion-exclusion. The same strategy can be applied to count isomorphic subgraphs, and the role of “sim-ple” objects is given to graphs with “easily” computable homomorphisms. From this perspective, graph homomorphism is a generic approach to several algorithmic results including Theorems4.4,4.5, and4.9. In what follows, we explain how graph homomorphisms can be used to prove Theorems4.5and4.9. In Sect.5.3, we use the ideas from this section about graph homomorphisms to count more complicated subgraphs in a graph than cycles and cliques.

LetF = (VF,EF)andG= (VG,EG)be two (directed or undirected) graphs. A homomorphismfromF toGis a mapping f fromVF toVG, that is f : VF→VG, such that if {u,v} ∈EF then {f(u),f(v)} ∈EG. Furthermore, if the mapping f is injective then f is called aninjective homomorphism. Let us note that there is an injective homomorphism fromFtoGif an only ifGcontains a subgraph isomorphic toF.

For example, for the graphs in Fig.4.2, the mapping f with f(2) =a, f(1) = f(3) =f(4) =bis a homomorphism fromFtoG. Furthermore the mappinggwith g(1) =a,g(2) =b,g(3) =e, and g(4) = f is an injective homomorphism from F toG. Let us note that in the case of an injective homomorphism, the image of F inGis isomorphic toF. For another example, if f is a homomorphism fromF

2 4

1 3

2 4

1 3

G:

G: F :

toG, and F is a clique on pvertices, then f(F)is also a clique on p vertices in G. If F = (v1,v2, . . . ,vp)is a path of length p−1, then (f(v1),f(v2), . . . ,f(vp)) is a walk of length p−1 inG. If f is an injective homomorphism from the path F= (v1,v2, . . . ,vp)toG, then the image ofFinGis also a path of lengthp−1.

We use hom(F,G), inj(F,G)and sub(F,G)to denote the number of homomor-phisms fromFtoG, the number of injective homomorphisms fromF toGand the number of distinct copies ofFinG, respectively. Let us remark that whileGhas a subgraph isomorphic toFif and only if there is an injective homomorphism fromF toG, the numbers inj(F,G)and sub(F,G)can be quite different. For example, take the graphFdepicted in Fig.4.2and the graphG=F. Of course, every graph is iso-morphic to itself, andGcontains exactly one copy ofF. However, there are several injective homomorphisms from F toF, also calledautomorphisms ofF. For the graphFin Fig.4.2there are 3!=6 injective homomorphisms. Each of these homo-morphisms has as a fixed point vertex 2 but the leaves of the tree are not necessarily fixed.

Let aut(F)be the number of automorphisms ofF, i.e. the number of injective homomorphisms fromF toF. The values inj(F,G)and sub(F,G)are related to each other by the following proposition which follows directly from the definitions of inj(F,G), sub(F,G), and aut(F).

Proposition 4.16.Let F and G be two graphs. Then sub(F,G) =inj(F,G) aut(F) .

The following is a general result relating the number of injective homomorphisms and the number of homomorphisms fromFtoGusing an inclusion-exclusion for-mula.

Theorem 4.17.Let F= (VF,EF)and G= (VG,EG)be two graphs with|VG|=|VF|.

Then

inj(F,G) =

W⊆VG

(−1)|W|hom(F,G\W).

Proof. Let us see how to obtain the proof of the theorem from Theorem4.2. Using the notation of Theorem4.2, the set of objects is the set of homomorphisms from F toG. For a homomorphism f and a vertexv∈VG we define the propertyQ(v) as the property that vdoes not belong to the image f(VF). In other words, f has propertyQ(v)if v6∈ ∪x∈VFf(x). Then for a subsetW ⊆VG,N(W)is the number of homomorphism f such that the image f(VF)does not contain a vertex fromW. Thus N(W) =hom(F,G\W). The number X is the number of homomorphisms possessing none of the propertiesQ(v),v∈VG. But this is exactly the number of injective homomorphisms and thusX=inj(F,G). Now the theorem follows from

(4.2). ut

How to use Theorem4.17 algorithmically? Assume that for some graphF we can compute the number of graph homomorphisms fromFto all the graphsG[W]in

4.4 Counting Subgraph Isomorphisms 71 timet(n), where|VF| ≤ |VG|andW ⊆VG. Then, as a consequence of Theorem4.17 , we can compute the value of inj(F,G)in timeO(2n·t(n))when|VF|=|VG|=n. A natural question arising here is to extend this to the case when the size ofVF, saynF, is less thann=|VG|. The easiest solution will be to enumerate all subsetsV0of size nFofVGand then to compute inj(F,G[V0]). But this will take timeO( nn

F

2nFt(n)), which in the worst case, isO(3n·t(n)). In the next theorem we show how to extend Theorem4.17to the casenF<n.

The last equality follows from the fact that for any subset Y with|Y| ≤nF, the value of hom(F,G[Y])is counted precisely for all those subsetsW for whichY ⊆ W and|W|=nF. On the other hand, for every fixedY, hom(F,G[Y])is counted once in the above sum for every supersetW ofY of sizenF. The number of such setsW is precisely n−|Y|

nF−|Y|

. Furthermore, for all such sets, we have the same sign corresponding toY, that is,(−1)nF−|Y|. This completes the proof. ut In what follows, we give two examples of the use of graph homomorphisms:

Counting Hamiltonian cycles and counting colorings. We come back to graph ho-momorphisms with applications to graphs of bounded treewidth in Chap.5.

Let #HAM(G)denote the number of Hamiltonian cycles in a graphG= (V,E),

|V|=n, andF=Cnbe a cycle of lengthn. To compute sub(F,G) =#HAM(G), we want to use Theorem4.17with Proposition4.16. To use them we need to compute aut(Cn)and hom(Cn,G).

The cycleCncan be represented as the 1-skeleton of a regularn-gon in the plane.

All its automorphisms corresponds to rotations and reflections of a regularn-gon.

We leave the proof of the following fact, that implies aut(Cn) =2n, as an exercise.

Exercise 4.19.There arenrotations andnreflections of a regularn-gon.

There are several ways of computing hom(Cn,G). One way is to observe that for every homomorphism the image ofCninGis a closed walk of lengthninG, i.e. the walk{v1,v2, . . . ,vn}, wherev1=vn. The number of closed walks of a given length can be computed in polynomial time by dynamic programming exactly as was done in the proof of Theorem4.5for (not closed) walks. Another way is to prove that hom(Cn,G) =∑ni=1λin, whereλ1, . . . ,λnare the eigenvalues of the adjacency matrix of G. The third way of counting hom(Cn,G)is based on dynamic programming on graphs of bounded treewidth. The treewidth of Cn is two, and hom(Cn,G) is computable in polynomial time by techniques that will be described in Chap.5.

Summarizing, hom(Cn,G)and aut(Cn)are computable in polynomial time. Thus by Theorem 4.17, the number of Hamiltonian cycles in Gcan be found in time which is proportional (up to a polynomial factor) to the number of subsets inV. This implies

Theorem 4.20.The number of Hamiltonian cycles #HAM(G)in an n-vertex graph G is computable in timeO(2n).

We already discussed how the chromatic number of a graph can be computed in timeO(2n). Let us see how (slightly worse) 2n+O(

n)running time can be achieved by treating the problem as a special case of the COUNTINGSUBGRAPHISOMOR

-PHISMSproblem, and thus by counting graph homomorphisms.

We denote byχ(G;k)the number ofk-colorings of a graphG. For example, if P3is a path on 3 vertices, thenχ(P3; 0) =χ(P3; 1) =0,χ(P3; 2) =2, andχ(P3; 3) = 12. Another example: for a complete graphKk,χ(Kk;k) =k!. Then the chromatic number of a graphGis the smallest integerk>0 such thatχ(G;k)>0. It is well known and easy to see that for an integerk≥0,χ(G;k) =hom(G,Kk), whereKkis a complete graph onkvertices. However to construct an exact algorithm, we have to look at homomorphisms from “the other side”.

As we already discussed, ak-coloring of a graphG= (V,E)can be viewed as par-titioning the vertex set of the given graph intokindependent sets, that is, a partition (V1, . . . ,Vk)ofV such that for everyi∈ {1, . . . ,k}, the graphG[Vi]does not contain an edge. For our purpose, we reformulate the COLORINGproblem as a problem of partitioning the vertex set intokcliques in the complement graph. LetG= (V,E)be the complement ofG. ThenGcan be partitioned intokindependent sets if and only ifGcan be partitioned intokcliques. We model this as a problem of subgraph iso-morphism as follows: we guess the sizest1,t2, . . . ,tkof these cliques, where∑iti=n.

ThenGcan be partitioned into cliques of sizest1,t2, . . . ,tkrespectively if and only if there is a subgraph ofGisomorphic to

F=∪ki=1Kti.

4.4 Counting Subgraph Isomorphisms 73 To compute the value of χ(G;k)for a given graphG, we count the number of partitions ofGintokcliques. Indeed, every partition ofGintokcliques corresponds tok! colorings ofG—we select one color for all vertices of the same clique, and the number of different choices isk!. LetPk(n)be the set of allunordered partitionsof nintokparts. For every partitionζ= (t1,t2, . . . ,tk)∈ Pk(n), letF(ζ) =∪kiKti. Then

χ(G;k) =

ζ∈Pk(n)

k!·sub(F(ζ),G). (4.10)

Indeed, every subgraph ofGisomorphic toF(ζ)corresponds to a partition ofG into cliques, which is a partition ofGinto independent sets. But every partition into kindependent sets corresponds tok! different colorings.

In order to obtain the desired running time, we need a classical result from num-ber theory giving an upper bound on the numnum-ber of unordered partitions ofn into kparts. Let p(n)be the partition function, i.e. the number of partitions ofn. The asymptotic behavior of p(n)was given by Hardy and Ramanujan in their paper in which they develop the famous “circle method” [110].

Theorem 4.21. p(n)∼eπ

q2n

3/4n√

3, as n→∞.

Furthermore one can give an algorithm listing all different unordered partitions ofnintokparts in time 2O(

n). Actually, such a listing is possible even with poly-nomial (time) delay. This type of algorithm is out of the scope of this book; we refer to the book of Nijenhuis and Wilf for details [165].

Now our strategy to compute χ(G;k) is as follows. For every partition ζ =

(t1,t2, . . . ,tk)∈ Pk(n) we want to compute the inner sum in (4.10). To compute

(4.10), we have to compute the value of sub(F(ζ),G), and to do this we use Theo-rem4.17. To implement Theorem4.17, we have to compute the values of

• aut(F(ζ)), and

• hom(F(ζ),G[V\W]), whereW ⊆V.

The computation of aut(F(ζ))is easy—the number of automorphisms of a com-plete graph ont vertices ist!. IfF(ζ)consists of several connected components, then every automorphism either maps a component (complete graph) into itself, or to a component of the same size. Letn(x)be the number of components of sizex inF(ζ)and letx1,x2, . . . ,xp,p≤k, be the sizes of the components inF(ζ). Let us note thatxiis not necessarily equal totibecause it is possible in the partitionζ that for somei6=j,ti=tj. Then

aut(F(ζ)) =

x∈{x1,x2,...,xp}

n(x)!x!,

and this value is computable in polynomial time for eachζ.

To compute hom(F(ζ),G[V\W])we observe that it is sufficient to count the homomorphisms from every component ofF(ζ). The following result for a graph Fwith several connected components is easy to obtain.

Proposition 4.22.Let F1, . . . ,F`be the connected components of the graph F. Then hom(F,G) =∏`i=1hom(Fi,G).

Since every component ofF(ζ)is a complete graph, by Proposition4.22all we need are the values of hom(Kt,G[V\W]). For every homomorphism from Kt to G[V\W]), the image of the complete graphKtis a clique and

hom(Kt,G[V\W]) =T[V\W][t]t!, whereT[V\W][t]is the number of cliques of sizetinG[V\W].

Thus to finish all computations we have to find the number of cliques of sizetin a graph. By making use of dynamic programming over vertex subsetsW ⊆V, we compute the numbersT[W][i], which is the number of cliques of sizeiinG[W]. Our dynamic programming algorithm is based on the observation that fori>0,

T[W][i] =T[W\ {v}][i] +T[N(v)∩W][i−1]

for some vertexv. Indeed, for every clique of sizeicontainingv, the neighborhood oficontains a clique of sizei−1. By making use of this observation, it is straight-forward now to compute the valuesT[W][i]for allW ⊆V and 0≤i≤n in time O(2nn2)using exponential space.

To conclude, we have shown that for every partitionζ, and all subsetsW⊆V, we are able to compute hom(F(ζ),G[V\W])in timeO(2n). Thus by Theorem4.17, sub(F(ζ),G)is computable in timeO(2n). By Theorem4.21, the number of all possible partitionsζ is 2O(

n), and thus by computing sub(F(ζ),G)for each parti-tion, we computeχ(G,k)in time 2n+O(

n)by making use of (4.10).

Theorem 4.23.The number of k-colorings of a graph G, denoted byχ(G,k), can be computed in time2n+O(

n).

Notes

The principle of inclusion-exclusion is one of the basic tools in combinatorics. The origin of the principle is not clear. Sometimes it is attributed to Sylvester. Almost every textbook in combinatorics devotes a chapter to this method. We refer to books of Aigner [2], Cameron, [46], van Lint and Wilson [153], Ryser [194], and Stanley [207] for detailed discussions of the method.

The application of the inclusion-exclusion principle to exact algorithms was re-discovered several times. In 1969, Kohn, Gottlieb, and Kohn [136] used it to obtain exact algorithms for the TRAVELLINGSALESMANproblem. It was used in 1982, by Karp [127] and in 1993, by Bax [11,12]. A generalization of inclusion-exclusion by finite difference sets was proposed by Bax and Franklin in [10].

The book of Minc is devoted to the study of permanents [156]. The formula for computing the permanent of a matrix is due to Ryser [194]. Exact algorithms to

4.4 Counting Subgraph Isomorphisms 75 compute the permanent of a matrix over rings and finite commutative semirings are given in [128,215]. The inclusion-exclusion algorithm of this chapter for count-ing Hamiltonian paths can be generalized to solve the TSPproblem on n cities with maximum distanceW between two cities to achieve running timeO(W2n) and spaceO(W). The self-reduction algorithm was communicated to us by Mikko Koivisto.

Bj¨orklund and Husfeldt [24] and Koivisto [137] presented the firstO(2n)time algorithm to solve COLORINGusing inclusion-exclusion. The journal version [30]

of both conference papers presents a general approach that solves various parti-tion problems. Our presentaparti-tion follows [24]. Nederlof [162] and Lokshtanov and Nederlof [154] further developed inclusion-exclusion based techniques to obtain a number of polynomial space algorithms. The approach based on a combination of branching and inclusion-exclusion is discussed in [190]. The use of homomor-phisms for solving the COUNTINGSUBGRAPHISOMORPHISMSproblem was stud-ied by Amini, Fomin and Saurabh in [5].

Chapter 5

Treewidth

The treewidth of a graph is one of the most fundamental notions in graph theory and graph algorithms. In this chapter, we give several applications of treewidth in exact algorithms. We also provide an exact algorithm computing the treewidth of a graph.