Enforcing Connectivity in a Fixed Degree Graph - Semantic Scholar

95 downloads 127991 Views 181KB Size Report
2Cork Constraint Computation Centre, Department of Computer Science, ... global constraints that enforce connectivity in a fixed-degree graph; and. 2. A case ...
Enforcing Connectivity in a Fixed Degree Graph: A Constraint Programming Case Study

?

J. Christopher Beck1 , Kenneth N. Brown2 , Ian Miguel3 , Patrick Prosser4 1

Department of Mechanical and Industrial Engineering, University of Toronto, Canada. 2 Cork Constraint Computation Centre, Department of Computer Science, University College Cork, Ireland. 3 School of Computer Science, University of St Andrews, UK. 4 Department of Computing Science, University of Glasgow, UK. [email protected], [email protected], [email protected], [email protected]

Abstract. Constraint programming provides a number of ways to solve any given problem. Pure constraint models use primitive constraints and variables to achieve the desired functionality. However it is often the case that, through necessity or inclination, researchers extend constraint toolkits by building customized global constraints around existing or novel algorithms. In this paper, we look at the problem of generating a connected graph given a fixed degree for each node. This problem appears as part of a number of interesting applications, for example, in computational chemistry and telecommunications. After proving that achieving generalized arc consistency for specified degree and connectivity together is NP-complete, we present seven implementations that achieve lower levels of consistency in polynomial time and space. These seven approaches are evaluated on three applications. Our results indicate, not unexpectedly, that the customized global constraints achieve the highest problem solving performance. However, we argue that the declarative nature of the primitive constraints brings significant advantages as well and question whether the pursuit of performance has not led constraint programming away from one of its fundamental goals: declarative problem solving.

1

Introduction

In this paper, we conduct a case study of adding functionality to a constraint toolkit. Specifically, we want to be able to enforce connectivity in a graph where each node has a fixed degree. The study presents seven different approaches: three models of connectivity using constraint primitives to encode the functionality and four models using customized global constraints to directly implement propagation algorithms. Our empirical results reveal some minor surprises, such ?

Ian Miguel is supported by a UK Royal Academy of Engineering/EPSRC Research Fellowship. This work was supported by ILOG, SA. We thank Steve Linton for suggesting the Compact Encoding, and Chris Jefferson for valuable discussions.

as the fact that a very simple-minded global constraint performs extremely well in a problem with few other constraints and that an ad hoc constraint is the overall best approach. In general, however, these results support the received wisdom: customized global constraints are significantly faster and more spaceefficient. We argue, however, that a more nuanced view of our case study is merited. Standard performance measures ignore important issues regarding the development of customized global constraints including the effort in implementation and debugging, the related concerns regarding correctness, and, perhaps most significantly, a lack of flexibility and extensibility. Furthermore, constraint solving through the creation of new customized global constraints is anti-thetical to one of the original goals of Constraint Programming (CP): declarative problem solving. The contribution of this paper is twofold: 1. The creation and empirical analysis of three constraint models and four global constraints that enforce connectivity in a fixed-degree graph; and 2. A case study of developing new functionality in a constraint toolkit leading to a discussion of trade-offs inherent in an important aspect of modeling in CP: pure models vs. customized global constraints.

2

Enforcing Connectivity in a Fixed-Degree Graph

We consider the problem of developing a CP approach to enforcing connectivity while generating graphs with a given degree sequence. Such functionality would appear to have wide applicability in solving important combinatorial optimization problems. For example, in computational chemistry, generating all possible single molecules formed from a set of atoms involves generating connected multi-graphs (where atoms are represented by vertices, valencies are represented by the degrees, and bonds are represented by edges [21]). In telecommunications network planning, base stations and hubs are vertices, the communication links are edges, and the network must clearly be connected. From Operations Research, solving the Travelling Salesperson Problem involves constructing a minimal length circuit which visits every vertex: a connected graph where every vertex has degree 2. Many such problems come with associated side constraints– legal bonds between atoms or acceptable delays on communications links–and thus a constraint programming solution may be desirable. More formally, we are given a set V = {v0 , v1 , . . . , vn−1 } of n nodes each with a corresponding integer degree, D = {d0 , d1 , . . . , dn−1 }. Optionally, we are given a set of edges EF = {{vi , vj }|i 6= j} representing edges that are forbidden in any realized graph, and a similar set ER , representing edges that are required. Our goal is to develop a constraint representation to be used as part of a larger problem and search procedure that ensures that for any simple graph, G = (V, E), generated by adding edges, E, between the nodes in V , G is connected, the degree of node vi , deg(vi ) = di , none of the edges in EF are present (EF ∩ E = φ), and all of the edges in ER are present (ER ⊆ E).

