Skip to content

Commit 83f51dd

Browse files
committed
add: from directed to undirected modularity
1 parent 45d8c14 commit 83f51dd

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

doc/source/community_detection_guide/notebooks/modularity.ipynb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
"\n",
200200
"The directed modularity formula is:\n",
201201
"\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",
202+
"$$Q = \\frac{1}{m} \\sum_{i,j} \\left[ A_{ij} - \\gamma \\frac{k_i^\\text{out} k_j^\\text{in}}{m} \\right] \\delta(c_i, c_j)$$\n",
203203
"\n",
204204
"Where:\n",
205205
"* $m$: Total number of directed edges in the graph.\n",
@@ -213,6 +213,23 @@
213213
"* **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",
214214
"* **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",
215215
"\n",
216+
"# From directed to undirected formula\n",
217+
"Start with the directed formula:\n",
218+
"\n",
219+
"$$Q = \\frac{1}{m} \\sum_{i,j} \\left[ A_{ij} - \\gamma \\frac{k_i^\\text{out} k_j^\\text{in}}{m} \\right] \\delta(c_i, c_j)$$\n",
220+
"\n",
221+
"Substitute $(k_i^\\text{out} = k_i)$ and $(k_j^\\text{in} = k_j)$:\n",
222+
"\n",
223+
"$$Q = \\frac{1}{m} \\sum_{i,j} \\left[ A_{ij} - \\gamma \\frac{k_i k_j}{m} \\right] \\delta(c_i, c_j)$$\n",
224+
"\n",
225+
"However, this sum is **over ordered pairs** \\((i,j)\\), so each undirected edge is **counted twice**. \n",
226+
"To avoid double counting, we divide by $2$:\n",
227+
"\n",
228+
"\n",
229+
"$$Q = \\frac{1}{2m} \\sum_{i,j} \\left[ A_{ij} - \\gamma \\frac{k_i k_j}{2m} \\right] \\delta(c_i, c_j)$$\n",
230+
"\n",
231+
"This matches the undirected formula exactly.\n",
232+
"\n",
216233
"\n",
217234
"\n",
218235
"# Why the resolution parameter is important\n",
@@ -253,6 +270,14 @@
253270
"\n",
254271
"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"
255272
]
273+
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": null,
277+
"id": "1b9c3fa0-05b7-461f-acf8-8348f344a2d9",
278+
"metadata": {},
279+
"outputs": [],
280+
"source": []
256281
}
257282
],
258283
"metadata": {

0 commit comments

Comments
 (0)