1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/graph/johnson_all_pairs_shortest.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,206 @@
1.4 +//=======================================================================
1.5 +// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
1.6 +// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
1.7 +//
1.8 +// Distributed under the Boost Software License, Version 1.0. (See
1.9 +// accompanying file LICENSE_1_0.txt or copy at
1.10 +// http://www.boost.org/LICENSE_1_0.txt)
1.11 +//=======================================================================
1.12 +
1.13 +/*
1.14 + This file implements the function
1.15 +
1.16 + template <class VertexAndEdgeListGraph, class DistanceMatrix,
1.17 + class P, class T, class R>
1.18 + bool
1.19 + johnson_all_pairs_shortest_paths
1.20 + (VertexAndEdgeListGraph& g,
1.21 + DistanceMatrix& D,
1.22 + const bgl_named_params<P, T, R>& params)
1.23 + */
1.24 +
1.25 +#ifndef BOOST_GRAPH_JOHNSON_HPP
1.26 +#define BOOST_GRAPH_JOHNSON_HPP
1.27 +
1.28 +#include <boost/graph/graph_traits.hpp>
1.29 +#include <boost/property_map.hpp>
1.30 +#include <boost/graph/bellman_ford_shortest_paths.hpp>
1.31 +#include <boost/graph/dijkstra_shortest_paths.hpp>
1.32 +#include <boost/graph/adjacency_list.hpp>
1.33 +#include <boost/pending/ct_if.hpp>
1.34 +#include <boost/type_traits/same_traits.hpp>
1.35 +
1.36 +namespace boost {
1.37 +
1.38 + template <class VertexAndEdgeListGraph, class DistanceMatrix,
1.39 + class VertexID, class Weight, typename BinaryPredicate,
1.40 + typename BinaryFunction, typename Infinity, class DistanceZero>
1.41 + bool
1.42 + johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
1.43 + DistanceMatrix& D,
1.44 + VertexID id1, Weight w1, const BinaryPredicate& compare,
1.45 + const BinaryFunction& combine, const Infinity& inf,
1.46 + DistanceZero zero)
1.47 + {
1.48 + typedef graph_traits<VertexAndEdgeListGraph> Traits1;
1.49 + typedef typename property_traits<Weight>::value_type DT;
1.50 + function_requires< BasicMatrixConcept<DistanceMatrix,
1.51 + typename Traits1::vertices_size_type, DT> >();
1.52 +
1.53 + typedef typename Traits1::directed_category DirCat;
1.54 + bool is_undirected = is_same<DirCat, undirected_tag>::value;
1.55 +
1.56 + typedef adjacency_list<vecS, vecS, directedS,
1.57 + property< vertex_distance_t, DT>,
1.58 + property< edge_weight_t, DT,
1.59 + property< edge_weight2_t, DT > > > Graph2;
1.60 + typedef graph_traits<Graph2> Traits2;
1.61 +
1.62 + Graph2 g2(num_vertices(g1) + 1);
1.63 + typename property_map<Graph2, edge_weight_t>::type
1.64 + w = get(edge_weight, g2);
1.65 + typename property_map<Graph2, edge_weight2_t>::type
1.66 + w_hat = get(edge_weight2, g2);
1.67 + typename property_map<Graph2, vertex_distance_t>::type
1.68 + d = get(vertex_distance, g2);
1.69 + typedef typename property_map<Graph2, vertex_index_t>::type VertexID2;
1.70 + VertexID2 id2 = get(vertex_index, g2);
1.71 +
1.72 + // Construct g2 where V[g2] = V[g1] U {s}
1.73 + // and E[g2] = E[g1] U {(s,v)| v in V[g1]}
1.74 + std::vector<typename Traits1::vertex_descriptor>
1.75 + verts1(num_vertices(g1) + 1);
1.76 + typename Traits2::vertex_descriptor s = *vertices(g2).first;
1.77 + {
1.78 + typename Traits1::vertex_iterator v, v_end;
1.79 + int i = 1;
1.80 + for (tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
1.81 + typename Traits2::edge_descriptor e; bool z;
1.82 + tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
1.83 + put(w, e, zero);
1.84 + verts1[i] = *v;
1.85 + }
1.86 + typename Traits1::edge_iterator e, e_end;
1.87 + for (tie(e, e_end) = edges(g1); e != e_end; ++e) {
1.88 + typename Traits2::edge_descriptor e2; bool z;
1.89 + tie(e2, z) = add_edge(get(id1, source(*e, g1)) + 1,
1.90 + get(id1, target(*e, g1)) + 1, g2);
1.91 + put(w, e2, get(w1, *e));
1.92 + if (is_undirected) {
1.93 + tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
1.94 + get(id1, source(*e, g1)) + 1, g2);
1.95 + put(w, e2, get(w1, *e));
1.96 + }
1.97 + }
1.98 + }
1.99 + typename Traits2::vertex_iterator v, v_end, u, u_end;
1.100 + typename Traits2::edge_iterator e, e_end;
1.101 + std::vector<DT> h_vec(num_vertices(g2));
1.102 + typedef typename std::vector<DT>::iterator iter_t;
1.103 + iterator_property_map<iter_t,VertexID2,DT,DT&> h(h_vec.begin(), id2);
1.104 +
1.105 + for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
1.106 + d[*v] = inf;
1.107 +
1.108 + put(d, s, zero);
1.109 + // Using the non-named parameter versions of bellman_ford and
1.110 + // dijkstra for portability reasons.
1.111 + dummy_property_map pred; bellman_visitor<> bvis;
1.112 + if (bellman_ford_shortest_paths
1.113 + (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
1.114 + for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
1.115 + put(h, *v, get(d, *v));
1.116 + // Reweight the edges to remove negatives
1.117 + for (tie(e, e_end) = edges(g2); e != e_end; ++e) {
1.118 + typename Traits2::vertex_descriptor a = source(*e, g2),
1.119 + b = target(*e, g2);
1.120 + put(w_hat, *e, get(w, *e) + get(h, a) - get(h, b));
1.121 + }
1.122 + for (tie(u, u_end) = vertices(g2); u != u_end; ++u) {
1.123 + dijkstra_visitor<> dvis;
1.124 + dijkstra_shortest_paths
1.125 + (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
1.126 + for (tie(v, v_end) = vertices(g2); v != v_end; ++v) {
1.127 + if (*u != s && *v != s) {
1.128 + typename Traits1::vertex_descriptor u1, v1;
1.129 + u1 = verts1[id2[*u]]; v1 = verts1[id2[*v]];
1.130 + D[id2[*u]-1][id2[*v]-1] = get(d, *v) + get(h, *v) - get(h, *u);
1.131 + }
1.132 + }
1.133 + }
1.134 + return true;
1.135 + } else
1.136 + return false;
1.137 + }
1.138 +
1.139 + template <class VertexAndEdgeListGraph, class DistanceMatrix,
1.140 + class VertexID, class Weight, class DistanceZero>
1.141 + bool
1.142 + johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
1.143 + DistanceMatrix& D,
1.144 + VertexID id1, Weight w1, DistanceZero zero)
1.145 + {
1.146 + typedef typename property_traits<Weight>::value_type WT;
1.147 + return johnson_all_pairs_shortest_paths(g1, D, id1, w1,
1.148 + std::less<WT>(),
1.149 + closed_plus<WT>(),
1.150 + (std::numeric_limits<WT>::max)(),
1.151 + zero);
1.152 + }
1.153 +
1.154 + namespace detail {
1.155 +
1.156 + template <class VertexAndEdgeListGraph, class DistanceMatrix,
1.157 + class P, class T, class R, class Weight,
1.158 + class VertexID>
1.159 + bool
1.160 + johnson_dispatch(VertexAndEdgeListGraph& g,
1.161 + DistanceMatrix& D,
1.162 + const bgl_named_params<P, T, R>& params,
1.163 + Weight w, VertexID id)
1.164 + {
1.165 + typedef typename property_traits<Weight>::value_type WT;
1.166 +
1.167 + return johnson_all_pairs_shortest_paths
1.168 + (g, D, id, w,
1.169 + choose_param(get_param(params, distance_compare_t()),
1.170 + std::less<WT>()),
1.171 + choose_param(get_param(params, distance_combine_t()),
1.172 + closed_plus<WT>()),
1.173 + choose_param(get_param(params, distance_inf_t()),
1.174 + std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
1.175 + choose_param(get_param(params, distance_zero_t()), WT()) );
1.176 + }
1.177 +
1.178 + } // namespace detail
1.179 +
1.180 + template <class VertexAndEdgeListGraph, class DistanceMatrix,
1.181 + class P, class T, class R>
1.182 + bool
1.183 + johnson_all_pairs_shortest_paths
1.184 + (VertexAndEdgeListGraph& g,
1.185 + DistanceMatrix& D,
1.186 + const bgl_named_params<P, T, R>& params)
1.187 + {
1.188 + return detail::johnson_dispatch
1.189 + (g, D, params,
1.190 + choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
1.191 + choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
1.192 + );
1.193 + }
1.194 +
1.195 + template <class VertexAndEdgeListGraph, class DistanceMatrix>
1.196 + bool
1.197 + johnson_all_pairs_shortest_paths
1.198 + (VertexAndEdgeListGraph& g, DistanceMatrix& D)
1.199 + {
1.200 + bgl_named_params<int,int> params(1);
1.201 + return detail::johnson_dispatch
1.202 + (g, D, params, get(edge_weight, g), get(vertex_index, g));
1.203 + }
1.204 +
1.205 +} // namespace boost
1.206 +
1.207 +#endif // BOOST_GRAPH_JOHNSON_HPP
1.208 +
1.209 +