graph_union

Contents

graph_union#

graph_tool.generation.graph_union(g1, g2, eweight1=None, eweight2=None, vmap='identity', props=[], mt='set', internal_props=False, in_place=False, multiset=False, **kwargs)[git][source]#

Return a graph containing the union of the vertices and edges of graphs g1 and g2.

Parameters:
g1Graph

Graph to be merged into.

g2Graph

Graph to be merged from.

eweight1EdgePropertyMap (optional, default: None)

If provided, this will determine the weights of the edges of graph g1. If not provided, each edge will be assumed to have unity weight.

eweight2EdgePropertyMap (optional, default: None)

If provided, this will determine the weights of the edges of graph g2. If not provided, each edge will be assumed to have unity weight.

vmapVertexPropertyMap, "identity", or None (optional, default: "identity")

Vertex property map owned by g2 which maps each of its vertices to vertex indices belonging to g1, in which case they are considered to be identical. Negative values mean no mapping exists, and thus both vertices in g1 and g2 will be present in the union graph. If this argument is not provided (i.e. vmap is None), both vertex sets will be added to the union. If vmap is g2.vertex_index, and the vertex indexes are identical in both graphs, the union will correspond only to the edge sets. A special value of vmap == "identity" is a shortcut for vmap = g2.vertex_index. Vertices of g2 mapped to the same vertex of g1 are merged into it, and their edges with the same endpoints into a single edge. A vertex filtered out of g1 is treated as absent: the vertices mapped to it are merged into a new vertex. An index \(i\) beyond the range of the vertex indices of g1 (including the filtered vertices) refers to a new vertex, with new vertices added for every index up to \(i\).

propslist of tuples of PropertyMap (optional, default: [])

Property maps to be included in the union. Each element of the list must be a pair of PropertyMap objects. The first element must be a property of g1, and the second of g2. If either value is None, a new map will be created, and set to appropriately empty values.

mtstr (optional, default: "set")

Merge rule for property values. Should be one of "set", "sum", "diff", "idx_inc", "append", "concat". See graph_merge() for further documentation.

internal_propsbool (optional, default: False)

If True all internal property maps of g1 and g2 will be included in the union.

in_placebool (optional, default: False)

If True, graph g2 is inserted in-place into g1, which is then modified. If False, a new graph is created, and both graphs remain unmodified.

multisetbool (optional, default: False)

If True, the edges in the union graph will form a multiset, so that a single edge that exists in both g1 and g2 becomes a parallel edge in the union graph.

**kwargsdict (optional, default: {})

Remaining keyword parameters will be forwarded to graph_merge().

Returns:
ugGraph

The union graph.

wEdgePropertyMap

The edge weights of the union graph. This is only returned if either eweight1 or eweight2 is not None.

propsdictionary with lists of PropertyMap objects

Dictionary with key mt mapping to the list of properties in the merged graph. This is only returned if props is not empty. Internal property maps are not included in this list.

See also

graph_merge

Merge the edge sets between two graphs.

graph_difference

Difference of the edge sets between two graphs.

graph_sym_difference

Symmetric difference of the edge sets between two graphs.

graph_intersection

Intersection of the edge sets between two graphs.

condensation_graph

Obtain the condensation graph.

Notes

This algorithm is a wrapper around graph_merge(), and shares with it the same algorithmic complexity. If multiset is False, every pair of vertices adjacent in either graph is joined by a single edge in the union, with the sum of the weights of the parallel edges of both graphs, so that the union does not depend on the order of the graphs. This needs a pass over the edges of g1, also if in_place is True.

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

Examples

>>> g = gt.collection.ns["lesmis"]
>>> u1 = gt.GraphView(g, efilt=np.random.random(g.num_edges()) < .5)
>>> u2 = gt.graph_difference(g, u1)
>>> u = gt.graph_union(u1, u2)
>>> gt.similarity(g, u)
1.0
>>> gt.graph_draw(u1, pos=g.vp._pos, output="gsub.pdf")
<...>
>>> gt.graph_draw(u2, pos=g.vp._pos, output="gdiff.pdf")
<...>
>>> gt.graph_draw(u, pos=g.vp._pos, output="gunion.pdf")
<...>
../_images/gsub.png ../_images/gdiff.png ../_images/gunion.png