/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2007 * * Last modified: * $Date: 2008-07-11 10:26:26 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $ * $Revision: 7330 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { namespace Int { namespace Circuit { template forceinline Base::Base(Space* home, ViewArray& x) : NaryPropagator(home,x), y(home,x) {} template forceinline Base::Base(Space* home, bool share, Base& p) : NaryPropagator(home,share,p) { y.update(home,share,p.y); } /// Information required for non-recursive checking for a single scc template class SsccInfo { public: int min, low, pre; ViewValues v; }; /// Information for performing a recorded tell template class TellInfo { public: View x; int n; }; template ExecStatus Base::connected(Space* home) { int n = x.size(); /// First non-assigned node. int start = 0; while (x[start].assigned()) { start = x[start].val(); if (start == 0) break; } /// Information needed for checking scc's GECODE_AUTOARRAY(SsccInfo,si,n); unsigned int n_edges = 0; for (int i=n; i--; ) { n_edges += x[i].size(); si[i].pre=-1; } // Stack to remember which nodes have not been processed completely GECODE_AUTOSTACK(int,-1,next,n); // Array to remember which mandatory tells need to be done GECODE_AUTOARRAY(TellInfo,eq,n); int n_eq = 0; // Array to remember which edges need to be pruned GECODE_AUTOARRAY(TellInfo,nq,n_edges); int n_nq = 0; /* * Check whether there is a single strongly connected component. * This is a downstripped version of Tarjan's algorithm as * the computation of sccs proper is not needed. In addition, it * checks a mandatory condition for a graph to be Hamiltonian * (due to Mats Carlsson). * * To quote Mats: Suppose you do a depth-first search of the graph. * In that search, the root node will have a number of child subtrees * T1, ..., Tn. By construction, if i 0) { ModEvent me = eq[n_eq].x.eq(home,eq[n_eq].n); if (me_failed(me)) return ES_FAILED; if (me_modified(me)) es = ES_NOFIX; } // Remove all edges that would require a non-simple cycle while (n_nq-- > 0) { ModEvent me = nq[n_nq].x.nq(home,nq[n_nq].n); if (me_failed(me)) return ES_FAILED; if (me_modified(me)) es = ES_NOFIX; } return es; } template ExecStatus Base::path(Space* home) { // Prunes that partial assigned paths are not completed to cycles int n=x.size(); // The path starting at assigned x[i] ends at x[end[j]] which is // not assigned. GECODE_AUTOARRAY(int,end,n); for (int i=n; i--; ) end[i]=-1; // A stack that records all indices i such that end[i] != -1 GECODE_AUTOSTACK(int,-1,tell,n); for (int i=y.size(); i--; ) { assert(!y[i].assigned()); // Non-assigned views serve as starting points for assigned paths ViewValues v(y[i]); // Try all connected values do { int j0=v.val(); // Starting point for not yet followed assigned path found if (x[j0].assigned() && (end[j0] < 0)) { // Follow assigned path until non-assigned view: // all assigned view on the paths can be skipped, as // if x[i] is assigned to j, then x[j] will only have // x[i] as predecessor due to propagating distinct. int j = j0; do { j=x[j].val(); } while (x[j].assigned()); // Now there cannot be a cycle from x[j] to x[v.val()]! // However, the tell cannot be done here as j might be // equal to i and might hence kill the iterator v! end[j0]=j; tell.push(j0); } ++v; } while (v()); } // Now do the tells based on the end information while (!tell.empty()) { int i = tell.pop(); assert(end[i] >= 0); GECODE_ME_CHECK(x[end[i]].nq(home,i)); } return ES_NOFIX; } template forceinline size_t Base::dispose(Space* home) { (void) NaryPropagator::dispose(home); return sizeof(*this); } template Reflection::ActorSpec Base::spec(const Space* home, Reflection::VarMap& m, const Support::Symbol& name) const { return NaryPropagator::spec(home, m, name); } }}} // STATISTICS: int-prop