Skip to content

Commit fea673d

Browse files
author
Daniel Precioso, PhD
committed
Enhance food graph analysis: add new edges, improve density checks, and update centrality explanations for clarity
1 parent 25e0087 commit fea673d

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

modules/networks/connectivity.qmd

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ G_foods = nx.from_edgelist([
2121
("bread", "chocolate"),
2222
("bread", "cured ham"),
2323
("cheese", "cured ham"),
24+
("chocolate", "cookies"),
25+
("chocolate", "milk"),
2426
("chocolate", "watermelon"),
27+
("cookies", "milk"),
2528
("cured ham", "watermelon"),
2629
])
2730
@@ -105,13 +108,17 @@ print("Density:", nx.density(G_foods))
105108
```
106109

107110
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.
108112

109113
```{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:
112117
print("The graph is sparse.")
113-
else:
118+
elif D > 0.6:
114119
print("The graph is dense.")
120+
else:
121+
print("The graph has intermediate density.")
115122
```
116123

117124
## Clustering Coefficient
@@ -128,6 +135,8 @@ $$
128135
where $k_i$ is the degree of node $i$, and $T_i$ is the number of triangles that include node $i$.
129136
If $k_i < 2$, we define $C_i = 0$.
130137

138+
In our food graph, $C_{\text{bread}} = 1/3$: "bread" has three neighbors, and only one neighbor-pair is connected.
139+
131140
The **average clustering coefficient** is:
132141

133142
$$
@@ -242,7 +251,7 @@ for node, c in sorted(deg_cent.items(), key=lambda kv: kv[1], reverse=True):
242251
print(f" {node}: {c:.3f}")
243252
```
244253

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.
246255

247256
### Closeness Centrality
248257

@@ -255,7 +264,7 @@ $$
255264

256265
where $d(i,j)$ is the shortest-path distance.
257266

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".
259268

260269
::: {.callout-note}
261270
## Disconnected graphs
@@ -288,7 +297,7 @@ for node, c in sorted(bc.items(), key=lambda kv: kv[1], reverse=True):
288297
print(f" {node}: {c:.3f}")
289298
```
290299

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.
292301

293302
### PageRank (Directed Networks)
294303

0 commit comments

Comments
 (0)