Iterator over isolates in the graph. An *isolate* is a node with no neighbors (that is, with degree zero). For directed graphs, this means no in-neighbors and no out-neighbors. Parameters ---------- G : NetworkX graph Returns ------- iterator An iterato
(G)
| 42 | |
| 43 | @nx._dispatchable |
| 44 | def isolates(G): |
| 45 | """Iterator over isolates in the graph. |
| 46 | |
| 47 | An *isolate* is a node with no neighbors (that is, with degree |
| 48 | zero). For directed graphs, this means no in-neighbors and no |
| 49 | out-neighbors. |
| 50 | |
| 51 | Parameters |
| 52 | ---------- |
| 53 | G : NetworkX graph |
| 54 | |
| 55 | Returns |
| 56 | ------- |
| 57 | iterator |
| 58 | An iterator over the isolates of `G`. |
| 59 | |
| 60 | Examples |
| 61 | -------- |
| 62 | To get a list of all isolates of a graph, use the :class:`list` |
| 63 | constructor: |
| 64 | |
| 65 | >>> G = nx.Graph() |
| 66 | >>> G.add_edge(1, 2) |
| 67 | >>> G.add_node(3) |
| 68 | >>> list(nx.isolates(G)) |
| 69 | [3] |
| 70 | |
| 71 | To remove all isolates in the graph, first create a list of the |
| 72 | isolates, then use :meth:`Graph.remove_nodes_from`: |
| 73 | |
| 74 | >>> G.remove_nodes_from(list(nx.isolates(G))) |
| 75 | >>> list(G) |
| 76 | [1, 2] |
| 77 | |
| 78 | For digraphs, isolates have zero in-degree and zero out_degree: |
| 79 | |
| 80 | >>> G = nx.DiGraph([(0, 1), (1, 2)]) |
| 81 | >>> G.add_node(3) |
| 82 | >>> list(nx.isolates(G)) |
| 83 | [3] |
| 84 | |
| 85 | """ |
| 86 | return (n for n, d in G.degree() if d == 0) |
| 87 | |
| 88 | |
| 89 | @nx._dispatchable |
no test coverage detected
searching dependent graphs…