def node_connected_component(G, n): """Return the nodes in the component of graph containing node n. Parameters ---------- G : NetworkX Graph An undirected graph. n : node label A node in G Returns ------- comp : lists A list of nodes in component of G containing node n. See Also -------- connected_components Notes ----- For undirected graphs only. """ return list(sp_length(G, n))
def is_connected(G): """Return True if the graph is connected, false otherwise. Parameters ---------- G : NetworkX Graph An undirected graph. Returns ------- connected : bool True if the graph is connected, false otherwise. Examples -------- >>> G = nx.path_graph(4) >>> print(nx.is_connected(G)) True See Also -------- connected_components Notes ----- For undirected graphs only. """ if len(G) == 0: raise nx.NetworkXPointlessConcept('Connectivity is undefined ', 'for the null graph.') return len(sp_length(G, next(G.nodes_iter()))) == len(G)
def connected_components(G): """Generate connected components. Parameters ---------- G : NetworkX graph An undirected graph Returns ------- comp : generator of lists A list of nodes for each component of G. Examples -------- Generate a sorted list of connected components, largest first. >>> G = nx.path_graph(4) >>> G.add_path([10, 11, 12]) >>> sorted(nx.connected_components(G), key = len, reverse=True) [[0, 1, 2, 3], [10, 11, 12]] See Also -------- strongly_connected_components Notes ----- For undirected graphs only. """ seen={} for v in G: if v not in seen: c = sp_length(G, v) yield list(c) seen.update(c)