graph_composition

graph_composition#

graph_tool.generation.graph_composition(g1, g2, multigraph=False)[git][source]#

Return the relational composition of two graphs.

Parameters:
g1Graph

First graph.

g2Graph

Second graph, with the same directedness as g1.

multigraphbool (optional, default: False)

If True, an edge is added for every path through an intermediate vertex, otherwise only once for every pair of vertices.

Returns:
gGraph

The composition, with \(\max(V_1, V_2)\) vertices, identified with those of both graphs by the order of their indices.

See also

graph_product

Product of two graphs

graph_power

Power of a graph

graph_tool.structure.transitive_closure

Transitive closure

Notes

The composition contains an edge \((i, j)\) if there is a vertex \(k\) such that \((i, k)\) is an edge of g1 and \((k, j)\) an edge of g2, i.e. it is the composition of the binary relations given by the graphs, whose adjacency matrix has the same nonzero entries as the product \(\boldsymbol A_1\boldsymbol A_2\). For undirected graphs, the edges are taken in both directions, and the result is symmetrized. With multigraph=True, there is an edge for every such pair of edges. The algorithm runs in time \(O(\sum_k k^{(1)}_kk^{(2)}_k)\), where \(k^{(1)}_k\) and \(k^{(2)}_k\) are the (in- and out-)degrees of \(k\) in both graphs.

Examples

>>> g = gt.graph_composition(gt.circular_graph(6), gt.circular_graph(6))
>>> print(g.num_vertices(), g.num_edges())
6 12

The composition of a directed path with itself joins the vertices at distance two:

>>> g = gt.named_graph("path", 4, directed=True)
>>> print(gt.graph_composition(g, g).get_edges())
[[0 2]
 [1 3]]

The composition of a cycle with itself is drawn as follows, with the vertices placed on a circle:

>>> g = gt.graph_composition(gt.circular_graph(8), gt.circular_graph(8))
>>> x = 2 * np.pi * np.arange(g.num_vertices()) / g.num_vertices()
>>> pos = g.new_vp("vector<double>",
...                vals=np.array([np.cos(x), np.sin(x)]).T)
>>> gt.graph_draw(g, pos=pos,
...               output="graph_composition-cycle.pdf")
<...>
../_images/graph_composition-cycle.png

Composition of the cycle \(C_8\) with itself, with the vertices placed on a circle in the order of their indices. Every vertex has a self-loop, from the paths that return to it.#