complement_graph

complement_graph#

graph_tool.generation.complement_graph(g, self_loops=False)[git][source]#

Return the complement of a graph.

Parameters:
gGraph

Graph to be used.

self_loopsbool (optional, default: False)

If True, every vertex without a self-loop gets one.

Returns:
uGraph

The graph on the same vertices (in the order of their indices) with an edge exactly between the pairs that are not adjacent in g.

See also

graph_join

Join of two graphs

graph_difference

Difference of two graphs

complete_graph

Complete graph

Notes

The complement of \(G=(V,E)\) is the graph with the same vertices and the edge set

\[\bar E = \{(u, v) : u, v \in V,\ u \neq v,\ (u, v)\notin E\},\]

with ordered pairs for directed graphs and unordered ones for undirected graphs, so that the complement has no parallel edges and parallel edges of g count as a single adjacency. With self_loops=True, the condition \(u \neq v\) is dropped, so that every vertex without a self-loop in g receives one. The algorithm runs in time \(O(V^2)\).

Examples

>>> g = gt.circular_graph(5)
>>> print(gt.isomorphism(g, gt.complement_graph(g)))
True

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

>>> g = gt.complement_graph(gt.circular_graph(10))
>>> 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="complement_graph-cycle.pdf")
<...>
../_images/complement_graph-cycle.png

Complement of the cycle \(C_{10}\), with the vertices placed on a circle in the order of their indices.#