The basic representation used in this paper is a n × n array A of constrained 0/1 variables representing potential undirected edges in a simple graph: Ai,j represents the edge (vi , vj ) in G. The array A is symmetric such that array element Ai,j and Aj,i point to the same constraint integer variable. There has been some previous work on reasoning about properties of graphs using constraint models. For example, [11] present a new variable type representing paths in a graph, [19] discusses a global constraint for graph isomorphism problems, and [13] generate optimal cycle covers for networks. Graph theoretic algorithms have been used extensively in constraint programming; see [16], for example, or [18] for a survey, and the Global Constraints Catalog [1]. More recently graph models have been inspired by problems in bioinformatics, such as the work in [9], [2], and the work of Dooms, Deville and Dupont [6]. This is the first work of which we are aware that directly addresses CP models for connectivity in a fixed-degree graph.

3

Theoretical Foundations

The standard approach to achieving the desired functionality is to enforce Generalized Arc Consistency (GAC). Given the adjacency matrix representation, GAC implies that in a given search state, for every potential edge, e, it can be proved both that based on the current set of variable assignments a connected graph with the required degree sequence including e exists and a connected graph with the required degree sequence not including e exists. If one of these is not true, the variable corresponding to e can be assigned. In this section, we show that achieving GAC on a fixed-degree, connected graph is NP-complete. We first define the Fixed Degree Connected Subgraph (FDCS) problem and demonstrate that enforcing GAC requires FDCS to be solved. We then show that FDCS is NP-complete by reduction from HAMILTONIAN-CIRCUIT. Fixed Degree Connected Subgraph (FDCS): Given a graph G = (V, E) with a degree di specified for each vertex vi ∈ V , and a subset ER ⊆ E of required edges, does there exist a simple connected graph G0 = (V, E 0 ) where E 0 is a subgraph of G s.t. deg(vi ) = di for all v ∈ V , and ER ⊆ E 0 ? Required edges in our original problem correspond to the edges in ER , while forbidden edges correspond to all possible edges not in E. Checking GAC requires us, naively, to solve FDCS twice for each potential edge, e. First we solve FDCS requiring that e ∈ E 0 to verify if a connected graph with required degree exists that includes e, then we solve FDCS requiring e ∈ / E 0 . More intelligent strategies can reduce the number of times the FDCS needs to be solved. However, we show below that FDCS is NP-complete and so the need to solve it at least once implies that enforcing GAC on a fixed-degree, connected graph is NP-complete. Theorem 1. FDCS is NP-complete. Proof: To prove this is NP-complete, we show that (i) it is in NP, and (ii) we can reduce an NP-complete problem to it. (i) FDCS is in NP: a certificate is a

proposed connected subgraph with the correct degree sequence. The certificate is of size O(n2 ), we can decide whether or not it is connected in time O(n2 ), we can check it is a subgraph in O(n2 ), and we can check the degrees of each vertex in time O(n2 ). (ii) FDCS is NP-complete, by reduction from HAMILTONIANCIRCUIT. A Hamiltonian circuit in G is a subgraph that is connected, and where every vertex has degree 2. Run an algorithm for FDCS on G with the required degree sequence deg(v) = 2 for all v ∈ V , and with an empty set of required edges. If the algorithm finds a connected subgraph with that degree sequence then it has found a Hamiltonian circuit and reports yes. If it cannot find a connected degree sequence then there is no Hamiltonian circuit and so reports no. Therefore HC is reducible to FDCS. The reduction is polynomial, since all we did was add n unary constraints. By (i) and (ii), FDCS is NP-complete. 

4

Models

We now present seven models of connectivity. Three of these use constraint primitives, the remainder being customized global n-ary constraints. Since FDCS is NP-complete, it is unreasonable enforce GAC. There are two standard approaches to such a problem. The first is to decompose the functionality (e.g., into multiple global constraints) such that GAC on each piece is polynomial. The obvious decomposition here is to decouple the degree requirements from the connectivity. All but one of the approaches investigated here follow this decomposition, though not all of them enforce GAC even with respect to connectivity. The second approach is to develop an ad hoc set of propagation rules that reason simultaneously about connectivity and degree sequence. These rules will not achieve GAC, but may lead to stronger propagation than the decomposition approach. Unless otherwise noted, the functionality of each model is GAC on the connectivity relation: for any edge that can still be rejected, there exists a connected graph that does not include that edge. Due to space limitations, we do not prove that each model enforces GAC. We make note of the space consumed by each model because, as we will see later, the space requirement can severely limit the size of the problem that we can model. In all cases we enforce the degree sequence constraints by constraining the row-sums of the matrix A: Pw=n ∀v ∈ V ( w=1 Av,w = deg(v)). 4.1

Primitive Models

