component ========== >>> import networkx as NX >>> from networkx.operators import convert_node_labels_to_integers as cnlti >>> G=cnlti(NX.grid_2d_graph(4,4),first_label=1,ordering="sorted") .. image:: paths_G.png >>> H=NX.cycle_graph(7) >>> DH=NX.cycle_graph(7,create_using=NX.DiGraph()) Connected components -------------------- >>> G1=cnlti(NX.grid_2d_graph(2,2),first_label=0,ordering="sorted") >>> G2=cnlti(NX.lollipop_graph(3,3),first_label=4,ordering="sorted") >>> G3=cnlti(NX.house_graph(),first_label=10,ordering="sorted") >>> DG=NX.DiGraph() >>> DG.add_edges_from([(1,2),(1,3),(2,3)]) >>> G=NX.union(G1,G2) >>> G=NX.union(G,G3) >>> sorted(NX.connected_components(G)) [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]] >>> NX.number_connected_components(G) 3 >>> G=cnlti(NX.grid_2d_graph(4,4),first_label=1) .. image:: paths_G.png >>> NX.number_connected_components(G) 1 >>> sorted(NX.connected_components(G)[0]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] >>> sorted(NX.node_connected_component(G,1)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] >>> H=NX.connected_component_subgraphs(G)[0] >>> sorted(H.nodes()) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] >>> NX.is_connected(G) True >>> G.add_edge('A','B') >>> NX.is_connected(G) False >>> NX.connected_components(DG) Traceback (most recent call last): ... NetworkXError: Not allowed for directed graph G. Use UG=G.to_undirected() to create an undirected graph. >>> NX.number_connected_components(DG) Traceback (most recent call last): ... NetworkXError: Not allowed for directed graph G. Use UG=G.to_undirected() to create an undirected graph. >>> NX.connected_component_subgraphs(DG) Traceback (most recent call last): ... NetworkXError: Not allowed for directed graph G. Use UG=G.to_undirected() to create an undirected graph. >>> NX.node_connected_component(DG,1) Traceback (most recent call last): ... NetworkXError: Not allowed for directed graph G. Use UG=G.to_undirected() to create an undirected graph. >>> NX.is_connected(DG) Traceback (most recent call last): ... NetworkXError: Not allowed for directed graph G. Use UG=G.to_undirected() to create an undirected graph.