1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
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;
99 MinCostArbType mcarb_test(g, cost);
100 const MinCostArbType& const_mcarb_test = mcarb_test;
103 .arborescenceMap(arb)
108 mcarb_test.addSource(s);
110 n = mcarb_test.processNextNode();
111 b = const_mcarb_test.emptyQueue();
112 i = const_mcarb_test.queueSize();
114 c = const_mcarb_test.arborescenceCost();
115 b = const_mcarb_test.arborescence(e);
116 e = const_mcarb_test.pred(n);
117 const MinCostArbType::ArborescenceMap &am =
118 const_mcarb_test.arborescenceMap();
119 const MinCostArbType::PredMap &pm =
120 const_mcarb_test.predMap();
121 b = const_mcarb_test.reached(n);
122 b = const_mcarb_test.processed(n);
124 i = const_mcarb_test.dualNum();
125 c = const_mcarb_test.dualValue();
126 i = const_mcarb_test.dualSize(i);
127 c = const_mcarb_test.dualValue(i);
129 ignore_unused_variable_warning(am);
130 ignore_unused_variable_warning(pm);
134 typedef SmartDigraph Digraph;
135 DIGRAPH_TYPEDEFS(Digraph);
137 typedef Digraph::ArcMap<double> CostMap;
140 CostMap cost(digraph);
143 std::istringstream is(test_lgf);
144 digraphReader(digraph, is).
145 arcMap("cost", cost).
146 node("source", source).run();
148 MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
151 vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
153 for (int i = 0; i < mca.dualNum(); ++i) {
154 dualSolution[i].first = mca.dualValue(i);
155 for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
156 it != INVALID; ++it) {
157 dualSolution[i].second.insert(it);
161 for (ArcIt it(digraph); it != INVALID; ++it) {
162 if (mca.reached(digraph.source(it))) {
164 for (int i = 0; i < int(dualSolution.size()); ++i) {
165 if (dualSolution[i].second.find(digraph.target(it))
166 != dualSolution[i].second.end() &&
167 dualSolution[i].second.find(digraph.source(it))
168 == dualSolution[i].second.end()) {
169 sum += dualSolution[i].first;
172 if (mca.arborescence(it)) {
173 check(sum == cost[it], "Invalid dual solution");
175 check(sum <= cost[it], "Invalid dual solution");
180 check(mca.dualValue() == mca.arborescenceCost(), "Invalid dual solution");
182 check(mca.reached(source), "Invalid arborescence");
183 for (ArcIt a(digraph); a != INVALID; ++a) {
184 check(!mca.reached(digraph.source(a)) ||
185 mca.reached(digraph.target(a)), "Invalid arborescence");
188 for (NodeIt n(digraph); n != INVALID; ++n) {
189 if (!mca.reached(n)) continue;
191 for (InArcIt a(digraph, n); a != INVALID; ++a) {
192 if (mca.arborescence(a)) {
193 check(mca.pred(n) == a, "Invalid arborescence");
197 check((n == source ? cnt == 0 : cnt == 1), "Invalid arborescence");
200 Digraph::ArcMap<bool> arborescence(digraph);
201 check(mca.arborescenceCost() ==
202 minCostArborescence(digraph, cost, source, arborescence),
203 "Wrong result of the function interface");