I have been playing around with cugraph and nx_cugraph in python, but I am struggling to calculate the number of connected components from the graph. I have been getting a lot of errors.
To calculate the number of connected components require the nx.Graph, but I am not sure what would be the best approach to calculate this. Do I transform the cugraph back to nx.Graph? Is there a better approach so I can leverage the GPU?
G = cugraph.Graph()
G.from_pandas_edgelist(df,
source='source',
destination='target',
edge_attr='weight',
renumber=True)
a = nx.number_connected_components(G)
Thanks!
I am not sure if there is a function to get the number of connected components, but there is definitely one to get the actual components, see here.
df = cugraph.connected_components(G)
It returns a data frame with a column for vertices and a column of the component identifier, so the number of unique values in the second column will give you the number of connected components.
df['labels'].nunique()
The same function works for both cugraphs and network graphs, the difference is in the return type. In case of networkx graphs, it returns a dictionary of vertex: component_label pairs.
1