python - Check if an undirected graph is a tree in networkx -


i know if there simple way check whether undirected graph in networkx tree or not

the fastest way graph g(v,e) might check if |v| = |e| + 1 , g connected:

import networkx nx def is_tree(g):     if nx.number_of_nodes(g) != nx.number_of_edges(g) + 1:         return false     return nx.is_connected(g)  if __name__ == '__main__':      print(is_tree(nx.path_graph(5)))     print(is_tree(nx.star_graph(5)))     print(is_tree(nx.house_graph())) 

Comments