source: NonGTP/Boost/boost/graph/johnson_all_pairs_shortest.hpp @ 857

Revision 857, 6.7 KB checked in by igarcia, 19 years ago (diff)
Line 
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.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>
32
33namespace boost {
34
35  template <class VertexAndEdgeListGraph, class DistanceMatrix,
36            class VertexID, class Weight, class DistanceZero>
37  bool
38  johnson_all_pairs_shortest_paths(VertexAndEdgeListGraph& g1,
39               DistanceMatrix& D,
40               VertexID id1, Weight w1, DistanceZero zero)
41  {
42    typedef graph_traits<VertexAndEdgeListGraph> Traits1;
43    typedef typename property_traits<Weight>::value_type DT;
44    function_requires< BasicMatrixConcept<DistanceMatrix,
45      typename Traits1::vertices_size_type, DT> >();
46
47    typedef typename Traits1::directed_category DirCat;
48    bool is_undirected = is_same<DirCat, undirected_tag>::value;
49
50    typedef adjacency_list<vecS, vecS, directedS,
51      property< vertex_distance_t, DT>,
52      property< edge_weight_t, DT,
53      property< edge_weight2_t, DT > > > Graph2;
54    typedef graph_traits<Graph2> Traits2;
55
56    Graph2 g2(num_vertices(g1) + 1);
57    typename property_map<Graph2, edge_weight_t>::type
58      w = get(edge_weight, g2);
59    typename property_map<Graph2, edge_weight2_t>::type
60      w_hat = get(edge_weight2, g2);
61    typename property_map<Graph2, vertex_distance_t>::type
62      d = get(vertex_distance, g2);
63    typedef typename property_map<Graph2, vertex_index_t>::type VertexID2;
64    VertexID2 id2 = get(vertex_index, g2);
65
66    // Construct g2 where V[g2] = V[g1] U {s}
67    //   and  E[g2] = E[g1] U {(s,v)| v in V[g1]}
68    std::vector<typename Traits1::vertex_descriptor>
69      verts1(num_vertices(g1) + 1);
70    typename Traits2::vertex_descriptor s = *vertices(g2).first;
71    {
72      typename Traits1::vertex_iterator v, v_end;
73      int i = 1;
74      for (tie(v, v_end) = vertices(g1); v != v_end; ++v, ++i) {
75        typename Traits2::edge_descriptor e; bool z;
76        tie(e, z) = add_edge(s, get(id1, *v) + 1, g2);
77        put(w, e, zero);
78        verts1[i] = *v;
79      }
80      typename Traits1::edge_iterator e, e_end;
81      for (tie(e, e_end) = edges(g1); e != e_end; ++e) {
82        typename Traits2::edge_descriptor e2; bool z;
83        tie(e2, z) = add_edge(get(id1, source(*e, g1)) + 1,
84                              get(id1, target(*e, g1)) + 1, g2);
85        put(w, e2, get(w1, *e));
86        if (is_undirected) {
87          tie(e2, z) = add_edge(get(id1, target(*e, g1)) + 1,
88                                get(id1, source(*e, g1)) + 1, g2);
89          put(w, e2, get(w1, *e));
90        }
91      }
92    }
93    typename Traits2::vertex_iterator v, v_end, u, u_end;
94    typename Traits2::edge_iterator e, e_end;
95    std::vector<DT> h_vec(num_vertices(g2));
96    typedef typename std::vector<DT>::iterator iter_t;
97    iterator_property_map<iter_t,VertexID2,DT,DT&> h(h_vec.begin(), id2);
98
99    DT inf = (std::numeric_limits<DT>::max)();
100    for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
101      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; closed_plus<DT> combine;
107    std::less<DT> compare; bellman_visitor<> bvis;
108    if (bellman_ford_shortest_paths
109        (g2, num_vertices(g2), w, pred, d, combine, compare, bvis)) {
110      for (tie(v, v_end) = vertices(g2); v != v_end; ++v)
111        put(h, *v, get(d, *v));
112      // Reweight the edges to remove negatives
113      for (tie(e, e_end) = edges(g2); e != e_end; ++e) {
114        typename Traits2::vertex_descriptor a = source(*e, g2),
115          b = target(*e, g2);
116        put(w_hat, *e, get(w, *e) + get(h, a) - get(h, b));
117      }
118      for (tie(u, u_end) = vertices(g2); u != u_end; ++u) {
119        dijkstra_visitor<> dvis;
120        dijkstra_shortest_paths
121          (g2, *u, pred, d, w_hat, id2, compare, combine, inf, zero,dvis);
122        for (tie(v, v_end) = vertices(g2); v != v_end; ++v) {
123          if (*u != s && *v != s) {
124            typename Traits1::vertex_descriptor u1, v1;
125            u1 = verts1[id2[*u]]; v1 = verts1[id2[*v]];
126            D[id2[*u]-1][id2[*v]-1] = get(d, *v) + get(h, *v) - get(h, *u);
127          }
128        }
129      }
130      return true;
131    } else
132      return false;
133  }
134
135  namespace detail {
136
137    template <class VertexAndEdgeListGraph, class DistanceMatrix,
138              class P, class T, class R, class Weight,
139              class VertexID>
140    bool
141    johnson_dispatch(VertexAndEdgeListGraph& g,
142                     DistanceMatrix& D,
143                     const bgl_named_params<P, T, R>& params,
144                     Weight w, VertexID id)
145    {
146      typedef typename property_traits<Weight>::value_type WT;
147     
148      return johnson_all_pairs_shortest_paths
149        (g, D, id, w,
150         choose_param(get_param(params, distance_zero_t()), WT()) );
151    }
152
153  } // namespace detail
154
155  template <class VertexAndEdgeListGraph, class DistanceMatrix,
156            class P, class T, class R>
157  bool
158  johnson_all_pairs_shortest_paths
159    (VertexAndEdgeListGraph& g,
160     DistanceMatrix& D,
161     const bgl_named_params<P, T, R>& params)
162  {
163    return detail::johnson_dispatch
164      (g, D, params,
165       choose_const_pmap(get_param(params, edge_weight), g, edge_weight),
166       choose_const_pmap(get_param(params, vertex_index), g, vertex_index)
167       );
168  }
169
170  template <class VertexAndEdgeListGraph, class DistanceMatrix>
171  bool
172  johnson_all_pairs_shortest_paths
173    (VertexAndEdgeListGraph& g, DistanceMatrix& D)
174  {
175    bgl_named_params<int,int> params(1);
176    return detail::johnson_dispatch
177      (g, D, params, get(edge_weight, g), get(vertex_index, g));
178  }
179
180} // namespace boost
181
182#endif // BOOST_GRAPH_JOHNSON_HPP
183
184
Note: See TracBrowser for help on using the repository browser.