Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //=======================================================================
0002 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
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 /*
0011   This file implements the function
0012 
0013   template <class VertexAndEdgeListGraph, class DistanceMatrix,
0014             class P, class T, class R>
0015   bool
0016   johnson_all_pairs_shortest_paths
0017     (VertexAndEdgeListGraph& g,
0018      DistanceMatrix& D,
0019      const bgl_named_params<P, T, R>& params)
0020  */
0021 
0022 #ifndef BOOST_GRAPH_JOHNSON_HPP
0023 #define BOOST_GRAPH_JOHNSON_HPP
0024 
0025 #include <boost/graph/graph_traits.hpp>
0026 #include <boost/property_map/property_map.hpp>
0027 #include <boost/property_map/shared_array_property_map.hpp>
0028 #include <boost/graph/bellman_ford_shortest_paths.hpp>
0029 #include <boost/graph/dijkstra_shortest_paths.hpp>
0030 #include <boost/graph/adjacency_list.hpp>
0031 #include <boost/type_traits/same_traits.hpp>
0032 #include <boost/concept/assert.hpp>
0033 
0034 namespace boost
0035 {
0036 
0037 template < class VertexAndEdgeListGraph, class DistanceMatrix, class VertexID,
0038     class Weight, typename BinaryPredicate, typename BinaryFunction,
0039     typename Infinity, class DistanceZero >
0040 bool johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
0041     DistanceMatrix& D, VertexID id1, Weight w1, const BinaryPredicate& compare,
0042     const BinaryFunction& combine, const Infinity& inf, DistanceZero zero)
0043 {
0044     typedef graph_traits< VertexAndEdgeListGraph > Traits1;
0045     typedef typename property_traits< Weight >::value_type DT;
0046     BOOST_CONCEPT_ASSERT((BasicMatrixConcept< DistanceMatrix,
0047         typename Traits1::vertices_size_type, DT >));
0048 
0049     typedef typename Traits1::directed_category DirCat;
0050     bool is_undirected = is_same< DirCat, undirected_tag >::value;
0051 
0052     typedef adjacency_list< vecS, vecS, directedS,
0053         property< vertex_distance_t, DT >,
0054         property< edge_weight_t, DT, property< edge_weight2_t, DT > > >
0055         Graph2;
0056     typedef graph_traits< Graph2 > Traits2;
0057 
0058     Graph2 g2(num_vertices(g1) + 1);
0059     typename property_map< Graph2, edge_weight_t >::type w
0060         = get(edge_weight, g2);
0061     typename property_map< Graph2, edge_weight2_t >::type w_hat
0062         = get(edge_weight2, g2);
0063     typename property_map< Graph2, vertex_distance_t >::type d
0064         = get(vertex_distance, g2);
0065     typedef typename property_map< Graph2, vertex_index_t >::type VertexID2;
0066     VertexID2 id2 = get(vertex_index, g2);
0067 
0068     // Construct g2 where V[g2] = V[g1] U {s}
0069     //   and  E[g2] = E[g1] U {(s,v)| v in V[g1]}
0070     std::vector< typename Traits1::vertex_descriptor > verts1(
0071         num_vertices(g1) + 1);
0072     typename Traits2::vertex_descriptor s = *vertices(g2).first;
0073     {
0074         typename Traits1::vertex_iterator v, v_end;
0075         int i = 1;
0076         for (boost::tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i)
0077         {
0078             typename Traits2::edge_descriptor e;
0079             bool z;
0080             boost::tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
0081             put(w, e, zero);
0082             verts1[i] = *v;
0083         }
0084         typename Traits1::edge_iterator e, e_end;
0085         for (boost::tie(e, e_end) = edges(g1); e != e_end; ++e)
0086         {
0087             typename Traits2::edge_descriptor e2;
0088             bool z;
0089             boost::tie(e2, z) = add_edge(
0090                 get(id1, source(*e, g1)) + 1, get(id1, target(*e, g1)) + 1, g2);
0091             put(w, e2, get(w1, *e));
0092             if (is_undirected)
0093             {
0094                 boost::tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
0095                     get(id1, source(*e, g1)) + 1, g2);
0096                 put(w, e2, get(w1, *e));
0097             }
0098         }
0099     }
0100     typename Traits2::vertex_iterator v, v_end, u, u_end;
0101     typename Traits2::edge_iterator e, e_end;
0102     shared_array_property_map< DT, VertexID2 > h(num_vertices(g2), id2);
0103 
0104     for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
0105         put(d, *v, inf);
0106 
0107     put(d, s, zero);
0108     // Using the non-named parameter versions of bellman_ford and
0109     // dijkstra for portability reasons.
0110     dummy_property_map pred;
0111     bellman_visitor<> bvis;
0112     if (bellman_ford_shortest_paths(
0113             g2, num_vertices(g2), w, pred, d, combine, compare, bvis))
0114     {
0115         for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
0116             put(h, *v, get(d, *v));
0117         // Reweight the edges to remove negatives
0118         for (boost::tie(e, e_end) = edges(g2); e != e_end; ++e)
0119         {
0120             typename Traits2::vertex_descriptor a = source(*e, g2),
0121                                                 b = target(*e, g2);
0122             put(w_hat, *e, combine((get(h, a) - get(h, b)), get(w, *e)));
0123         }
0124         for (boost::tie(u, u_end) = vertices(g2); u != u_end; ++u)
0125         {
0126             dijkstra_visitor<> dvis;
0127             dijkstra_shortest_paths(
0128                 g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero, dvis);
0129             for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
0130             {
0131                 if (*u != s && *v != s)
0132                 {
0133                     D[get(id2, *u) - 1][get(id2, *v) - 1]
0134                         = combine((get(h, *v) - get(h, *u)), get(d, *v));
0135                 }
0136             }
0137         }
0138         return true;
0139     }
0140     else
0141         return false;
0142 }
0143 
0144 template < class VertexAndEdgeListGraph, class DistanceMatrix, class VertexID,
0145     class Weight, class DistanceZero >
0146 bool johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
0147     DistanceMatrix& D, VertexID id1, Weight w1, DistanceZero zero)
0148 {
0149     typedef typename property_traits< Weight >::value_type WT;
0150     return johnson_all_pairs_shortest_paths(g1, D, id1, w1, std::less< WT >(),
0151         closed_plus< WT >(), (std::numeric_limits< WT >::max)(), zero);
0152 }
0153 
0154 namespace detail
0155 {
0156 
0157     template < class VertexAndEdgeListGraph, class DistanceMatrix, class P,
0158         class T, class R, class Weight, class VertexID >
0159     bool johnson_dispatch(VertexAndEdgeListGraph& g, DistanceMatrix& D,
0160         const bgl_named_params< P, T, R >& params, Weight w, VertexID id)
0161     {
0162         typedef typename property_traits< Weight >::value_type WT;
0163 
0164         return johnson_all_pairs_shortest_paths(g, D, id, w,
0165             choose_param(
0166                 get_param(params, distance_compare_t()), std::less< WT >()),
0167             choose_param(
0168                 get_param(params, distance_combine_t()), closed_plus< WT >()),
0169             choose_param(get_param(params, distance_inf_t()),
0170                 std::numeric_limits< WT >::max
0171                     BOOST_PREVENT_MACRO_SUBSTITUTION()),
0172             choose_param(get_param(params, distance_zero_t()), WT()));
0173     }
0174 
0175 } // namespace detail
0176 
0177 template < class VertexAndEdgeListGraph, class DistanceMatrix, class P, class T,
0178     class R >
0179 bool johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g,
0180     DistanceMatrix& D, const bgl_named_params< P, T, R >& params)
0181 {
0182     return detail::johnson_dispatch(g, D, params,
0183         choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
0184         choose_const_pmap(get_param(params, vertex_index), g, vertex_index));
0185 }
0186 
0187 template < class VertexAndEdgeListGraph, class DistanceMatrix >
0188 bool johnson_all_pairs_shortest_paths(
0189     VertexAndEdgeListGraph& g, DistanceMatrix& D)
0190 {
0191     bgl_named_params< int, int > params(1);
0192     return detail::johnson_dispatch(
0193         g, D, params, get(edge_weight, g), get(vertex_index, g));
0194 }
0195 
0196 } // namespace boost
0197 
0198 #endif // BOOST_GRAPH_JOHNSON_HPP