annotate DEPENDENCIES/generic/include/boost/graph/johnson_all_pairs_shortest.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 //=======================================================================
Chris@16 2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
Chris@16 3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 //=======================================================================
Chris@16 9
Chris@16 10 /*
Chris@16 11 This file implements the function
Chris@16 12
Chris@16 13 template <class VertexAndEdgeListGraph, class DistanceMatrix,
Chris@16 14 class P, class T, class R>
Chris@16 15 bool
Chris@16 16 johnson_all_pairs_shortest_paths
Chris@16 17 (VertexAndEdgeListGraph& g,
Chris@16 18 DistanceMatrix& D,
Chris@16 19 const bgl_named_params<P, T, R>& params)
Chris@16 20 */
Chris@16 21
Chris@16 22 #ifndef BOOST_GRAPH_JOHNSON_HPP
Chris@16 23 #define BOOST_GRAPH_JOHNSON_HPP
Chris@16 24
Chris@16 25 #include <boost/graph/graph_traits.hpp>
Chris@16 26 #include <boost/property_map/property_map.hpp>
Chris@16 27 #include <boost/property_map/shared_array_property_map.hpp>
Chris@16 28 #include <boost/graph/bellman_ford_shortest_paths.hpp>
Chris@16 29 #include <boost/graph/dijkstra_shortest_paths.hpp>
Chris@16 30 #include <boost/graph/adjacency_list.hpp>
Chris@16 31 #include <boost/type_traits/same_traits.hpp>
Chris@16 32 #include <boost/concept/assert.hpp>
Chris@16 33
Chris@16 34 namespace boost {
Chris@16 35
Chris@16 36 template <class VertexAndEdgeListGraph, class DistanceMatrix,
Chris@16 37 class VertexID, class Weight, typename BinaryPredicate,
Chris@16 38 typename BinaryFunction, typename Infinity, class DistanceZero>
Chris@16 39 bool
Chris@16 40 johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
Chris@16 41 DistanceMatrix& D,
Chris@16 42 VertexID id1, Weight w1, const BinaryPredicate& compare,
Chris@16 43 const BinaryFunction& combine, const Infinity& inf,
Chris@16 44 DistanceZero zero)
Chris@16 45 {
Chris@16 46 typedef graph_traits<VertexAndEdgeListGraph> Traits1;
Chris@16 47 typedef typename property_traits<Weight>::value_type DT;
Chris@16 48 BOOST_CONCEPT_ASSERT(( BasicMatrixConcept<DistanceMatrix,
Chris@16 49 typename Traits1::vertices_size_type, DT> ));
Chris@16 50
Chris@16 51 typedef typename Traits1::directed_category DirCat;
Chris@16 52 bool is_undirected = is_same<DirCat, undirected_tag>::value;
Chris@16 53
Chris@16 54 typedef adjacency_list<vecS, vecS, directedS,
Chris@16 55 property< vertex_distance_t, DT>,
Chris@16 56 property< edge_weight_t, DT,
Chris@16 57 property< edge_weight2_t, DT > > > Graph2;
Chris@16 58 typedef graph_traits<Graph2> Traits2;
Chris@16 59
Chris@16 60 Graph2 g2(num_vertices(g1) + 1);
Chris@16 61 typename property_map<Graph2, edge_weight_t>::type
Chris@16 62 w = get(edge_weight, g2);
Chris@16 63 typename property_map<Graph2, edge_weight2_t>::type
Chris@16 64 w_hat = get(edge_weight2, g2);
Chris@16 65 typename property_map<Graph2, vertex_distance_t>::type
Chris@16 66 d = get(vertex_distance, g2);
Chris@16 67 typedef typename property_map<Graph2, vertex_index_t>::type VertexID2;
Chris@16 68 VertexID2 id2 = get(vertex_index, g2);
Chris@16 69
Chris@16 70 // Construct g2 where V[g2] = V[g1] U {s}
Chris@16 71 // and E[g2] = E[g1] U {(s,v)| v in V[g1]}
Chris@16 72 std::vector<typename Traits1::vertex_descriptor>
Chris@16 73 verts1(num_vertices(g1) + 1);
Chris@16 74 typename Traits2::vertex_descriptor s = *vertices(g2).first;
Chris@16 75 {
Chris@16 76 typename Traits1::vertex_iterator v, v_end;
Chris@16 77 int i = 1;
Chris@16 78 for (boost::tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
Chris@16 79 typename Traits2::edge_descriptor e; bool z;
Chris@16 80 boost::tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
Chris@16 81 put(w, e, zero);
Chris@16 82 verts1[i] = *v;
Chris@16 83 }
Chris@16 84 typename Traits1::edge_iterator e, e_end;
Chris@16 85 for (boost::tie(e, e_end) = edges(g1); e != e_end; ++e) {
Chris@16 86 typename Traits2::edge_descriptor e2; bool z;
Chris@16 87 boost::tie(e2, z) = add_edge(get(id1, source(*e, g1)) + 1,
Chris@16 88 get(id1, target(*e, g1)) + 1, g2);
Chris@16 89 put(w, e2, get(w1, *e));
Chris@16 90 if (is_undirected) {
Chris@16 91 boost::tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
Chris@16 92 get(id1, source(*e, g1)) + 1, g2);
Chris@16 93 put(w, e2, get(w1, *e));
Chris@16 94 }
Chris@16 95 }
Chris@16 96 }
Chris@16 97 typename Traits2::vertex_iterator v, v_end, u, u_end;
Chris@16 98 typename Traits2::edge_iterator e, e_end;
Chris@16 99 shared_array_property_map<DT,VertexID2> h(num_vertices(g2), id2);
Chris@16 100
Chris@16 101 for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
Chris@16 102 put(d, *v, inf);
Chris@16 103
Chris@16 104 put(d, s, zero);
Chris@16 105 // Using the non-named parameter versions of bellman_ford and
Chris@16 106 // dijkstra for portability reasons.
Chris@16 107 dummy_property_map pred; bellman_visitor<> bvis;
Chris@16 108 if (bellman_ford_shortest_paths
Chris@16 109 (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
Chris@16 110 for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
Chris@16 111 put(h, *v, get(d, *v));
Chris@16 112 // Reweight the edges to remove negatives
Chris@16 113 for (boost::tie(e, e_end) = edges(g2); e != e_end; ++e) {
Chris@16 114 typename Traits2::vertex_descriptor a = source(*e, g2),
Chris@16 115 b = target(*e, g2);
Chris@16 116 put(w_hat, *e, combine((get(h, a) - get(h, b)), get(w, *e)));
Chris@16 117 }
Chris@16 118 for (boost::tie(u, u_end) = vertices(g2); u != u_end; ++u) {
Chris@16 119 dijkstra_visitor<> dvis;
Chris@16 120 dijkstra_shortest_paths
Chris@16 121 (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
Chris@16 122 for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v) {
Chris@16 123 if (*u != s && *v != s) {
Chris@16 124 D[get(id2, *u)-1][get(id2, *v)-1] = combine((get(h, *v) - get(h, *u)), get(d, *v));
Chris@16 125 }
Chris@16 126 }
Chris@16 127 }
Chris@16 128 return true;
Chris@16 129 } else
Chris@16 130 return false;
Chris@16 131 }
Chris@16 132
Chris@16 133 template <class VertexAndEdgeListGraph, class DistanceMatrix,
Chris@16 134 class VertexID, class Weight, class DistanceZero>
Chris@16 135 bool
Chris@16 136 johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
Chris@16 137 DistanceMatrix& D,
Chris@16 138 VertexID id1, Weight w1, DistanceZero zero)
Chris@16 139 {
Chris@16 140 typedef typename property_traits<Weight>::value_type WT;
Chris@16 141 return johnson_all_pairs_shortest_paths(g1, D, id1, w1,
Chris@16 142 std::less<WT>(),
Chris@16 143 closed_plus<WT>(),
Chris@16 144 (std::numeric_limits<WT>::max)(),
Chris@16 145 zero);
Chris@16 146 }
Chris@16 147
Chris@16 148 namespace detail {
Chris@16 149
Chris@16 150 template <class VertexAndEdgeListGraph, class DistanceMatrix,
Chris@16 151 class P, class T, class R, class Weight,
Chris@16 152 class VertexID>
Chris@16 153 bool
Chris@16 154 johnson_dispatch(VertexAndEdgeListGraph& g,
Chris@16 155 DistanceMatrix& D,
Chris@16 156 const bgl_named_params<P, T, R>& params,
Chris@16 157 Weight w, VertexID id)
Chris@16 158 {
Chris@16 159 typedef typename property_traits<Weight>::value_type WT;
Chris@16 160
Chris@16 161 return johnson_all_pairs_shortest_paths
Chris@16 162 (g, D, id, w,
Chris@16 163 choose_param(get_param(params, distance_compare_t()),
Chris@16 164 std::less<WT>()),
Chris@16 165 choose_param(get_param(params, distance_combine_t()),
Chris@16 166 closed_plus<WT>()),
Chris@16 167 choose_param(get_param(params, distance_inf_t()),
Chris@16 168 std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
Chris@16 169 choose_param(get_param(params, distance_zero_t()), WT()) );
Chris@16 170 }
Chris@16 171
Chris@16 172 } // namespace detail
Chris@16 173
Chris@16 174 template <class VertexAndEdgeListGraph, class DistanceMatrix,
Chris@16 175 class P, class T, class R>
Chris@16 176 bool
Chris@16 177 johnson_all_pairs_shortest_paths
Chris@16 178 (VertexAndEdgeListGraph& g,
Chris@16 179 DistanceMatrix& D,
Chris@16 180 const bgl_named_params<P, T, R>& params)
Chris@16 181 {
Chris@16 182 return detail::johnson_dispatch
Chris@16 183 (g, D, params,
Chris@16 184 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
Chris@16 185 choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
Chris@16 186 );
Chris@16 187 }
Chris@16 188
Chris@16 189 template <class VertexAndEdgeListGraph, class DistanceMatrix>
Chris@16 190 bool
Chris@16 191 johnson_all_pairs_shortest_paths
Chris@16 192 (VertexAndEdgeListGraph& g, DistanceMatrix& D)
Chris@16 193 {
Chris@16 194 bgl_named_params<int,int> params(1);
Chris@16 195 return detail::johnson_dispatch
Chris@16 196 (g, D, params, get(edge_weight, g), get(vertex_index, g));
Chris@16 197 }
Chris@16 198
Chris@16 199 } // namespace boost
Chris@16 200
Chris@16 201 #endif // BOOST_GRAPH_JOHNSON_HPP
Chris@16 202
Chris@16 203