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 # ifndef FLOYD_H
00045 # define FLOYD_H
00046
00047 # include <ahFunction.H>
00048 # include <tpl_matgraph.H>
00049
00050 namespace Aleph
00051 {
00052
00053 template <class GT, class Distance, class Compare, class Plus>
00054 void floyd_all_shortest_paths
00055 (GT & g,
00056 Ady_Mat<GT, typename Distance::Distance_Type> & dist,
00057 Ady_Mat<GT, long> & path);
00058 template <class GT, class Distance>
00059 void floyd_all_shortest_paths
00060 (GT & g,
00061 Ady_Mat<GT, typename Distance::Distance_Type> & dist,
00062 Ady_Mat<GT, long> & path)
00063 {
00064 typedef typename Distance::Distance_Type Dist_T;
00065
00066 floyd_all_shortest_paths
00067 <GT, Distance, Aleph::less<Dist_T>, Aleph::plus<Dist_T> > (g, dist, path);
00068 }
00069 template <class AM, class Distance>
00070 struct Initialize_Dist_Floyd
00071 {
00072 typedef typename AM::List_Graph_Type::Arc_Type Arc_Type;
00073 typedef typename AM::List_Graph_Type GT;
00074 typedef typename GT::Node Node;
00075 typedef typename GT::Arc Arc;
00076 typedef typename Distance::Distance_Type Distance_Type;
00077
00078
00079 void operator () (AM & mat,
00080 Node * src, Node * tgt,
00081 const long & i, const long & j,
00082 Distance_Type & entry,
00083 void * p)
00084 {
00085 Ady_Mat <typename AM::List_Graph_Type, long> & path =
00086 * reinterpret_cast <Ady_Mat <typename AM::List_Graph_Type, long> *> (p);
00087
00088 if (i == j)
00089 {
00090 entry = Distance::Zero_Distance;
00091 path(i, j) = j;
00092 return;
00093 }
00094
00095 GT & g = mat.get_list_graph();
00096
00097 Arc * arc = g.search_arc(src, tgt);
00098
00099 if (arc == NULL)
00100 {
00101 entry = Distance::Max_Distance;
00102 return;
00103 }
00104
00105 entry = Distance () (arc);
00106 path(i, j) = j;
00107 }
00108 };
00186 template <class GT, typename Distance, class Compare, class Plus>
00187 void floyd_all_shortest_paths
00188 (GT & g,
00189 Ady_Mat<GT, typename Distance::Distance_Type> & dist,
00190 Ady_Mat<GT, long> & path)
00191 {
00192 typedef typename Distance::Distance_Type Dist_Type;
00193 typedef Ady_Mat <GT, Dist_Type> Dist_Mat;
00194
00195 dist.template operate_all_arcs_matrix
00196 <Initialize_Dist_Floyd <Dist_Mat, Distance> > (&path);
00197 const Dist_Type & max = Distance::Max_Distance;
00198 const long & n = g.get_num_nodes();
00199 for (int k = 0; k < n; ++k)
00200 for (int i = 0; i < n; ++i)
00201 if (dist(i, k) < max)
00202 for (int j = 0; j < n; ++j)
00203 {
00204 Dist_Type new_dist = Plus () (dist(i, k), dist(k, j));
00205
00206 if (Compare()(new_dist, dist(i,j)))
00207 {
00208 dist(i, j) = new_dist;
00209 path(i, j) = path(i, k);
00210 }
00211 }
00212 }
00213
00214 }
00215
00216 # endif // FLOYD_H
00217