A fraud ring can be hard to spot when its transactions look ordinary one by one. Community detection looks for groups of graph nodes that are more densely connected to one another than to the rest of the network. In a transaction graph, such groups may reveal coordinated accounts or shared resources worth investigating—not establish fraud on their own. section 9.0 section 9.3.3
A practical workflow moves from transaction records to inspectable clusters:
- Build a graph with nodes for users, IP addresses, devices, phones, and cards. Connect entities that occur in the same transaction; an edge weight records how many transactions linked the pair. section 9.3.2
- Run Louvain on the constructed graph, then assign its partition result to each node as a community_id. The example uses community_louvain.best_partition(G, random_state=42, resolution=1.0). section 9.3.3
- Group nodes by community ID. For each induced subgraph, calculate node count and density, and count each entity type. Density measures internal connectedness; 1 denotes a fully connected clique. section 9.3.3
- Compare community summaries, then visualize a selected subgraph with distinct markers for entity types. Look for structural patterns such as many users sharing one IP, and use those findings to guide investigation. section 9.3.3 section 9.6
Listing 9.3 Applying Louvain community detection
# Ensure NetworkX (nx) is imported and G is the constructed graph from Listing 9.2
# Ensure pandas as pd and display from IPython.display are imported
if G is not None and G.number_of_nodes() > 0:
print("\nPerforming Louvain community detection...")
try:
import community.community_louvain as community_louvain
partition = community_louvain.best_partition(G, random_state=42, resolution=1.0)
nx.set_node_attributes(G, partition, 'community_id')
num_communities = len(set(partition.values()))
print(f"Louvain algorithm detected {num_communities} communities.")
except ImportError:
print("Error: 'community_louvain' library not found. Please install it using 'pip install python-louvain'.")
partition = None
except Exception as e:
print(f"An error occurred during community detection: {e}")
partition = None
else:
print("Graph G not available or empty. Skipping community detection.")
partition = None # Ensure partition is defined
The Louvain listing shows the partition call, node-attribute assignment, community count, and a missing-library check. It requires an already constructed, nonempty graph. section 9.3.3
From graph statistics to a reviewable cluster
One transaction graph contains 14,906 entity nodes and 30,567 edges; Louvain reports 2,913 communities. That count is a starting point, not a fraud finding. A summary of the largest communities adds size, density, and entity-type counts, while a visualization of the largest shows relationships and possible bottlenecks. section 9.3.2 section 9.3.3



