mirror of
https://github.com/boostorg/graph.git
synced 2026-02-12 00:02:10 +00:00
fixed to do topological order (not reverse topo)
[SVN r10197]
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
#ifndef BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
|
||||
#define BOOST_GRAPH_DAG_SHORTEST_PATHS_HPP
|
||||
|
||||
#include <boost/graph/relax.hpp>
|
||||
#include <boost/graph/depth_first_search.hpp>
|
||||
|
||||
// UNDER CONSTRUCTION
|
||||
#include <boost/graph/topological_sort.hpp>
|
||||
#include <boost/graph/dijkstra_shortest_paths.hpp>
|
||||
|
||||
// single-source shortest paths for a Directed Acyclic Graph (DAG)
|
||||
|
||||
@@ -12,42 +10,10 @@ namespace boost {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <class DijkstraVisitor,
|
||||
class WeightMap, class DistanceMap,
|
||||
class BinaryFunction, class BinaryPredicate>
|
||||
struct dag_sp_visitor : public dfs_visitor<>
|
||||
{
|
||||
dag_sp_visitor(DijkstraVisitor vis, WeightMap w, DistanceMap d,
|
||||
BinaryFunction cmb, BinaryPredicate cmp)
|
||||
: m_vis(vis), m_weight(w), m_distance(d),
|
||||
m_combine(cmb), m_compare(cmp) { }
|
||||
|
||||
template <class Vertex, class Graph>
|
||||
void finish_vertex(const Vertex& u, const Graph& g)
|
||||
{
|
||||
m_vis.examine_vertex(u, g);
|
||||
typename graph_traits<Graph>::out_edge_iterator e, e_end;
|
||||
for (tie(e, e_end) = out_edges(u, g); e != e_end; ++e) {
|
||||
m_vis.discover_vertex(target(*e, g), g);
|
||||
bool decreased = relax(*e, g, m_weight, m_distance,
|
||||
m_combine, m_compare);
|
||||
if (decreased)
|
||||
m_vis.edge_relaxed(*e, g);
|
||||
else
|
||||
m_vis.edge_not_relaxed(*e, g);
|
||||
}
|
||||
m_vis.finish_vertex(u, g);
|
||||
}
|
||||
|
||||
WeightMap m_weight;
|
||||
DistanceMap m_distance;
|
||||
BinaryFunction m_combine;
|
||||
BinaryPredicate m_compare;
|
||||
};
|
||||
|
||||
// Initalize distances and call depth first search
|
||||
template <class VertexListGraph, class DijkstraVisitor,
|
||||
class DistanceMap, class WeightMap, class ColorMap,
|
||||
class PredecessorMap,
|
||||
class Compare, class Combine,
|
||||
class DistInf, class DistZero>
|
||||
inline void
|
||||
@@ -55,30 +21,48 @@ namespace boost {
|
||||
(const VertexListGraph& g,
|
||||
typename graph_traits<VertexListGraph>::vertex_descriptor s,
|
||||
DistanceMap distance, WeightMap weight, ColorMap color,
|
||||
PredecessorMap pred,
|
||||
DijkstraVisitor vis, Compare compare, Combine combine,
|
||||
DistInf init, DistZero zero)
|
||||
DistInf inf_gen, DistZero zero)
|
||||
{
|
||||
typedef typename graph_traits<VertexListGraph>::vertex_descriptor Vertex;
|
||||
std::vector<Vertex> rev_topo_order;
|
||||
rev_topo_order.reserve(num_vertices(g));
|
||||
topological_sort(g, std::back_inserter(rev_topo_order));
|
||||
|
||||
typename graph_traits<VertexListGraph>::vertex_iterator ui, ui_end;
|
||||
for (tie(ui, ui_end) = vertices(g); ui != ui_end; ++ui) {
|
||||
put(distance, *ui, init());
|
||||
put(distance, *ui, inf_gen());
|
||||
put(pred, *ui, *ui);
|
||||
vis.initialize_vertex(*ui, g);
|
||||
}
|
||||
|
||||
put(distance, s, zero());
|
||||
vis.discover_vertex(s, g);
|
||||
|
||||
dag_sp_visitor<DijkstraVisitor, WeightMap, DistanceMap,
|
||||
Combine, Compare> v(vis, weight, distance, combine, compare);
|
||||
|
||||
depth_first_visit(g, s, v, color);
|
||||
std::vector<Vertex>::reverse_iterator i;
|
||||
for (i = rev_topo_order.rbegin(); i != rev_topo_order.rend(); ++i) {
|
||||
Vertex u = *i;
|
||||
vis.examine_vertex(u, g);
|
||||
typename graph_traits<VertexListGraph>::out_edge_iterator e, e_end;
|
||||
for (tie(e, e_end) = out_edges(u, g); e != e_end; ++e) {
|
||||
vis.discover_vertex(target(*e, g), g);
|
||||
bool decreased = relax(*e, g, weight, pred, distance,
|
||||
combine, compare);
|
||||
if (decreased)
|
||||
vis.edge_relaxed(*e, g);
|
||||
else
|
||||
vis.edge_not_relaxed(*e, g);
|
||||
}
|
||||
vis.finish_vertex(u, g);
|
||||
}
|
||||
}
|
||||
|
||||
// Defaults are the same as Dijkstra's algorithm
|
||||
|
||||
// Handle Distance Compare, Combine, Inf and Zero defaults
|
||||
template <class VertexListGraph, class DijkstraVisitor,
|
||||
class DistanceMap, class WeightMap, class ColorMap,
|
||||
class Params>
|
||||
class DistanceMap, class WeightMap, class ColorMap,
|
||||
class IndexMap, class Params>
|
||||
inline void
|
||||
dag_sp_dispatch2
|
||||
(const VertexListGraph& g,
|
||||
@@ -87,8 +71,11 @@ namespace boost {
|
||||
DijkstraVisitor vis, const Params& params)
|
||||
{
|
||||
typedef typename property_traits<DistanceMap>::value_type D;
|
||||
dummy_property_map p_map;
|
||||
detail::dag_sp_dispatch3
|
||||
(g, s, distance, weight, color, vis,
|
||||
(g, s, distance, weight, color,
|
||||
choose_param(get_param(params, vertex_predecessor), p_map),
|
||||
vis,
|
||||
choose_param(get_param(params, distance_compare_t()), std::less<D>()),
|
||||
choose_param(get_param(params, distance_combine_t()), std::plus<D>()),
|
||||
choose_param(get_param(params, distance_inf_t()),
|
||||
@@ -114,15 +101,17 @@ namespace boost {
|
||||
std::vector<T> distance_map(n);
|
||||
n = is_default_param(color) ? num_vertices(g) : 1;
|
||||
std::vector<default_color_type> color_map(n);
|
||||
|
||||
dag_sp_dispatch2
|
||||
(g, s,
|
||||
choose_param(distance,
|
||||
make_iterator_property_map(distance_map.begin(), id,
|
||||
distance_map[0])),
|
||||
distance_map[0])),
|
||||
weight,
|
||||
choose_param(color,
|
||||
make_iterator_property_map(color_map.begin(), id, color_map[0])),
|
||||
vis, params);
|
||||
make_iterator_property_map(color_map.begin(), id,
|
||||
color_map[0])),
|
||||
id, vis, params);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
@@ -139,9 +128,9 @@ namespace boost {
|
||||
detail::dag_sp_dispatch1
|
||||
(g, s,
|
||||
get_param(params, vertex_distance),
|
||||
choose_pmap(get_param(params, edge_weight), g, edge_weight),
|
||||
choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
|
||||
get_param(params, vertex_color),
|
||||
choose_pmap(get_param(params, vertex_index), g, vertex_index),
|
||||
choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
|
||||
choose_param(get_param(params, graph_visitor),
|
||||
make_dijkstra_visitor(null_vis)),
|
||||
params);
|
||||
|
||||
Reference in New Issue
Block a user