Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/operators/unary.py: 46%

13 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1"""Unary operations on graphs""" 

2import networkx as nx 

3 

4__all__ = ["complement", "reverse"] 

5 

6 

7@nx._dispatch 

8def complement(G): 

9 """Returns the graph complement of G. 

10 

11 Parameters 

12 ---------- 

13 G : graph 

14 A NetworkX graph 

15 

16 Returns 

17 ------- 

18 GC : A new graph. 

19 

20 Notes 

21 ----- 

22 Note that `complement` does not create self-loops and also 

23 does not produce parallel edges for MultiGraphs. 

24 

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

26 

27 Examples 

28 -------- 

29 >>> G = nx.Graph([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5)]) 

30 >>> G_complement = nx.complement(G) 

31 >>> G_complement.edges() # This shows the edges of the complemented graph 

32 EdgeView([(1, 4), (1, 5), (2, 4), (2, 5), (4, 5)]) 

33 

34 """ 

35 R = G.__class__() 

36 R.add_nodes_from(G) 

37 R.add_edges_from( 

38 ((n, n2) for n, nbrs in G.adjacency() for n2 in G if n2 not in nbrs if n != n2) 

39 ) 

40 return R 

41 

42 

43@nx._dispatch 

44def 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 Raises 

61 ------ 

62 NetworkXError 

63 If graph is undirected. 

64 

65 Examples 

66 -------- 

67 >>> G = nx.DiGraph([(1, 2), (1, 3), (2, 3), (3, 4), (3, 5)]) 

68 >>> G_reversed = nx.reverse(G) 

69 >>> G_reversed.edges() 

70 OutEdgeView([(2, 1), (3, 1), (3, 2), (4, 3), (5, 3)]) 

71 

72 """ 

73 if not G.is_directed(): 

74 raise nx.NetworkXError("Cannot reverse an undirected graph.") 

75 else: 

76 return G.reverse(copy=copy)