Skip to content

Commit 0b57ee7

Browse files
committed
add: directed and density based modualrity
1 parent 820b3fd commit 0b57ee7

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

doc/source/community_detection_guide/notebooks/modularity.ipynb

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,29 @@
193193
"source": [
194194
"*Note:* Based on our previous analysis, the \"good\" partitioning yields a significantly higher modularity score. It is important to note, however, that a high modularity score is not always a definitive indicator of a better community partitioning, as was previously demonstrated with the Grid Graph [here](test_significance_of_community.ipynb).\n",
195195
"\n",
196-
"### Why the Resolution Parameter is Important\n",
196+
"# Directed Modularity\n",
197+
"\n",
198+
"While the classic modularity formula works for undirected networks, a different approach is needed for **directed networks**, where edges have a specific direction (e.g., from node *i* to node *j*). In this context, the direction of an edge is crucial and should not be ignored.\n",
199+
"\n",
200+
"The directed modularity formula is:\n",
201+
"\n",
202+
"$$Q_{dir} = \\frac{1}{m} \\sum_{i,j} \\left[ A_{ij} - \\gamma \\frac{k_{i}^{out} k_{j}^{in}}{m} \\right] \\delta(c_i, c_j)$$\n",
203+
"\n",
204+
"Where:\n",
205+
"* $m$: Total number of directed edges in the graph.\n",
206+
"* $A_{ij}$: Element of the adjacency matrix, representing the weight of the edge from node $i$ to node $j$.\n",
207+
"* $k_{i}^{out}$: The **out-degree** of node $i$ (the sum of weights of edges leaving node *i*).\n",
208+
"* $k_{j}^{in}$: The **in-degree** of node $j$ (the sum of weights of edges entering node *j*).\n",
209+
"* $\\gamma$ is the resolution parameter. The default value is 1.0.\n",
210+
"* $\\delta(c_i, c_j)$: Kronecker delta function, which is 1 if node $i$ and node $j$ belong to the same community ($c_i = c_j$), and 0 otherwise.\n",
211+
"\n",
212+
"### Why does the direction of edges matter?\n",
213+
"* **Flipping a single edge:** Reversing a single edge (e.g., from A → B to B → A) will change the modularity score. This is because the out-degree of A and the in-degree of B would change, altering the null model's calculation and, consequently, the overall score.\n",
214+
"* **Flipping all edges:** If you reverse the direction of **every single edge** in the network, the modularity score will **remain the same**. This is due to a symmetry property of the formula. The set of in-degrees becomes the new set of out-degrees, and vice versa. When the formula is applied to this completely reversed network, the total modularity score is unchanged. This is a fascinating property of directed modularity.\n",
215+
"\n",
216+
"\n",
217+
"\n",
218+
"# Why the resolution parameter is important\n",
197219
"\n",
198220
"The resolution parameter addresses a fundamental limitation of the original modularity measure, known as the **\"resolution limit\"**. This is the tendency of the original formula (where $\\gamma=1$) to fail at detecting small communities, especially in large graphs. It often merges smaller, distinct communities into a single larger one to maximize the modularity score.\n",
199221
"\n",
@@ -202,7 +224,34 @@
202224
"* $\\gamma > 1$: Increasing the resolution parameter penalizes connections more heavily. This encourages the algorithm to find **fewer and larger communities**, as it becomes harder for smaller, highly connected groups to form their own separate communities.\n",
203225
"* $\\gamma < 1$: Decreasing the resolution parameter reduces the penalty. This allows the algorithm to find **more and smaller communities**, as it becomes easier for closely-knit groups to be identified as their own communities.\n",
204226
"\n",
205-
"In essence, the resolution parameter provides a flexible way to explore the community structure of a network at different scales, moving beyond the limitations of a single, fixed-scale partition."
227+
"In essence, the resolution parameter provides a flexible way to explore the community structure of a network at different scales, moving beyond the limitations of a single, fixed-scale partition.\n",
228+
"# Density-based modularity for undirected graphs\n",
229+
"\n",
230+
"While modularity is a powerful metric, it suffers from a well-known flaw called the **resolution limit**. This problem causes the modularity-maximizing algorithm to fail to detect small, tightly-knit communities, especially in large networks. Instead of finding these small groups, it often merges them into a single larger one to maximize the modularity score.\n",
231+
"\n",
232+
"One alternative is to use a variant based on an **Erdős-Rényi null model**, sometimes called **density-based modularity**. Instead of comparing communities to a degree-preserving random graph (configuration model), it compares them to a uniform random graph with the same overall density.\n",
233+
"\n",
234+
"For a graph \\( G = (V, E) \\) with \\( n = |V| \\) nodes and \\( m = |E| \\) edges, the global **edge density** is\n",
235+
"\n",
236+
"$$p = \\frac{2m}{n(n-1)}$$\n",
237+
"\n",
238+
"\n",
239+
"The **density-based modualrity** is defined as:\n",
240+
"\n",
241+
"\n",
242+
"$$Q_{\\text{density}} = \\frac{1}{2m} \\sum_{i,j} \n",
243+
"\\Big( A_{ij} - p \\Big) \\, \\delta(c_i, c_j),$$\n",
244+
"\n",
245+
"\n",
246+
"where:\n",
247+
"\n",
248+
"* $n$: Total number of nodes in the graph.\n",
249+
"* $m$: Total number of edges in the graph.\n",
250+
"* $A_{ij}$: Element of the adjacency matrix, representing the weight of the edge from node $i$ to node $j$.\n",
251+
"* $p$: expected probability of an edge.\n",
252+
"* $\\delta(c_i, c_j)$: Kronecker delta function, which is 1 if node $i$ and node $j$ belong to the same community ($c_i = c_j$), and 0 otherwise. \n",
253+
"\n",
254+
"In this formulation, the null model assumes **uniform edge probability**, so communities are favored if their **internal density** is higher than the global density.\n"
206255
]
207256
}
208257
],

0 commit comments

Comments
 (0)