Commit eca90cc4 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix bug in graph_union() when an intersection is given

parent 01ddad4f
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
using namespace graph_tool; using namespace graph_tool;
using namespace boost; using namespace boost;
typedef property_map_type::apply<int32_t, typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type GraphInterface::vertex_index_map_t>::type
vprop_t; vprop_t;
......
...@@ -34,29 +34,25 @@ struct graph_union ...@@ -34,29 +34,25 @@ struct graph_union
const const
{ {
Graph& g = *gp; Graph& g = *gp;
typename graph_traits<Graph>::vertex_iterator v, v_end; for (auto v : vertices_range(g))
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
{ {
if (vmap[*v] == 0) if (vmap[v] < 0)
{ {
vmap[*v] = add_vertex(ug); vmap[v] = add_vertex(ug);
} }
else else
{ {
typename graph_traits<UnionGraph>::vertex_descriptor w = auto w = vertex(vmap[v], ug);
vertex(vmap[*v] - 1, ug); if (w == graph_traits<UnionGraph>::null_vertex() || w >= num_vertices(ug))
if (w == graph_traits<UnionGraph>::null_vertex() || vmap[v] = add_vertex(ug);
w >= num_vertices(g))
vmap[*v] = add_vertex(ug);
else else
vmap[*v] = w; vmap[v] = w;
} }
} }
typename graph_traits<Graph>::edge_iterator e, e_end; for (auto e : edges_range(g))
for (tie(e,e_end) = edges(g); e != e_end; ++e) emap[e] = add_edge(vertex(vmap[source(e, g)], ug),
emap[*e] = add_edge(vertex(vmap[source(*e,g)], g), vertex(vmap[target(e, g)], ug), ug).first;
vertex(vmap[target(*e,g)], g), ug).first;
} }
}; };
...@@ -69,8 +65,7 @@ struct property_union ...@@ -69,8 +65,7 @@ struct property_union
UnionProp uprop, boost::any aprop) const UnionProp uprop, boost::any aprop) const
{ {
Graph& g = *gp; Graph& g = *gp;
typename UnionProp::checked_t prop = auto prop = any_cast<typename UnionProp::checked_t>(aprop);
any_cast<typename UnionProp::checked_t>(aprop);
dispatch(ug, g, vmap, emap, uprop, prop, dispatch(ug, g, vmap, emap, uprop, prop,
std::is_same<typename property_traits<UnionProp>::key_type, std::is_same<typename property_traits<UnionProp>::key_type,
typename graph_traits<Graph>::vertex_descriptor>()); typename graph_traits<Graph>::vertex_descriptor>());
...@@ -78,12 +73,11 @@ struct property_union ...@@ -78,12 +73,11 @@ struct property_union
template <class UnionGraph, class Graph, class VertexMap, class EdgeMap, template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
class UnionProp, class Prop> class UnionProp, class Prop>
void dispatch(UnionGraph&, Graph& g, VertexMap vmap, EdgeMap, void dispatch(UnionGraph& ug, Graph& g, VertexMap vmap, EdgeMap,
UnionProp uprop, Prop prop, std::true_type) const UnionProp uprop, Prop prop, std::true_type) const
{ {
typename graph_traits<Graph>::vertex_iterator v, v_end; for (auto v : vertices_range(g))
for (tie(v,v_end) = vertices(g); v != v_end; ++v) uprop[vertex(vmap[v], ug)] = prop[v];
uprop[vertex(vmap[*v], g)] = prop[*v];
} }
template <class UnionGraph, class Graph, class VertexMap, class EdgeMap, template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
...@@ -91,9 +85,8 @@ struct property_union ...@@ -91,9 +85,8 @@ struct property_union
void dispatch(UnionGraph&, Graph& g, VertexMap, EdgeMap emap, void dispatch(UnionGraph&, Graph& g, VertexMap, EdgeMap emap,
UnionProp uprop, Prop prop, std::false_type) const UnionProp uprop, Prop prop, std::false_type) const
{ {
typename graph_traits<Graph>::edge_iterator e, e_end; for (auto e : edges_range(g))
for (tie(e,e_end) = edges(g); e != e_end; ++e) uprop[emap[e]] = prop[e];
uprop[emap[*e]] = prop[*e];
} }
}; };
......
...@@ -29,7 +29,7 @@ using namespace graph_tool; ...@@ -29,7 +29,7 @@ using namespace graph_tool;
using namespace boost; using namespace boost;
typedef property_map_type::apply<int32_t, typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type GraphInterface::vertex_index_map_t>::type
vprop_t; vprop_t;
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
using namespace graph_tool; using namespace graph_tool;
using namespace boost; using namespace boost;
typedef property_map_type::apply<int32_t, typedef property_map_type::apply<int64_t,
GraphInterface::vertex_index_map_t>::type GraphInterface::vertex_index_map_t>::type
vprop_t; vprop_t;
......
...@@ -897,8 +897,9 @@ def line_graph(g): ...@@ -897,8 +897,9 @@ def line_graph(g):
def graph_union(g1, g2, intersection=None, props=None, include=False, def graph_union(g1, g2, intersection=None, props=None, include=False,
internal_props=False): internal_props=False):
"""Return the union of graphs g1 and g2, composed of all edges and vertices """Return the union of graphs ``g1`` and ``g2``, composed of all edges and
of g1 and g2, without overlap. vertices of ``g1`` and ``g2``, without overlap (if ``intersection ==
None``).
Parameters Parameters
---------- ----------
...@@ -907,8 +908,8 @@ def graph_union(g1, g2, intersection=None, props=None, include=False, ...@@ -907,8 +908,8 @@ def graph_union(g1, g2, intersection=None, props=None, include=False,
g2 : :class:`~graph_tool.Graph` g2 : :class:`~graph_tool.Graph`
Second graph in the union. Second graph in the union.
intersection : :class:`~graph_tool.PropertyMap` (optional, default: ``None``) intersection : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
Vertex property map owned by `g1` which maps each of its vertices Vertex property map owned by `g2` which maps each of its vertices
to vertex indexes belonging to `g2`. Negative values mean no mapping to vertex indexes belonging to `g1`. Negative values mean no mapping
exists, and thus both vertices in `g1` and `g2` will be present in the exists, and thus both vertices in `g1` and `g2` will be present in the
union graph. union graph.
props : list of tuples of :class:`~graph_tool.PropertyMap` (optional, default: ``None``) props : list of tuples of :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
...@@ -1034,12 +1035,9 @@ def graph_union(g1, g2, intersection=None, props=None, include=False, ...@@ -1034,12 +1035,9 @@ def graph_union(g1, g2, intersection=None, props=None, include=False,
vmask_flipped = True vmask_flipped = True
if intersection is None: if intersection is None:
intersection = g1.new_vertex_property("int32_t") intersection = g2.new_vertex_property("int64_t", -1)
intersection.a = 0
else: else:
intersection = intersection.copy("int32_t") intersection = intersection.copy("int64_t")
intersection.a[intersection.a >= 0] += 1
intersection.a[intersection.a < 0] = 0
u1 = GraphView(g1, directed=True, skip_properties=True) u1 = GraphView(g1, directed=True, skip_properties=True)
u2 = GraphView(g2, directed=True, skip_properties=True) u2 = GraphView(g2, directed=True, skip_properties=True)
...@@ -1642,4 +1640,4 @@ class DynamicSampler(libgraph_tool_generation.DynamicSampler): ...@@ -1642,4 +1640,4 @@ class DynamicSampler(libgraph_tool_generation.DynamicSampler):
libgraph_tool_generation.DynamicSampler.__init__(self, values, probs) libgraph_tool_generation.DynamicSampler.__init__(self, values, probs)
def sample(self): def sample(self):
return libgraph_tool_generation.DynamicSampler.sample(self, _get_rng()) return libgraph_tool_generation.DynamicSampler.sample(self, _get_rng())
\ No newline at end of file
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