The three-dimensional matrix models that follow are all composed of 0/1 variables. The rows and columns of each plane always represent individual nodes and are indexed 1..n. For ease of presentation, the planes are indexed from 0. Plane 0 is always an adjacency matrix. Hence, the variable at column i and row j in plane 0 is assigned 1 if and only if there is an edge connecting nodes i and j. In all cases, there is a symmetry across the leading diagonal, which can be exploited to reduce the size of the model.

A Simple Encoding (SE) A Simple Encoding uses an (n − 1)-plane matrix, P , hence O(n3 ) variables. It also consists of O(n3 ) constraints of arity n. Planes 1..n − 2 are the natural extension to the adjacency matrix, recording whether there is a path of a certain length between a pair of nodes. Specifically, for each k in 0..n − 2, Pi,j,k is assigned 1 if and only if there is a path of length k + 1 between nodes i and j. Two sets of constraints on P are necessary to enforce connectivity: X Pi,j,k > 0 (i < j in 1..n) (1) k

Pi,j,k = (P

,i,0

× Pj,

,k−1

6= 0) (i < j in 1..n, k in 1..n − 2)

(2)

where P ,i,k denotes row i of plane k (similarly for the jth column), and P ,i,k × Pj, ,k denotes the scalar product of the ith row and jth column. Constraint (1) ensures that there is a path of some length between every pair of vertices. Constraint (2) captures that there is a path of length k between a pair of vertices, i and j, if there exists a third vertex that is directly connected to i and has a path of length k − 1 to j. This is modelled by reifying the constraint that the scalar product of a row on plane 0 and a row on plane k − 1 is non-zero. Encoding the Floyd-Warshall Algorithm (FW) A second encoding is based on the Floyd-Warshall algorithm for testing whether a graph is connected [8]. It employs an (n + 1)-plane matrix F , hence again O(n3 ) variables. It also uses O(n3 ) constraints, but only of arity 4, and is therefore more compact than SE. Planes 1..n are each associated with a particular node. Fi,j,k is assigned 1 if and only if there is a path between i and j involving vertices 1..k. F is constrained as follows to enforce connectivity:

Fi,j,k

Fi,j,n = 1 (i 6= j in 1..n) = max(Fi,j,k−1 , Fk,j,k−1 × Fi,k,k−1 ) (i, j, k in 1..n)

(3) (4)

Constraint (3) ensures that there is a path between every pair of nodes. This is done by assigning every variable, except those on the leading diagonal, in the nth plane to be 1, meaning that there is a path between every pair of nodes involving the nodes 1..n. Constraint (4) captures that there is a path between nodes i and j involving nodes 1..k if and only if either there is a path between the two involving nodes 1..k − 1, or there is a path from i to k and from k to j. A Compact Encoding (CE) A final encoding employs a log2 n-plane matrix, C, hence O(n2 .log2 (n)) variables. It contains O(n2 .log2 (n)) constraints of arity n. Each plane subsequent to the adjacency matrix is obtained from the square of the previous one, substituting the value 1 for all non-zero values. If plane 0 contains no self-loops (i.e. ∀i Ci,i,0 = 0), then if Ci,j,k is assigned 1 there is a path of length 2k between vertices vi and vj . If we force self-loops in the initial plane (i.e. ∀i Ci,i,0 = 1), then Ci,j,k = 1 implies there is a path of length less

than or equal to 2k . To ensure connectivity, we require a path of length n − 1 or less between all pairs of nodes, and so we constrain all variables in the final plane to be assigned the value 1. More formally, the constraints are Ci,i,0 = 1 (i in 1..n) Ci,j,k = (Ci,

4.2

× C ,j,k−1 6= 0) (i ≤ j in 1..n, k in 1..log2 (n − 1)) Pi,j,log2 (n−1) = 1 (i, j in 1..n)

,k−1

(5) (6) (7)

Global Constraints

Component-based Dead-end Detection (Comp) For a graph to be connected there must be a single component. When an edge (vi , vj ) is added to a graph and vi and vj are in different components then these components are merged into one and the number of components is reduced by one. The CONNECTED-COMPONENTS [5] (p. 441) algorithm determines if a given graph G = (V, E) is connected. The algorithm starts with n disjoint sets S0 to Sn−1 , where initially Si = {vi } and iterates over all edges e ∈ E. If edge (vi , vj ) has vertices vi and vj in different components (different sets) then the sets are merged and the location of each vertex is updated. Therefore we associate with each node vi an integer loc(vi ) representing its set or location. When sets Si and Sj are united, set Si becomes equal to the set of elements Si ∪ Sj and the locations of elements in Sj are updated as follows ∀vk ∈Sj (loc(vj ) = i). On termination, if there is a single component the graph must be connected. The algorithm requires that a union operation be performed between two disjoint sets and that the location of a node (i.e. the identity of the set that a node belongs to) can be updated and found quickly. A simple n-ary connectivity constraint that does not enforce GAC on connectivity can be implemented based on the CONNECTED-COMPONENTS algorithm. Reversible variables are used to represent the components, the location of vertices in components, the number of components c, and a count of the number of edges that have neither been accepted nor rejected, i.e free edges f . When an edge is selected, and that edge spans components, components are merged, c is decremented and f is decremented. If a non-spanning edge is selected or any edge is rejected then f is decremented. If at any search node f < c − 1 a failure is detected and backtracking takes place. This constraint does no inference, only detecting a dead-end when there are insufficient edges remaining to join together all the remaining components. For further details see [15]. Our implementation uses O(n2 ) space, made up of reversible variables, and is referred to as Comp. Lazy Bridge Detection Constraint (LazyBridge) To enforce GAC on the connectivity relation, it is sufficient to to detect that for each potential edge, e, no connected graph exists in a graph without e. In graph theoretic terms, it is necessary to be able to identify “bridges” [5, 17]. A depth-first traversal (DFS) of a graph partitions the edges into “tree” edges, T , and “back” edges, B. We use this partition in this and the following global constraint implementations.

Sedgewick [17] proves that a tree edge (vi , vj ) is a bridge if and only if there are no back edges that connect a descendant of vj to an ancestor of vi and provides a simple DFS-based algorithm to detect all bridges in time linear in the number of edges. We use this algorithm directly at initialization and then whenever an edge is removed from the graph. As the DFS algorithm is run whenever an edge is removed, no reversible data structures are required. It is only necessary to maintain the DFS traversal index of each vertex in O(n) standard variables. We refer to this constraint as LazyBridge. Bridge Detection Constraint (Bridge) We also implement an incremental bridge detection constraint that is based on detecting bridges but maintains a reversible data structure to enable inferences without recreating the DFS tree in many cases. As above, the DFS process generates two sets of edges: T and B. The connectivity constraint associates a counter, ncvi ,vj , initially 0, with each tree edge (vi , vj ). On encountering a back edge (vt , vw ) we increment the counters on the tree edges spanned by that back edge, i.e. we increment the set of counters {ncvi ,vj | (vi , vj ) ∈ path(vt , vw )} where path(vt , vw ) is the set of tree edges that make up the path from vt to vw . The counter is incremented to indicate that there is at least one more cycle in the graph involving edge (vi , vj ), i.e. ncvi ,vj is a count of the number of cycles passing through edge (vi , vj ) using edges in T and B. If on termination of the DFS process, any counter, say ncvx ,vy , equals zero then that edge (vx , vy ) is a bridge and must be forced. Consequently the value 0 is removed from the domain of variable Avx ,vy . This is done when the constraint is initialized and has a time complexity of O(n3 ). Subsequent actions only take place when an edge (vi , vj ) is rejected by the search process. If (vi , vj ) is a back edge then the counters on the path from vj to vi in T are decremented and if any counter reaches zero then the associated edge is a bridge and must be selected. If (vi , vj ) is a tree edge then some edge in B must become a tree edge in T . The tree structure is then repaired and this may again result in cycle counters falling to zero and selection of edges being forced. For a full account of this constraint see [14]. The constraint uses O(n3 ) space in terms of reversible variables. We call this constraint Bridge. Residual Degree (RD) The global constraint Comp reasons about components and the effect of rejecting an edge. An alternative approach using both connectivity and degree is to reason about the effect of accepting an edge. During the search, each vertex vi maintains a residual degree, r(vi ): the difference between the required degree of the vertex and the number of incident edges currently selected. Similarly, each component cj and the entire graph G has a residual degree r(cj ) and r(G), respectively. Note that r(G) will be double the number of edges able to be accepted. While maintaining components as before, we can also maintain counts of residual degree. Each time an edge (vi , vj ) is selected, we decrement r(vi ) and r(vj ), and decrease r(G) by 2; if vi and vj were in the same component ck , then we decrease r(ck ) by 2, and otherwise we create a new component ck , with residual degree r(ck ) = r(ci ) + r(cj ) − 2.

We can use the components and residual degree to implement an ad hoc global constraint (RD) for ensuring connected fixed degree graphs, but which again does not maintain GAC. If r(ck ) becomes 0 for any component, and ck is not the only component, then ck cannot be connected to the rest of the graph and we force a backtrack. Similarly, if r(G) < 2c − 2, where c is the number of components, the graph cannot be connected, and so we backtrack. Finally, we can propagate using similar reasoning. If r(ci ) ≤ 2 and c > 1, then we reject all internal edges in ci . If r(ci ) = 1 and r(cj ) = 1, and c > 2, then we reject all edges connecting ci and cj . If r(G) = 2c − 2, then we reject all internal edges for all components. Our implementation requires O(n) space, and at most O(n2 ) steps for each update. For further details, see [4].

