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 FLOYD_H
00063 # define FLOYD_H
00064
00065 # include <ahFunction.H>
00066 # include <tpl_matgraph.H>
00067
00068 namespace Aleph
00069 {
00070 template <class GT, typename Distance,
00071 class Compare = Aleph::less<typename Distance::Distance_Type>,
00072 class Plus = Aleph::plus<typename Distance::Distance_Type>,
00073 class SA = Default_Show_Arc<GT> >
00074 void floyd_all_shortest_paths
00075 (GT & g,
00076 Ady_Mat<GT, typename Distance::Distance_Type, SA> & dist,
00077 Ady_Mat<GT, long, SA> & path);
00078 template <class AM, class Distance, class SA> struct Initialize_Dist_Floyd
00079 {
00080 typedef typename AM::List_Graph_Type::Arc_Type Arc_Type;
00081 typedef typename AM::List_Graph_Type GT;
00082 typedef typename GT::Node Node;
00083 typedef typename GT::Arc Arc;
00084 typedef typename Distance::Distance_Type Distance_Type;
00085
00086
00087 void operator () (AM & mat, Node * src, Node * tgt,
00088 const long & i, const long & j, Distance_Type & entry,
00089 void * p) const
00090 {
00091 Ady_Mat <typename AM::List_Graph_Type, long, SA> & path =
00092 * (Ady_Mat <typename AM::List_Graph_Type, long> *) (p);
00093
00094 if (i == j)
00095 {
00096 entry = Distance::Zero_Distance;
00097 path(i, j) = j;
00098 return;
00099 }
00100 GT & g = mat.get_list_graph();
00101 Arc * arc = search_arc(g, src, tgt);
00102 if (arc == NULL)
00103 {
00104 entry = Distance::Max_Distance;
00105 return;
00106 }
00107
00108 entry = Distance () (arc);
00109 path(i, j) = j;
00110 }
00111 };
00186 template <class GT, typename Distance,
00187 class Compare = Aleph::less<typename Distance::Distance_Type>,
00188 class Plus = Aleph::plus<typename Distance::Distance_Type>,
00189 class SA = Default_Show_Arc<GT> >
00190 void floyd_all_shortest_paths
00191 (GT & g,
00192 Ady_Mat<GT, typename Distance::Distance_Type, SA> & dist,
00193 Ady_Mat<GT, long, SA> & path)
00194 {
00195 typedef typename Distance::Distance_Type Dist_Type;
00196 typedef Ady_Mat <GT, Dist_Type> Dist_Mat;
00197 dist.template operate_all_arcs_matrix
00198 <Initialize_Dist_Floyd <Dist_Mat, Distance, SA> > (&path);
00199 const Dist_Type & max = Distance::Max_Distance;
00200 const long & n = g.get_num_nodes();
00201 for (int k = 0; k < n; ++k)
00202 for (int i = 0; i < n; ++i)
00203 if (dist(i, k) < max)
00204 for (int j = 0; j < n; ++j)
00205 {
00206 Dist_Type new_dist = Plus () (dist(i, k), dist(k, j));
00207 if (Compare () (new_dist, dist(i,j)))
00208 {
00209 dist(i, j) = new_dist;
00210 path(i, j) = path(i, k);
00211 }
00212 }
00213 }
00214
00281 template <class GT, typename Distance,
00282 class Compare = Aleph::less<typename Distance::Distance_Type>,
00283 class Plus = Aleph::plus<typename Distance::Distance_Type>,
00284 class SA = Default_Show_Arc<GT> >
00285 class Floyd_All_Shortest_Paths
00286 {
00287 public:
00288
00300 void
00301 operator () (GT & g,
00302 Ady_Mat<GT, typename Distance::Distance_Type, SA> & dist,
00303 Ady_Mat<GT, long, SA> & path) const
00304 {
00305 floyd_all_shortest_paths <GT, Distance, Compare, Plus, SA> (g, dist, path);
00306 }
00307 };
00308
00309 template <class Mat>
00310 void find_min_path(Mat & p, const long & src_index, const long & tgt_index,
00311 Path<typename Mat::Graph_Type> & path);
00342 template <class Mat>
00343 void find_min_path(Mat & p, typename Mat::Node * src_node,
00344 typename Mat::Node * tgt_node,
00345 Path<typename Mat::Graph_Type> & path)
00346 {
00347 find_min_path(p, p(src_node), p(tgt_node, path));
00348 }
00349
00350 template <class Mat>
00351 class Find_Min_Path
00352 {
00353 public:
00354
00355 void operator () (Mat & p,
00356 typename Mat::Node * src_node,
00357 typename Mat::Node * tgt_node,
00358 Path<typename Mat::Graph_Type> & path) const
00359 {
00360 find_min_path <Mat> (p, src_node, tgt_node, path);
00361 }
00362
00363 void operator () (Mat & p,
00364 const long & src_index, const long & tgt_index,
00365 Path<typename Mat::Graph_Type> & path) const
00366 {
00367 find_min_path <Mat> (p, src_index, tgt_index, path);
00368 }
00369 };
00370
00404 template <class Mat> void
00405 find_min_path(Mat & p, const long & src_idx, const long & tgt_idx,
00406 Path<typename Mat::Graph_Type> & path)
00407 {
00408 typedef typename Mat::Graph_Type GT;
00409 GT & g = p.get_list_graph();
00410 typename GT::Node * src = p(src_idx);
00411 path.set_graph(g, src);
00412 for (int i = src_idx, j; j != tgt_idx; i = j)
00413 {
00414 j = p(i, tgt_idx);
00415 path.append(p(j));
00416 }
00417 }
00418
00419 }
00420
00421 # endif // FLOYD_H
00422