Chris@16
|
1 // Copyright (C) 2006 The Trustees of Indiana University.
|
Chris@16
|
2
|
Chris@16
|
3 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // Authors: Douglas Gregor
|
Chris@16
|
8 // Andrew Lumsdaine
|
Chris@16
|
9 #ifndef BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|
Chris@16
|
10 #define BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/graph/graph_traits.hpp>
|
Chris@16
|
13 #include <boost/graph/two_bit_color_map.hpp>
|
Chris@16
|
14 #include <boost/graph/iteration_macros.hpp>
|
Chris@16
|
15 #include <boost/pending/queue.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost { namespace graph {
|
Chris@16
|
18
|
Chris@16
|
19 template<typename Graph, typename ColorMap>
|
Chris@16
|
20 bool
|
Chris@16
|
21 st_connected(const Graph& g,
|
Chris@16
|
22 typename graph_traits<Graph>::vertex_descriptor s,
|
Chris@16
|
23 typename graph_traits<Graph>::vertex_descriptor t,
|
Chris@16
|
24 ColorMap color)
|
Chris@16
|
25 {
|
Chris@16
|
26 typedef typename property_traits<ColorMap>::value_type Color;
|
Chris@16
|
27 typedef color_traits<Color> ColorTraits;
|
Chris@16
|
28 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
|
Chris@16
|
29
|
Chris@16
|
30 // Set all vertices to white (unvisited)
|
Chris@16
|
31 BGL_FORALL_VERTICES_T(v, g, Graph)
|
Chris@16
|
32 put(color, v, ColorTraits::white());
|
Chris@16
|
33
|
Chris@16
|
34 // Vertices found from the source are grey
|
Chris@16
|
35 put(color, s, ColorTraits::gray());
|
Chris@16
|
36
|
Chris@16
|
37 // Vertices found from the target are greeen
|
Chris@16
|
38 put(color, t, ColorTraits::green());
|
Chris@16
|
39 queue<Vertex> Q;
|
Chris@16
|
40 Q.push(s);
|
Chris@16
|
41 Q.push(t);
|
Chris@16
|
42
|
Chris@16
|
43 while (!Q.empty()) {
|
Chris@16
|
44 Vertex u = Q.top(); Q.pop();
|
Chris@16
|
45 Color u_color = get(color, u);
|
Chris@16
|
46
|
Chris@16
|
47 BGL_FORALL_OUTEDGES_T(u, e, g, Graph) {
|
Chris@16
|
48 Vertex v = target(e, g);
|
Chris@16
|
49 Color v_color = get(color, v);
|
Chris@16
|
50 if (v_color == ColorTraits::white()) {
|
Chris@16
|
51 // We have not seen "v" before; mark it with the same color as u
|
Chris@16
|
52 Color u_color = get(color, u);
|
Chris@16
|
53 put(color, v, u_color);
|
Chris@16
|
54
|
Chris@16
|
55 // Push it on the queue
|
Chris@16
|
56 Q.push(v);
|
Chris@16
|
57 } else if (v_color != ColorTraits::black() && u_color != v_color) {
|
Chris@16
|
58 // Colors have collided. We're done!
|
Chris@16
|
59 return true;
|
Chris@16
|
60 }
|
Chris@16
|
61 }
|
Chris@16
|
62 // u is done, so mark it black
|
Chris@16
|
63 put(color, u, ColorTraits::black());
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 return false;
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 template<typename Graph>
|
Chris@16
|
70 inline bool
|
Chris@16
|
71 st_connected(const Graph& g,
|
Chris@16
|
72 typename graph_traits<Graph>::vertex_descriptor s,
|
Chris@16
|
73 typename graph_traits<Graph>::vertex_descriptor t)
|
Chris@16
|
74 {
|
Chris@16
|
75 return st_connected(g, s, t,
|
Chris@16
|
76 make_two_bit_color_map(num_vertices(g),
|
Chris@16
|
77 get(vertex_index, g)));
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 } } // end namespace boost::graph
|
Chris@16
|
81
|
Chris@16
|
82 #endif // BOOST_GRAPH_DISTRIBUTED_ST_CONNECTED_HPP
|