5

Applications

We now present three applications of our connectivity constraints: generating all connected graphs with a given number of nodes, finding a closed knight’s tour, and solving the Hashiwokakero puzzle. 5.1

Generating All Connected Graphs with n Nodes

As a basic test for our different models and constraints, we use a two-step process to generate all connected graphs with n nodes. Given n as input, in an initial CP model we define n variables, seqi , 1 ≤ i ≤ n each with domain [1..n] representing the degree sequence of a desired graph. To remove symmetries, we add constraints: seqi ≤ seqj , j = i + 1. Finally, we use a simple algorithm based on Erd¨ os-Gallai theorem [7, 4] as a filter to ensure that each degree sequence generated corresponds to a valid, but possibly disconnected, graph. Each such degree sequence for the given n is generated and passed to a second constraint model, which simply uses the connected degree sequence models introduced above to generate all connected graphs with degree sequence. Randomized static heuristics are employed for the search by randomly permuting the decision variables and with probability of 0.5 negating the variable. By using a lexicographic variable ordering and a value ordering that assigns the lowest value first, we achieve a static, randomized search. For n values from 4 to 8 and for ten different random seeds, each approach generated all connected graphs. All approaches agree on the total number of graphs generated. In Table 1, we report the mean run-time to generate all connected graphs and the mean number of fails. Since all GAC-enforcing approaches result in the same search, we display only one set of fail statistics: all others are identical. All approaches are implemented in ILOG Solver 6.2. In terms of run-time, among the model-based approaches, FW is at least twice as fast as the others on the larger problems. However, FW is about five times slower than the global constraint approaches. Interestingly, Comp, which does no propagation is the best performing global constraint by a small margin. The reason for this can be observed in the fail statistics: enforcing GAC only

n

Run-time (secs) # Fails SE FW CE Comp LazyBridge Bridge RD GAC Comp RD 4 0.006 0.009 0.007 0.003 0.003 0.003 0.003 0.6 0.6 0.4 5 0.045 0.044 0.038 0.014 0.014 0.015 0.012 10 10 8 6 0.604 0.338 0.502 0.075 0.075 0.079 0.074 218 218 179 7 12.991 3.628 7.850 0.612 0.659 0.808 0.618 4746 4776 4372 8 634.38 102.09 290.73 14.11 18.80 24.15 14.87 185288 185997 180754 Table 1. The mean run-time (in seconds) and the mean number of fails for each approach to find all connected graphs of a given size. The mean number of fails for the GAC approaches (SE, FW, CE, LazyBridge, and Bridge) and the non-GAC approaches to find all connected graphs of a given size.

results in a savings of a very few fails (less than 0.4% for n = 8): there is little benefit from propagation. 5.2

Closed Knight’s Tour

Turning to a more challenging problem, we use our approaches to generate a closed knight’s tour. A knight’s tour of a chess board is a sequence of moves by a knight such that each square of the board is visited once and once only and such that the final square visited is a knight’s move away from the starting position. We represent each square on the n × n board as a vertex in a graph. An n2 × n2 matrix A of 0/1 decision variables represents the moves in the closed tour, i.e. edges in the graph. If Ai,j is assigned 1 then a knight’s move from board position i to j is part of the tour. Initially, A is processed such that if positions i and j cannot be adjacent via a knight’s move then Ai,j is set to zero.1 In any tour, each vertex of the graph must be involved in two edges, i.e. P 2 for all i: nj=0 Ai,j = 2, and the resultant graph must be connected. Hence, our fixed-degree, connectivity models are posted in A. A good heuristic for the closed knight’s tour is due to H.C. von Warnsdorf, who in 1823 proposed that the next position to consider is the one that has the fewest remaining moves (i.e., it is a fail-first heuristic). We implement the Warnsdorf heuristic by using an array of n2 auxiliary integer variables, one for each square in the chess board. The value of each variable is the number of remaining possible edges for the corresponding node. Each variable is maintained by counting the number of unassigned variables in the corresponding row of A and this count is maintained by a sum constraint on the row. When choosing the next edge to assign, the square with the lowest number (greater than 0) of unassigned edges is chosen. The edge is then chosen lexicographically and set to 0. An edge variable is only set to 1 on backtracking or propagation. The 1

Clearly this is wasteful in space and, as we will see, memory exhaustion plays an important role in our results. A better approach in such sparse graphs would be based on adjacency lists. We believe that each of our approaches has an adjacency list counter-part that we leave for future work.

