2 * src/test/max_matching_test.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
23 #include "test_tools.h"
24 #include <lemon/invalid.h>
25 #include <lemon/list_graph.h>
26 #include <lemon/max_matching.h>
29 using namespace lemon;
33 typedef UndirListGraph Graph;
35 typedef Graph::Edge Edge;
36 typedef Graph::UndirEdgeIt UndirEdgeIt;
37 typedef Graph::IncEdgeIt IncEdgeIt;
38 typedef Graph::NodeIt NodeIt;
39 typedef Graph::Node Node;
44 std::vector<Graph::Node> nodes;
45 for (int i=0; i<13; ++i)
46 nodes.push_back(g.addNode());
48 g.addEdge(nodes[0], nodes[0]);
49 g.addEdge(nodes[6], nodes[10]);
50 g.addEdge(nodes[5], nodes[10]);
51 g.addEdge(nodes[4], nodes[10]);
52 g.addEdge(nodes[3], nodes[11]);
53 g.addEdge(nodes[1], nodes[6]);
54 g.addEdge(nodes[4], nodes[7]);
55 g.addEdge(nodes[1], nodes[8]);
56 g.addEdge(nodes[0], nodes[8]);
57 g.addEdge(nodes[3], nodes[12]);
58 g.addEdge(nodes[6], nodes[9]);
59 g.addEdge(nodes[9], nodes[11]);
60 g.addEdge(nodes[2], nodes[10]);
61 g.addEdge(nodes[10], nodes[8]);
62 g.addEdge(nodes[5], nodes[8]);
63 g.addEdge(nodes[6], nodes[3]);
64 g.addEdge(nodes[0], nodes[5]);
65 g.addEdge(nodes[6], nodes[12]);
67 MaxMatching<Graph> max_matching(g);
68 max_matching.runEdmonds(0);
71 Graph::NodeMap<Node> mate(g,INVALID);
72 max_matching.writeNMapNode(mate);
73 for(NodeIt v(g); v!=INVALID; ++v) {
74 if ( mate[v]!=INVALID ) ++s;
76 int size=(int)s/2; //size will be used as the size of a maxmatching
78 for(NodeIt v(g); v!=INVALID; ++v) {
82 check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
84 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(g);
85 max_matching.writePos(pos0);
87 max_matching.resetMatching();
88 max_matching.runEdmonds(1);
90 max_matching.writeNMapNode(mate);
91 for(NodeIt v(g); v!=INVALID; ++v) {
92 if ( mate[v]!=INVALID ) ++s;
94 check ( (int)s/2 == size, "The size does not equal!" );
96 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(g);
97 max_matching.writePos(pos1);
101 max_matching.writeNMapNode(mate);
102 for(NodeIt v(g); v!=INVALID; ++v) {
103 if ( mate[v]!=INVALID ) ++s;
105 check ( (int)s/2 == size, "The size does not equal!" );
107 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(g);
108 max_matching.writePos(pos2);
110 max_matching.resetMatching();
113 max_matching.writeNMapNode(mate);
114 for(NodeIt v(g); v!=INVALID; ++v) {
115 if ( mate[v]!=INVALID ) ++s;
117 check ( (int)s/2 == size, "The size does not equal!" );
119 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(g);
120 max_matching.writePos(pos);
122 bool ismatching=true;
123 for(NodeIt v(g); v!=INVALID; ++v) {
124 if ( mate[v]!=INVALID ) {
126 if (mate[u]!=v) ismatching=false;
129 check ( ismatching, "It is not a matching!" );
132 for(NodeIt v(g); v!=INVALID; ++v) {
133 if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
137 check ( coincide, "The decompositions do not coincide! " );
140 for(UndirEdgeIt e(g); e!=INVALID; ++e) {
141 if ( (pos[g.target(e)]==max_matching.C && pos[g.source(e)]==max_matching.D) ||
142 (pos[g.target(e)]==max_matching.D && pos[g.source(e)]==max_matching.C) )
145 check ( noedge, "There are edges between D and C!" );
148 Graph::NodeMap<bool> todo(g,true);
150 for(NodeIt v(g); v!=INVALID; ++v) {
151 if ( pos[v]==max_matching.D && todo[v] ) {
160 for(IncEdgeIt e(g,w); e!=INVALID; ++e) {
162 if ( pos[u]==max_matching.D && todo[u] ) {
169 if ( !(comp_size % 2) ) oddcomp=false;
172 check ( oddcomp, "A component of g[D] is not odd." );
175 for(NodeIt v(g); v!=INVALID; ++v) {
176 if ( pos[v]==max_matching.A ) ++barrier;
178 int expected_size=(int)( countNodes(g)-num_comp+barrier)/2;
179 check ( size==expected_size, "The size of the matching is wrong." );