Loading…
DS · Graphs · Program 35 · Bonus
Community detection in a social network graph.
1import networkx as nx2from networkx.algorithms.community import greedy_modularity_communities3 4G = nx.karate_club_graph()5communities = list(greedy_modularity_communities(G))6 7print(f"Detected {len(communities)} communities:")8for i, comm in enumerate(communities, 1):9 print(f"Community {i}: {sorted(comm)}")Detected 3 communities: Community 1: [8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33] Community 2: [1, 2, 3, 7, 9, 12, 13, 17, 21] Community 3: [0, 4, 5, 6, 10, 11, 16, 19]
Run program 35 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.