Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of connected components #13

Open
mjafarpour87 opened this issue Mar 18, 2023 · 0 comments
Open

Number of connected components #13

mjafarpour87 opened this issue Mar 18, 2023 · 0 comments

Comments

@mjafarpour87
Copy link
Collaborator

Networkx does not compute the connected components for directed graphs. The connected component problem with (multi)digraphs can be solved by
(1) creating first an undirected graph,
(2) getting the components, then
(3) creating digraphs from the edgelist of the undirected components.

Directed graph from the largest connected component (python):

ug = nx.Graph()
ug.add_edges_from(edgetable) # undirected global graph; edgetable is just a list of A->B, A->C, C->D etc.
# largest connected component
 if nx.number_connected_components(ug)>1:
    lcug = nx.connected_component_subgraphs(ug)[0]
else:
    lcug = ug
del ug
           
dg = nx.DiGraph()
dg.add_edges_from(lcug.edges()) # directed global graph from the largest connected component

IMPORTANT NOTE: G.to_directed() creates edges A->B, B->A from A-B! Don't use it if you want the undirected and directed components to be similar (only difference is the directedness).
G.subgraph() operations could work, but are slower compared to the above solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant