"""Unary operations on graphs"""
# Copyright (C) 2004-2019 by
# Aric Hagberg <email@example.com>
# Dan Schult <firstname.lastname@example.org>
# Pieter Swart <email@example.com>
# All rights reserved.
# BSD license.
import networkx as nx
from networkx.utils import not_implemented_for
__author__ = """\n""".join(['Aric Hagberg <firstname.lastname@example.org>',
'Pieter Swart (email@example.com)',
__all__ = ['complement', 'reverse']
"""Returns the graph complement of G.
G : graph
A NetworkX graph
GC : A new graph.
Note that complement() does not create self-loops and also
does not produce parallel edges for MultiGraphs.
Graph, node, and edge data are not propagated to the new graph.
R = G.__class__()
for n, nbrs in G.adjacency()
for n2 in G if n2 not in nbrs
if n != n2))
[docs]def reverse(G, copy=True):
"""Returns the reverse directed graph of G.
G : directed graph
A NetworkX directed graph
copy : bool
If True, then a new graph is returned. If False, then the graph is
reversed in place.
H : directed graph
The reversed G.
if not G.is_directed():
raise nx.NetworkXError("Cannot reverse an undirected graph.")