• Aucun résultat trouvé

Directed Hamiltonian Path

4.2 Some Inclusion-Exclusion Algorithms

4.2.2 Directed Hamiltonian Path

In the DIRECTED HAMILTONIANs,t-PATHproblem we are given a directed and simple graphG= (V,E)with vertex set{s,t} ∪ {v1,v2, . . . ,vn}and edge setE,|E|= m. The task is to determine whether there is a Hamiltonian path fromstotinG, i.e.

a directed pathP= (s, . . . ,t)of lengthn+1 inGsuch that each vertex ofGoccurs precisely once inP.

To solve the decision problem DIRECTEDHAMILTONIANs,t-PATHwe actually solve the corresponding counting problem.

Theorem 4.5.The number of directed Hamiltonian s,t-paths in a graph can be com-puted in timeO(2n)and in polynomial space.

Proof. Our algorithm will determine the number of Hamiltonian paths fromstot inG. To apply the inclusion-exclusion principle of counting, we use the scenario of Theorem4.2. The objects are directed walks fromstotof lengthn+1. Adirected walkfromstotof lengthn+1 is a sequences,v1, . . . ,vn,tof vertices such that every pair of consecutive vertices is connected by an edge; vertices and edges may show up repeatedly in a walk. A walk (or an object) has propertyQ(i)if it contains vertex vi. Hence for every subsetW ⊆ {1,2, . . . ,n},N(W)is the number of objects having none of the propertiesQ(i), for alli∈W. In other words,N(W)is the number of directed walks of lengthn+1 fromstot containing no vertexvi withi∈W. We defineX to be the number of directed walks of lengthn+1 containing all vertices of{v1,v2, . . . ,vn}. ThusX is the number of Hamiltonian paths fromstot. By the inclusion-exclusion principle as given in Theorem4.2, we have that

X=

W⊆{1,2,...,n}

(−1)|W|N(W). (4.4)

Now all that remains is to computeN(W)for every subsetW⊆ {1,2, . . . ,n}. For every fixed subsetW this is easy, and actually can be performed in polynomial time.

There are several ways of computingN(W).

4.2 Some Inclusion-Exclusion Algorithms 57 One way is to use adjacency matrices. LetAbe the adjacency matrix ofG\W. Recall that in thekth powerAkofA, the entry at the intersection of rowiand column jcounts the number of walks withk+1 vertices inG\W that start at vertexiand end at vertex j. Therefore, the numberN(W)can be extracted fromAn+1which can be computed in polynomial time.

Another way of computing N(W) is via dynamic programming. For a subset W ⊆ {1,2, . . . ,n}and vertexu∈ {vi:i6∈W} ∪ {t}, we definePW(u,k)to be the number of directed walks of lengthkfromstouavoiding all verticesviwithi∈W. With such a notation we haveN(W) =PW(t,n+1).

The dynamic programming algorithm is based on the following recurrence.

PW(u,0) =

(0, ifu6=s 1, ifu=s PW(u,k) =

{vi:(vi,u)∈E,andi6∈W}

PW(vi,k−1),for 1≤k≤n+1.

For each subset W, the dynamic programming algorithm based on the above recurrence performs O(nm) operations with integer-valued variables of the type PW(u,k). Thus the running time on the unit-cost RAM isO(nm). On the log-cost RAM the running time of this step is slightly different: The number of walks is at most 2m, and thus each integer-value of a variable PW(u,k)can be encoded by making use ofO(m)bits resulting inO(nm2)time.

Note that at a typical point during the execution of the algorithm it stores for all suitable verticesu, the valuesPW(u,k)to be computed and the valuesPW(u,k−1) needed for the computation of the values of PW(u,k). Hence the required space is O(nm).

The overall inclusion-exclusion algorithm needs to plug all the 2nvalues ofN(W) computed by the polynomial time dynamic programming algorithm into the for-mula (4.4). Hence its running time isO(nm2n). Moreover the algorithm uses only space O(nm)because it may use the same space for each computation ofN(W),

(−1)|S|N(S)and the corresponding partial sum. ut

Self-reduction. It is natural to wonder whether such an algorithm counting the Hamiltonian paths from stot via an inclusion-exclusion formula and via count-ings,t-walks can actually compute at least one Hamiltonian path fromstotif there is one. Indeed using an inclusion-exclusion algorithm to compute a (optimal) solu-tion can often be done by a method calledself-reduction. For the Hamiltonian path it can be done at the cost of an additional multiplicative factor lognand still within polynomial space.

Let us sketch the idea. By Theorem4.5, we can count the number of Hamiltonian paths in time O(p(n)2n), where p(n)is some polynomial in n. If the algorithm outputs that the number of Hamiltonian paths in some input directed graph Gis positive, we know that there exists at least one such path inG. We take an edgeeof the input graphG, delete it fromGand run the counting algorithm on the modified

instance. If the number of paths in the new graph is positive, then we conclude that there is at least one Hamiltonian path in G that does not containe. In this case we proceed recursively, i.e. pick another edge of the new graph, remove it, count the number of paths in the reduced instance, etc. If the graphG\ {e}has no Hamiltonian path, we conclude that every Hamiltonian path inGgoes throughe. In this case we puteback intoG, label it as an edge of the Hamiltonian path, so we never try to remove it again from the graph, and recursively try to remove another edge. By the end of this procedure we are left with a Hamiltonian path ofG. However, such an approach requires to run the counting algorithmmtimes, resulting in running time O(m·p(n)·2n).

We first show how to decrease the number of runs frommton. Instead of trying all edges, we start from trying only edges going from s. There are at most n− 1 such edges, and if for some of the edges, say (s,s0), we have that the amounts of Hamiltonian paths from s tot in graphs G andG\ {(s,s0)} are different, we know that there is at least one path inGusing(s,s0). Moreover, in this path(s,s0) is the only edge going from or to s. Thus in this case we can removes with all incident edges and proceed constructing a Hamiltonian path froms0totinG\ {s}

recursively. For a subgraphFofGonkvertices, we count the number of paths inF in timeO(p(k)·2k) =O(p(n)·2k), and thus the running time of the algorithm can be estimated as

O(n·p(n)·(2n+2n−1+· · ·+20)) =O(n·p(n)·2n).

Finally, to speed up the algorithm, we can search for an edge(s,s0)whose removal changes the number of paths by performing binary search. We partition the set of edges incident tosinto setsAandBof equal sizes and run counting algorithm on the graphG\Aresulting fromGby removing all edges fromA. If the number of paths inGandG\Aare the same, then(s,s0)must be inB. Otherwise, it should be inA. In this way, we can find(s,s0)inO(logn·p(n)·2n)time, resulting in the running time

O(logn·p(n)·(2n+2n−1+· · ·+20)) =O(logn·p(n)·2n).

We would like to emphasize that with few modifications the algorithm to count thes,t-Hamiltonian paths of a directed graph also works for the Hamiltonian cycle problem on directed and also on undirected graphs. It also can easily be modified to solve theTSPproblem. However when distances between cities might be expo-nential inn(the number of cities), then the algorithm will require exponential space as well. More precisely, with the maximum distance between citiesW, the running time of the inclusion-exlcusion algorithm isO(2nW), while the space required is O(W). Since the input length isO(nlogW), the space requirement is exponential.

The existence of a polynomial space algorithm solving theTSPproblem in time O(2n)is an interesting open problem. In Chap.10we discuss techniques allowing us to solve theTSPproblem in polynomial space, but with worse running time.

4.2 Some Inclusion-Exclusion Algorithms 59