1 | /* -*- C++ -*- |
---|
2 | * src/test/max_matching_test.cc - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
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. |
---|
10 | * |
---|
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 |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #include <iostream> |
---|
18 | #include <vector> |
---|
19 | #include <queue> |
---|
20 | #include <math.h> |
---|
21 | #include <cstdlib> |
---|
22 | |
---|
23 | #include "test_tools.h" |
---|
24 | #include <lemon/invalid.h> |
---|
25 | #include <lemon/list_graph.h> |
---|
26 | #include <lemon/max_matching.h> |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | using namespace lemon; |
---|
30 | |
---|
31 | int main() { |
---|
32 | |
---|
33 | typedef UndirListGraph Graph; |
---|
34 | |
---|
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; |
---|
40 | |
---|
41 | Graph g; |
---|
42 | g.clear(); |
---|
43 | |
---|
44 | std::vector<Graph::Node> nodes; |
---|
45 | for (int i=0; i<13; ++i) |
---|
46 | nodes.push_back(g.addNode()); |
---|
47 | |
---|
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]); |
---|
66 | |
---|
67 | MaxMatching<Graph> max_matching(g); |
---|
68 | max_matching.runEdmonds(0); |
---|
69 | |
---|
70 | int s=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; |
---|
75 | } |
---|
76 | int size=(int)s/2; //size will be used as the size of a maxmatching |
---|
77 | |
---|
78 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
79 | max_matching.mate(v); |
---|
80 | } |
---|
81 | |
---|
82 | check ( size == max_matching.size(), "mate() returns a different size matching than max_matching.size()" ); |
---|
83 | |
---|
84 | Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos0(g); |
---|
85 | max_matching.writePos(pos0); |
---|
86 | |
---|
87 | max_matching.resetMatching(); |
---|
88 | max_matching.runEdmonds(1); |
---|
89 | s=0; |
---|
90 | max_matching.writeNMapNode(mate); |
---|
91 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
92 | if ( mate[v]!=INVALID ) ++s; |
---|
93 | } |
---|
94 | check ( (int)s/2 == size, "The size does not equal!" ); |
---|
95 | |
---|
96 | Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos1(g); |
---|
97 | max_matching.writePos(pos1); |
---|
98 | |
---|
99 | max_matching.run(); |
---|
100 | s=0; |
---|
101 | max_matching.writeNMapNode(mate); |
---|
102 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
103 | if ( mate[v]!=INVALID ) ++s; |
---|
104 | } |
---|
105 | check ( (int)s/2 == size, "The size does not equal!" ); |
---|
106 | |
---|
107 | Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos2(g); |
---|
108 | max_matching.writePos(pos2); |
---|
109 | |
---|
110 | max_matching.resetMatching(); |
---|
111 | max_matching.run(); |
---|
112 | s=0; |
---|
113 | max_matching.writeNMapNode(mate); |
---|
114 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
115 | if ( mate[v]!=INVALID ) ++s; |
---|
116 | } |
---|
117 | check ( (int)s/2 == size, "The size does not equal!" ); |
---|
118 | |
---|
119 | Graph::NodeMap<MaxMatching<Graph>::pos_enum> pos(g); |
---|
120 | max_matching.writePos(pos); |
---|
121 | |
---|
122 | bool ismatching=true; |
---|
123 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
124 | if ( mate[v]!=INVALID ) { |
---|
125 | Node u=mate[v]; |
---|
126 | if (mate[u]!=v) ismatching=false; |
---|
127 | } |
---|
128 | } |
---|
129 | check ( ismatching, "It is not a matching!" ); |
---|
130 | |
---|
131 | bool coincide=true; |
---|
132 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
133 | if ( pos0[v] != pos1[v] || pos1[v]!=pos2[v] || pos2[v]!=pos[v] ) { |
---|
134 | coincide=false; |
---|
135 | } |
---|
136 | } |
---|
137 | check ( coincide, "The decompositions do not coincide! " ); |
---|
138 | |
---|
139 | bool noedge=true; |
---|
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) ) |
---|
143 | noedge=false; |
---|
144 | } |
---|
145 | check ( noedge, "There are edges between D and C!" ); |
---|
146 | |
---|
147 | bool oddcomp=true; |
---|
148 | Graph::NodeMap<bool> todo(g,true); |
---|
149 | int num_comp=0; |
---|
150 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
151 | if ( pos[v]==max_matching.D && todo[v] ) { |
---|
152 | int comp_size=1; |
---|
153 | ++num_comp; |
---|
154 | std::queue<Node> Q; |
---|
155 | Q.push(v); |
---|
156 | todo.set(v,false); |
---|
157 | while (!Q.empty()) { |
---|
158 | Node w=Q.front(); |
---|
159 | Q.pop(); |
---|
160 | for(IncEdgeIt e(g,w); e!=INVALID; ++e) { |
---|
161 | Node u=g.target(e); |
---|
162 | if ( pos[u]==max_matching.D && todo[u] ) { |
---|
163 | ++comp_size; |
---|
164 | Q.push(u); |
---|
165 | todo.set(u,false); |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | if ( !(comp_size % 2) ) oddcomp=false; |
---|
170 | } |
---|
171 | } |
---|
172 | check ( oddcomp, "A component of g[D] is not odd." ); |
---|
173 | |
---|
174 | int barrier=0; |
---|
175 | for(NodeIt v(g); v!=INVALID; ++v) { |
---|
176 | if ( pos[v]==max_matching.A ) ++barrier; |
---|
177 | } |
---|
178 | int expected_size=(int)( countNodes(g)-num_comp+barrier)/2; |
---|
179 | check ( size==expected_size, "The size of the matching is wrong." ); |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|