0

I'm trying to count the number of connected components (=isolated networks) in the whole network graph, but it takes much more memory and time than I expected.
I referenced the python code from https://frhyme.github.io/python-libs/nx_algorithm_clique/ and checked the similar question in the stackoverflow (Find and count the number of isolated and semi-isolated nodes in a network)

import networkx as nx
from networkx.algorithms.approximation import clique
import time

import matplotlib.pyplot as plt

N = 50
p = 0.05
G1 = networkx.fast_gnp_random_graph(N, p, seed=32)

print("== nx.enumerate_all_cliques(G1)")
clique_num = 0
for clique in networkx.enumerate_all_cliques(G1):
    # clique type: list
    clique_num+=1
print(f"all cliques count: {clique_num}")
print("=="*30)

The network contains about 9,800 nodes and 36,000 edges wtih high sparsity (density=0.1).
I'm sorry for lack of example network, but can anyone suggest other way for counting cliques as below?

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75
Ssong
  • 184
  • 1
  • 10
  • 2
    clique is not the same as isolated network, which one are you interested in? – matszwecja Mar 29 '22 at 14:54
  • Also, you should provide a minimal reproducible example. Either a small sample that you design manually, or code to automate the generation of a larger example if needed. – mozway Mar 29 '22 at 14:58
  • @matszwecja I confused the concept of them. Please check the picture that I attached.. – Ssong Mar 29 '22 at 15:04
  • @mozway I added the reproducible example referenced from https://frhyme.github.io/python-libs/nx_clique/ – Ssong Mar 29 '22 at 15:05
  • 1
    Yes, those are definitely not cliques (well, 2 is but only because it's a trivial case). You want [connected_components](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.connected_components.html) – matszwecja Mar 29 '22 at 15:06
  • @Ssong in your example (from the [link](https://frhyme.github.io/python-libs/nx_clique/)), all nodes are connected – mozway Mar 29 '22 at 15:14
  • @mozway I changed the example and attached the result.. – Ssong Mar 29 '22 at 15:20
  • @matszwecja Thank you for your recommendation! I solved the problem using the code as below: len([len(c) for c in sorted(networkx.connected_components(G1), key=len, reverse=True)]) – Ssong Mar 29 '22 at 15:22
  • 1
    What is wrong with the approach in the linked QA? Try: `list(enumerate(sorted(nx.connected_components(G1), key=len), start=1))` you'll get the numbered sub-graphs – mozway Mar 29 '22 at 15:23
  • 1
    @mozway Thank you for your comment! Now I can the count the sub-graphs in the original network! – Ssong Mar 29 '22 at 15:25
  • 1
    NB. if you only want to **count** the subgraphs `len(list(nx.connected_components(G1)))` is enough – mozway Mar 29 '22 at 15:27
  • @mozway You are right. Thank you for your suggestion! – Ssong Mar 29 '22 at 15:29

0 Answers0