1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
11 This file implements the function
13 template <class VertexAndEdgeListGraph, class DistanceMatrix,
14 class P, class T, class R>
16 johnson_all_pairs_shortest_paths
17 (VertexAndEdgeListGraph& g,
19 const bgl_named_params<P, T, R>& params)
22 #ifndef BOOST_GRAPH_JOHNSON_HPP
23 #define BOOST_GRAPH_JOHNSON_HPP
25 #include <boost/graph/graph_traits.hpp>
26 #include <boost/property_map.hpp>
27 #include <boost/graph/bellman_ford_shortest_paths.hpp>
28 #include <boost/graph/dijkstra_shortest_paths.hpp>
29 #include <boost/graph/adjacency_list.hpp>
30 #include <boost/pending/ct_if.hpp>
31 #include <boost/type_traits/same_traits.hpp>
35 template <class VertexAndEdgeListGraph, class DistanceMatrix,
36 class VertexID, class Weight, typename BinaryPredicate,
37 typename BinaryFunction, typename Infinity, class DistanceZero>
39 johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
41 VertexID id1, Weight w1, const BinaryPredicate& compare,
42 const BinaryFunction& combine, const Infinity& inf,
45 typedef graph_traits<VertexAndEdgeListGraph> Traits1;
46 typedef typename property_traits<Weight>::value_type DT;
47 function_requires< BasicMatrixConcept<DistanceMatrix,
48 typename Traits1::vertices_size_type, DT> >();
50 typedef typename Traits1::directed_category DirCat;
51 bool is_undirected = is_same<DirCat, undirected_tag>::value;
53 typedef adjacency_list<vecS, vecS, directedS,
54 property< vertex_distance_t, DT>,
55 property< edge_weight_t, DT,
56 property< edge_weight2_t, DT > > > Graph2;
57 typedef graph_traits<Graph2> Traits2;
59 Graph2 g2(num_vertices(g1) + 1);
60 typename property_map<Graph2, edge_weight_t>::type
61 w = get(edge_weight, g2);
62 typename property_map<Graph2, edge_weight2_t>::type
63 w_hat = get(edge_weight2, g2);
64 typename property_map<Graph2, vertex_distance_t>::type
65 d = get(vertex_distance, g2);
66 typedef typename property_map<Graph2, vertex_index_t>::type VertexID2;
67 VertexID2 id2 = get(vertex_index, g2);
69 // Construct g2 where V[g2] = V[g1] U {s}
70 // and E[g2] = E[g1] U {(s,v)| v in V[g1]}
71 std::vector<typename Traits1::vertex_descriptor>
72 verts1(num_vertices(g1) + 1);
73 typename Traits2::vertex_descriptor s = *vertices(g2).first;
75 typename Traits1::vertex_iterator v, v_end;
77 for (tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
78 typename Traits2::edge_descriptor e; bool z;
79 tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
83 typename Traits1::edge_iterator e, e_end;
84 for (tie(e, e_end) = edges(g1); e != e_end; ++e) {
85 typename Traits2::edge_descriptor e2; bool z;
86 tie(e2, z) = add_edge(get(id1, source(*e, g1)) + 1,
87 get(id1, target(*e, g1)) + 1, g2);
88 put(w, e2, get(w1, *e));
90 tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
91 get(id1, source(*e, g1)) + 1, g2);
92 put(w, e2, get(w1, *e));
96 typename Traits2::vertex_iterator v, v_end, u, u_end;
97 typename Traits2::edge_iterator e, e_end;
98 std::vector<DT> h_vec(num_vertices(g2));
99 typedef typename std::vector<DT>::iterator iter_t;
100 iterator_property_map<iter_t,VertexID2,DT,DT&> h(h_vec.begin(), id2);
102 for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
106 // Using the non-named parameter versions of bellman_ford and
107 // dijkstra for portability reasons.
108 dummy_property_map pred; bellman_visitor<> bvis;
109 if (bellman_ford_shortest_paths
110 (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
111 for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
112 put(h, *v, get(d, *v));
113 // Reweight the edges to remove negatives
114 for (tie(e, e_end) = edges(g2); e != e_end; ++e) {
115 typename Traits2::vertex_descriptor a = source(*e, g2),
117 put(w_hat, *e, get(w, *e) + get(h, a) - get(h, b));
119 for (tie(u, u_end) = vertices(g2); u != u_end; ++u) {
120 dijkstra_visitor<> dvis;
121 dijkstra_shortest_paths
122 (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
123 for (tie(v, v_end) = vertices(g2); v != v_end; ++v) {
124 if (*u != s && *v != s) {
125 typename Traits1::vertex_descriptor u1, v1;
126 u1 = verts1[id2[*u]]; v1 = verts1[id2[*v]];
127 D[id2[*u]-1][id2[*v]-1] = get(d, *v) + get(h, *v) - get(h, *u);
136 template <class VertexAndEdgeListGraph, class DistanceMatrix,
137 class VertexID, class Weight, class DistanceZero>
139 johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
141 VertexID id1, Weight w1, DistanceZero zero)
143 typedef typename property_traits<Weight>::value_type WT;
144 return johnson_all_pairs_shortest_paths(g1, D, id1, w1,
147 (std::numeric_limits<WT>::max)(),
153 template <class VertexAndEdgeListGraph, class DistanceMatrix,
154 class P, class T, class R, class Weight,
157 johnson_dispatch(VertexAndEdgeListGraph& g,
159 const bgl_named_params<P, T, R>& params,
160 Weight w, VertexID id)
162 typedef typename property_traits<Weight>::value_type WT;
164 return johnson_all_pairs_shortest_paths
166 choose_param(get_param(params, distance_compare_t()),
168 choose_param(get_param(params, distance_combine_t()),
170 choose_param(get_param(params, distance_inf_t()),
171 std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
172 choose_param(get_param(params, distance_zero_t()), WT()) );
175 } // namespace detail
177 template <class VertexAndEdgeListGraph, class DistanceMatrix,
178 class P, class T, class R>
180 johnson_all_pairs_shortest_paths
181 (VertexAndEdgeListGraph& g,
183 const bgl_named_params<P, T, R>& params)
185 return detail::johnson_dispatch
187 choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
188 choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
192 template <class VertexAndEdgeListGraph, class DistanceMatrix>
194 johnson_all_pairs_shortest_paths
195 (VertexAndEdgeListGraph& g, DistanceMatrix& D)
197 bgl_named_params<int,int> params(1);
198 return detail::johnson_dispatch
199 (g, D, params, get(edge_weight, g), get(vertex_index, g));
204 #endif // BOOST_GRAPH_JOHNSON_HPP