Backport relevant parts of bugfixes [ad22262328b3], [61fdd06833a6] and [4add05447ca0] to branch 1.2 (#623)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
24 #include <lemon/smart_graph.h>
25 #include <lemon/min_cost_arborescence.h>
26 #include <lemon/lgf_reader.h>
27 #include <lemon/concepts/digraph.h>
29 #include "test_tools.h"
31 using namespace lemon;
34 const char test_lgf[] =
75 void checkMinCostArborescenceCompile()
78 typedef concepts::Digraph Digraph;
79 typedef concepts::ReadMap<Digraph::Arc, VType> CostMap;
80 typedef Digraph::Node Node;
81 typedef Digraph::Arc Arc;
82 typedef concepts::WriteMap<Digraph::Arc, bool> ArbMap;
83 typedef concepts::ReadWriteMap<Digraph::Node, Digraph::Arc> PredMap;
85 typedef MinCostArborescence<Digraph, CostMap>::
86 SetArborescenceMap<ArbMap>::
87 SetPredMap<PredMap>::Create MinCostArbType;
94 ::lemon::ignore_unused_variable_warning(c,b);
100 MinCostArbType mcarb_test(g, cost);
101 const MinCostArbType& const_mcarb_test = mcarb_test;
104 .arborescenceMap(arb)
109 mcarb_test.addSource(s);
111 n = mcarb_test.processNextNode();
112 b = const_mcarb_test.emptyQueue();
113 i = const_mcarb_test.queueSize();
115 c = const_mcarb_test.arborescenceCost();
116 b = const_mcarb_test.arborescence(e);
117 e = const_mcarb_test.pred(n);
118 const MinCostArbType::ArborescenceMap &am =
119 const_mcarb_test.arborescenceMap();
120 const MinCostArbType::PredMap &pm =
121 const_mcarb_test.predMap();
122 b = const_mcarb_test.reached(n);
123 b = const_mcarb_test.processed(n);
125 i = const_mcarb_test.dualNum();
126 c = const_mcarb_test.dualValue();
127 i = const_mcarb_test.dualSize(i);
128 c = const_mcarb_test.dualValue(i);
130 ::lemon::ignore_unused_variable_warning(am);
131 ::lemon::ignore_unused_variable_warning(pm);
135 typedef SmartDigraph Digraph;
136 DIGRAPH_TYPEDEFS(Digraph);
138 typedef Digraph::ArcMap<double> CostMap;
141 CostMap cost(digraph);
144 std::istringstream is(test_lgf);
145 digraphReader(digraph, is).
146 arcMap("cost", cost).
147 node("source", source).run();
149 MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
152 vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
154 for (int i = 0; i < mca.dualNum(); ++i) {
155 dualSolution[i].first = mca.dualValue(i);
156 for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
157 it != INVALID; ++it) {
158 dualSolution[i].second.insert(it);
162 for (ArcIt it(digraph); it != INVALID; ++it) {
163 if (mca.reached(digraph.source(it))) {
165 for (int i = 0; i < int(dualSolution.size()); ++i) {
166 if (dualSolution[i].second.find(digraph.target(it))
167 != dualSolution[i].second.end() &&
168 dualSolution[i].second.find(digraph.source(it))
169 == dualSolution[i].second.end()) {
170 sum += dualSolution[i].first;
173 if (mca.arborescence(it)) {
174 check(sum == cost[it], "Invalid dual solution");
176 check(sum <= cost[it], "Invalid dual solution");
181 check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
183 check(mca.reached(source), "Invalid arborescence");
184 for (ArcIt a(digraph); a != INVALID; ++a) {
185 check(!mca.reached(digraph.source(a)) ||
186 mca.reached(digraph.target(a)), "Invalid arborescence");
189 for (NodeIt n(digraph); n != INVALID; ++n) {
190 if (!mca.reached(n)) continue;
192 for (InArcIt a(digraph, n); a != INVALID; ++a) {
193 if (mca.arborescence(a)) {
194 check(mca.pred(n) == a, "Invalid arborescence");
198 check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
201 Digraph::ArcMap<bool> arborescence(digraph);
202 check(mca.arborescenceCost() ==
203 minCostArborescence(digraph, cost, source, arborescence),
204 "Wrong result of the function interface");