The documentation says that isolated vertices in graph can be obtained using networkx.isolates(*G*)
. It adds that the isolated vertices can be removed from a graph G using the code *G*.remove_nodes_from(nx.isolates(*G*))
.
But I get the run time error "dictionary changed size during iteration" when I run the code.
Error report:-
>>> G.remove_nodes_from(nx.isolates(G)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/classes/graph.py", line 617, in remove_nodes_from for n in nodes: File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/algorithms/isolate.py", line 94, in <genexpr> return (n for n, d in G.degree() if d == 0) File "/home/iiitdm/anaconda2/lib/python2.7/site-packages/networkx/classes/reportviews.py", line 443, in __iter__ for n in self._nodes: RuntimeError: dictionary changed size during iteration
It is understandable and was expected because (I think) the generator object created using the function isolates()
changes with G and hence changing graph G while it is being 'iterated' should give a similar error. Then that line in the documentation must be wrong, isn't it? Am I completely off the mark? I am pretty new to python.
By the way, the object returned by networkx.isolates()
is a generator object.