Commit fb37166a authored by Tiago Peixoto's avatar Tiago Peixoto

Add support for iterating over all vertices in {dfs,bfs,dijkstra}_{search,iterator}()

parent 5752f5eb
Pipeline #262 passed with stage
in 184 minutes and 22 seconds
......@@ -576,4 +576,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
.add_property("gcc_version", &LibInfo::GetGCCVersion);
def("get_graph_type", &get_graph_type);
def("get_null_vertex",
+[](){ return graph_traits<GraphInterface::multigraph_t>::null_vertex();});
}
......@@ -106,20 +106,28 @@ private:
boost::python::object _vis;
};
struct do_bfs
{
template <class Graph, class Visitor>
void operator()(Graph& g, size_t s, Visitor vis) const
{
breadth_first_search(g, vertex(s, g), visitor(vis));
}
};
void bfs_search(GraphInterface& g, size_t s, python::object vis)
void bfs_search(GraphInterface& gi, size_t s, python::object vis)
{
run_action<graph_tool::all_graph_views,mpl::true_>()
(g, std::bind(do_bfs(), std::placeholders::_1, s,
BFSVisitorWrapper(g, vis)))();
(gi,
[&](auto &g)
{
typedef typename std::remove_reference<decltype(g)>::type g_t;
typename vprop_map_t<default_color_type>::type
color(get(vertex_index_t(), g));
auto visw = BFSVisitorWrapper(gi, vis);
auto v = vertex(s, g);
if (v == graph_traits<g_t>::null_vertex())
{
for (auto u : vertices_range(g))
breadth_first_search(g, u, visitor(visw).color_map(color));
}
else
{
breadth_first_visit(g, v, visitor(visw).color_map(color));
}
})();
}
#ifdef HAVE_BOOST_COROUTINE
......@@ -152,7 +160,28 @@ boost::python::object bfs_search_generator(GraphInterface& g, size_t s)
{
BFSGeneratorVisitor vis(g, yield);
run_action<graph_tool::all_graph_views,mpl::true_>()
(g, std::bind(do_bfs(), std::placeholders::_1, s, vis))();
(g,
[&](auto &g)
{
typedef typename std::remove_reference<decltype(g)>::type g_t;
typename vprop_map_t<default_color_type>::type
color(get(vertex_index_t(), g));
auto v = vertex(s, g);
if (v == graph_traits<g_t>::null_vertex())
{
for (auto u : vertices_range(g))
{
if (color[u] == color_traits<default_color_type>::black())
continue;
breadth_first_visit(g, u, visitor(vis).color_map(color));
}
}
else
{
breadth_first_visit(g, v, visitor(vis).color_map(color));
}
})();
};
return boost::python::object(CoroGenerator(dispatch));
#else
......
......@@ -90,25 +90,22 @@ private:
python::object _vis;
};
struct do_dfs
void dfs_search(GraphInterface& gi, size_t s, python::object vis)
{
template <class Graph, class VertexIndexMap, class Visitor>
void operator()(Graph& g, VertexIndexMap vertex_index, size_t s,
Visitor vis) const
{
typename property_map_type::apply<default_color_type,
VertexIndexMap>::type
color(vertex_index);
depth_first_visit(g, vertex(s, g), vis, color);
}
};
void dfs_search(GraphInterface& g, size_t s, python::object vis)
{
run_action<graph_tool::all_graph_views,mpl::true_>()
(g, std::bind(do_dfs(), std::placeholders::_1, g.get_vertex_index(),
s, DFSVisitorWrapper(g, vis)))();
run_action<graph_tool::all_graph_views, mpl::true_>()
(gi,
[&](auto &g)
{
typedef typename std::remove_reference<decltype(g)>::type g_t;
typename vprop_map_t<default_color_type>::type
color(get(vertex_index_t(), g));
auto visw = DFSVisitorWrapper(gi, vis);
auto v = vertex(s, g);
if (v == graph_traits<g_t>::null_vertex())
depth_first_search(g, visw, color);
else
depth_first_visit(g, v, visw, color);
})();
}
#ifdef HAVE_BOOST_COROUTINE
......@@ -142,8 +139,18 @@ boost::python::object dfs_search_generator(GraphInterface& g, size_t s)
{
DFSGeneratorVisitor vis(g, yield);
run_action<graph_tool::all_graph_views,mpl::true_>()
(g, std::bind(do_dfs(), std::placeholders::_1,
g.get_vertex_index(), s, vis))();
(g,
[&](auto &g)
{
typedef typename std::remove_reference<decltype(g)>::type g_t;
typename vprop_map_t<default_color_type>::type
color(get(vertex_index_t(), g));
auto v = vertex(s, g);
if (v == graph_traits<g_t>::null_vertex())
depth_first_search(g, vis, color);
else
depth_first_visit(g, v, vis, color);
})();
};
return boost::python::object(CoroGenerator(dispatch));
#else
......
......@@ -140,11 +140,33 @@ struct do_djk_search
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
DynamicPropertyMapWrap<dtype_t, edge_t> weight(aweight,
edge_properties());
dijkstra_shortest_paths_no_color_map
(g, vertex(s, g), visitor(vis).weight_map(weight).
predecessor_map(pred_map).
distance_map(dist).distance_compare(cmp).
distance_combine(cmb).distance_inf(i).distance_zero(z));
if (vertex(s, g) == graph_traits<Graph>::null_vertex())
{
for (auto u : vertices_range(g))
{
vis.initialize_vertex(u, g);
put(dist, u, i);
put(pred_map, u, u);
}
for (auto u : vertices_range(g))
{
if (dist[u] != i)
continue;
dist[u] = z;
dijkstra_shortest_paths_no_color_map_no_init
(g, u, pred_map, dist, weight, get(vertex_index_t(), g),
cmp, cmb, i, z, vis);
}
}
else
{
dijkstra_shortest_paths_no_color_map
(g, vertex(s, g), visitor(vis).weight_map(weight).
predecessor_map(pred_map).
distance_map(dist).distance_compare(cmp).
distance_combine(cmb).distance_inf(i).distance_zero(z));
}
}
};
......@@ -158,9 +180,31 @@ struct do_djk_search_fast
typedef typename property_traits<DistanceMap>::value_type dtype_t;
dtype_t z = python::extract<dtype_t>(range.first);
dtype_t i = python::extract<dtype_t>(range.second);
dijkstra_shortest_paths_no_color_map
(g, vertex(s, g), visitor(vis).weight_map(weight).
distance_map(dist).distance_inf(i).distance_zero(z));
if (vertex(s, g) == graph_traits<Graph>::null_vertex())
{
for (auto u : vertices_range(g))
{
vis.initialize_vertex(u, g);
put(dist, u, i);
}
for (auto u : vertices_range(g))
{
if (dist[u] != i)
continue;
dist[u] = z;
dijkstra_shortest_paths_no_color_map_no_init
(g, u, dummy_property_map(), dist, weight,
get(vertex_index_t(), g), std::less<dtype_t>(),
boost::closed_plus<dtype_t>(), i, z, vis);
}
}
else
{
dijkstra_shortest_paths_no_color_map
(g, vertex(s, g), visitor(vis).weight_map(weight).
distance_map(dist).distance_inf(i).distance_zero(z));
}
}
};
......
......@@ -3261,6 +3261,7 @@ VertexBase.is_valid = Vertex.is_valid
Vertex = VertexBase
Vertex.__name__ = "Vertex"
_get_null_vertex = libcore.get_null_vertex
# Add convenience function to vector classes
def _get_array_view(self):
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment