comparison env/lib/python3.9/site-packages/networkx/algorithms/isolate.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 Functions for identifying isolate (degree zero) nodes.
3 """
4
5 __all__ = ["is_isolate", "isolates", "number_of_isolates"]
6
7
8 def is_isolate(G, n):
9 """Determines whether a node is an isolate.
10
11 An *isolate* is a node with no neighbors (that is, with degree
12 zero). For directed graphs, this means no in-neighbors and no
13 out-neighbors.
14
15 Parameters
16 ----------
17 G : NetworkX graph
18
19 n : node
20 A node in `G`.
21
22 Returns
23 -------
24 is_isolate : bool
25 True if and only if `n` has no neighbors.
26
27 Examples
28 --------
29 >>> G = nx.Graph()
30 >>> G.add_edge(1, 2)
31 >>> G.add_node(3)
32 >>> nx.is_isolate(G, 2)
33 False
34 >>> nx.is_isolate(G, 3)
35 True
36 """
37 return G.degree(n) == 0
38
39
40 def isolates(G):
41 """Iterator over isolates in the graph.
42
43 An *isolate* is a node with no neighbors (that is, with degree
44 zero). For directed graphs, this means no in-neighbors and no
45 out-neighbors.
46
47 Parameters
48 ----------
49 G : NetworkX graph
50
51 Returns
52 -------
53 iterator
54 An iterator over the isolates of `G`.
55
56 Examples
57 --------
58 To get a list of all isolates of a graph, use the :class:`list`
59 constructor::
60
61 >>> G = nx.Graph()
62 >>> G.add_edge(1, 2)
63 >>> G.add_node(3)
64 >>> list(nx.isolates(G))
65 [3]
66
67 To remove all isolates in the graph, first create a list of the
68 isolates, then use :meth:`Graph.remove_nodes_from`::
69
70 >>> G.remove_nodes_from(list(nx.isolates(G)))
71 >>> list(G)
72 [1, 2]
73
74 For digraphs, isolates have zero in-degree and zero out_degre::
75
76 >>> G = nx.DiGraph([(0, 1), (1, 2)])
77 >>> G.add_node(3)
78 >>> list(nx.isolates(G))
79 [3]
80
81 """
82 return (n for n, d in G.degree() if d == 0)
83
84
85 def number_of_isolates(G):
86 """Returns the number of isolates in the graph.
87
88 An *isolate* is a node with no neighbors (that is, with degree
89 zero). For directed graphs, this means no in-neighbors and no
90 out-neighbors.
91
92 Parameters
93 ----------
94 G : NetworkX graph
95
96 Returns
97 -------
98 int
99 The number of degree zero nodes in the graph `G`.
100
101 """
102 # TODO This can be parallelized.
103 return sum(1 for v in isolates(G))