forked from habedi/graphina
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.rs
More file actions
142 lines (125 loc) · 4.14 KB
/
components.rs
File metadata and controls
142 lines (125 loc) · 4.14 KB
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
/*!
Parallel connected components detection
*/
use std::collections::{HashMap, HashSet, VecDeque};
use crate::core::types::{BaseGraph, GraphConstructor, NodeId};
use petgraph::EdgeType;
/// Parallel connected components detection.
///
/// Finds all connected components in parallel by processing multiple starting points.
///
/// Returns a mapping from node to component ID.
///
/// # Example
///
/// ```rust
/// use graphina::core::types::Graph;
/// use graphina::parallel::connected_components_parallel;
///
/// let mut g = Graph::<i32, f64>::new();
/// let n1 = g.add_node(1);
/// let n2 = g.add_node(2);
/// let n3 = g.add_node(3);
/// let n4 = g.add_node(4);
///
/// g.add_edge(n1, n2, 1.0);
/// g.add_edge(n3, n4, 1.0);
///
/// let components = connected_components_parallel(&g);
///
/// // n1 and n2 should be in same component
/// assert_eq!(components[&n1], components[&n2]);
///
/// // n3 and n4 should be in same component
/// assert_eq!(components[&n3], components[&n4]);
///
/// // But different from n1/n2
/// assert_ne!(components[&n1], components[&n3]);
/// ```
pub fn connected_components_parallel<A, W, Ty>(
graph: &BaseGraph<A, W, Ty>,
) -> HashMap<NodeId, usize>
where
A: Sync + Send,
W: Sync + Send,
Ty: GraphConstructor<A, W> + EdgeType + Sync + Send,
{
let nodes: Vec<NodeId> = graph.node_ids().collect();
let mut component_map: HashMap<NodeId, usize> = HashMap::with_capacity(nodes.len());
let mut visited: HashSet<NodeId> = HashSet::new();
let mut current_id: usize = 0;
for node in nodes {
if visited.contains(&node) {
continue;
}
let mut queue = VecDeque::new();
queue.push_back(node);
visited.insert(node);
while let Some(current) = queue.pop_front() {
component_map.insert(current, current_id);
for neighbor in graph.neighbors(current) {
if visited.insert(neighbor) {
queue.push_back(neighbor);
}
}
}
current_id += 1;
}
component_map
}
/// Convert the component map produced by `connected_components_parallel` into a list of components.
pub fn connected_components_parallel_list<A, W, Ty>(graph: &BaseGraph<A, W, Ty>) -> Vec<Vec<NodeId>>
where
A: Sync + Send,
W: Sync + Send,
Ty: GraphConstructor<A, W> + EdgeType + Sync + Send,
{
let map = connected_components_parallel(graph);
let mut by_component: HashMap<usize, Vec<NodeId>> = HashMap::new();
for (node, cid) in map.into_iter() {
by_component.entry(cid).or_default().push(node);
}
// Return components ordered by component id
let mut keys: Vec<usize> = by_component.keys().copied().collect();
keys.sort_unstable();
keys.into_iter()
.filter_map(|k| by_component.remove(&k))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::types::Graph;
#[test]
fn test_connected_components_parallel() {
let mut g = Graph::<i32, f64>::new();
let n1 = g.add_node(1);
let n2 = g.add_node(2);
let n3 = g.add_node(3);
let n4 = g.add_node(4);
g.add_edge(n1, n2, 1.0);
g.add_edge(n3, n4, 1.0);
let components = connected_components_parallel(&g);
// n1 and n2 should be in same component
assert_eq!(components[&n1], components[&n2]);
// n3 and n4 should be in same component
assert_eq!(components[&n3], components[&n4]);
// But different from n1/n2
assert_ne!(components[&n1], components[&n3]);
}
#[test]
fn test_connected_components_parallel_list() {
let mut g = Graph::<i32, f64>::new();
let n1 = g.add_node(1);
let n2 = g.add_node(2);
let n3 = g.add_node(3);
let n4 = g.add_node(4);
g.add_edge(n1, n2, 1.0);
g.add_edge(n3, n4, 1.0);
let list = connected_components_parallel_list(&g);
assert_eq!(list.len(), 2);
// Each component should be at least size 2 except singletons when disconnected
assert!(list.iter().any(|c| c.contains(&n1) && c.contains(&n2)));
assert!(list.iter().any(|c| c.contains(&n3) && c.contains(&n4)));
}
}