- Better Makefile.am. (I hope.)
- Some more tests
7 #include <for_each_macros.h>
10 ///\brief Kruskal's algorithm to compute a minimum cost tree
14 /// Kruskal's algorithm to compute a minimum cost tree
16 template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
17 typename InputEdgeOrder::ValueType
18 Kruskal(Graph const& G, InputEdgeOrder const& edges,
21 typedef typename InputEdgeOrder::ValueType EdgeCost;
22 typedef typename Graph::NodeMap<int> NodeIntMap;
23 typedef typename Graph::Node Node;
25 NodeIntMap comp_map(G, -1);
26 UnionFind<Node,NodeIntMap> uf(comp_map);
28 EdgeCost tot_cost = 0;
29 for (typename InputEdgeOrder::const_iterator p = edges.begin();
30 p!=edges.end(); ++p ) {
31 if ( uf.join(G.head(edges.first(p)),
32 G.tail(edges.first(p))) ) {
33 out_map.set(edges.first(p), true);
34 tot_cost += edges.second(p);
37 out_map.set(edges.first(p), false);
43 /* A work-around for running Kruskal with const-reference bool maps... */
45 template<typename Map>
49 typedef typename Map::ValueType ValueType;
51 NonConstMapWr(const Map &_m) : m(_m) {}
53 template<typename KeyType>
54 void set(KeyType const& k, ValueType const &v) const { m.set(k,v); }
57 template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
59 typename InputEdgeOrder::ValueType
60 Kruskal(Graph const& G, InputEdgeOrder const& edges,
61 OutBoolMap const& out_map)
63 NonConstMapWr<OutBoolMap> map_wr(out_map);
64 return Kruskal(G, edges, map_wr);
68 /* ** ** Output-objektumok: egyszeruen extra bool mapek ** ** */
70 /// A writable bool-map that makes a sequence of "true" keys
72 /// A writable bool-map that creates a sequence out of keys that receive
74 /// \warning Not a regular property map, as it doesn't know its KeyType
76 template<typename Iterator>
77 class SequenceOutput {
81 typedef bool ValueType;
83 SequenceOutput(Iterator const &_it) : it(_it) {}
85 template<typename KeyType>
86 void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
89 template<typename Iterator>
91 SequenceOutput<Iterator>
92 makeSequenceOutput(Iterator it) {
93 return SequenceOutput<Iterator>(it);
96 /* ** ** InputSource -ok ** ** */
98 template<typename Key, typename Val>
99 class KruskalPairVec : public std::vector< std::pair<Key,Val> > {
102 typedef std::vector< std::pair<Key,Val> > Parent;
104 typedef Val ValueType;
105 typedef std::pair<Key,Val> PairType;
107 typedef typename Parent::iterator iterator;
108 typedef typename Parent::const_iterator const_iterator;
113 bool operator()(PairType const& a, PairType const& b) {
114 return a.second < b.second;
121 // KruskalPairVec(Parent const& p) : Parent(p) {}
124 std::sort(begin(), end(), comparePair());
127 // FIXME: nem nagyon illik ez ide...
128 template<typename Graph, typename Map>
129 KruskalPairVec(Graph const& G, Map const& m) {
130 typedef typename Graph::EdgeIt EdgeIt;
133 FOR_EACH_LOC(EdgeIt, e, G) {
134 // for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
135 push_back(make_pair(e, m[e]));
140 Key const& first(const_iterator i) const { return i->first; }
141 Key& first(iterator i) { return i->first; }
143 Val const& second(const_iterator i) const { return i->second; }
144 Val& second(iterator i) { return i->second; }
148 template <typename Map>
149 class KruskalMapVec : public std::vector<typename Map::KeyType> {
152 typedef typename Map::KeyType KeyType;
153 typedef typename Map::ValueType ValueType;
155 typedef typename std::vector<KeyType> Parent;
156 typedef typename Parent::iterator iterator;
157 typedef typename Parent::const_iterator const_iterator;
166 compareKeys(Map const &_m) : m(_m) {}
167 bool operator()(KeyType const& a, KeyType const& b) {
174 KruskalMapVec(Map const& _m) : m(_m) {}
177 std::sort(begin(), end(), compareKeys(m));
180 // FIXME: nem nagyon illik ez ide...
181 template<typename Graph>
182 KruskalMapVec(Graph const& G, Map const& _m) : m(_m) {
183 typedef typename Graph::EdgeIt EdgeIt;
186 FOR_EACH_LOC(EdgeIt, e, G) {
187 // for (EdgeIt e=G.template first<EdgeIt>(); G.valid(e); G.next(e)) {
193 KeyType const& first(const_iterator i) const { return *i; }
194 KeyType& first(iterator i) { return *i; }
196 ValueType const& second(const_iterator i) const { return m[*i]; }
197 ValueType& second(iterator i) { return m[*i]; }
200 /* ** ** Wrapper fuggvenyek ** ** */
203 /// \brief Wrapper to Kruskal().
204 /// Input is from an EdgeMap, output is a plain boolmap.
205 template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
207 typename EdgeCostMap::ValueType
208 Kruskal_EdgeCostMapIn_BoolMapOut(Graph const& G,
209 EdgeCostMap const& edge_costs,
210 RetEdgeBoolMap &ret_bool_map) {
212 typedef KruskalPairVec<typename Graph::Edge, typename EdgeCostMap::ValueType>
214 InputVec iv(G, edge_costs);
216 return Kruskal(G, iv, ret_bool_map);
220 /// \brief Wrapper to Kruskal().
221 /// Input is from an EdgeMap, output is to a sequence.
222 template <typename Graph, typename EdgeCostMap, typename RetIterator>
224 typename EdgeCostMap::ValueType
225 Kruskal_EdgeCostMapIn_IteratorOut(Graph const& G,
226 EdgeCostMap const& edge_costs,
227 RetIterator ret_iterator) {
228 typedef SequenceOutput<RetIterator> OutMap;
229 OutMap out(ret_iterator);
231 typedef KruskalPairVec<typename Graph::Edge, typename EdgeCostMap::ValueType>
233 InputVec iv(G, edge_costs);
235 return Kruskal(G, iv, out);
241 #endif //HUGO_KRUSKAL_H