3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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
22 #include "test_tools.h"
23 #include <lemon/maps.h>
24 #include <lemon/kruskal.h>
25 #include <lemon/list_graph.h>
27 #include <lemon/concepts/maps.h>
28 #include <lemon/concepts/graph.h>
29 #include <lemon/concepts/ugraph.h>
33 using namespace lemon;
35 void checkCompileKruskal()
37 concepts::WriteMap<concepts::Graph::Edge,bool> w;
38 concepts::WriteMap<concepts::UGraph::UEdge,bool> uw;
40 concepts::ReadMap<concepts::Graph::Edge,int> r;
41 concepts::ReadMap<concepts::UGraph::UEdge,int> ur;
49 std::vector<std::pair<concepts::Graph::Edge, int> > rs;
50 std::vector<std::pair<concepts::UGraph::UEdge, int> > urs;
55 std::vector<concepts::Graph::Edge> ws;
56 std::vector<concepts::UGraph::UEdge> uws;
58 kruskal(g, r, ws.begin());
59 kruskal(ug, ur, uws.begin());
61 Kruskal<concepts::UGraph,concepts::ReadMap<concepts::UGraph::UEdge,int> >
65 alg.initPresorted(uws.begin(), uws.end());
71 alg.processNextEdge();
72 alg.processEdge(concepts::UGraph::UEdge());
77 alg.tree(concepts::UGraph::UEdge());
82 typedef ListUGraph::Node Node;
83 typedef ListUGraph::UEdge UEdge;
84 typedef ListUGraph::NodeIt NodeIt;
85 typedef ListUGraph::EdgeIt EdgeIt;
96 UEdge e1 = G.addEdge(s, v1);
97 UEdge e2 = G.addEdge(s, v2);
98 UEdge e3 = G.addEdge(v1, v2);
99 UEdge e4 = G.addEdge(v2, v1);
100 UEdge e5 = G.addEdge(v1, v3);
101 UEdge e6 = G.addEdge(v3, v2);
102 UEdge e7 = G.addEdge(v2, v4);
103 UEdge e8 = G.addEdge(v4, v3);
104 UEdge e9 = G.addEdge(v3, t);
105 UEdge e10 = G.addEdge(v4, t);
107 typedef ListUGraph::UEdgeMap<int> ECostMap;
108 typedef ListUGraph::UEdgeMap<bool> EBoolMap;
110 ECostMap edge_cost_map(G, 2);
111 EBoolMap tree_map(G);
114 //Test with const map.
115 check(kruskal(G, ConstMap<ListUGraph::UEdge,int>(2), tree_map)==10,
116 "Total cost should be 10");
117 //Test with a edge map (filled with uniform costs).
118 check(kruskal(G, edge_cost_map, tree_map)==10,
119 "Total cost should be 10");
121 edge_cost_map.set(e1, -10);
122 edge_cost_map.set(e2, -9);
123 edge_cost_map.set(e3, -8);
124 edge_cost_map.set(e4, -7);
125 edge_cost_map.set(e5, -6);
126 edge_cost_map.set(e6, -5);
127 edge_cost_map.set(e7, -4);
128 edge_cost_map.set(e8, -3);
129 edge_cost_map.set(e9, -2);
130 edge_cost_map.set(e10, -1);
132 vector<UEdge> tree_edge_vec(5);
134 //Test with a edge map and inserter.
135 check(kruskal(G, edge_cost_map,
136 tree_edge_vec.begin())
138 "Total cost should be -31.");
140 tree_edge_vec.clear();
142 check(kruskal(G, edge_cost_map,
143 back_inserter(tree_edge_vec))
145 "Total cost should be -31.");
147 // tree_edge_vec.clear();
149 // //The above test could also be coded like this:
151 // makeKruskalMapInput(G, edge_cost_map),
152 // makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
154 // "Total cost should be -31.");
156 check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
158 check(tree_edge_vec[0]==e1 &&
159 tree_edge_vec[1]==e2 &&
160 tree_edge_vec[2]==e5 &&
161 tree_edge_vec[3]==e7 &&
162 tree_edge_vec[4]==e9,
165 Kruskal<ListUGraph, ECostMap> ka(G, edge_cost_map);
176 check(ka.treeValue() == -31,
177 "Total cost should be -31.");