### annotate env/lib/python3.9/site-packages/networkx/algorithms/operators/unary.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
shellac
parents:
diff changeset
1 """Unary operations on graphs"""
shellac
parents:
diff changeset
2 import networkx as nx
shellac
parents:
diff changeset
3
shellac
parents:
diff changeset
4 __all__ = ["complement", "reverse"]
shellac
parents:
diff changeset
5
shellac
parents:
diff changeset
6
shellac
parents:
diff changeset
7 def complement(G):
shellac
parents:
diff changeset
8 """Returns the graph complement of G.
shellac
parents:
diff changeset
9
shellac
parents:
diff changeset
10 Parameters
shellac
parents:
diff changeset
11 ----------
shellac
parents:
diff changeset
12 G : graph
shellac
parents:
diff changeset
13 A NetworkX graph
shellac
parents:
diff changeset
14
shellac
parents:
diff changeset
15 Returns
shellac
parents:
diff changeset
16 -------
shellac
parents:
diff changeset
17 GC : A new graph.
shellac
parents:
diff changeset
18
shellac
parents:
diff changeset
19 Notes
shellac
parents:
diff changeset
20 ------
shellac
parents:
diff changeset
21 Note that complement() does not create self-loops and also
shellac
parents:
diff changeset
22 does not produce parallel edges for MultiGraphs.
shellac
parents:
diff changeset
23
shellac
parents:
diff changeset
24 Graph, node, and edge data are not propagated to the new graph.
shellac
parents:
diff changeset
25 """
shellac
parents:
diff changeset
26 R = G.__class__()
shellac
parents:
diff changeset
shellac
parents:
diff changeset
shellac
parents:
diff changeset
29 ((n, n2) for n, nbrs in G.adjacency() for n2 in G if n2 not in nbrs if n != n2)
shellac
parents:
diff changeset
30 )
shellac
parents:
diff changeset
31 return R
shellac
parents:
diff changeset
32
shellac
parents:
diff changeset
33
shellac
parents:
diff changeset
34 def reverse(G, copy=True):
shellac
parents:
diff changeset
35 """Returns the reverse directed graph of G.
shellac
parents:
diff changeset
36
shellac
parents:
diff changeset
37 Parameters
shellac
parents:
diff changeset
38 ----------
shellac
parents:
diff changeset
39 G : directed graph
shellac
parents:
diff changeset
40 A NetworkX directed graph
shellac
parents:
diff changeset
41 copy : bool
shellac
parents:
diff changeset
42 If True, then a new graph is returned. If False, then the graph is
shellac
parents:
diff changeset
43 reversed in place.
shellac
parents:
diff changeset
44
shellac
parents:
diff changeset
45 Returns
shellac
parents:
diff changeset
46 -------
shellac
parents:
diff changeset
47 H : directed graph
shellac
parents:
diff changeset
48 The reversed G.
shellac
parents:
diff changeset
49
shellac
parents:
diff changeset
50 """
shellac
parents:
diff changeset
51 if not G.is_directed():
shellac
parents:
diff changeset
52 raise nx.NetworkXError("Cannot reverse an undirected graph.")