alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2391
|
5 |
* Copyright (C) 2003-2007
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
8 |
*
|
alpar@906
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
12 |
*
|
alpar@906
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
15 |
* purpose.
|
alpar@906
|
16 |
*
|
alpar@906
|
17 |
*/
|
alpar@906
|
18 |
|
alpar@810
|
19 |
#include <iostream>
|
alpar@810
|
20 |
#include <vector>
|
alpar@810
|
21 |
|
alpar@810
|
22 |
#include "test_tools.h"
|
alpar@921
|
23 |
#include <lemon/maps.h>
|
alpar@921
|
24 |
#include <lemon/kruskal.h>
|
alpar@921
|
25 |
#include <lemon/list_graph.h>
|
deba@2424
|
26 |
|
alpar@2260
|
27 |
#include <lemon/concepts/maps.h>
|
alpar@2260
|
28 |
#include <lemon/concepts/graph.h>
|
deba@2424
|
29 |
#include <lemon/concepts/ugraph.h>
|
alpar@810
|
30 |
|
alpar@810
|
31 |
|
alpar@810
|
32 |
using namespace std;
|
alpar@921
|
33 |
using namespace lemon;
|
alpar@810
|
34 |
|
alpar@810
|
35 |
void checkCompileKruskal()
|
alpar@810
|
36 |
{
|
alpar@2260
|
37 |
concepts::WriteMap<concepts::Graph::Edge,bool> w;
|
deba@2424
|
38 |
concepts::WriteMap<concepts::UGraph::UEdge,bool> uw;
|
deba@2424
|
39 |
|
deba@2424
|
40 |
concepts::ReadMap<concepts::Graph::Edge,int> r;
|
deba@2424
|
41 |
concepts::ReadMap<concepts::UGraph::UEdge,int> ur;
|
alpar@810
|
42 |
|
alpar@2260
|
43 |
concepts::Graph g;
|
deba@2424
|
44 |
concepts::UGraph ug;
|
deba@2424
|
45 |
|
deba@2424
|
46 |
kruskal(g, r, w);
|
deba@2424
|
47 |
kruskal(ug, ur, uw);
|
deba@2424
|
48 |
|
deba@2424
|
49 |
std::vector<std::pair<concepts::Graph::Edge, int> > rs;
|
deba@2424
|
50 |
std::vector<std::pair<concepts::UGraph::UEdge, int> > urs;
|
deba@2424
|
51 |
|
deba@2424
|
52 |
kruskal(g, rs, w);
|
deba@2424
|
53 |
kruskal(ug, urs, uw);
|
deba@2424
|
54 |
|
deba@2424
|
55 |
std::vector<concepts::Graph::Edge> ws;
|
deba@2424
|
56 |
std::vector<concepts::UGraph::UEdge> uws;
|
deba@2424
|
57 |
|
deba@2424
|
58 |
kruskal(g, r, ws.begin());
|
deba@2424
|
59 |
kruskal(ug, ur, uws.begin());
|
deba@2424
|
60 |
|
deba@2424
|
61 |
Kruskal<concepts::UGraph,concepts::ReadMap<concepts::UGraph::UEdge,int> >
|
deba@2424
|
62 |
alg(ug, ur);
|
deba@2424
|
63 |
|
deba@2424
|
64 |
alg.init();
|
deba@2424
|
65 |
alg.initPresorted(uws.begin(), uws.end());
|
deba@2424
|
66 |
alg.reinit();
|
deba@2424
|
67 |
|
deba@2424
|
68 |
alg.emptyQueue();
|
deba@2424
|
69 |
|
deba@2424
|
70 |
alg.nextEdge();
|
deba@2424
|
71 |
alg.processNextEdge();
|
deba@2424
|
72 |
alg.processEdge(concepts::UGraph::UEdge());
|
deba@2424
|
73 |
|
deba@2424
|
74 |
alg.run();
|
deba@2424
|
75 |
|
deba@2424
|
76 |
alg.treeMap();
|
deba@2424
|
77 |
alg.tree(concepts::UGraph::UEdge());
|
alpar@810
|
78 |
}
|
alpar@810
|
79 |
|
alpar@810
|
80 |
int main() {
|
alpar@810
|
81 |
|
deba@2424
|
82 |
typedef ListUGraph::Node Node;
|
deba@2424
|
83 |
typedef ListUGraph::UEdge UEdge;
|
deba@2424
|
84 |
typedef ListUGraph::NodeIt NodeIt;
|
deba@2424
|
85 |
typedef ListUGraph::EdgeIt EdgeIt;
|
alpar@810
|
86 |
|
deba@2424
|
87 |
ListUGraph G;
|
alpar@810
|
88 |
|
alpar@810
|
89 |
Node s=G.addNode();
|
alpar@810
|
90 |
Node v1=G.addNode();
|
alpar@810
|
91 |
Node v2=G.addNode();
|
alpar@810
|
92 |
Node v3=G.addNode();
|
alpar@810
|
93 |
Node v4=G.addNode();
|
alpar@810
|
94 |
Node t=G.addNode();
|
alpar@810
|
95 |
|
deba@2424
|
96 |
UEdge e1 = G.addEdge(s, v1);
|
deba@2424
|
97 |
UEdge e2 = G.addEdge(s, v2);
|
deba@2424
|
98 |
UEdge e3 = G.addEdge(v1, v2);
|
deba@2424
|
99 |
UEdge e4 = G.addEdge(v2, v1);
|
deba@2424
|
100 |
UEdge e5 = G.addEdge(v1, v3);
|
deba@2424
|
101 |
UEdge e6 = G.addEdge(v3, v2);
|
deba@2424
|
102 |
UEdge e7 = G.addEdge(v2, v4);
|
deba@2424
|
103 |
UEdge e8 = G.addEdge(v4, v3);
|
deba@2424
|
104 |
UEdge e9 = G.addEdge(v3, t);
|
deba@2424
|
105 |
UEdge e10 = G.addEdge(v4, t);
|
alpar@810
|
106 |
|
deba@2424
|
107 |
typedef ListUGraph::UEdgeMap<int> ECostMap;
|
deba@2424
|
108 |
typedef ListUGraph::UEdgeMap<bool> EBoolMap;
|
alpar@810
|
109 |
|
alpar@810
|
110 |
ECostMap edge_cost_map(G, 2);
|
alpar@810
|
111 |
EBoolMap tree_map(G);
|
alpar@810
|
112 |
|
alpar@810
|
113 |
|
alpar@810
|
114 |
//Test with const map.
|
deba@2424
|
115 |
check(kruskal(G, ConstMap<ListUGraph::UEdge,int>(2), tree_map)==10,
|
alpar@810
|
116 |
"Total cost should be 10");
|
alpar@810
|
117 |
//Test with a edge map (filled with uniform costs).
|
alpar@1557
|
118 |
check(kruskal(G, edge_cost_map, tree_map)==10,
|
alpar@810
|
119 |
"Total cost should be 10");
|
alpar@810
|
120 |
|
alpar@810
|
121 |
edge_cost_map.set(e1, -10);
|
alpar@810
|
122 |
edge_cost_map.set(e2, -9);
|
alpar@810
|
123 |
edge_cost_map.set(e3, -8);
|
alpar@810
|
124 |
edge_cost_map.set(e4, -7);
|
alpar@810
|
125 |
edge_cost_map.set(e5, -6);
|
alpar@810
|
126 |
edge_cost_map.set(e6, -5);
|
alpar@810
|
127 |
edge_cost_map.set(e7, -4);
|
alpar@810
|
128 |
edge_cost_map.set(e8, -3);
|
alpar@810
|
129 |
edge_cost_map.set(e9, -2);
|
alpar@810
|
130 |
edge_cost_map.set(e10, -1);
|
alpar@810
|
131 |
|
deba@2424
|
132 |
vector<UEdge> tree_edge_vec(5);
|
alpar@810
|
133 |
|
alpar@810
|
134 |
//Test with a edge map and inserter.
|
alpar@1557
|
135 |
check(kruskal(G, edge_cost_map,
|
alpar@1557
|
136 |
tree_edge_vec.begin())
|
alpar@810
|
137 |
==-31,
|
alpar@810
|
138 |
"Total cost should be -31.");
|
alpar@1557
|
139 |
|
klao@885
|
140 |
tree_edge_vec.clear();
|
klao@885
|
141 |
|
alpar@1557
|
142 |
check(kruskal(G, edge_cost_map,
|
alpar@1557
|
143 |
back_inserter(tree_edge_vec))
|
alpar@1557
|
144 |
==-31,
|
alpar@1557
|
145 |
"Total cost should be -31.");
|
alpar@1557
|
146 |
|
deba@2424
|
147 |
// tree_edge_vec.clear();
|
alpar@1557
|
148 |
|
deba@2424
|
149 |
// //The above test could also be coded like this:
|
deba@2424
|
150 |
// check(kruskal(G,
|
deba@2424
|
151 |
// makeKruskalMapInput(G, edge_cost_map),
|
deba@2424
|
152 |
// makeKruskalSequenceOutput(back_inserter(tree_edge_vec)))
|
deba@2424
|
153 |
// ==-31,
|
deba@2424
|
154 |
// "Total cost should be -31.");
|
klao@885
|
155 |
|
alpar@810
|
156 |
check(tree_edge_vec.size()==5,"The tree should have 5 edges.");
|
alpar@810
|
157 |
|
alpar@810
|
158 |
check(tree_edge_vec[0]==e1 &&
|
alpar@810
|
159 |
tree_edge_vec[1]==e2 &&
|
alpar@810
|
160 |
tree_edge_vec[2]==e5 &&
|
alpar@810
|
161 |
tree_edge_vec[3]==e7 &&
|
alpar@810
|
162 |
tree_edge_vec[4]==e9,
|
alpar@810
|
163 |
"Wrong tree.");
|
alpar@810
|
164 |
|
deba@2424
|
165 |
Kruskal<ListUGraph, ECostMap> ka(G, edge_cost_map);
|
deba@2424
|
166 |
|
deba@2424
|
167 |
ka.run();
|
deba@2424
|
168 |
|
deba@2424
|
169 |
check(ka.tree(e1) &&
|
deba@2424
|
170 |
ka.tree(e2) &&
|
deba@2424
|
171 |
ka.tree(e5) &&
|
deba@2424
|
172 |
ka.tree(e7) &&
|
deba@2424
|
173 |
ka.tree(e9),
|
deba@2424
|
174 |
"Wrong tree.");
|
deba@2424
|
175 |
|
deba@2424
|
176 |
check(ka.treeValue() == -31,
|
deba@2424
|
177 |
"Total cost should be -31.");
|
deba@2424
|
178 |
|
alpar@810
|
179 |
return 0;
|
alpar@810
|
180 |
}
|