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
//! Graph motif detection for visibility graphs.
//!
//! This module provides algorithms for detecting recurring subgraph patterns
//! (motifs) in visibility graphs, which can reveal important structural properties.
use crate::core::VisibilityGraph;
use std::collections::HashMap;
/// Types of common 3-node motifs in directed graphs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Motif3 {
/// No edges between nodes
Empty,
/// Single edge: A → B
SingleEdge,
/// Two edges: A → B → C (chain)
Chain,
/// Two edges: A → B, A → C (fork)
Fork,
/// Three edges forming a triangle
Triangle,
/// Cycle: A → B → C → A
Cycle,
}
/// Motif detection result.
#[derive(Debug, Clone)]
pub struct MotifCounts {
/// Counts for each motif type
pub counts: HashMap<String, usize>,
/// Total number of subgraphs examined
pub total_subgraphs: usize,
}
impl<T> VisibilityGraph<T> {
/// Detects 3-node motifs in the graph.
///
/// ⚠️ **Performance Warning:** This method has **O(n³)** complexity where n is the number
/// of nodes. It examines all possible 3-node combinations. For large graphs (> 500 nodes),
/// this can be slow. The method checks C(n,3) = n×(n-1)×(n-2)/6 subgraphs.
///
/// Examines all possible 3-node subgraphs and counts occurrences
/// of each motif pattern.
///
/// # Returns
///
/// `MotifCounts` with frequency of each pattern
///
/// # Examples
///
/// ```rust
/// use rustygraph::{TimeSeries, VisibilityGraph};
///
/// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 3.0]).unwrap();
/// let graph = VisibilityGraph::from_series(&series)
/// .natural_visibility()
/// .unwrap();
///
/// let motifs = graph.detect_3node_motifs();
/// println!("Found {} different motif types", motifs.counts.len());
/// println!("Total subgraphs: {}", motifs.total_subgraphs);
/// ```
pub fn detect_3node_motifs(&self) -> MotifCounts {
let n = self.node_count;
let mut counts: HashMap<String, usize> = HashMap::new();
let mut total = 0;
// Examine all 3-node combinations
for i in 0..n {
for j in (i + 1)..n {
for k in (j + 1)..n {
total += 1;
let motif_type = self.classify_3node_motif(i, j, k);
*counts.entry(motif_type).or_insert(0) += 1;
}
}
}
MotifCounts {
counts,
total_subgraphs: total,
}
}
/// Classifies a 3-node subgraph pattern.
fn classify_3node_motif(&self, a: usize, b: usize, c: usize) -> String {
let ab = self.has_edge(a, b);
let ba = self.has_edge(b, a);
let ac = self.has_edge(a, c);
let ca = self.has_edge(c, a);
let bc = self.has_edge(b, c);
let cb = self.has_edge(c, b);
let edge_count = [ab, ba, ac, ca, bc, cb].iter().filter(|&&x| x).count();
match edge_count {
0 => "empty".to_string(),
1 => "single_edge".to_string(),
2 => {
// Check for chain vs fork
if (ab && bc) || (ba && cb) || (ac && cb) || (ca && ba) || (ab && ac) || (ba && ca) {
"chain".to_string()
} else {
"fork".to_string()
}
}
3 => {
// Check for cycle
if (ab && bc && ca) || (ba && cb && ac) {
"cycle".to_string()
} else {
"three_edges".to_string()
}
}
4 => "four_edges".to_string(),
5 => "five_edges".to_string(),
6 => "complete_triangle".to_string(),
_ => "unknown".to_string(),
}
}
/// Detects 4-node motifs in the graph.
///
/// ⚠️ **Performance Warning:** This method has **O(n⁴)** complexity where n is the number
/// of nodes. It examines all possible 4-node combinations. For large graphs (> 200 nodes),
/// this is VERY expensive. The method checks C(n,4) = n×(n-1)×(n-2)×(n-3)/24 subgraphs.
/// **Use with caution!** Consider using 3-node motifs instead for large graphs.
///
/// # Returns
///
/// `MotifCounts` with frequency of each pattern
///
/// # Examples
///
/// ```rust
/// use rustygraph::{TimeSeries, VisibilityGraph};
///
/// let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 3.0, 5.0]).unwrap();
/// let graph = VisibilityGraph::from_series(&series)
/// .natural_visibility()
/// .unwrap();
///
/// let motifs = graph.detect_4node_motifs();
/// println!("4-node motifs: {:?}", motifs.counts);
/// ```
pub fn detect_4node_motifs(&self) -> MotifCounts {
let n = self.node_count;
let mut counts: HashMap<String, usize> = HashMap::new();
let mut total = 0;
// Examine all 4-node combinations
for i in 0..n {
for j in (i + 1)..n {
for k in (j + 1)..n {
for l in (k + 1)..n {
total += 1;
let motif_type = self.classify_4node_motif(i, j, k, l);
*counts.entry(motif_type).or_insert(0) += 1;
}
}
}
}
MotifCounts {
counts,
total_subgraphs: total,
}
}
/// Classifies a 4-node subgraph pattern.
fn classify_4node_motif(&self, a: usize, b: usize, c: usize, d: usize) -> String {
let edges = [
(a, b), (b, a), (a, c), (c, a), (a, d), (d, a),
(b, c), (c, b), (b, d), (d, b), (c, d), (d, c),
];
let edge_count = edges.iter().filter(|&&(from, to)| self.has_edge(from, to)).count();
match edge_count {
0 => "empty".to_string(),
1 => "single_edge".to_string(),
2 => "two_edges".to_string(),
3 => "three_edges".to_string(),
4 => "four_edges".to_string(),
_ => format!("{}_edges", edge_count),
}
}
/// Computes motif significance score (Z-score).
///
/// Compares observed motif counts against a random graph model.
///
/// # Arguments
///
/// * `motifs` - Observed motif counts
/// * `num_random` - Number of random graphs to generate
///
/// # Returns
///
/// HashMap of motif type to Z-score
pub fn motif_significance(&self, motifs: &MotifCounts, num_random: usize) -> HashMap<String, f64>
where
T: Clone,
{
let mut random_counts: HashMap<String, Vec<usize>> = HashMap::new();
// Generate random graphs and count motifs
for _ in 0..num_random {
let random_graph = self.randomize_edges();
let random_motifs = random_graph.detect_3node_motifs();
for (motif_type, count) in random_motifs.counts {
random_counts.entry(motif_type).or_default().push(count);
}
}
// Compute Z-scores
let mut z_scores = HashMap::new();
for (motif_type, observed) in &motifs.counts {
if let Some(random_values) = random_counts.get(motif_type) {
let mean = random_values.iter().sum::<usize>() as f64 / random_values.len() as f64;
let variance = random_values.iter()
.map(|&x| (x as f64 - mean).powi(2))
.sum::<f64>() / random_values.len() as f64;
let std = variance.sqrt();
if std > 0.0 {
let z_score = (*observed as f64 - mean) / std;
z_scores.insert(motif_type.clone(), z_score);
}
}
}
z_scores
}
/// Creates a randomized version of the graph preserving degree distribution.
///
/// Note: This is a simplified placeholder. In production, use proper
/// degree-preserving randomization algorithms.
fn randomize_edges(&self) -> Self
where
T: Clone,
{
// For now, just return a copy of the graph
// In a real implementation, you would:
// 1. Extract edge list
// 2. Randomly rewire while preserving degree sequence
// 3. Reconstruct graph
// This is a placeholder - motif significance would need proper implementation
// For demonstration purposes, we return the same graph
// In production: implement edge-swapping Monte Carlo randomization
// Since we can't access private fields directly and this is a complex algorithm,
// we'll note this as a limitation in the current implementation
unimplemented!("Full edge randomization requires advanced algorithms - placeholder for API demonstration")
}
}
impl MotifCounts {
/// Returns the most frequent motif.
pub fn most_frequent(&self) -> Option<(&String, &usize)> {
self.counts.iter().max_by_key(|(_, &count)| count)
}
/// Returns motif frequency as percentages.
pub fn frequencies(&self) -> HashMap<String, f64> {
let total = self.total_subgraphs as f64;
self.counts
.iter()
.map(|(k, &v)| (k.clone(), v as f64 / total * 100.0))
.collect()
}
/// Prints a summary of motif counts.
pub fn print_summary(&self) {
println!("Motif Detection Summary");
println!("=======================");
println!("Total subgraphs examined: {}", self.total_subgraphs);
println!("\nMotif frequencies:");
let mut sorted: Vec<_> = self.counts.iter().collect();
sorted.sort_by_key(|(_, &count)| std::cmp::Reverse(count));
for (motif, count) in sorted {
let pct = *count as f64 / self.total_subgraphs as f64 * 100.0;
println!(" {}: {} ({:.2}%)", motif, count, pct);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::TimeSeries;
#[test]
fn test_motif_detection() {
let series = TimeSeries::from_raw(vec![1.0, 3.0, 2.0, 4.0, 3.0]).unwrap();
let graph = VisibilityGraph::from_series(&series)
.natural_visibility()
.unwrap();
let motifs = graph.detect_3node_motifs();
assert!(motifs.total_subgraphs > 0);
assert!(!motifs.counts.is_empty());
}
#[test]
fn test_motif_frequencies() {
let series = TimeSeries::from_raw(vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let graph = VisibilityGraph::from_series(&series)
.natural_visibility()
.unwrap();
let motifs = graph.detect_3node_motifs();
let freqs = motifs.frequencies();
let total: f64 = freqs.values().sum();
assert!((total - 100.0).abs() < 1e-6);
}
}