sz-orm-graph 5.1.0

Graph database support for sz-orm: Neo4j Cypher query, typed mapping, declarative modeling
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! 图路径分析(Path Analysis)
//!
//! 提供路径查找、路径枚举、可达性分析等功能。

use std::collections::{HashMap, HashSet, VecDeque};

use crate::algorithm::{DirectedGraph, NodeId};

/// 路径分析器
pub struct PathAnalyzer;

impl PathAnalyzer {
    /// 检查从 `from` 到 `to` 是否可达
    pub fn is_reachable(graph: &DirectedGraph, from: NodeId, to: NodeId) -> bool {
        if from == to {
            return true;
        }
        if !graph.has_node(from) || !graph.has_node(to) {
            return false;
        }
        let bfs = graph.bfs(from);
        bfs.contains(&to)
    }

    /// 查找从 `from` 到 `to` 的所有简单路径(限制最大长度)
    ///
    /// 简单路径:不重复访问节点。
    /// `max_depth` 限制路径最大长度,防止指数爆炸。
    pub fn find_all_paths(
        graph: &DirectedGraph,
        from: NodeId,
        to: NodeId,
        max_depth: usize,
    ) -> Vec<Vec<NodeId>> {
        if !graph.has_node(from) || !graph.has_node(to) {
            return Vec::new();
        }
        let mut results = Vec::new();
        let mut current_path = vec![from];
        let mut visited = HashSet::new();
        visited.insert(from);
        Self::find_paths_dfs(
            graph,
            from,
            to,
            max_depth,
            &mut current_path,
            &mut visited,
            &mut results,
        );
        results
    }

    fn find_paths_dfs(
        graph: &DirectedGraph,
        current: NodeId,
        target: NodeId,
        max_depth: usize,
        current_path: &mut Vec<NodeId>,
        visited: &mut HashSet<NodeId>,
        results: &mut Vec<Vec<NodeId>>,
    ) {
        if current == target {
            results.push(current_path.clone());
            return;
        }
        if current_path.len() >= max_depth {
            return;
        }
        if let Some(neighbors) = graph.neighbors(current) {
            for &(neighbor, _) in neighbors {
                if visited.insert(neighbor) {
                    current_path.push(neighbor);
                    Self::find_paths_dfs(
                        graph,
                        neighbor,
                        target,
                        max_depth,
                        current_path,
                        visited,
                        results,
                    );
                    current_path.pop();
                    visited.remove(&neighbor);
                }
            }
        }
    }

    /// 计算从 `from` 到 `to` 的最短路径长度(BFS)
    pub fn shortest_path_length(graph: &DirectedGraph, from: NodeId, to: NodeId) -> Option<usize> {
        if from == to {
            return Some(0);
        }
        if !graph.has_node(from) || !graph.has_node(to) {
            return None;
        }
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        visited.insert(from);
        queue.push_back((from, 0usize));
        while let Some((node, dist)) = queue.pop_front() {
            if let Some(neighbors) = graph.neighbors(node) {
                for &(neighbor, _) in neighbors {
                    if neighbor == to {
                        return Some(dist + 1);
                    }
                    if visited.insert(neighbor) {
                        queue.push_back((neighbor, dist + 1));
                    }
                }
            }
        }
        None
    }

    /// 计算从 `start` 到所有可达节点的最短距离
    pub fn bfs_distances(graph: &DirectedGraph, start: NodeId) -> HashMap<NodeId, usize> {
        let mut distances = HashMap::new();
        if !graph.has_node(start) {
            return distances;
        }
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        distances.insert(start, 0);
        visited.insert(start);
        queue.push_back(start);
        while let Some(node) = queue.pop_front() {
            let dist = distances[&node];
            if let Some(neighbors) = graph.neighbors(node) {
                for &(neighbor, _) in neighbors {
                    if visited.insert(neighbor) {
                        distances.insert(neighbor, dist + 1);
                        queue.push_back(neighbor);
                    }
                }
            }
        }
        distances
    }

    /// 查找从 `start` 可达的所有节点
    pub fn reachable_nodes(graph: &DirectedGraph, start: NodeId) -> HashSet<NodeId> {
        graph.bfs(start).into_iter().collect()
    }

    /// 计算图的直径(最长最短路径)
    ///
    /// 对于无权图,直径是所有节点对之间最短路径的最大值。
    pub fn diameter(graph: &DirectedGraph) -> Option<usize> {
        let nodes: Vec<NodeId> = graph.nodes().collect();
        let mut max_dist = 0;
        let mut found = false;
        for &start in &nodes {
            let distances = Self::bfs_distances(graph, start);
            for &dist in distances.values() {
                if dist > max_dist {
                    max_dist = dist;
                    found = true;
                }
            }
        }
        if found {
            Some(max_dist)
        } else {
            None
        }
    }

    /// 计算节点偏心度(到最远可达节点的距离)
    pub fn eccentricity(graph: &DirectedGraph, node: NodeId) -> Option<usize> {
        let distances = Self::bfs_distances(graph, node);
        distances.values().copied().max()
    }

    /// 计算图的半径(最小偏心度)
    pub fn radius(graph: &DirectedGraph) -> Option<usize> {
        let nodes: Vec<NodeId> = graph.nodes().collect();
        let mut min_ecc = usize::MAX;
        for &node in &nodes {
            if let Some(ecc) = Self::eccentricity(graph, node) {
                if ecc < min_ecc {
                    min_ecc = ecc;
                }
            }
        }
        if min_ecc != usize::MAX {
            Some(min_ecc)
        } else {
            None
        }
    }
}

/// 可达性矩阵
pub struct ReachabilityMatrix {
    matrix: HashMap<(NodeId, NodeId), bool>,
    node_count: usize,
}

impl ReachabilityMatrix {
    /// 从图构建可达性矩阵
    pub fn from_graph(graph: &DirectedGraph) -> Self {
        let nodes: Vec<NodeId> = graph.nodes().collect();
        let mut matrix = HashMap::new();
        for &start in &nodes {
            let reachable = PathAnalyzer::reachable_nodes(graph, start);
            for &end in &nodes {
                matrix.insert((start, end), reachable.contains(&end));
            }
        }
        Self {
            matrix,
            node_count: nodes.len(),
        }
    }

    /// 检查 `from` 是否可达 `to`
    pub fn is_reachable(&self, from: NodeId, to: NodeId) -> bool {
        self.matrix.get(&(from, to)).copied().unwrap_or(false)
    }

    /// 节点数
    pub fn node_count(&self) -> usize {
        self.node_count
    }

    /// 可达对数
    pub fn reachable_pairs(&self) -> usize {
        self.matrix.values().filter(|&&v| v).count()
    }

    /// 总对数
    pub fn total_pairs(&self) -> usize {
        self.matrix.len()
    }

    /// 可达率
    pub fn reachability_rate(&self) -> f64 {
        let total = self.total_pairs();
        if total > 0 {
            self.reachable_pairs() as f64 / total as f64
        } else {
            0.0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_reachable_same_node() {
        let mut g = DirectedGraph::new();
        g.add_node(1);
        assert!(PathAnalyzer::is_reachable(&g, 1, 1));
    }

    #[test]
    fn test_is_reachable_direct_edge() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        assert!(PathAnalyzer::is_reachable(&g, 1, 2));
    }

    #[test]
    fn test_is_reachable_transitive() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        assert!(PathAnalyzer::is_reachable(&g, 1, 3));
    }

    #[test]
    fn test_is_reachable_unreachable() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_node(3);
        assert!(!PathAnalyzer::is_reachable(&g, 1, 3));
    }

    #[test]
    fn test_find_all_paths_single() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        let paths = PathAnalyzer::find_all_paths(&g, 1, 2, 10);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], vec![1, 2]);
    }

    #[test]
    fn test_find_all_paths_multiple() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 4);
        g.add_edge_unweighted(1, 3);
        g.add_edge_unweighted(3, 4);
        let paths = PathAnalyzer::find_all_paths(&g, 1, 4, 10);
        assert_eq!(paths.len(), 2);
    }

    #[test]
    fn test_find_all_paths_none() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_node(3);
        let paths = PathAnalyzer::find_all_paths(&g, 1, 3, 10);
        assert!(paths.is_empty());
    }

    #[test]
    fn test_shortest_path_length_direct() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        assert_eq!(PathAnalyzer::shortest_path_length(&g, 1, 2), Some(1));
    }

    #[test]
    fn test_shortest_path_length_transitive() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        assert_eq!(PathAnalyzer::shortest_path_length(&g, 1, 3), Some(2));
    }

    #[test]
    fn test_shortest_path_length_same() {
        let mut g = DirectedGraph::new();
        g.add_node(1);
        assert_eq!(PathAnalyzer::shortest_path_length(&g, 1, 1), Some(0));
    }

    #[test]
    fn test_shortest_path_length_unreachable() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_node(3);
        assert_eq!(PathAnalyzer::shortest_path_length(&g, 1, 3), None);
    }

    #[test]
    fn test_bfs_distances() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(1, 3);
        g.add_edge_unweighted(2, 4);
        let dist = PathAnalyzer::bfs_distances(&g, 1);
        assert_eq!(dist[&1], 0);
        assert_eq!(dist[&2], 1);
        assert_eq!(dist[&3], 1);
        assert_eq!(dist[&4], 2);
    }

    #[test]
    fn test_reachable_nodes() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        g.add_node(4);
        let reachable = PathAnalyzer::reachable_nodes(&g, 1);
        assert_eq!(reachable.len(), 3);
        assert!(!reachable.contains(&4));
    }

    #[test]
    fn test_diameter() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        g.add_edge_unweighted(3, 4);
        assert_eq!(PathAnalyzer::diameter(&g), Some(3));
    }

    #[test]
    fn test_diameter_disconnected() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(3, 4);
        assert_eq!(PathAnalyzer::diameter(&g), Some(1));
    }

    #[test]
    fn test_eccentricity() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        assert_eq!(PathAnalyzer::eccentricity(&g, 1), Some(2));
        assert_eq!(PathAnalyzer::eccentricity(&g, 2), Some(1));
    }

    #[test]
    fn test_radius() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        let radius = PathAnalyzer::radius(&g).unwrap();
        assert!((0..=2).contains(&radius));
    }

    #[test]
    fn test_reachability_matrix() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        let matrix = ReachabilityMatrix::from_graph(&g);
        assert!(matrix.is_reachable(1, 3));
        assert!(!matrix.is_reachable(3, 1));
    }

    #[test]
    fn test_reachability_matrix_node_count() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        let matrix = ReachabilityMatrix::from_graph(&g);
        assert_eq!(matrix.node_count(), 2);
    }

    #[test]
    fn test_reachability_matrix_reachable_pairs() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        let matrix = ReachabilityMatrix::from_graph(&g);
        assert!(matrix.reachable_pairs() > 0);
    }

    #[test]
    fn test_reachability_matrix_rate() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 1);
        let matrix = ReachabilityMatrix::from_graph(&g);
        assert!((matrix.reachability_rate() - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_find_all_paths_max_depth() {
        let mut g = DirectedGraph::new();
        g.add_edge_unweighted(1, 2);
        g.add_edge_unweighted(2, 3);
        g.add_edge_unweighted(3, 4);
        let paths = PathAnalyzer::find_all_paths(&g, 1, 4, 2);
        assert!(paths.is_empty());
    }
}