Skip to content

Commit df11d8f

Browse files
author
Daniel Precioso
committed
Enhance connectivity and models documentation: add clustering coefficient section and clarify metric definitions
1 parent 208d971 commit df11d8f

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

modules/networks/connectivity.qmd

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ subtitle: "Paths, Degree, Connected Components"
44
format: html
55
---
66

7-
In this page, we will explore some basic properties of networks, such as degree and density.
7+
In this page, we will explore some basic properties of networks, such as **degree**, **density**, **clustering**, and **paths / connected components**.
88

99
If you are coding alongside me, I recommend you to start a new notebook and follow the code snippets below. You can also check the [Network Fundamentals](fundamentals.qmd) page for more details on how to create graphs with `networkx`.
1010

@@ -77,8 +77,8 @@ print("Average degree:", avg_degree)
7777
In a directed graph, we distinguish between in-degree (number of incoming edges) and out-degree (number of outgoing edges).
7878

7979
```{python}
80-
print("In-degree of 'rabbit' (number of predators):", D_food_chain.in_degree("rabbit"))
81-
print("Out-degree of 'rabbit' (number of prey):", D_food_chain.out_degree("rabbit"))
80+
print("In-degree of 'antelope' (number of prey):", D_food_chain.in_degree("antelope"))
81+
print("Out-degree of 'antelope' (number of predators):", D_food_chain.out_degree("antelope"))
8282
```
8383

8484
## Density
@@ -114,6 +114,46 @@ else:
114114
print("The graph is dense.")
115115
```
116116

117+
## Clustering Coefficient
118+
119+
Many real networks have **triangles**: if A is connected to B and C, there is a higher-than-random chance that B is also connected to C.
120+
This tendency is captured by the **clustering coefficient**.
121+
122+
For an undirected graph, the *local clustering coefficient* of node $i$ is:
123+
124+
$$
125+
C_i = \frac{2T_i}{k_i(k_i - 1)}
126+
$$
127+
128+
where $k_i$ is the degree of node $i$, and $T_i$ is the number of triangles that include node $i$.
129+
If $k_i < 2$, we define $C_i = 0$.
130+
131+
The **average clustering coefficient** is:
132+
133+
$$
134+
C = \frac{1}{N}\sum_{i=1}^N C_i.
135+
$$
136+
137+
In NetworkX:
138+
139+
- `nx.clustering(G)` returns $C_i$ for every node.
140+
- `nx.average_clustering(G)` returns the average $C$.
141+
142+
We will use $C$ later to compare **random** vs **small-world** vs **scale-free** synthetic networks.
143+
144+
```{python}
145+
# Local clustering coefficient for each node
146+
clustering = nx.clustering(G_foods)
147+
print("Local clustering:")
148+
for node, c in clustering.items():
149+
print(f" {node}: {c:.3f}")
150+
151+
print("Average clustering:", nx.average_clustering(G_foods))
152+
153+
# Another common global measure is transitivity (ratio of triangles to connected triples)
154+
print("Transitivity:", nx.transitivity(G_foods))
155+
```
156+
117157
## Connectivity in Undirected Graphs
118158

119159
Connectivity is a fundamental property of graphs that describes how well the nodes are connected to each other. In an undirected graph, a graph is **connected** if there is a path between any two nodes. If a graph is not connected, it consists of multiple **connected components**, which are subgraphs in which any two nodes are connected by a path.
@@ -259,6 +299,7 @@ print("Average shortest path length of the largest strongly connected component:
259299

260300
## What's Next?
261301

262-
In the next page, you will explore a real-world network of US air travel routes. You will analyze its connectivity and identify important nodes (airports) in the network. You will also see how the structure of the network affects the flow of information (or in this case, passengers) through it.
302+
In the next page, we will learn how to generate *synthetic* graphs (random, small-world, scale-free) and compare their properties.
303+
304+
[Graph Models](models.qmd){.btn .btn-primary}
263305

264-
[OpenFlights Network](flights.qmd){.btn .btn-primary}

modules/networks/models.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ We will also compute a few summary metrics for each model:
1919
- Average clustering coefficient $C$
2020
- Average shortest path length $L$ (on the largest connected component if needed)
2121

22+
If you have not seen these metrics before, the definitions and examples are in the previous page: [Network Connectivity](connectivity.qmd).
23+
2224
```{python}
2325
import matplotlib.pyplot as plt
2426
import networkx as nx

0 commit comments

Comments
 (0)