1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/graph/dag_shortest_paths.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,157 @@
1.4 +//=======================================================================
1.5 +// Copyright 2002 Indiana University.
1.6 +// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
1.7 +//
1.8 +// Distributed under the Boost Software License, Version 1.0. (See
1.9 +// accompanying file LICENSE_1_0.txt or copy at
1.10 +// http://www.boost.org/LICENSE_1_0.txt)
1.11 +//=======================================================================
1.12 +
1.13 +#ifndef BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
1.14 +#define BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
1.15 +
1.16 +#include <boost/graph/topological_sort.hpp>
1.17 +#include <boost/graph/dijkstra_shortest_paths.hpp>
1.18 +
1.19 +// single-source shortest paths for a Directed Acyclic Graph (DAG)
1.20 +
1.21 +namespace boost {
1.22 +
1.23 + // Initalize distances and call depth first search
1.24 + template <class VertexListGraph, class DijkstraVisitor,
1.25 + class DistanceMap, class WeightMap, class ColorMap,
1.26 + class PredecessorMap,
1.27 + class Compare, class Combine,
1.28 + class DistInf, class DistZero>
1.29 + inline void
1.30 + dag_shortest_paths
1.31 + (const VertexListGraph& g,
1.32 + typename graph_traits<VertexListGraph>::vertex_descriptor s,
1.33 + DistanceMap distance, WeightMap weight, ColorMap color,
1.34 + PredecessorMap pred,
1.35 + DijkstraVisitor vis, Compare compare, Combine combine,
1.36 + DistInf inf, DistZero zero)
1.37 + {
1.38 + typedef typename graph_traits<VertexListGraph>::vertex_descriptor Vertex;
1.39 + std::vector<Vertex> rev_topo_order;
1.40 + rev_topo_order.reserve(num_vertices(g));
1.41 +
1.42 + // Call 'depth_first_visit', not 'topological_sort', because we don't
1.43 + // want to traverse the entire graph, only vertices reachable from 's',
1.44 + // and 'topological_sort' will traverse everything. The logic below
1.45 + // is the same as for 'topological_sort', only we call 'depth_first_visit'
1.46 + // and 'topological_sort' calls 'depth_first_search'.
1.47 + topo_sort_visitor<std::back_insert_iterator<std::vector<Vertex> > >
1.48 + topo_visitor(std::back_inserter(rev_topo_order));
1.49 + depth_first_visit(g, s, topo_visitor, color);
1.50 +
1.51 + typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
1.52 + for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
1.53 + put(distance, *ui, inf);
1.54 + put(pred, *ui, *ui);
1.55 + }
1.56 +
1.57 + put(distance, s, zero);
1.58 + vis.discover_vertex(s, g);
1.59 + typename std::vector<Vertex>::reverse_iterator i;
1.60 + for (i = rev_topo_order.rbegin(); i != rev_topo_order.rend(); ++i) {
1.61 + Vertex u = *i;
1.62 + vis.examine_vertex(u, g);
1.63 + typename graph_traits<VertexListGraph>::out_edge_iterator e, e_end;
1.64 + for (tie(e, e_end) = out_edges(u, g); e != e_end; ++e) {
1.65 + vis.discover_vertex(target(*e, g), g);
1.66 + bool decreased = relax(*e, g, weight, pred, distance,
1.67 + combine, compare);
1.68 + if (decreased)
1.69 + vis.edge_relaxed(*e, g);
1.70 + else
1.71 + vis.edge_not_relaxed(*e, g);
1.72 + }
1.73 + vis.finish_vertex(u, g);
1.74 + }
1.75 + }
1.76 +
1.77 + namespace detail {
1.78 +
1.79 + // Defaults are the same as Dijkstra's algorithm
1.80 +
1.81 + // Handle Distance Compare, Combine, Inf and Zero defaults
1.82 + template <class VertexListGraph, class DijkstraVisitor,
1.83 + class DistanceMap, class WeightMap, class ColorMap,
1.84 + class IndexMap, class Params>
1.85 + inline void
1.86 + dag_sp_dispatch2
1.87 + (const VertexListGraph& g,
1.88 + typename graph_traits<VertexListGraph>::vertex_descriptor s,
1.89 + DistanceMap distance, WeightMap weight, ColorMap color, IndexMap id,
1.90 + DijkstraVisitor vis, const Params& params)
1.91 + {
1.92 + typedef typename property_traits<DistanceMap>::value_type D;
1.93 + dummy_property_map p_map;
1.94 + dag_shortest_paths
1.95 + (g, s, distance, weight, color,
1.96 + choose_param(get_param(params, vertex_predecessor), p_map),
1.97 + vis,
1.98 + choose_param(get_param(params, distance_compare_t()), std::less<D>()),
1.99 + choose_param(get_param(params, distance_combine_t()), closed_plus<D>()),
1.100 + choose_param(get_param(params, distance_inf_t()),
1.101 + (std::numeric_limits<D>::max)()),
1.102 + choose_param(get_param(params, distance_zero_t()),
1.103 + D()));
1.104 + }
1.105 +
1.106 + // Handle DistanceMap and ColorMap defaults
1.107 + template <class VertexListGraph, class DijkstraVisitor,
1.108 + class DistanceMap, class WeightMap, class ColorMap,
1.109 + class IndexMap, class Params>
1.110 + inline void
1.111 + dag_sp_dispatch1
1.112 + (const VertexListGraph& g,
1.113 + typename graph_traits<VertexListGraph>::vertex_descriptor s,
1.114 + DistanceMap distance, WeightMap weight, ColorMap color, IndexMap id,
1.115 + DijkstraVisitor vis, const Params& params)
1.116 + {
1.117 + typedef typename property_traits<WeightMap>::value_type T;
1.118 + typename std::vector<T>::size_type n;
1.119 + n = is_default_param(distance) ? num_vertices(g) : 1;
1.120 + std::vector<T> distance_map(n);
1.121 + n = is_default_param(color) ? num_vertices(g) : 1;
1.122 + std::vector<default_color_type> color_map(n);
1.123 +
1.124 + dag_sp_dispatch2
1.125 + (g, s,
1.126 + choose_param(distance,
1.127 + make_iterator_property_map(distance_map.begin(), id,
1.128 + distance_map[0])),
1.129 + weight,
1.130 + choose_param(color,
1.131 + make_iterator_property_map(color_map.begin(), id,
1.132 + color_map[0])),
1.133 + id, vis, params);
1.134 + }
1.135 +
1.136 + } // namespace detail
1.137 +
1.138 + template <class VertexListGraph, class Param, class Tag, class Rest>
1.139 + inline void
1.140 + dag_shortest_paths
1.141 + (const VertexListGraph& g,
1.142 + typename graph_traits<VertexListGraph>::vertex_descriptor s,
1.143 + const bgl_named_params<Param,Tag,Rest>& params)
1.144 + {
1.145 + // assert that the graph is directed...
1.146 + null_visitor null_vis;
1.147 + detail::dag_sp_dispatch1
1.148 + (g, s,
1.149 + get_param(params, vertex_distance),
1.150 + choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
1.151 + get_param(params, vertex_color),
1.152 + choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
1.153 + choose_param(get_param(params, graph_visitor),
1.154 + make_dijkstra_visitor(null_vis)),
1.155 + params);
1.156 + }
1.157 +
1.158 +} // namespace boost
1.159 +
1.160 +#endif // BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP