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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2026 Dragonscale Team
//! Minimum Spanning Tree (MST) Algorithm.
//!
//! Uses Kruskal's algorithm to find the Minimum Spanning Tree of a weighted graph.
//! Treating graph as undirected (if include_reverse is true, we dedup edges; if not, we treat directed as undirected structure).
//! Returns the edges in the MST and total weight.
use crate::algo::GraphProjection;
use crate::algo::algorithms::Algorithm;
use uni_common::core::id::Vid;
pub struct MinimumSpanningTree;
#[derive(Debug, Clone, Default)]
pub struct MstConfig {
// If true, treats graph as undirected by considering u->v and v->u as same edge.
// If false, treats directed edges, but MST is usually defined for undirected.
// Kruskal's works on edges.
}
pub struct MstResult {
pub edges: Vec<(Vid, Vid, f64)>, // (u, v, weight)
pub total_weight: f64,
}
impl Algorithm for MinimumSpanningTree {
type Config = MstConfig;
type Result = MstResult;
fn name() -> &'static str {
"mst"
}
fn run(graph: &GraphProjection, _config: Self::Config) -> Self::Result {
let n = graph.vertex_count();
if n == 0 {
return MstResult {
edges: Vec::new(),
total_weight: 0.0,
};
}
// Collect all edges
// If we want to treat graph as undirected, we should dedup (u, v) and (v, u).
// Standard approach: normalize (min, max).
let mut edges = Vec::new();
for u in 0..n as u32 {
for (i, &v) in graph.out_neighbors(u).iter().enumerate() {
// If treating as undirected, only add if u < v to avoid duplicates
// assuming symmetry. If not symmetric, Kruskal's on directed graph
// produces Minimum Spanning Forest (Arborescence is different).
// Let's assume undirected MST on the underlying graph structure.
if u < v {
let weight = if graph.has_weights() {
graph.out_weight(u, i)
} else {
1.0
};
edges.push((u, v, weight));
}
}
}
// Sort by weight. Use total_cmp for a total order over f64 so NaN
// weights sort last (deprioritized) instead of panicking via partial_cmp.
edges.sort_by(|a, b| a.2.total_cmp(&b.2));
// Union-Find
let mut parent: Vec<u32> = (0..n as u32).collect();
let mut rank: Vec<u8> = vec![0; n];
fn find(parent: &mut [u32], mut x: u32) -> u32 {
while parent[x as usize] != x {
parent[x as usize] = parent[parent[x as usize] as usize];
x = parent[x as usize];
}
x
}
fn union(parent: &mut [u32], rank: &mut [u8], x: u32, y: u32) -> bool {
let px = find(parent, x);
let py = find(parent, y);
if px == py {
return false;
}
match rank[px as usize].cmp(&rank[py as usize]) {
std::cmp::Ordering::Less => parent[px as usize] = py,
std::cmp::Ordering::Greater => parent[py as usize] = px,
std::cmp::Ordering::Equal => {
parent[py as usize] = px;
rank[px as usize] += 1;
}
}
true
}
let mut mst_edges = Vec::new();
let mut total_weight = 0.0;
for (u, v, w) in edges {
if union(&mut parent, &mut rank, u, v) {
mst_edges.push((graph.to_vid(u), graph.to_vid(v), w));
total_weight += w;
}
}
MstResult {
edges: mst_edges,
total_weight,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::algo::test_utils::build_test_graph;
#[test]
fn test_mst_simple() {
// 0-1 (1.0), 1-2 (2.0), 0-2 (10.0)
// MST should be (0,1) and (1,2) => weight 3.0
let vids = vec![Vid::from(0), Vid::from(1), Vid::from(2)];
let edges = vec![
(Vid::from(0), Vid::from(1)),
(Vid::from(1), Vid::from(2)),
(Vid::from(0), Vid::from(2)),
];
// build_test_graph does not support weights yet.
// I need to update build_test_graph or create a new helper.
// Or I can modify GraphProjection field directly since it's pub(crate) and I'm in same crate.
let mut graph = build_test_graph(vids, edges);
// Inject weights manually
// Edges in build_test_graph are added in order of iteration over `edges`.
// Order: (0,1), (1,2), (0,2).
// Node 0: out_neighbors [1, 2]
// Node 1: out_neighbors [2]
// Node 2: []
// We need to match this structure.
// 0->1 (idx 0 for node 0) -> weight 1.0
// 0->2 (idx 1 for node 0) -> weight 10.0
// 1->2 (idx 0 for node 1) -> weight 2.0
// Flattened weights vector for GraphProjection:
// Node 0 edges, then Node 1 edges, etc.
// Node 0: [1.0, 10.0]
// Node 1: [2.0]
// Node 2: []
graph.out_weights = Some(vec![1.0, 10.0, 2.0]);
let result = MinimumSpanningTree::run(&graph, MstConfig::default());
assert_eq!(result.total_weight, 3.0);
assert_eq!(result.edges.len(), 2);
}
/// Regression: a `NaN` edge weight must not panic the sort comparator.
///
/// `f64::partial_cmp` returns `None` for `NaN`, so the `sort_by`
/// comparator's `.unwrap()` in `run` panics when any edge weight is
/// `NaN`. A `NaN` weight can reach the sort directly from a graph float
/// property with no upstream filter. The MST should still be produced.
// Rust guideline compliant
#[test]
fn test_mst_nan_weight_does_not_panic() {
// 0-1 (1.0), 1-2 (NaN), 0-2 (10.0)
// Regardless of where the NaN edge is ordered, Kruskal must connect
// all three vertices, yielding a spanning tree of exactly 2 edges.
let vids = vec![Vid::from(0), Vid::from(1), Vid::from(2)];
let edges = vec![
(Vid::from(0), Vid::from(1)),
(Vid::from(1), Vid::from(2)),
(Vid::from(0), Vid::from(2)),
];
let mut graph = build_test_graph(vids, edges);
// Flattened CSR weights, matching the layout used by `test_mst_simple`:
// Node 0: [0->1 = 1.0, 0->2 = 10.0]
// Node 1: [1->2 = NaN]
// Node 2: []
graph.out_weights = Some(vec![1.0, 10.0, f64::NAN]);
let result = MinimumSpanningTree::run(&graph, MstConfig::default());
assert_eq!(result.edges.len(), 2);
}
}