You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example demonstrates the variability of stochastic community detection methods by analyzing the consistency of multiple partitions using similarity measures (NMI, VI, RI) on both random and structured graphs.
8
+
This example demonstrates the variability of stochastic community detection methods by analyzing the consistency of multiple partitions using similarity measures normalized mutual information (NMI), variation of information (VI), rand index (RI) on both random and structured graphs.
9
9
10
10
"""
11
11
# %%
12
-
# Import Libraries
12
+
# Import libraries
13
13
importigraphasig
14
-
importnumpyasnp
15
14
importmatplotlib.pyplotasplt
16
15
importitertools
17
16
18
17
# %%
19
18
# First, we generate a graph.
20
-
# Generates a random Erdos-Renyi graph (no clear community structure)
21
-
defgenerate_random_graph(n, p):
22
-
returnig.Graph.Erdos_Renyi(n=n, p=p)
19
+
# Load the karate club network
20
+
karate=ig.Graph.Famous("Zachary")
23
21
24
22
# %%
25
-
# Generates a clustered graph with clear communities using the Stochastic Block Model (SBM)
# Runs Louvain's method iteratively to generate partitions
43
-
# Computes similarity metrics:
64
+
# We have used, stochastic community detection using the Louvain method, iteratively generating partitions and computing similarity metrics to assess stability.
65
+
# The Louvain method is a modularity maximization approach for community detection.
66
+
# Since exact modularity maximization is NP-hard, the algorithm employs a greedy heuristic that processes vertices in a random order.
67
+
# This randomness leads to variations in the detected communities across different runs, which is why results may differ each time the method is applied.
44
68
defrun_experiment(graph, iterations=50):
45
-
"""Runs the stochastic method multiple times and collects community partitions."""
axes[i][1].set_title(f"Probability Density of {measure} - Erdős-Rényi Graph")
84
107
axes[i][1].set_xlabel(f"{measure} Score")
108
+
axes[i][1].set_xlim(lower, upper) # Set axis limits explicitly
85
109
86
110
plt.tight_layout()
87
111
plt.show()
88
112
89
113
# %%
90
-
# The results are plotted as histograms for random vs. clustered graphs, highlighting differences in detected community structures.
91
-
#The key reason for the inconsistency in random graphs and higher consistency in structured graphs is due to community structure strength:
92
-
#Random Graphs: Lack clear communities, leading to unstable partitions. Stochastic algorithms detect different structures across runs, resulting in low NMI, high VI, and inconsistent RI.
93
-
#Structured Graphs: Have well-defined communities, so detected partitions are more stable across multiple runs, leading to high NMI, low VI, and stable RI.
114
+
# We have compared the probability density of NMI, VI, and RI for the Karate Club network (structured) and an Erdős-Rényi random graph.
115
+
#
116
+
# **NMI (Normalized Mutual Information):**
117
+
#
118
+
# - Karate Club Network: The distribution is concentrated near 1, indicating high similarity across multiple runs, suggesting stable community detection.
119
+
# - Erdős-Rényi Graph: The values are more spread out, with lower NMI scores, showing inconsistent partitions due to the lack of clear community structures.
120
+
#
121
+
# **VI (Variation of Information):**
122
+
#
123
+
# - Karate Club Network: The values are low and clustered, indicating stable partitioning with minor variations across runs.
124
+
# - Erdős-Rényi Graph: The distribution is broader and shifted toward higher VI values, meaning higher partition variability and less consistency.
125
+
#
126
+
# **RI (Rand Index):**
127
+
#
128
+
# - Karate Club Network: The RI values are high and concentrated near 1, suggesting consistent clustering results across multiple iterations.
129
+
# - Erdős-Rényi Graph: The distribution is more spread out, but with lower RI values, confirming unstable community detection.
130
+
#
131
+
# **Conclusion**
132
+
#
133
+
# The Karate Club Network exhibits strong, well-defined community structures, leading to consistent results across runs.
134
+
# The Erdős-Rényi Graph, being random, lacks clear communities, causing high variability in detected partitions.
0 commit comments