00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 # ifndef KRUSKAL_H
00063 # define KRUSKAL_H
00064
00065 # include <ahFunction.H>
00066 # include <tpl_graph.H>
00067 # include <tpl_graph_utils.H>
00068 # include <tpl_test_acyclique.H>
00069
00070 using namespace Aleph;
00071
00072 namespace Aleph {
00073
00074
00110 template <class GT, class Distance,
00111 class Compare = Aleph::less<typename Distance::Distance_Type>,
00112 class SA = Default_Show_Arc<GT> >
00113 class Kruskal_Min_Spanning_Tree
00114 {
00115 Distance & dist;
00116 Compare & cmp;
00117 SA & sa;
00118
00119 public:
00120
00127 Kruskal_Min_Spanning_Tree(Distance && __dist = Distance(),
00128 Compare && __cmp = Compare(),
00129 SA && __sa = SA())
00130 : dist(__dist), cmp(__cmp), sa(__sa)
00131 {
00132
00133 }
00134
00135 Kruskal_Min_Spanning_Tree(Distance & __dist, Compare & __cmp, SA & __sa)
00136 : dist(__dist), cmp(__cmp), sa(__sa)
00137 {
00138
00139 }
00140
00141 private:
00142
00143 void min_spanning_tree(GT & g, GT & tree)
00144 {
00145 if (g.is_digraph())
00146 throw std::domain_error("g is a digraph");
00147
00148 g.reset_bit_nodes(Aleph::Kruskal);
00149 clear_graph(tree);
00150
00151 typedef Distance_Compare<GT, Distance, Compare> DCMP;
00152 DCMP comp(dist, cmp);
00153 g.template sort_arcs<DCMP>(comp);
00154
00155
00156
00157 for (Arc_Iterator<GT, SA> arc_itor(g, sa);
00158 tree.get_num_arcs() < g.get_num_nodes() - 1; arc_itor.next())
00159 {
00160 typename GT::Arc * arc = arc_itor.get_current_arc();
00161
00162 typename GT::Node * g_src_node = g.get_src_node(arc);
00163 typename GT::Node * tree_src_node;
00164 if (not IS_NODE_VISITED(g_src_node, Aleph::Kruskal))
00165 {
00166 NODE_BITS(g_src_node).set_bit(Aleph::Kruskal, true);
00167 tree_src_node = tree.insert_node(g_src_node->get_info());
00168 GT::map_nodes(g_src_node, tree_src_node);
00169 }
00170 else
00171 tree_src_node = mapped_node<GT>(g_src_node);
00172
00173 typename GT::Node * g_tgt_node = g.get_tgt_node(arc);
00174 typename GT::Node * tree_tgt_node;
00175 if (not IS_NODE_VISITED(g_tgt_node, Aleph::Kruskal))
00176 {
00177 NODE_BITS(g_tgt_node).set_bit(Aleph::Kruskal, true);
00178 tree_tgt_node = tree.insert_node(g_tgt_node->get_info());
00179 GT::map_nodes(g_tgt_node, tree_tgt_node);
00180 }
00181 else
00182 tree_tgt_node = mapped_node<GT>(g_tgt_node);
00183
00184 typename GT::Arc * arc_in_tree =
00185 tree.insert_arc(tree_src_node, tree_tgt_node, arc->get_info());
00186
00187 if (Has_Cycle<GT, SA> (sa) (tree))
00188 {
00189 tree.remove_arc(arc_in_tree);
00190 continue;
00191 }
00192
00193 GT::map_arcs(arc, arc_in_tree);
00194 }
00195 }
00196
00197 public:
00198
00210 void operator () (GT & g, GT & tree)
00211 {
00212 min_spanning_tree (g, tree);
00213 }
00214 };
00215
00216
00217 }
00218
00219 # endif // KRUSKAL_H