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
A graph is **sparse** when $D \ll 1$ and **dense** when $D \approx 1$.
111
+
In practice, many real graphs fall in an intermediate range, so density is often better viewed as a spectrum rather than a strict binary label.
108
112
109
113
```{python}
110
-
# Check if the graph is sparse or dense
111
-
if nx.density(G_foods) < 0.1:
114
+
# Check if the graph is sparse, intermediate, or dense
115
+
D = nx.density(G_foods)
116
+
if D < 0.1:
112
117
print("The graph is sparse.")
113
-
else:
118
+
elif D > 0.6:
114
119
print("The graph is dense.")
120
+
else:
121
+
print("The graph has intermediate density.")
115
122
```
116
123
117
124
## Clustering Coefficient
@@ -128,6 +135,8 @@ $$
128
135
where $k_i$ is the degree of node $i$, and $T_i$ is the number of triangles that include node $i$.
129
136
If $k_i < 2$, we define $C_i = 0$.
130
137
138
+
In our food graph, $C_{\text{bread}} = 1/3$: "bread" has three neighbors, and only one neighbor-pair is connected.
139
+
131
140
The **average clustering coefficient** is:
132
141
133
142
$$
@@ -242,7 +251,7 @@ for node, c in sorted(deg_cent.items(), key=lambda kv: kv[1], reverse=True):
242
251
print(f" {node}: {c:.3f}")
243
252
```
244
253
245
-
A higher degree centrality means the node is more "important" in terms of connectivity. In our food graph, "bread" and "cured ham" have the highest degree centrality because they are connected to the most other nodes.
254
+
A higher degree centrality means the node is more "important" in terms of connectivity. In our food graph, "chocolate" has the highest degree centrality, while "bread" and "cured ham" are the next most connected nodes.
246
255
247
256
### Closeness Centrality
248
257
@@ -255,7 +264,7 @@ $$
255
264
256
265
where $d(i,j)$ is the shortest-path distance.
257
266
258
-
A higher closeness centrality means the node can reach other nodes more quickly. In our food graph, "bread" and "cured ham" have the highest closeness centrality because they are connected to all other nodes through short paths.
267
+
A higher closeness centrality means the node can reach other nodes more quickly. In our food graph, "chocolate" has the highest closeness centrality, followed by "bread".
259
268
260
269
::: {.callout-note}
261
270
## Disconnected graphs
@@ -288,7 +297,7 @@ for node, c in sorted(bc.items(), key=lambda kv: kv[1], reverse=True):
288
297
print(f" {node}: {c:.3f}")
289
298
```
290
299
291
-
Higher betweenness centrality means the node acts as a bridge between different parts of the graph. In our food graph, "bread" and "cured ham" have the highest betweenness centrality because they connect different groups of nodes.
300
+
Higher betweenness centrality means the node acts as a bridge between different parts of the graph. In our food graph, "chocolate" has the highest betweenness centrality, with "bread" as the second most important bridge.
0 commit comments