complexity of this implementation is O(n2 ) as the auxiliary variable for each square must be examined. n SE FW CE Comp LazyBridge Bridge RD 8 - 4.22 9.64 19.44 0.042 0.055 0.040 10 0.098 0.120 0.040 12 0.194 0.227 0.099 14 0.272 0.374 0.204 16 0.454 0.674 0.504 18 0.706 1.194 0.724 20 1.103 1.908 1.102 22 1.64 2.98 1.57 24 2.38 4.42 2.31 26 3.41 6.38 3.21 28 4.75∗ 9.04∗ 4.74∗ 30 Table 2. The mean run-time (in seconds) to generate a closed knight’s tour. ’-’ indicates an inability to find a solution in 5 minutes of wall-clock run-time. The ∗ symbol denotes memory exhaustion and a wall-clock run-time of about 1 minute. The user time is reported.

The results of our implementations in ILOG Solver 6.2 (run on a 2.4 GHz Pentium 4 with 512M of main memory under Fedora Core 2) are shown in Table 2. The primitive encodings scale very poorly due to memory exhaustion. This is not very surprising given that the SE and FW require O(n6 ) variables and CE requires O(n4 log n) (recall that the chess board is n × n). The Comp global constraint also fails to scale beyond n = 8. However, the reason for this is not memory consumption, but simply an inability to find a solution within the alloted CPU time. Clearly, the extra computational effort to enforce GAC is worthwhile on these problems. Bridge and LazyBridge scale well until n = 28 with RD performing marginally better than LazyBridge. Interestingly, the GAC approaches and RD incur very few fails (usually less than 10) and, in fact, RD incurs no fails except at n ∈ {24, 28} where it it fails 3 and 152 times, respectively. 5.3

The Hashiwokakero Puzzle

According to Wikipedia,2 the Hashiwokakero puzzle first appeared in Puzzle Communication Nikolai in September 1990. It is a logic puzzle played on a 2-dimensional grid where some of the grid squares are labeled as islands and the rest are empty. The goal is to connect the islands into a single component via bridges. Bridges must start and end at different islands, cannot cross other bridges or islands, and must be drawn horizontally or vertically on the grid. 2

http://en.wikipedia.org/wiki/Hashiwokakero

Furthermore, each island has a number from 1 to 8 and the number of bridges connected to an island must match that number. Finally, a maximum of two bridges can connect any pair of islands. Figure 1 shows an instance of the puzzle with 23 islands, and on the right its solution. This instance was generated and displayed using the Bridges program from Simon Tatham’s Portable Puzzle Collection [20].

Fig. 1. An instance of Hashiwokakero on the left, and a solution on the right.

Solving Hashiwokakero can be considered as finding a connected planar multigraph with a given degree sequence, subject to the constraints that certain edges are disallowed and certain edges cannot coexist. The example puzzle is modelled using a 23-by-23 symmetric array A of constrained integer variables, where a variable can take a value 0, 1, or 2. The ith row of the array corresponds to the multi-edges incident on the ith island such that Ai,j is the number of bridges between islands i and j. Islands are numbered counting from left to right and top to bottom, so that the first island (top left) is 1 (with degree 1), then right of it is island 2 (with degree 3) then island 3 on top right (degree 3). Island 4 is on the second line (with degree 1) and so on. It is easy to generate, as input to the problem, the set of edges that are disallowed. For example, the only edges allowed from island/vertex 1 are (1,2) and (1,6). Therefore, all other entries in the first row of array A can be set to 0. There are also disjunctive pairs of edges, representing the edges that are not allowed to cross. An example of this is the pair of edges (8,14) and (11,12). Given a disjunctive pair of edges ((u, v), (x, y)) we post a constraint Au,v × Ax,y = 0. Our primary interest in addressing Hashiwokakero is to investigate the effort required to modify our different constraint models: the fact that multiple bridges can connect the same two islands means that none of our approaches are directly applicable. For all the primitive models and for the global constraint models that only reason about connectivity (i.e., all but RD) the modification is trivial. It is sufficient to generalize the selection of an edge, (u, v), from assigning 1 to the Au,v to simply removing 0 from the domain of Au,v . This required the modification of a few lines of code resulting in models that work in both the original scenario and in the multi-graph scenario. Recall that the degree sequence is enforced by summing the rows of the adjacency matrix and so no modification is necessary.

For RD, it was not obvious what changes to the algorithm were needed to ensure correctness, and so RD was not applied to this problem. A subset of our models (SE, FW, CE, Comp, Bridge) were implemented in JChoco and applied to solving Hashiwokakero using a 3.01 GHz machine with 512 MB of RAM under Windows XP Professional. Because we were unable to find any Hashiwokakero instances that caused a single fail, we did not test the other approaches. Using the 23-by-23 sample problem, we found the following results (consistent with the other instances tried): the Bridge and Comp global constraints took about 31 milliseconds to model and solve the problem; the FW and CE constraints took at least ten times longer; and the SE model exhausted memory before finding a solution. In our experiments we found that instances with about 80 islands could not be modelled with our primitive constraint models due to memory exhaustion whereas the specialized constraints continued to solve these problems quickly.

