1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
impl Graph {
/// Calculate modularity gain from moving a node to a different community.
fn modularity_gain(
&self,
node: NodeId,
from_comm: usize,
to_comm: usize,
node_to_comm: &[usize],
) -> f64 {
if from_comm == to_comm {
return 0.0;
}
let m = self.n_edges as f64;
if m == 0.0 {
return 0.0;
}
let two_m = 2.0 * m;
let k_i = self.neighbors(node).len() as f64;
// Count edges from node to target community
let mut k_i_to = 0.0;
for &neighbor in self.neighbors(node) {
if node_to_comm[neighbor] == to_comm {
k_i_to += 1.0;
}
}
// Count edges from node to current community (excluding self)
let mut k_i_from = 0.0;
for &neighbor in self.neighbors(node) {
if node_to_comm[neighbor] == from_comm && neighbor != node {
k_i_from += 1.0;
}
}
// Total degree of target community (excluding node)
let sigma_tot_to: f64 = node_to_comm
.iter()
.enumerate()
.filter(|&(n, &comm)| comm == to_comm && n != node)
.map(|(n, _)| self.neighbors(n).len() as f64)
.sum();
// Total degree of current community (excluding node)
let sigma_tot_from: f64 = node_to_comm
.iter()
.enumerate()
.filter(|&(n, &comm)| comm == from_comm && n != node)
.map(|(n, _)| self.neighbors(n).len() as f64)
.sum();
// Modularity gain formula
(k_i_to - k_i_from) / m - k_i * (sigma_tot_to - sigma_tot_from) / (two_m * m)
}
/// BFS to compute shortest path distances from a source node.
fn bfs_distances(&self, source: NodeId) -> Vec<usize> {
let mut distances = vec![usize::MAX; self.n_nodes];
distances[source] = 0;
let mut queue = VecDeque::new();
queue.push_back(source);
while let Some(v) = queue.pop_front() {
for &w in self.neighbors(v) {
if distances[w] == usize::MAX {
distances[w] = distances[v] + 1;
queue.push_back(w);
}
}
}
distances
}
/// Compute graph density.
///
/// Density is the ratio of actual edges to possible edges.
/// For undirected: d = 2m / (n(n-1))
/// For directed: d = m / (n(n-1))
///
/// # Returns
/// Density in [0, 1] where 1 is a complete graph
///
/// # Examples
/// ```
/// use aprender::graph::Graph;
///
/// // Complete graph K4 has density 1.0
/// let g = Graph::from_edges(&[(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)], false);
/// assert!((g.density() - 1.0).abs() < 1e-6);
/// ```
#[must_use]
pub fn density(&self) -> f64 {
if self.n_nodes <= 1 {
return 0.0;
}
let n = self.n_nodes as f64;
let m = self.n_edges as f64;
let possible = n * (n - 1.0);
if self.is_directed {
m / possible
} else {
(2.0 * m) / possible
}
}
/// Compute graph diameter (longest shortest path).
///
/// Returns None if graph is disconnected.
///
/// # Returns
/// Some(diameter) if connected, None otherwise
///
/// # Performance
/// O(n·(n + m)) - runs BFS from all nodes
///
/// # Examples
/// ```
/// use aprender::graph::Graph;
///
/// // Path graph 0--1--2--3 has diameter 3
/// let g = Graph::from_edges(&[(0,1), (1,2), (2,3)], false);
/// assert_eq!(g.diameter(), Some(3));
/// ```
#[must_use]
pub fn diameter(&self) -> Option<usize> {
if self.n_nodes == 0 {
return None;
}
let mut max_dist = 0;
for v in 0..self.n_nodes {
let distances = self.bfs_distances(v);
for &dist in &distances {
if dist == usize::MAX {
// Disconnected graph
return None;
}
if dist > max_dist {
max_dist = dist;
}
}
}
Some(max_dist)
}
/// Compute global clustering coefficient.
///
/// Measures the probability that two neighbors of a node are connected.
/// Formula: C = 3 × triangles / triads
///
/// # Returns
/// Clustering coefficient in [0, 1]
///
/// # Performance
/// O(n·d²) where d = average degree
///
/// # Examples
/// ```
/// use aprender::graph::Graph;
///
/// // Triangle has clustering coefficient 1.0
/// let g = Graph::from_edges(&[(0,1), (1,2), (2,0)], false);
/// assert!((g.clustering_coefficient() - 1.0).abs() < 1e-6);
/// ```
#[allow(clippy::cast_lossless)]
#[must_use]
pub fn clustering_coefficient(&self) -> f64 {
if self.n_nodes == 0 {
return 0.0;
}
let mut triangles = 0;
let mut triads = 0;
for v in 0..self.n_nodes {
let neighbors = self.neighbors(v);
let deg = neighbors.len();
if deg < 2 {
continue;
}
// Count triads (pairs of neighbors)
triads += deg * (deg - 1) / 2;
// Count triangles (connected pairs of neighbors)
for i in 0..neighbors.len() {
for j in (i + 1)..neighbors.len() {
let u = neighbors[i];
let w = neighbors[j];
// Check if u and w are connected
if self.neighbors(u).contains(&w) {
triangles += 1;
}
}
}
}
if triads == 0 {
return 0.0;
}
// Each triangle is counted 3 times (once from each vertex)
// So we divide by 3 to get actual triangle count
(triangles as f64) / (triads as f64)
}
/// Compute degree assortativity coefficient.
///
/// Measures correlation between degrees of connected nodes.
/// Positive: high-degree nodes connect to high-degree nodes
/// Negative: high-degree nodes connect to low-degree nodes
///
/// # Returns
/// Assortativity coefficient in [-1, 1]
///
/// # Performance
/// O(m) where m = edges
///
/// # Examples
/// ```
/// use aprender::graph::Graph;
///
/// // Star graph has negative assortativity
/// let g = Graph::from_edges(&[(0,1), (0,2), (0,3)], false);
/// assert!(g.assortativity() < 0.0);
/// ```
#[must_use]
pub fn assortativity(&self) -> f64 {
if self.n_edges == 0 {
return 0.0;
}
// Compute degrees
let degrees: Vec<f64> = (0..self.n_nodes)
.map(|i| self.neighbors(i).len() as f64)
.collect();
let m = self.n_edges as f64;
let mut sum_jk = 0.0;
let mut sum_j = 0.0;
let mut sum_k = 0.0;
let mut sum_j_sq = 0.0;
let mut sum_k_sq = 0.0;
// Sum over all edges
for v in 0..self.n_nodes {
let j = degrees[v];
for &u in self.neighbors(v) {
let k = degrees[u];
sum_jk += j * k;
sum_j += j;
sum_k += k;
sum_j_sq += j * j;
sum_k_sq += k * k;
}
}
// For undirected graphs, each edge is counted twice
let normalization = if self.is_directed { m } else { 2.0 * m };
sum_jk /= normalization;
sum_j /= normalization;
sum_k /= normalization;
sum_j_sq /= normalization;
sum_k_sq /= normalization;
let numerator = sum_jk - sum_j * sum_k;
let denominator = ((sum_j_sq - sum_j * sum_j) * (sum_k_sq - sum_k * sum_k)).sqrt();
if denominator < 1e-10 {
return 0.0;
}
numerator / denominator
}
/// Compute shortest path between two nodes using BFS.
///
/// Finds the shortest path (minimum number of hops) from source to target
/// using breadth-first search. Works for both directed and undirected graphs.
///
/// # Algorithm
/// Uses BFS with predecessor tracking (Pohl 1971, bidirectional variant).
///
/// # Arguments
/// * `source` - Starting node ID
/// * `target` - Destination node ID
///
/// # Returns
/// * `Some(path)` - Shortest path as vector of node IDs from source to target
/// * `None` - No path exists between source and target
///
/// # Complexity
/// * Time: O(n + m) where n = nodes, m = edges
/// * Space: O(n) for predecessor tracking
///
/// # Examples
/// ```
/// use aprender::graph::Graph;
///
/// let edges = vec![(0, 1), (1, 2), (2, 3), (0, 3)];
/// let g = Graph::from_edges(&edges, false);
///
/// // Shortest path from 0 to 3
/// let path = g.shortest_path(0, 3).expect("path from 0 to 3 should exist");
/// assert_eq!(path.len(), 2); // 0 -> 3 (direct edge)
/// assert_eq!(path[0], 0);
/// assert_eq!(path[1], 3);
///
/// // Path 0 to 2
/// let path = g.shortest_path(0, 2).expect("path from 0 to 2 should exist");
/// assert!(path.len() <= 3); // Either 0->1->2 or 0->3->2
/// ```
#[must_use]
pub fn shortest_path(&self, source: NodeId, target: NodeId) -> Option<Vec<NodeId>> {
// Bounds checking
if source >= self.n_nodes || target >= self.n_nodes {
return None;
}
// Special case: source == target
if source == target {
return Some(vec![source]);
}
// BFS with predecessor tracking
let mut visited = vec![false; self.n_nodes];
let mut predecessor = vec![None; self.n_nodes];
let mut queue = VecDeque::new();
visited[source] = true;
queue.push_back(source);
while let Some(v) = queue.pop_front() {
// Early termination if we reach target
if v == target {
break;
}
for &w in self.neighbors(v) {
if !visited[w] {
visited[w] = true;
predecessor[w] = Some(v);
queue.push_back(w);
}
}
}
// Reconstruct path from target to source
if !visited[target] {
return None; // No path exists
}
let mut path = Vec::new();
let mut current = Some(target);
while let Some(node) = current {
path.push(node);
current = predecessor[node];
}
path.reverse();
Some(path)
}
}