PageRenderTime 47ms CodeModel.GetById 6ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/graph/johnson_all_pairs_shortest.hpp

http://hadesmem.googlecode.com/
C++ Header | 204 lines | 158 code | 22 blank | 24 comment | 20 complexity | 098324f7f67ccd12000fbc9f2a6c2e68 MD5 | raw file
  1//=======================================================================
  2// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4//
  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//=======================================================================
  9
 10/*
 11  This file implements the function
 12
 13  template <class VertexAndEdgeListGraph, class DistanceMatrix,
 14            class P, class T, class R>
 15  bool
 16  johnson_all_pairs_shortest_paths
 17    (VertexAndEdgeListGraph& g, 
 18     DistanceMatrix& D,
 19     const bgl_named_params<P, T, R>& params)
 20 */
 21
 22#ifndef BOOST_GRAPH_JOHNSON_HPP
 23#define BOOST_GRAPH_JOHNSON_HPP
 24
 25#include <boost/graph/graph_traits.hpp>
 26#include <boost/property_map/property_map.hpp>
 27#include <boost/property_map/shared_array_property_map.hpp>
 28#include <boost/graph/bellman_ford_shortest_paths.hpp>
 29#include <boost/graph/dijkstra_shortest_paths.hpp>
 30#include <boost/graph/adjacency_list.hpp>
 31#include <boost/type_traits/same_traits.hpp>
 32
 33namespace boost {
 34
 35  template <class VertexAndEdgeListGraph, class DistanceMatrix,
 36            class VertexID, class Weight, typename BinaryPredicate, 
 37            typename BinaryFunction, typename Infinity, class DistanceZero>
 38  bool
 39  johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1, 
 40               DistanceMatrix& D,
 41               VertexID id1, Weight w1, const BinaryPredicate& compare, 
 42               const BinaryFunction& combine, const Infinity& inf,
 43               DistanceZero zero)
 44  {
 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> >();
 49
 50    typedef typename Traits1::directed_category DirCat;
 51    bool is_undirected = is_same<DirCat, undirected_tag>::value;
 52
 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;
 58
 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);
 68
 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;
 74    {
 75      typename Traits1::vertex_iterator v, v_end;
 76      int i = 1;
 77      for (boost::tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
 78        typename Traits2::edge_descriptor e; bool z;
 79        boost::tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
 80        put(w, e, zero);
 81        verts1[i] = *v;
 82      }
 83      typename Traits1::edge_iterator e, e_end;
 84      for (boost::tie(e, e_end) = edges(g1); e != e_end; ++e) {
 85        typename Traits2::edge_descriptor e2; bool z;
 86        boost::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));
 89        if (is_undirected) {
 90          boost::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));
 93        }
 94      }
 95    }
 96    typename Traits2::vertex_iterator v, v_end, u, u_end;
 97    typename Traits2::edge_iterator e, e_end;
 98    shared_array_property_map<DT,VertexID2> h(num_vertices(g2), id2);
 99
100    for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
101      put(d, *v, inf);
102
103    put(d, s, zero);
104    // Using the non-named parameter versions of bellman_ford and
105    // dijkstra for portability reasons.
106    dummy_property_map pred; bellman_visitor<> bvis;
107    if (bellman_ford_shortest_paths
108        (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
109      for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v)
110        put(h, *v, get(d, *v));
111      // Reweight the edges to remove negatives
112      for (boost::tie(e, e_end) = edges(g2); e != e_end; ++e) {
113        typename Traits2::vertex_descriptor a = source(*e, g2),
114          b = target(*e, g2);
115        put(w_hat, *e, combine(get(w, *e), (get(h, a) - get(h, b))));
116      }
117      for (boost::tie(u, u_end) = vertices(g2); u != u_end; ++u) {
118        dijkstra_visitor<> dvis;
119        dijkstra_shortest_paths
120          (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
121        for (boost::tie(v, v_end) = vertices(g2); v != v_end; ++v) {
122          if (*u != s && *v != s) {
123            typename Traits1::vertex_descriptor u1, v1;
124            u1 = verts1[get(id2, *u)]; v1 = verts1[get(id2, *v)];
125            D[get(id2, *u)-1][get(id2, *v)-1] = combine(get(d, *v), (get(h, *v) - get(h, *u)));
126          }
127        }
128      }
129      return true;
130    } else
131      return false;
132  }
133
134  template <class VertexAndEdgeListGraph, class DistanceMatrix,
135            class VertexID, class Weight, class DistanceZero>
136  bool
137  johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1, 
138               DistanceMatrix& D,
139               VertexID id1, Weight w1, DistanceZero zero)
140  {
141    typedef typename property_traits<Weight>::value_type WT;
142    return johnson_all_pairs_shortest_paths(g1, D, id1, w1, 
143                                            std::less<WT>(),
144                                            closed_plus<WT>(),
145                                            (std::numeric_limits<WT>::max)(),
146                                            zero);
147  }
148
149  namespace detail {
150
151    template <class VertexAndEdgeListGraph, class DistanceMatrix,
152              class P, class T, class R, class Weight, 
153              class VertexID>
154    bool
155    johnson_dispatch(VertexAndEdgeListGraph& g, 
156                     DistanceMatrix& D,
157                     const bgl_named_params<P, T, R>& params,
158                     Weight w, VertexID id)
159    {
160      typedef typename property_traits<Weight>::value_type WT;
161      
162      return johnson_all_pairs_shortest_paths
163        (g, D, id, w,
164        choose_param(get_param(params, distance_compare_t()), 
165          std::less<WT>()),
166        choose_param(get_param(params, distance_combine_t()), 
167          closed_plus<WT>()),
168        choose_param(get_param(params, distance_inf_t()), 
169          std::numeric_limits<WT>::max BOOST_PREVENT_MACRO_SUBSTITUTION()),
170         choose_param(get_param(params, distance_zero_t()), WT()) );
171    }
172
173  } // namespace detail
174
175  template <class VertexAndEdgeListGraph, class DistanceMatrix,
176            class P, class T, class R>
177  bool
178  johnson_all_pairs_shortest_paths
179    (VertexAndEdgeListGraph& g, 
180     DistanceMatrix& D,
181     const bgl_named_params<P, T, R>& params)
182  {
183    return detail::johnson_dispatch
184      (g, D, params,
185       choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
186       choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
187       );
188  }
189
190  template <class VertexAndEdgeListGraph, class DistanceMatrix>
191  bool
192  johnson_all_pairs_shortest_paths
193    (VertexAndEdgeListGraph& g, DistanceMatrix& D)
194  {
195    bgl_named_params<int,int> params(1);
196    return detail::johnson_dispatch
197      (g, D, params, get(edge_weight, g), get(vertex_index, g));
198  }
199
200} // namespace boost
201
202#endif // BOOST_GRAPH_JOHNSON_HPP
203
204