6

Discussion

The discussion is divided into two parts. First, we present a relatively standard discussion of the empirical results, concluding that there are a few minor surprises here, but that overall the results match our expectations. Then we turn to a broader examination of the issues that are not as easy to evaluate in a traditional numerical experiment but, perhaps, should be taken into account. 6.1

Empirical Results

The experimental results agree with the received wisdom in CP: based on speed and memory, customized global constraints outperform constraint models composed of primitive constraints. Often in our experiments, the memory requirements were onerous enough that the difference translates into an inability to solve problems that are easily solved by other approaches. We see two aspects of the empirical results that are worth noting. The performance of Comp on the first and third application is somewhat surprising given that it is a simple dead-end detection approach. The performance of Comp on the closed knight’s tour is closer to what would be predicted. These results serve to remind us that the performance of a given CP component cannot be evaluated wholly independently of the wider problem that is being solved. In rich problems with many solutions and in problems where failure is not expensive, simple dead-end detection outperforms models that enforce GAC. When failures become common and expensive, for example, when the set of constraints is more complex, the extra effort to enforce GAC is rewarded. This is not a particularly novel or shocking observation. However, it does have two important implications. First, good methodology suggests that one should always use a simple dead-end detection algorithm as a straw-man in empirical experiments to judge that the problem instances that are used for evaluation present sufficient challenge. Second, and more important, any implementation of functionality that will be used

as part of a toolkit necessarily makes performance trade-offs. The “right” implementation depends on the specific applications and the author of a toolkit cannot anticipate all such applications. Overall, the pattern of performance observed is that the pure techniques perform worst, followed by GAC-based global constraints, followed by an ad hoc global constraint. Such a pattern, while not surprising, calls into question the trend toward formality that has taken place in CP over the past decade. It is surely useful on an academic and intellectual level to design global constraints which can be formally characterized and for which properties can be proved. However, if our interest is in solving problems faster, the careful design of such techniques may be less useful. It is dangerous for CP if we adopt the convention that a problem is “solved” when a provably GAC global constraint of reasonable performance is invented. Symmetrically, we risk stagnation if formal foundations are required of all work that we judge worthy of publication. 6.2

Reconsidering the Basis for Evaluation

The above observations rest strongly on the assumption that problem solving performance is by far the most important criteria for work such as is presented in this paper. Such an assumption would seem to inherently favor languages over packages: it is only through the exposure of low-level functionality that high performance is achievable.3 The focus on problem solving performance obscures a number of qualities that we would like our CP toolkits to have. In particular, we identify the following differences amongst our approaches: – Simplicity: While CP modeling is still an art-form, in our experience, conceptualizing primitive constraint models is not any more difficult than inventing a global constraint approach. What is different, however, is that modeling with primitive constraints allows a direct translation from conceptualization to implementation. In contrast, for the global constraints developed in this paper, there were a number of competing ideas (including ones not described here) for the algorithm to enforce GAC connectivity. It was not apparent a priori which would be the better choice. Even once it was decided to implement a particular approach, we were left with little support in the language aside from a abstract constraint class to inherit from and documentation of the behavior of the propagation mechanism. Significant time and effort was spent in implementing, debugging, and optimizing the global constraint approaches despite the fact that we are not inexperienced with the toolkits we employed. – Transferability: The relative ease of implementing a conceptual constraint model using primitive constraints is also reflected in the ease of re-implementation of a model in another toolkit. A large set of primitive constraints are available in most toolkits. Transferring a primitive model is then simply an exercise 3

Though work on solvers in CP [10], SAT [12], and mixed-integer programming [3] are good counter-examples to this belief.

in syntax. In contrast, transferring a global constraint is significantly more difficult due to the complexity of the implementation, differences in the internal behavior of the different toolkits, and difference in the underlying programming languages. – Extensibility: Global constraint implementations are less extensible than primitive models. In addition to the challenges of Hashiwokakero noted above, it is more difficult to incorporate other changes to global constraint models. For example, allowing a range of possible degrees rather than a fixed degree for each node is easily incorporated into all models except RD. More interestingly, the multiple layers of the SE and FW models provide an easy and direct way to express constraints on the diameter of any connected graph. All the global constraint implementations would require significant changes to express such constraints. A unifying theme for these three criteria is the extent to which a constraint model is declarative. While we continue, within CP, to pay lip service to declarative problem solving, global constraint-based approaches inherently move us toward procedural problem solving.4 The problem solving power of global constraints seems to be unassailable. However, in our pursuit of performance have we given up one of the key reasons for using constraint programming to begin with?

