findEdge bugfix.
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
22 #include "test_tools.h"
23 #include <lemon/invalid.h>
24 #include <lemon/list_graph.h>
25 #include <graph_gen.h>
26 #include <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 randomGraph(G, random(5000), random(20000) );
46 MaxMatching<Graph> max_matching(G);
47 max_matching.runEdmonds(0);
50 Graph::NodeMap<Node> mate(G,INVALID);
51 max_matching.writeNMapNode(mate);
52 for(NodeIt v(G); v!=INVALID; ++v) {
53 if ( mate[v]!=INVALID ) ++s;
55 int size=(int)s/2; //size will be used as the size of a maxmatching
57 check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" );
59 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(G);
60 max_matching.writePos(pos0);
62 max_matching.resetPos();
63 max_matching.resetMatching();
64 max_matching.runEdmonds(1);
66 max_matching.writeNMapNode(mate);
67 for(NodeIt v(G); v!=INVALID; ++v) {
68 if ( mate[v]!=INVALID ) ++s;
70 check ( (int)s/2 == size, "The size does not equal!" );
72 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(G);
73 max_matching.writePos(pos1);
75 max_matching.resetPos();
78 max_matching.writeNMapNode(mate);
79 for(NodeIt v(G); v!=INVALID; ++v) {
80 if ( mate[v]!=INVALID ) ++s;
82 check ( (int)s/2 == size, "The size does not equal!" );
84 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(G);
85 max_matching.writePos(pos2);
87 max_matching.resetPos();
88 max_matching.resetMatching();
91 max_matching.writeNMapNode(mate);
92 for(NodeIt v(G); v!=INVALID; ++v) {
93 if ( mate[v]!=INVALID ) ++s;
95 check ( (int)s/2 == size, "The size does not equal!" );
97 Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(G);
98 max_matching.writePos(pos);
100 bool ismatching=true;
101 for(NodeIt v(G); v!=INVALID; ++v) {
102 if ( mate[v]!=INVALID ) {
104 if (mate[u]!=v) ismatching=false;
107 check ( ismatching, "It is not a matching!" );
110 for(NodeIt v(G); v!=INVALID; ++v) {
111 if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) {
115 check ( coincide, "The decompositions do not coincide! " );
118 for(UndirEdgeIt e(G); e!=INVALID; ++e) {
119 if ( (pos[G.target(e)]==max_matching.C && pos[G.source(e)]==max_matching.D) ||
120 (pos[G.target(e)]==max_matching.D && pos[G.source(e)]==max_matching.C) )
123 check ( noedge, "There are edges between D and C!" );
126 Graph::NodeMap<bool> todo(G,true);
128 for(NodeIt v(G); v!=INVALID; ++v) {
129 if ( pos[v]==max_matching.D && todo[v] ) {
138 for(IncEdgeIt e(G,w); e!=INVALID; ++e) {
140 if ( pos[u]==max_matching.D && todo[u] ) {
147 if ( !(comp_size % 2) ) oddcomp=false;
150 check ( oddcomp, "A component of G[D] is not odd." );
153 for(NodeIt v(G); v!=INVALID; ++v) {
154 if ( pos[v]==max_matching.A ) ++barrier;
156 int expected_size=(int)( countNodes(G)-num_comp+barrier)/2;
157 check ( size==expected_size, "The size of the matching is wrong." );
165 unsigned int seed = getpid();
175 return int( double(m) * rand() / (RAND_MAX + 1.0) );
179 /// Generates a random graph with n nodes and m edges.
180 /// Before generating the random graph, \c g.clear() is called.
181 template<typename Graph>
182 void randomGraph(Graph& g, int n, int m) {
184 std::vector<typename Graph::Node> nodes;
185 for (int i=0; i<n; ++i)
186 nodes.push_back(g.addNode());
187 for (int i=0; i<m; ++i)
188 g.addEdge(nodes[random(n)], nodes[random(n)]);