& params) */ #ifndef BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP #define BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP #include #include #include #include #include #include #include namespace boost { template struct BellmanFordVisitorConcept { void constraints() { function_requires< CopyConstructibleConcept >(); vis.examine_edge(e, g); vis.edge_relaxed(e, g); vis.edge_not_relaxed(e, g); vis.edge_minimized(e, g); vis.edge_not_minimized(e, g); } Visitor vis; Graph g; typename graph_traits::edge_descriptor e; }; template class bellman_visitor { public: bellman_visitor() { } bellman_visitor(Visitors vis) : m_vis(vis) { } template void examine_edge(Edge u, Graph& g) { invoke_visitors(m_vis, u, g, on_examine_edge()); } template void edge_relaxed(Edge u, Graph& g) { invoke_visitors(m_vis, u, g, on_edge_relaxed()); } template void edge_not_relaxed(Edge u, Graph& g) { invoke_visitors(m_vis, u, g, on_edge_not_relaxed()); } template void edge_minimized(Edge u, Graph& g) { invoke_visitors(m_vis, u, g, on_edge_minimized()); } template void edge_not_minimized(Edge u, Graph& g) { invoke_visitors(m_vis, u, g, on_edge_not_minimized()); } protected: Visitors m_vis; }; template bellman_visitor make_bellman_visitor(Visitors vis) { return bellman_visitor(vis); } typedef bellman_visitor<> default_bellman_visitor; template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance, BinaryFunction combine, BinaryPredicate compare, BellmanFordVisitor v) { function_requires >(); typedef graph_traits GTraits; typedef typename GTraits::edge_descriptor Edge; typedef typename GTraits::vertex_descriptor Vertex; function_requires >(); function_requires >(); typedef typename property_traits::value_type D_value; typedef typename property_traits::value_type W_value; typename GTraits::edge_iterator i, end; for (Size k = 0; k < N; ++k) { bool at_least_one_edge_relaxed = false; for (tie(i, end) = edges(g); i != end; ++i) { v.examine_edge(*i, g); if (relax(*i, g, weight, pred, distance, combine, compare)) { at_least_one_edge_relaxed = true; v.edge_relaxed(*i, g); } else v.edge_not_relaxed(*i, g); } if (!at_least_one_edge_relaxed) break; } for (tie(i, end) = edges(g); i != end; ++i) if (compare(combine(get(distance, source(*i, g)), get(weight, *i)), get(distance, target(*i,g)))) { v.edge_not_minimized(*i, g); return false; } else v.edge_minimized(*i, g); return true; } namespace detail { template bool bellman_dispatch2 (VertexAndEdgeListGraph& g, typename graph_traits::vertex_descriptor s, Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance, const bgl_named_params& params) { typedef typename property_traits::value_type D; bellman_visitor<> null_vis; typedef typename property_traits::value_type weight_type; typename graph_traits::vertex_iterator v, v_end; for (tie(v, v_end) = vertices(g); v != v_end; ++v) { put(distance, *v, (std::numeric_limits::max)()); put(pred, *v, *v); } put(distance, s, weight_type(0)); return bellman_ford_shortest_paths (g, N, weight, pred, distance, choose_param(get_param(params, distance_combine_t()), closed_plus()), choose_param(get_param(params, distance_compare_t()), std::less()), choose_param(get_param(params, graph_visitor), null_vis) ); } template bool bellman_dispatch2 (VertexAndEdgeListGraph& g, detail::error_property_not_found, Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance, const bgl_named_params& params) { typedef typename property_traits::value_type D; bellman_visitor<> null_vis; return bellman_ford_shortest_paths (g, N, weight, pred, distance, choose_param(get_param(params, distance_combine_t()), closed_plus()), choose_param(get_param(params, distance_compare_t()), std::less()), choose_param(get_param(params, graph_visitor), null_vis) ); } template bool bellman_dispatch(EdgeListGraph& g, Size N, WeightMap weight, DistanceMap distance, const bgl_named_params& params) { dummy_property_map dummy_pred; return detail::bellman_dispatch2 (g, get_param(params, root_vertex_t()), N, weight, choose_param(get_param(params, vertex_predecessor), dummy_pred), distance, params); } } // namespace detail template bool bellman_ford_shortest_paths (EdgeListGraph& g, Size N, const bgl_named_params& params) { return detail::bellman_dispatch (g, N, choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N) { bgl_named_params params(0); return bellman_ford_shortest_paths(g, N, params); } template bool bellman_ford_shortest_paths (VertexAndEdgeListGraph& g, const bgl_named_params& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
& params) { typedef typename property_traits::value_type D; bellman_visitor<> null_vis; typedef typename property_traits::value_type weight_type; typename graph_traits::vertex_iterator v, v_end; for (tie(v, v_end) = vertices(g); v != v_end; ++v) { put(distance, *v, (std::numeric_limits::max)()); put(pred, *v, *v); } put(distance, s, weight_type(0)); return bellman_ford_shortest_paths (g, N, weight, pred, distance, choose_param(get_param(params, distance_combine_t()), closed_plus()), choose_param(get_param(params, distance_compare_t()), std::less()), choose_param(get_param(params, graph_visitor), null_vis) ); } template bool bellman_dispatch2 (VertexAndEdgeListGraph& g, detail::error_property_not_found, Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance, const bgl_named_params& params) { typedef typename property_traits::value_type D; bellman_visitor<> null_vis; return bellman_ford_shortest_paths (g, N, weight, pred, distance, choose_param(get_param(params, distance_combine_t()), closed_plus()), choose_param(get_param(params, distance_compare_t()), std::less()), choose_param(get_param(params, graph_visitor), null_vis) ); } template bool bellman_dispatch(EdgeListGraph& g, Size N, WeightMap weight, DistanceMap distance, const bgl_named_params& params) { dummy_property_map dummy_pred; return detail::bellman_dispatch2 (g, get_param(params, root_vertex_t()), N, weight, choose_param(get_param(params, vertex_predecessor), dummy_pred), distance, params); } } // namespace detail template bool bellman_ford_shortest_paths (EdgeListGraph& g, Size N, const bgl_named_params& params) { return detail::bellman_dispatch (g, N, choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N) { bgl_named_params params(0); return bellman_ford_shortest_paths(g, N, params); } template bool bellman_ford_shortest_paths (VertexAndEdgeListGraph& g, const bgl_named_params& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
& params) { typedef typename property_traits::value_type D; bellman_visitor<> null_vis; return bellman_ford_shortest_paths (g, N, weight, pred, distance, choose_param(get_param(params, distance_combine_t()), closed_plus()), choose_param(get_param(params, distance_compare_t()), std::less()), choose_param(get_param(params, graph_visitor), null_vis) ); } template bool bellman_dispatch(EdgeListGraph& g, Size N, WeightMap weight, DistanceMap distance, const bgl_named_params& params) { dummy_property_map dummy_pred; return detail::bellman_dispatch2 (g, get_param(params, root_vertex_t()), N, weight, choose_param(get_param(params, vertex_predecessor), dummy_pred), distance, params); } } // namespace detail template bool bellman_ford_shortest_paths (EdgeListGraph& g, Size N, const bgl_named_params& params) { return detail::bellman_dispatch (g, N, choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N) { bgl_named_params params(0); return bellman_ford_shortest_paths(g, N, params); } template bool bellman_ford_shortest_paths (VertexAndEdgeListGraph& g, const bgl_named_params& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
& params) { dummy_property_map dummy_pred; return detail::bellman_dispatch2 (g, get_param(params, root_vertex_t()), N, weight, choose_param(get_param(params, vertex_predecessor), dummy_pred), distance, params); } } // namespace detail template bool bellman_ford_shortest_paths (EdgeListGraph& g, Size N, const bgl_named_params& params) { return detail::bellman_dispatch (g, N, choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N) { bgl_named_params params(0); return bellman_ford_shortest_paths(g, N, params); } template bool bellman_ford_shortest_paths (VertexAndEdgeListGraph& g, const bgl_named_params& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
& params) { return detail::bellman_dispatch (g, N, choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } template bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N) { bgl_named_params params(0); return bellman_ford_shortest_paths(g, N, params); } template bool bellman_ford_shortest_paths (VertexAndEdgeListGraph& g, const bgl_named_params& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP
& params) { function_requires >(); return detail::bellman_dispatch (g, num_vertices(g), choose_const_pmap(get_param(params, edge_weight), g, edge_weight), choose_pmap(get_param(params, vertex_distance), g, vertex_distance), params); } } // namespace boost #endif // BOOST_GRAPH_BELLMAN_FORD_SHORTEST_PATHS_HPP