EhsanBitaraf / triple-a

Article Analysis Assistant
Apache License 2.0
16 stars 2 forks source link

Number of connected components #13

Open mjafarpour87 opened 1 year ago

mjafarpour87 commented 1 year ago

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.