Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:25

0001 //=======================================================================
0002 // Copyright 2002 Indiana University.
0003 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See
0006 // accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 //=======================================================================
0009 
0010 #ifndef BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
0011 #define BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
0012 
0013 #include <boost/graph/topological_sort.hpp>
0014 #include <boost/graph/dijkstra_shortest_paths.hpp>
0015 
0016 // single-source shortest paths for a Directed Acyclic Graph (DAG)
0017 
0018 namespace boost
0019 {
0020 
0021 // Initalize distances and call depth first search
0022 template < class VertexListGraph, class DijkstraVisitor, class DistanceMap,
0023     class WeightMap, class ColorMap, class PredecessorMap, class Compare,
0024     class Combine, class DistInf, class DistZero >
0025 inline void dag_shortest_paths(const VertexListGraph& g,
0026     typename graph_traits< VertexListGraph >::vertex_descriptor s,
0027     DistanceMap distance, WeightMap weight, ColorMap color, PredecessorMap pred,
0028     DijkstraVisitor vis, Compare compare, Combine combine, DistInf inf,
0029     DistZero zero)
0030 {
0031     typedef typename graph_traits< VertexListGraph >::vertex_descriptor Vertex;
0032     std::vector< Vertex > rev_topo_order;
0033     rev_topo_order.reserve(num_vertices(g));
0034 
0035     // Call 'depth_first_visit', not 'topological_sort', because we don't
0036     // want to traverse the entire graph, only vertices reachable from 's',
0037     // and 'topological_sort' will traverse everything. The logic below
0038     // is the same as for 'topological_sort', only we call 'depth_first_visit'
0039     // and 'topological_sort' calls 'depth_first_search'.
0040     topo_sort_visitor< std::back_insert_iterator< std::vector< Vertex > > >
0041         topo_visitor(std::back_inserter(rev_topo_order));
0042     depth_first_visit(g, s, topo_visitor, color);
0043 
0044     typename graph_traits< VertexListGraph >::vertex_iterator ui, ui_end;
0045     for (boost::tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui)
0046     {
0047         put(distance, *ui, inf);
0048         put(pred, *ui, *ui);
0049     }
0050 
0051     put(distance, s, zero);
0052     vis.discover_vertex(s, g);
0053     typename std::vector< Vertex >::reverse_iterator i;
0054     for (i = rev_topo_order.rbegin(); i != rev_topo_order.rend(); ++i)
0055     {
0056         Vertex u = *i;
0057         vis.examine_vertex(u, g);
0058         typename graph_traits< VertexListGraph >::out_edge_iterator e, e_end;
0059         for (boost::tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
0060         {
0061             vis.discover_vertex(target(*e, g), g);
0062             bool decreased
0063                 = relax(*e, g, weight, pred, distance, combine, compare);
0064             if (decreased)
0065                 vis.edge_relaxed(*e, g);
0066             else
0067                 vis.edge_not_relaxed(*e, g);
0068         }
0069         vis.finish_vertex(u, g);
0070     }
0071 }
0072 
0073 namespace detail
0074 {
0075 
0076     // Defaults are the same as Dijkstra's algorithm
0077 
0078     // Handle Distance Compare, Combine, Inf and Zero defaults
0079     template < class VertexListGraph, class DijkstraVisitor, class DistanceMap,
0080         class WeightMap, class ColorMap, class IndexMap, class Params >
0081     inline void dag_sp_dispatch2(const VertexListGraph& g,
0082         typename graph_traits< VertexListGraph >::vertex_descriptor s,
0083         DistanceMap distance, WeightMap weight, ColorMap color, IndexMap /*id*/,
0084         DijkstraVisitor vis, const Params& params)
0085     {
0086         typedef typename property_traits< DistanceMap >::value_type D;
0087         dummy_property_map p_map;
0088         D inf = choose_param(get_param(params, distance_inf_t()),
0089             (std::numeric_limits< D >::max)());
0090         dag_shortest_paths(g, s, distance, weight, color,
0091             choose_param(get_param(params, vertex_predecessor), p_map), vis,
0092             choose_param(
0093                 get_param(params, distance_compare_t()), std::less< D >()),
0094             choose_param(
0095                 get_param(params, distance_combine_t()), closed_plus< D >(inf)),
0096             inf, choose_param(get_param(params, distance_zero_t()), D()));
0097     }
0098 
0099     // Handle DistanceMap and ColorMap defaults
0100     template < class VertexListGraph, class DijkstraVisitor, class DistanceMap,
0101         class WeightMap, class ColorMap, class IndexMap, class Params >
0102     inline void dag_sp_dispatch1(const VertexListGraph& g,
0103         typename graph_traits< VertexListGraph >::vertex_descriptor s,
0104         DistanceMap distance, WeightMap weight, ColorMap color, IndexMap id,
0105         DijkstraVisitor vis, const Params& params)
0106     {
0107         typedef typename property_traits< WeightMap >::value_type T;
0108         typename std::vector< T >::size_type n;
0109         n = is_default_param(distance) ? num_vertices(g) : 1;
0110         std::vector< T > distance_map(n);
0111         n = is_default_param(color) ? num_vertices(g) : 1;
0112         std::vector< default_color_type > color_map(n);
0113 
0114         dag_sp_dispatch2(g, s,
0115             choose_param(distance,
0116                 make_iterator_property_map(
0117                     distance_map.begin(), id, distance_map[0])),
0118             weight,
0119             choose_param(color,
0120                 make_iterator_property_map(
0121                     color_map.begin(), id, color_map[0])),
0122             id, vis, params);
0123     }
0124 
0125 } // namespace detail
0126 
0127 template < class VertexListGraph, class Param, class Tag, class Rest >
0128 inline void dag_shortest_paths(const VertexListGraph& g,
0129     typename graph_traits< VertexListGraph >::vertex_descriptor s,
0130     const bgl_named_params< Param, Tag, Rest >& params)
0131 {
0132     // assert that the graph is directed...
0133     null_visitor null_vis;
0134     detail::dag_sp_dispatch1(g, s, get_param(params, vertex_distance),
0135         choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
0136         get_param(params, vertex_color),
0137         choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
0138         choose_param(
0139             get_param(params, graph_visitor), make_dijkstra_visitor(null_vis)),
0140         params);
0141 }
0142 
0143 } // namespace boost
0144 
0145 #endif // BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP