/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2007 * * Last modified: * $Date: 2011-09-19 22:02:26 +1000 (Mon, 19 Sep 2011) $ by $Author: schulte $ * $Revision: 12400 $ * * 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(Home home, ViewArray& x, Offset& o0) : NaryPropagator(home,x), y(home,x), o(o0) { home.notice(*this,AP_WEAKLY); } template forceinline Base::Base(Space& home, bool share, Base& p) : NaryPropagator(home,share,p) { o.update(p.o); y.update(home,share,p.y); } /// Information required for non-recursive checking for a single scc template class SsccInfo { public: int min, low, pre; Int::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 = o(x[start]).val(); if (start == 0) break; } /// Information needed for checking scc's Region r(home); typedef typename Offset::ViewType OView; SsccInfo* si = r.alloc >(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 Support::StaticStack next(r,n); // Array to remember which mandatory tells need to be done TellInfo* eq = r.alloc >(n); int n_eq = 0; // Array to remember which edges need to be pruned TellInfo* nq = r.alloc >(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(); Region r(home); // The path starting at assigned x[i] ends at x[end[j]] which is // not assigned. int* end = r.alloc(n); for (int i=n; i--; ) end[i]=-1; // A stack that records all indices i such that end[i] != -1 Support::StaticStack tell(r,n); typedef typename Offset::ViewType OView; for (int i=y.size(); i--; ) { assert(!y[i].assigned()); // Non-assigned views serve as starting points for assigned paths Int::ViewValues v(o(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=o(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(o(x[end[i]]).nq(home,i)); } return ES_NOFIX; } template forceinline size_t Base::dispose(Space& home) { home.ignore(*this,AP_WEAKLY); (void) NaryPropagator::dispose(home); return sizeof(*this); } }}} // STATISTICS: int-prop