ioftools / networkxMiCe / networkxmaster / networkx / algorithms / operators / unary.py @ 5cef0f13
History  View  Annotate  Download (1.61 KB)
1 
"""Unary operations on graphs"""


2 
# Copyright (C) 20042019 by

3 
# Aric Hagberg <hagberg@lanl.gov>

4 
# Dan Schult <dschult@colgate.edu>

5 
# Pieter Swart <swart@lanl.gov>

6 
# All rights reserved.

7 
# BSD license.

8 
import networkx as nx 
9 
from networkx.utils import not_implemented_for 
10 
__author__ = """\n""".join(['Aric Hagberg <aric.hagberg@gmail.com>', 
11 
'Pieter Swart (swart@lanl.gov)',

12 
'Dan Schult(dschult@colgate.edu)'])

13 
__all__ = ['complement', 'reverse'] 
14  
15  
16 
def complement(G): 
17 
"""Returns the graph complement of G.

18 

19 
Parameters

20 


21 
G : graph

22 
A NetworkX graph

23 

24 
Returns

25 


26 
GC : A new graph.

27 

28 
Notes

29 


30 
Note that complement() does not create selfloops and also

31 
does not produce parallel edges for MultiGraphs.

32 

33 
Graph, node, and edge data are not propagated to the new graph.

34 
"""

35 
R = G.__class__() 
36 
R.add_nodes_from(G) 
37 
R.add_edges_from(((n, n2) 
38 
for n, nbrs in G.adjacency() 
39 
for n2 in G if n2 not in nbrs 
40 
if n != n2))

41 
return R

42  
43  
44 
def reverse(G, copy=True): 
45 
"""Returns the reverse directed graph of G.

46 

47 
Parameters

48 


49 
G : directed graph

50 
A NetworkX directed graph

51 
copy : bool

52 
If True, then a new graph is returned. If False, then the graph is

53 
reversed in place.

54 

55 
Returns

56 


57 
H : directed graph

58 
The reversed G.

59 

60 
"""

61 
if not G.is_directed(): 
62 
raise nx.NetworkXError("Cannot reverse an undirected graph.") 
63 
else:

64 
return G.reverse(copy=copy)
