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