7

Conclusions

In this paper, we investigated CP techniques for enforcing connectivity in a fixed degree graph represented as an adjacency matrix. We proved that achieving GAC on both connectivity and fixed degree is NP-complete. We then introduced seven CP approaches that detect dead-ends with respect to connectivity, achieve GAC on connectivity alone, or make inferences based on reasoning about both connectivity and degree. Three models are based on using auxiliary variables and primitive constraints, while the four other models extend constraint toolkits via the creation of customized global constraints. Our empirical experiments, concentrating on the problem solving performance in three applications, showed that the ad hoc global constraint is able to perform slightly better than the GAC global constraints which are substantially better than the primitive constraint models. More broadly, we argued that the approaches that perform poorly have advantages due to their declarative nature that are not shared by the ad hoc constraint and are not present to the same extent in the other global constraints. If the original promise of declarative problem solving is still important to CP, we need to squarely address the apparent trade-off between declarativeness and performance. 4

One might argue that once we have a standard set of global constraint implementations, they can be used declaratively. Given that at least 235 global constraints are known [1], we remain skeptical both of establishing a standard set and of developing the knowledge required to use them declaratively.

References 1. Nicolas Beldiceanu, Mats Carlson, and Jean-Xavier Rampon. Global Constraint Catalog. In SICS-T-2005/08-SE, 2005. 2. Nicolas Beldiceanu, Pierre Flener, and Xavier Lorca. The tree Constraint. In CPAIOR2005 (LNCS 3524), 2005. 3. R. E. Bixby. Solving real-world linear programs. Operations Research, 50(1):3–15, 2002. 4. Kenneth N. Brown, Patrick Prosser, J. Christopher Beck, and Christine Wu. Exploring the use of constraint programming for enforcing connectivity during graph generation. In The 5th workshop on modelling and solving problems with constraints (held at IJCAI05), 2005. 5. T.H. Corman, C.E. Leirson, and R.L. Rivest. Introduction to Algorithms. 1996. 6. Gregorie Dooms, Yves Deville, and Pierre Dupont. CP(Graph): Introducing a Graph Computation Domain in Constraint Programming. In CP2005 (LNCS 3709), 2005. 7. P. Erd¨ os and T. Gallai. Graphs with prescribed degrees of vertices. Mat. Lapok, 11:264–274, 1960. 8. R.W. Floyd. Algorithm 97 (SHORTEST PATH). CACM, 5(6):345, 1968. 9. Ian P. Gent, Patrick Prosser, Barbara M. Smith, and Christine Wu Wei. Supertree Construction with Constraint Programming. In CP2003 (LNCS 2833), 2003. 10. I. Miguel I.P. Gent, C. Jefferson. Minion: Lean, fast constraint solving. In Proceedings of the 17th European Conference on Artificial Intelligence (to appear), 2006. 11. C. Le Pape, L. Perron, J.-C. R´egin, and P. Shaw. Robust and parallel solving of a network design problem. In CP2002 (ed. P. van Hentenryck), LNCS 2470, pages 633–648, 2002. 12. Matthew W. Moskewicz, Conor F. Madigan, Ying Zhao, Lintao Zhang, and Sharad Malik. Chaff: Engineering an Efficient SAT Solver. In Proceedings of the 38th Design Automation Conference (DAC’01), 2001. 13. G. Pesant and P. Soriano. An optimal strategy for the constrained cycle cover problem. Annals of Mathematics and Artificial Intelligence, 34:313–325, 2002. 14. Patrick Prosser and Chris Unsworth. A Connectivity Constraint using Bridges. In ECAI, 2006. 15. Patrick Prosser and Chris Unsworth. Rooted Tree and Spanning Tree Constraints. In ECAI Workshop on Modelling and Solving Problems with Constraints (submitted), 2006. 16. J.-C. R´egin and C. Gomes. The cardinality matrix constraint. In CP2004 (ed. M. Wallace), LNCS 3258, pages 572–587, 2004. 17. R. Sedgewick. Algorithms in C++: Graph Algorithms, volume 5. Addison-Wesley, 2002. 18. H. Simonis. Constraint applications using graph theory results. CPAIOR 2004 masterclass, 2004. http://www.icparc.ic.ac.uk/ hs/. 19. S. Sorlin and C. Solnon. A global constraint for graph isomorphism problems. In CPAIOR 2004, LNCS 3011, pages 287–301, 2004. 20. Simon Tatham. Simon Tatham’s Portable Puzzle Collection. http://www.chiark.greenend.org.uk. 21. C. W. Wu. Modelling chemical reactions using constraint programming and molecular graphs. In CP2004 (ed. M. Wallace), LNCS 3258, page 808, 2004.

Suggest Documents