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>
28 #include "test_tools.h"
30 using namespace lemon;
33 const char test_lgf[] =
74 typedef SmartDigraph Digraph;
75 DIGRAPH_TYPEDEFS(Digraph);
77 typedef Digraph::ArcMap<double> CostMap;
80 CostMap cost(digraph);
83 std::istringstream is(test_lgf);
84 digraphReader(digraph, is).
86 node("source", source).run();
88 MinCostArborescence<Digraph, CostMap> mca(digraph, cost);
91 vector<pair<double, set<Node> > > dualSolution(mca.dualNum());
93 for (int i = 0; i < mca.dualNum(); ++i) {
94 dualSolution[i].first = mca.dualValue(i);
95 for (MinCostArborescence<Digraph, CostMap>::DualIt it(mca, i);
96 it != INVALID; ++it) {
97 dualSolution[i].second.insert(it);
101 for (ArcIt it(digraph); it != INVALID; ++it) {
102 if (mca.reached(digraph.source(it))) {
104 for (int i = 0; i < int(dualSolution.size()); ++i) {
105 if (dualSolution[i].second.find(digraph.target(it))
106 != dualSolution[i].second.end() &&
107 dualSolution[i].second.find(digraph.source(it))
108 == dualSolution[i].second.end()) {
109 sum += dualSolution[i].first;
112 if (mca.arborescence(it)) {
113 check(sum == cost[it], "INVALID DUAL");
115 check(sum <= cost[it], "INVALID DUAL");
120 check(mca.dualValue() == mca.arborescenceValue(), "INVALID DUAL");
122 check(mca.reached(source), "INVALID ARBORESCENCE");
123 for (ArcIt a(digraph); a != INVALID; ++a) {
124 check(!mca.reached(digraph.source(a)) ||
125 mca.reached(digraph.target(a)), "INVALID ARBORESCENCE");
128 for (NodeIt n(digraph); n != INVALID; ++n) {
129 if (!mca.reached(n)) continue;
131 for (InArcIt a(digraph, n); a != INVALID; ++a) {
132 if (mca.arborescence(a)) {
133 check(mca.pred(n) == a, "INVALID ARBORESCENCE");
137 check((n == source ? cnt == 0 : cnt == 1), "INVALID ARBORESCENCE");
140 Digraph::ArcMap<bool> arborescence(digraph);
141 check(mca.arborescenceValue() ==
142 minCostArborescence(digraph, cost, source, arborescence),