# encoding: utf-8 """ Functions for identifying isolate (degree zero) nodes. """ # Copyright (C) 2004-2010 by # Aric Hagberg # Dan Schult # Pieter Swart # All rights reserved. # BSD license. __author__ = """\n""".join(['Drew Conway ', 'Aric Hagberg ']) __all__=['is_isolate','isolates'] import networkx as nx def is_isolate(G,n): """Determine of node n is an isolate (degree zero). Parameters ---------- G : graph A networkx graph n : node A node in G Returns ------- isolate : bool True if n has no neighbors, False otherwise. Examples -------- >>> G=nx.Graph() >>> G.add_edge(1,2) >>> G.add_node(3) >>> nx.is_isolate(G,2) False >>> nx.is_isolate(G,3) True """ return len(G[n])==0 def isolates(G): """Return list of isolates in the graph. Isolates are nodes with no neighbors (degree zero). Parameters ---------- G : graph A networkx graph Returns ------- isolates : list List of isolate nodes. Examples -------- >>> G=nx.Graph() >>> G.add_edge(1,2) >>> G.add_node(3) >>> nx.isolates(G) [3] To remove all isolates in the graph use >>> G.remove_nodes_from(nx.isolates(G)) >>> G.nodes() [1, 2] """ return [n for n in G if len(G[n])==0]