1 # encoding: utf-8 2 """ 3 Functions for identifying isolate (degree zero) nodes. 4 """ 5 # Copyright (C) 2004-2011 by 6 # Aric Hagberg <hagberg (at] lanl.gov> 7 # Dan Schult <dschult (at] colgate.edu> 8 # Pieter Swart <swart (at] lanl.gov> 9 # All rights reserved. 10 # BSD license. 11 import networkx as nx 12 __author__ = """\n""".join(['Drew Conway <drew.conway (at] nyu.edu>', 13 'Aric Hagberg <hagberg (at] lanl.gov>']) 14 __all__=['is_isolate','isolates'] 15 16 def is_isolate(G,n): 17 """Determine of node n is an isolate (degree zero). 18 19 Parameters 20 ---------- 21 G : graph 22 A networkx graph 23 n : node 24 A node in G 25 26 Returns 27 ------- 28 isolate : bool 29 True if n has no neighbors, False otherwise. 30 31 Examples 32 -------- 33 >>> G=nx.Graph() 34 >>> G.add_edge(1,2) 35 >>> G.add_node(3) 36 >>> nx.is_isolate(G,2) 37 False 38 >>> nx.is_isolate(G,3) 39 True 40 """ 41 return G.degree(n)==0 42 43 def isolates(G): 44 """Return list of isolates in the graph. 45 46 Isolates are nodes with no neighbors (degree zero). 47 48 Parameters 49 ---------- 50 G : graph 51 A networkx graph 52 53 Returns 54 ------- 55 isolates : list 56 List of isolate nodes. 57 58 Examples 59 -------- 60 >>> G = nx.Graph() 61 >>> G.add_edge(1,2) 62 >>> G.add_node(3) 63 >>> nx.isolates(G) 64 [3] 65 66 To remove all isolates in the graph use 67 >>> G.remove_nodes_from(nx.isolates(G)) 68 >>> G.nodes() 69 [1, 2] 70 71 For digraphs isolates have zero in-degree and zero out_degre 72 >>> G = nx.DiGraph([(0,1),(1,2)]) 73 >>> G.add_node(3) 74 >>> nx.isolates(G) 75 [3] 76 """ 77 return [n for (n,d) in G.degree_iter() if d==0] 78