@@ -326,6 +326,8 @@ fn test_thickness_weight_functions() {
326326}
327327
328328#[ test]
329+ #[ cfg( not( feature = "wgpu" ) ) ]
330+ #[ cfg( not( feature = "cuda" ) ) ]
329331fn test_mst_k_neighbors_parameter ( ) {
330332 crate :: tests:: init ( ) ;
331333 let device = Default :: default ( ) ;
@@ -362,6 +364,67 @@ fn test_mst_k_neighbors_parameter() {
362364 }
363365}
364366
367+ #[ test]
368+ fn test_mst_k_neighbors_parameter_no_rand ( ) {
369+ crate :: tests:: init ( ) ;
370+ let device = Default :: default ( ) ;
371+
372+ // ✅ Use a deterministic grid layout to guarantee connectivity
373+ // 10 centroids in 5D space, arranged in a line with known distances
374+ let centroids_data: Vec < f32 > = ( 0 ..10 )
375+ . flat_map ( |i| {
376+ let x = i as f32 * 2.0 ; // Spread along x-axis
377+ vec ! [ x, 0.0 , 0.0 , 0.0 , 0.0 ] // 5 features
378+ } )
379+ . collect ( ) ;
380+
381+ let centroids = Tensor :: < TestBackend , 2 > :: from_data (
382+ TensorData :: new ( centroids_data, Shape :: new ( [ 10 , 5 ] ) ) ,
383+ & device,
384+ ) ;
385+
386+ let counts = Tensor :: < TestBackend , 1 , Int > :: ones ( [ 10 ] , & device) ;
387+ let state = CentroidState :: from_clustering ( centroids, counts, 0.1 ) ;
388+
389+ // Test with different k values
390+ for k in [ 2 , 4 , 8 ] {
391+ let config = MSTConfig {
392+ k_neighbors : k,
393+ ..Default :: default ( )
394+ } ;
395+
396+ let output = MSTStage :: new ( config) . execute ( & state) ;
397+
398+ // With linear layout, k=2 is sufficient for connectivity
399+ // MST should always have C-1 edges for connected graph
400+ assert_eq ! (
401+ output. mst_edges. len( ) ,
402+ 9 ,
403+ "MST should have 9 edges (k={}, got {} edges)" ,
404+ k,
405+ output. mst_edges. len( )
406+ ) ;
407+
408+ // Candidate graph should have ~k edges per node (directed)
409+ let avg_edges = output. candidate_edges . len ( ) as f32 / 10.0 ;
410+ assert ! (
411+ avg_edges >= k as f32 * 0.8 && avg_edges <= k as f32 * 1.2 ,
412+ "Average edges per node should be ~{}, got {:.1} (k={})" ,
413+ k,
414+ avg_edges,
415+ k
416+ ) ;
417+
418+ // All nodes should be covered
419+ assert_eq ! (
420+ output. centroid_order. len( ) ,
421+ 10 ,
422+ "All 10 centroids should be in the ordering (k={})" ,
423+ k
424+ ) ;
425+ }
426+ }
427+
365428#[ test]
366429fn test_mst_config_presets ( ) {
367430 crate :: tests:: init ( ) ;
0 commit comments