beckerjc@150
|
1 |
// -*- c++ -*- //
|
beckerjc@150
|
2 |
#ifndef HUGO_KRUSKAL_H
|
beckerjc@150
|
3 |
#define HUGO_KRUSKAL_H
|
beckerjc@150
|
4 |
|
beckerjc@150
|
5 |
#include "unionfind.h"
|
beckerjc@150
|
6 |
#include <algorithm>
|
beckerjc@150
|
7 |
|
beckerjc@150
|
8 |
namespace hugo {
|
beckerjc@150
|
9 |
|
beckerjc@246
|
10 |
template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
|
beckerjc@246
|
11 |
typename EdgeCostMap::ValueType
|
beckerjc@246
|
12 |
kruskal1(Graph const& G, EdgeCostMap const& edge_costs,
|
beckerjc@246
|
13 |
RetEdgeBoolMap &ret_bool_map);
|
beckerjc@150
|
14 |
|
beckerjc@246
|
15 |
template <typename Graph, typename EdgeCostMap, typename RetIterator>
|
beckerjc@246
|
16 |
typename EdgeCostMap::ValueType
|
beckerjc@246
|
17 |
kruskal2(Graph const& G, EdgeCostMap const& edge_costs,
|
beckerjc@246
|
18 |
RetIterator ret_iterator);
|
beckerjc@246
|
19 |
|
beckerjc@246
|
20 |
// FIXME: ret_iterator nem lehet referencia!!!
|
beckerjc@246
|
21 |
|
beckerjc@246
|
22 |
template <typename Graph, typename EdgeCostPairVec, typename RetEdgeBoolMap>
|
beckerjc@246
|
23 |
typename EdgeCostPairVec::value_type::second_type
|
beckerjc@246
|
24 |
kruskal3(Graph const& G, EdgeCostPairVec const& edge_cost_pair_vec,
|
beckerjc@246
|
25 |
RetEdgeBoolMap &ret_bool_map);
|
beckerjc@246
|
26 |
|
beckerjc@246
|
27 |
template <typename Graph, typename EdgeCostPairVec, typename RetIterator>
|
beckerjc@246
|
28 |
typename EdgeCostPairVec::value_type::second_type
|
beckerjc@246
|
29 |
kruskal4(Graph const& G, EdgeCostPairVec const& edge_cost_pair_vec,
|
beckerjc@246
|
30 |
RetIterator &ret_iterator);
|
beckerjc@246
|
31 |
|
beckerjc@246
|
32 |
template <typename Graph, typename EdgePairVec, typename RetEdgeBoolMap>
|
beckerjc@246
|
33 |
int
|
beckerjc@246
|
34 |
kruskal5(Graph const& G, EdgePairVec const& edge_pair_vec,
|
beckerjc@246
|
35 |
RetEdgeBoolMap &ret_bool_map);
|
beckerjc@246
|
36 |
|
beckerjc@246
|
37 |
template <typename Graph, typename EdgePairVec, typename RetIterator>
|
beckerjc@246
|
38 |
int
|
beckerjc@246
|
39 |
kruskal6(Graph const& G, EdgePairVec const& edge_pair_vec,
|
beckerjc@246
|
40 |
RetIterator &ret_iterator);
|
beckerjc@246
|
41 |
|
beckerjc@246
|
42 |
|
beckerjc@246
|
43 |
template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap,
|
beckerjc@246
|
44 |
typename RetIterator>
|
beckerjc@246
|
45 |
typename EdgeCostMap::ValueType
|
beckerjc@246
|
46 |
kruskal7(Graph const& G, EdgeCostMap const& edge_costs,
|
beckerjc@246
|
47 |
RetEdgeBoolMap &ret_bool_map, RetIterator &ret_iterator);
|
beckerjc@246
|
48 |
|
beckerjc@246
|
49 |
template <typename Graph, typename EdgeCostPairVec, typename RetEdgeBoolMap,
|
beckerjc@246
|
50 |
typename RetIterator>
|
beckerjc@246
|
51 |
typename EdgeCostPairVec::value_type::second_type
|
beckerjc@246
|
52 |
kruskal8(Graph const& G, EdgeCostPairVec const& edge_cost_pair_vec,
|
beckerjc@246
|
53 |
RetEdgeBoolMap &ret_bool_map, RetIterator &ret_iterator);
|
beckerjc@246
|
54 |
|
beckerjc@246
|
55 |
template <typename Graph, typename EdgePairVec, typename RetEdgeBoolMap,
|
beckerjc@246
|
56 |
typename RetIterator>
|
beckerjc@246
|
57 |
int
|
beckerjc@246
|
58 |
kruskal9(Graph const& G, EdgePairVec const& edge_pair_vec,
|
beckerjc@246
|
59 |
RetEdgeBoolMap &ret_bool_map, RetIterator &ret_iterator);
|
beckerjc@246
|
60 |
|
beckerjc@246
|
61 |
|
beckerjc@246
|
62 |
|
beckerjc@246
|
63 |
|
beckerjc@246
|
64 |
template <typename Graph, typename InputEdgeOrder, typename OutputObserver>
|
beckerjc@218
|
65 |
class MinCostTreeKruskal
|
beckerjc@218
|
66 |
{
|
beckerjc@150
|
67 |
|
beckerjc@218
|
68 |
typedef typename Graph::EdgeIt EdgeIt;
|
beckerjc@218
|
69 |
typedef typename Graph::Edge Edge;
|
beckerjc@150
|
70 |
|
beckerjc@218
|
71 |
public:
|
beckerjc@246
|
72 |
typedef typename InputEdgeOrder::ValueType EdgeCost;
|
beckerjc@150
|
73 |
|
beckerjc@218
|
74 |
private:
|
beckerjc@218
|
75 |
Graph const &G;
|
beckerjc@246
|
76 |
InputEdgeOrder const &edges;
|
beckerjc@246
|
77 |
OutputObserver &out_obs;
|
beckerjc@150
|
78 |
|
beckerjc@218
|
79 |
public:
|
beckerjc@150
|
80 |
|
beckerjc@150
|
81 |
|
beckerjc@246
|
82 |
MinCostTreeKruskal(Graph const& _G, InputEdgeOrder const& _edges,
|
beckerjc@246
|
83 |
OutputObserver& _out_obs) :
|
beckerjc@246
|
84 |
G(_G), edges(_edges), out_obs(_out_obs) {}
|
beckerjc@218
|
85 |
|
beckerjc@218
|
86 |
|
beckerjc@246
|
87 |
EdgeCost run()
|
beckerjc@218
|
88 |
{
|
beckerjc@218
|
89 |
typedef typename Graph::NodeMap<int> NodeIntMap;
|
beckerjc@218
|
90 |
typedef typename Graph::Node Node;
|
beckerjc@218
|
91 |
NodeIntMap comp_map(G, -1);
|
beckerjc@218
|
92 |
UnionFind<Node,NodeIntMap> uf(comp_map);
|
beckerjc@218
|
93 |
|
beckerjc@218
|
94 |
EdgeCost tot_cost = 0;
|
beckerjc@246
|
95 |
for (typename InputEdgeOrder::const_iterator p = edges.begin();
|
beckerjc@246
|
96 |
p!=edges.end(); ++p ) {
|
beckerjc@246
|
97 |
if ( uf.joinComponents(G.head(edges.first(p)),
|
beckerjc@246
|
98 |
G.tail(edges.first(p))) ) {
|
beckerjc@246
|
99 |
out_obs.setTrue(edges.first(p));
|
beckerjc@246
|
100 |
tot_cost += edges.second(p);
|
beckerjc@218
|
101 |
}
|
beckerjc@218
|
102 |
else {
|
beckerjc@246
|
103 |
out_obs.setFalse(edges.first(p));
|
beckerjc@218
|
104 |
}
|
beckerjc@218
|
105 |
}
|
beckerjc@218
|
106 |
return tot_cost;
|
beckerjc@218
|
107 |
}
|
beckerjc@150
|
108 |
|
beckerjc@218
|
109 |
};
|
beckerjc@150
|
110 |
|
beckerjc@246
|
111 |
|
beckerjc@246
|
112 |
/* ** ** Output-objektumok (Observerek (?)) ** ** */
|
beckerjc@246
|
113 |
|
beckerjc@246
|
114 |
template <typename BoolMap>
|
beckerjc@246
|
115 |
class BoolMapOutput {
|
beckerjc@246
|
116 |
BoolMap &bm;
|
beckerjc@246
|
117 |
typedef typename BoolMap::KeyType KeyType;
|
beckerjc@246
|
118 |
|
beckerjc@246
|
119 |
public:
|
beckerjc@246
|
120 |
BoolMapOutput(BoolMap &_bm) : bm(_bm) {}
|
beckerjc@246
|
121 |
|
beckerjc@246
|
122 |
void setTrue(KeyType const& k) { bm.set(k, true); }
|
beckerjc@246
|
123 |
void setFalse(KeyType const& k) { bm.set(k, false); }
|
beckerjc@246
|
124 |
};
|
beckerjc@246
|
125 |
|
beckerjc@246
|
126 |
template <typename Iterator>
|
beckerjc@246
|
127 |
class SequenceOutput {
|
beckerjc@246
|
128 |
Iterator ⁢
|
beckerjc@246
|
129 |
|
beckerjc@246
|
130 |
public:
|
beckerjc@246
|
131 |
SequenceOutput(Iterator &_it) : it(_it) {}
|
beckerjc@246
|
132 |
|
beckerjc@246
|
133 |
template<typename KeyType>
|
beckerjc@246
|
134 |
void setTrue(KeyType const& k) { *it=k; ++it; }
|
beckerjc@246
|
135 |
template<typename KeyType>
|
beckerjc@246
|
136 |
void setFalse(KeyType const& k) {}
|
beckerjc@246
|
137 |
};
|
beckerjc@246
|
138 |
|
beckerjc@246
|
139 |
template <typename BoolMap, typename Iterator>
|
beckerjc@246
|
140 |
class CombinedOutput : BoolMapOutput<BoolMap>, SequenceOutput<Iterator> {
|
beckerjc@246
|
141 |
typedef BoolMapOutput<BoolMap> BMO;
|
beckerjc@246
|
142 |
typedef SequenceOutput<Iterator> SO;
|
beckerjc@246
|
143 |
public:
|
beckerjc@246
|
144 |
CombinedOutput(BoolMap &_bm, Iterator &_it) :
|
beckerjc@246
|
145 |
BMO(_bm), SO(_it) {}
|
beckerjc@246
|
146 |
|
beckerjc@246
|
147 |
template<typename KeyType>
|
beckerjc@246
|
148 |
void setTrue(KeyType const& k) {
|
beckerjc@246
|
149 |
BMO::setTrue(k);
|
beckerjc@246
|
150 |
SO::setTrue(k);
|
beckerjc@246
|
151 |
}
|
beckerjc@246
|
152 |
template<typename KeyType>
|
beckerjc@246
|
153 |
void setFalse(KeyType const& k) {
|
beckerjc@246
|
154 |
BMO::setFalse(k);
|
beckerjc@246
|
155 |
SO::setFalse(k);
|
beckerjc@246
|
156 |
}
|
beckerjc@246
|
157 |
};
|
beckerjc@246
|
158 |
|
beckerjc@246
|
159 |
|
beckerjc@246
|
160 |
/* ** ** InputSource -ok ** ** */
|
beckerjc@246
|
161 |
|
beckerjc@246
|
162 |
template<typename Key, typename Val>
|
beckerjc@246
|
163 |
class PairVec : public std::vector< std::pair<Key,Val> > {
|
beckerjc@246
|
164 |
|
beckerjc@246
|
165 |
public:
|
beckerjc@246
|
166 |
typedef std::vector< std::pair<Key,Val> > Parent;
|
beckerjc@246
|
167 |
typedef Key KeyType;
|
beckerjc@246
|
168 |
typedef Val ValueType;
|
beckerjc@246
|
169 |
typedef std::pair<Key,Val> PairType;
|
beckerjc@246
|
170 |
|
beckerjc@246
|
171 |
typedef typename Parent::iterator iterator;
|
beckerjc@246
|
172 |
typedef typename Parent::const_iterator const_iterator;
|
beckerjc@246
|
173 |
|
beckerjc@246
|
174 |
|
beckerjc@246
|
175 |
private:
|
beckerjc@246
|
176 |
|
beckerjc@246
|
177 |
class comparePair {
|
beckerjc@246
|
178 |
public:
|
beckerjc@246
|
179 |
bool operator()(PairType const& a, PairType const& b) {
|
beckerjc@246
|
180 |
return a.second < b.second;
|
beckerjc@246
|
181 |
}
|
beckerjc@246
|
182 |
};
|
beckerjc@246
|
183 |
|
beckerjc@246
|
184 |
public:
|
beckerjc@246
|
185 |
|
beckerjc@246
|
186 |
// FIXME: kell ez?
|
beckerjc@246
|
187 |
// PairVec(Parent const& p) : Parent(p) {}
|
beckerjc@246
|
188 |
|
beckerjc@246
|
189 |
void sort() {
|
beckerjc@246
|
190 |
std::sort(begin(), end(), comparePair());
|
beckerjc@246
|
191 |
}
|
beckerjc@246
|
192 |
|
beckerjc@246
|
193 |
// FIXME: nem nagyon illik ez ide...
|
beckerjc@246
|
194 |
template<typename Graph, typename Map>
|
beckerjc@246
|
195 |
void readGraphEdgeMap(Graph const& G, Map const& m) {
|
beckerjc@246
|
196 |
typedef typename Graph::EdgeIt EdgeIt;
|
beckerjc@246
|
197 |
|
beckerjc@246
|
198 |
clear();
|
beckerjc@246
|
199 |
for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
|
beckerjc@246
|
200 |
push_back(make_pair(e, m[e]));
|
beckerjc@246
|
201 |
}
|
beckerjc@246
|
202 |
|
beckerjc@246
|
203 |
sort();
|
beckerjc@246
|
204 |
}
|
beckerjc@246
|
205 |
|
beckerjc@246
|
206 |
int alma() { return 5; /* FIXME */ }
|
beckerjc@246
|
207 |
|
beckerjc@246
|
208 |
Key const& first(const_iterator i) const { return i->first; }
|
beckerjc@246
|
209 |
Key& first(iterator i) { return i->first; }
|
beckerjc@246
|
210 |
|
beckerjc@246
|
211 |
Val const& second(const_iterator i) const { return i->second; }
|
beckerjc@246
|
212 |
Val& second(iterator i) { return i->second; }
|
beckerjc@246
|
213 |
};
|
beckerjc@246
|
214 |
|
beckerjc@246
|
215 |
/* ** ** Wrapper fuggvenyek ** ** */
|
beckerjc@246
|
216 |
|
beckerjc@246
|
217 |
template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
|
beckerjc@246
|
218 |
typename EdgeCostMap::ValueType
|
beckerjc@246
|
219 |
kruskal1(Graph const& G, EdgeCostMap const& edge_costs,
|
beckerjc@246
|
220 |
RetEdgeBoolMap &ret_bool_map) {
|
beckerjc@246
|
221 |
typedef BoolMapOutput<RetEdgeBoolMap> OutObs;
|
beckerjc@246
|
222 |
OutObs out(ret_bool_map);
|
beckerjc@246
|
223 |
|
beckerjc@246
|
224 |
typedef PairVec<typename Graph::Edge, typename EdgeCostMap::ValueType>
|
beckerjc@246
|
225 |
InputVec;
|
beckerjc@246
|
226 |
InputVec iv;
|
beckerjc@246
|
227 |
iv.readGraphEdgeMap(G, edge_costs);
|
beckerjc@246
|
228 |
|
beckerjc@246
|
229 |
MinCostTreeKruskal<Graph,InputVec,OutObs> mctk(G, iv, out);
|
beckerjc@246
|
230 |
return mctk.run();
|
beckerjc@246
|
231 |
}
|
beckerjc@246
|
232 |
|
beckerjc@246
|
233 |
template <typename Graph, typename EdgeCostMap, typename RetIterator>
|
beckerjc@246
|
234 |
typename EdgeCostMap::ValueType
|
beckerjc@246
|
235 |
kruskal2(Graph const& G, EdgeCostMap const& edge_costs,
|
beckerjc@246
|
236 |
RetIterator ret_iterator) {
|
beckerjc@246
|
237 |
typedef SequenceOutput<RetIterator> OutObs;
|
beckerjc@246
|
238 |
OutObs out(ret_iterator);
|
beckerjc@246
|
239 |
|
beckerjc@246
|
240 |
typedef PairVec<typename Graph::Edge, typename EdgeCostMap::ValueType>
|
beckerjc@246
|
241 |
InputVec;
|
beckerjc@246
|
242 |
InputVec iv;
|
beckerjc@246
|
243 |
iv.readGraphEdgeMap(G, edge_costs);
|
beckerjc@246
|
244 |
|
beckerjc@246
|
245 |
MinCostTreeKruskal<Graph,InputVec,OutObs> mctk(G, iv, out);
|
beckerjc@246
|
246 |
return mctk.run();
|
beckerjc@246
|
247 |
}
|
beckerjc@246
|
248 |
|
beckerjc@150
|
249 |
|
beckerjc@150
|
250 |
} //namespace hugo
|
beckerjc@150
|
251 |
|
beckerjc@150
|
252 |
#endif //HUGO_KRUSKAL_H
|