ladanyi@506: /* -*- mode: C++; indent-tabs-mode: nil; -*- ladanyi@506: * ladanyi@506: * This file is a part of LEMON, a generic C++ optimization library. ladanyi@506: * ladanyi@506: * Copyright (C) 2003-2009 ladanyi@506: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport ladanyi@506: * (Egervary Research Group on Combinatorial Optimization, EGRES). ladanyi@506: * ladanyi@506: * Permission to use, modify and distribute this software is granted ladanyi@506: * provided that this copyright notice appears in all copies. For ladanyi@506: * precise terms see the accompanying LICENSE file. ladanyi@506: * ladanyi@506: * This software is provided "AS IS" with no warranty of any kind, ladanyi@506: * express or implied, and with no claim as to its suitability for any ladanyi@506: * purpose. ladanyi@506: * ladanyi@506: */ ladanyi@506: ladanyi@506: #include ladanyi@506: #include ladanyi@506: #include ladanyi@506: ladanyi@506: using namespace lemon; ladanyi@506: ladanyi@506: template ladanyi@506: void checkDiEulerIt(const Digraph& g) ladanyi@506: { kpeter@517: typename Digraph::template ArcMap visitationNumber(g, 0); ladanyi@506: ladanyi@506: DiEulerIt e(g); ladanyi@506: typename Digraph::Node firstNode = g.source(e); kpeter@517: typename Digraph::Node lastNode = g.target(e); ladanyi@506: ladanyi@506: for (; e != INVALID; ++e) ladanyi@506: { ladanyi@506: if (e != INVALID) ladanyi@506: { ladanyi@506: lastNode = g.target(e); ladanyi@506: } ladanyi@506: ++visitationNumber[e]; ladanyi@506: } ladanyi@506: ladanyi@506: check(firstNode == lastNode, ladanyi@506: "checkDiEulerIt: first and last node are not the same"); ladanyi@506: ladanyi@506: for (typename Digraph::ArcIt a(g); a != INVALID; ++a) ladanyi@506: { ladanyi@506: check(visitationNumber[a] == 1, ladanyi@506: "checkDiEulerIt: not visited or multiple times visited arc found"); ladanyi@506: } ladanyi@506: } ladanyi@506: ladanyi@506: template ladanyi@506: void checkEulerIt(const Graph& g) ladanyi@506: { kpeter@517: typename Graph::template EdgeMap visitationNumber(g, 0); ladanyi@506: ladanyi@506: EulerIt e(g); ladanyi@506: typename Graph::Node firstNode = g.u(e); kpeter@517: typename Graph::Node lastNode = g.v(e); ladanyi@506: ladanyi@506: for (; e != INVALID; ++e) ladanyi@506: { ladanyi@506: if (e != INVALID) ladanyi@506: { ladanyi@506: lastNode = g.v(e); ladanyi@506: } ladanyi@506: ++visitationNumber[e]; ladanyi@506: } ladanyi@506: ladanyi@506: check(firstNode == lastNode, ladanyi@506: "checkEulerIt: first and last node are not the same"); ladanyi@506: ladanyi@506: for (typename Graph::EdgeIt e(g); e != INVALID; ++e) ladanyi@506: { ladanyi@506: check(visitationNumber[e] == 1, ladanyi@506: "checkEulerIt: not visited or multiple times visited edge found"); ladanyi@506: } ladanyi@506: } ladanyi@506: ladanyi@506: int main() ladanyi@506: { ladanyi@506: typedef ListDigraph Digraph; ladanyi@506: typedef ListGraph Graph; ladanyi@506: ladanyi@506: Digraph digraphWithEulerianCircuit; ladanyi@506: { ladanyi@506: Digraph& g = digraphWithEulerianCircuit; ladanyi@506: ladanyi@506: Digraph::Node n0 = g.addNode(); ladanyi@506: Digraph::Node n1 = g.addNode(); ladanyi@506: Digraph::Node n2 = g.addNode(); ladanyi@506: ladanyi@506: g.addArc(n0, n1); ladanyi@506: g.addArc(n1, n0); ladanyi@506: g.addArc(n1, n2); ladanyi@506: g.addArc(n2, n1); ladanyi@506: } ladanyi@506: ladanyi@506: Digraph digraphWithoutEulerianCircuit; ladanyi@506: { ladanyi@506: Digraph& g = digraphWithoutEulerianCircuit; ladanyi@506: ladanyi@506: Digraph::Node n0 = g.addNode(); ladanyi@506: Digraph::Node n1 = g.addNode(); ladanyi@506: Digraph::Node n2 = g.addNode(); ladanyi@506: ladanyi@506: g.addArc(n0, n1); ladanyi@506: g.addArc(n1, n0); ladanyi@506: g.addArc(n1, n2); ladanyi@506: } ladanyi@506: ladanyi@506: Graph graphWithEulerianCircuit; ladanyi@506: { ladanyi@506: Graph& g = graphWithEulerianCircuit; ladanyi@506: ladanyi@506: Graph::Node n0 = g.addNode(); ladanyi@506: Graph::Node n1 = g.addNode(); ladanyi@506: Graph::Node n2 = g.addNode(); ladanyi@506: ladanyi@506: g.addEdge(n0, n1); ladanyi@506: g.addEdge(n1, n2); ladanyi@506: g.addEdge(n2, n0); ladanyi@506: } ladanyi@506: ladanyi@506: Graph graphWithoutEulerianCircuit; ladanyi@506: { ladanyi@506: Graph& g = graphWithoutEulerianCircuit; ladanyi@506: ladanyi@506: Graph::Node n0 = g.addNode(); ladanyi@506: Graph::Node n1 = g.addNode(); ladanyi@506: Graph::Node n2 = g.addNode(); ladanyi@506: ladanyi@506: g.addEdge(n0, n1); ladanyi@506: g.addEdge(n1, n2); ladanyi@506: } ladanyi@506: ladanyi@506: checkDiEulerIt(digraphWithEulerianCircuit); ladanyi@506: ladanyi@506: checkEulerIt(graphWithEulerianCircuit); ladanyi@506: ladanyi@506: check(eulerian(digraphWithEulerianCircuit), ladanyi@506: "this graph should have an Eulerian circuit"); ladanyi@506: check(!eulerian(digraphWithoutEulerianCircuit), ladanyi@506: "this graph should not have an Eulerian circuit"); ladanyi@506: ladanyi@506: check(eulerian(graphWithEulerianCircuit), ladanyi@506: "this graph should have an Eulerian circuit"); ladanyi@506: check(!eulerian(graphWithoutEulerianCircuit), ladanyi@506: "this graph should have an Eulerian circuit"); ladanyi@506: ladanyi@506: return 0; ladanyi@506: }