def is_forest(G):
 if (len(G) == 0):
  raise nx.exception.NetworkXPointlessConcept('G has no nodes.')
 if G.is_directed():
  components = nx.weakly_connected_component_subgraphs
 else:
  components = nx.connected_component_subgraphs
 return all((((len(c) - 1) == c.number_of_edges()) for c in components(G)))

def is_forest ( G ) : if ( len ( G ) == 0 ) : raise nx . exception . NetworkXPointlessConcept ( 'G has no nodes.' ) if G . is_directed ( ) : components = nx . weakly_connected_component_subgraphs else : components = nx . connected_component_subgraphs return all ( ( ( ( len ( c ) - 1 ) == c . number_of_edges ( ) ) for c in components ( G ) ) )
returns true if g is a forest .