| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/euler.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <test/test_tools.h> |
| 22 | 22 |
|
| 23 | 23 |
using namespace lemon; |
| 24 | 24 |
|
| 25 | 25 |
template <typename Digraph> |
| 26 | 26 |
void checkDiEulerIt(const Digraph& g) |
| 27 | 27 |
{
|
| 28 |
typename Digraph::template ArcMap<int> visitationNumber(g); |
|
| 28 |
typename Digraph::template ArcMap<int> visitationNumber(g, 0); |
|
| 29 | 29 |
|
| 30 | 30 |
DiEulerIt<Digraph> e(g); |
| 31 | 31 |
typename Digraph::Node firstNode = g.source(e); |
| 32 |
typename Digraph::Node lastNode; |
|
| 32 |
typename Digraph::Node lastNode = g.target(e); |
|
| 33 | 33 |
|
| 34 | 34 |
for (; e != INVALID; ++e) |
| 35 | 35 |
{
|
| 36 | 36 |
if (e != INVALID) |
| 37 | 37 |
{
|
| 38 | 38 |
lastNode = g.target(e); |
| 39 | 39 |
} |
| 40 | 40 |
++visitationNumber[e]; |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 | 43 |
check(firstNode == lastNode, |
| 44 | 44 |
"checkDiEulerIt: first and last node are not the same"); |
| 45 | 45 |
|
| 46 | 46 |
for (typename Digraph::ArcIt a(g); a != INVALID; ++a) |
| 47 | 47 |
{
|
| 48 | 48 |
check(visitationNumber[a] == 1, |
| 49 | 49 |
"checkDiEulerIt: not visited or multiple times visited arc found"); |
| 50 | 50 |
} |
| 51 | 51 |
} |
| 52 | 52 |
|
| 53 | 53 |
template <typename Graph> |
| 54 | 54 |
void checkEulerIt(const Graph& g) |
| 55 | 55 |
{
|
| 56 |
typename Graph::template EdgeMap<int> visitationNumber(g); |
|
| 56 |
typename Graph::template EdgeMap<int> visitationNumber(g, 0); |
|
| 57 | 57 |
|
| 58 | 58 |
EulerIt<Graph> e(g); |
| 59 | 59 |
typename Graph::Node firstNode = g.u(e); |
| 60 |
typename Graph::Node lastNode; |
|
| 60 |
typename Graph::Node lastNode = g.v(e); |
|
| 61 | 61 |
|
| 62 | 62 |
for (; e != INVALID; ++e) |
| 63 | 63 |
{
|
| 64 | 64 |
if (e != INVALID) |
| 65 | 65 |
{
|
| 66 | 66 |
lastNode = g.v(e); |
| 67 | 67 |
} |
| 68 | 68 |
++visitationNumber[e]; |
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 | 71 |
check(firstNode == lastNode, |
| 72 | 72 |
"checkEulerIt: first and last node are not the same"); |
| 73 | 73 |
|
| 74 | 74 |
for (typename Graph::EdgeIt e(g); e != INVALID; ++e) |
| 75 | 75 |
{
|
| 76 | 76 |
check(visitationNumber[e] == 1, |
| 77 | 77 |
"checkEulerIt: not visited or multiple times visited edge found"); |
| 78 | 78 |
} |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
int main() |
| 82 | 82 |
{
|
| 83 | 83 |
typedef ListDigraph Digraph; |
| 84 | 84 |
typedef ListGraph Graph; |
| 85 | 85 |
|
| 86 | 86 |
Digraph digraphWithEulerianCircuit; |
| 87 | 87 |
{
|
| 88 | 88 |
Digraph& g = digraphWithEulerianCircuit; |
| 89 | 89 |
|
| 90 | 90 |
Digraph::Node n0 = g.addNode(); |
| 91 | 91 |
Digraph::Node n1 = g.addNode(); |
| 92 | 92 |
Digraph::Node n2 = g.addNode(); |
| 93 | 93 |
|
| 94 | 94 |
g.addArc(n0, n1); |
| 95 | 95 |
g.addArc(n1, n0); |
| 96 | 96 |
g.addArc(n1, n2); |
| 97 | 97 |
g.addArc(n2, n1); |
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 | 100 |
Digraph digraphWithoutEulerianCircuit; |
| 101 | 101 |
{
|
| 102 | 102 |
Digraph& g = digraphWithoutEulerianCircuit; |
| 103 | 103 |
|
| 104 | 104 |
Digraph::Node n0 = g.addNode(); |
| 105 | 105 |
Digraph::Node n1 = g.addNode(); |
| 106 | 106 |
Digraph::Node n2 = g.addNode(); |
| 107 | 107 |
|
| 108 | 108 |
g.addArc(n0, n1); |
| 109 | 109 |
g.addArc(n1, n0); |
| 110 | 110 |
g.addArc(n1, n2); |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
Graph graphWithEulerianCircuit; |
| 114 | 114 |
{
|
| 115 | 115 |
Graph& g = graphWithEulerianCircuit; |
| 116 | 116 |
|
| 117 | 117 |
Graph::Node n0 = g.addNode(); |
| 118 | 118 |
Graph::Node n1 = g.addNode(); |
| 119 | 119 |
Graph::Node n2 = g.addNode(); |
| 120 | 120 |
|
| 121 | 121 |
g.addEdge(n0, n1); |
| 122 | 122 |
g.addEdge(n1, n2); |
| 123 | 123 |
g.addEdge(n2, n0); |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
Graph graphWithoutEulerianCircuit; |
| 127 | 127 |
{
|
| 128 | 128 |
Graph& g = graphWithoutEulerianCircuit; |
| 129 | 129 |
|
| 130 | 130 |
Graph::Node n0 = g.addNode(); |
| 131 | 131 |
Graph::Node n1 = g.addNode(); |
| 132 | 132 |
Graph::Node n2 = g.addNode(); |
| 133 | 133 |
|
| 134 | 134 |
g.addEdge(n0, n1); |
| 135 | 135 |
g.addEdge(n1, n2); |
| 136 | 136 |
} |
| 137 | 137 |
|
| 138 | 138 |
checkDiEulerIt(digraphWithEulerianCircuit); |
| 139 | 139 |
|
| 140 | 140 |
checkEulerIt(graphWithEulerianCircuit); |
| 141 | 141 |
|
| 142 | 142 |
check(eulerian(digraphWithEulerianCircuit), |
| 143 | 143 |
"this graph should have an Eulerian circuit"); |
| 144 | 144 |
check(!eulerian(digraphWithoutEulerianCircuit), |
| 145 | 145 |
"this graph should not have an Eulerian circuit"); |
| 146 | 146 |
|
| 147 | 147 |
check(eulerian(graphWithEulerianCircuit), |
| 148 | 148 |
"this graph should have an Eulerian circuit"); |
| 149 | 149 |
check(!eulerian(graphWithoutEulerianCircuit), |
| 150 | 150 |
"this graph should have an Eulerian circuit"); |
| 151 | 151 |
|
| 152 | 152 |
return 0; |
| 153 | 153 |
} |
0 comments (0 inline)