@@ -8,6 +8,59 @@ namespace PSGraph.Tests
88{
99 public class DsmPartitioningTests
1010 {
11+
12+ private Matrix < float > ReorderMatrix ( Matrix < float > original , Dictionary < PSVertex , int > actualIndex , string [ ] expectedOrder )
13+ {
14+ int size = expectedOrder . Length ;
15+ var reordered = Matrix < float > . Build . Dense ( size , size ) ;
16+
17+ for ( int i = 0 ; i < size ; i ++ )
18+ {
19+ var rowVertex = new PSVertex ( expectedOrder [ i ] ) ;
20+ int sourceRow = actualIndex [ rowVertex ] ;
21+
22+ for ( int j = 0 ; j < size ; j ++ )
23+ {
24+ var colVertex = new PSVertex ( expectedOrder [ j ] ) ;
25+ int sourceCol = actualIndex [ colVertex ] ;
26+
27+ reordered [ i , j ] = original [ sourceRow , sourceCol ] ;
28+ }
29+ }
30+
31+ return reordered ;
32+ }
33+
34+ private void AssertVertexGroupsAreClustered ( Dictionary < PSVertex , int > rowIndex , List < List < PSVertex > > expectedGroups )
35+ {
36+ var allIndices = new Dictionary < string , int > ( ) ;
37+ foreach ( var kvp in rowIndex )
38+ allIndices [ kvp . Key . Name ] = kvp . Value ;
39+
40+ for ( int groupIdx = 0 ; groupIdx < expectedGroups . Count ; groupIdx ++ )
41+ {
42+ var group = expectedGroups [ groupIdx ] ;
43+ var indices = group . Select ( v => allIndices [ v . Label ] ) . OrderBy ( i => i ) . ToList ( ) ;
44+
45+ // Все индексы должны быть рядом
46+ for ( int i = 1 ; i < indices . Count ; i ++ )
47+ {
48+ ( indices [ i ] - indices [ i - 1 ] ) . Should ( ) . BeLessThanOrEqualTo ( 1 ,
49+ $ "vertices in group { groupIdx } ({ string . Join ( "," , group ) } ) should be adjacent") ;
50+ }
51+
52+ // Между группами индексы должны быть дальше
53+ for ( int j = groupIdx + 1 ; j < expectedGroups . Count ; j ++ )
54+ {
55+ var otherGroup = expectedGroups [ j ] ;
56+ var otherIndices = otherGroup . Select ( v => allIndices [ v . Label ] ) . OrderBy ( i => i ) . ToList ( ) ;
57+
58+ indices . Last ( ) . Should ( ) . BeLessThan ( otherIndices . First ( ) ,
59+ $ "group { groupIdx } should come before group { j } ") ;
60+ }
61+ }
62+ }
63+
1164 [ Fact ]
1265 public void BasicPartitioning_ShouldPartitionCorrectly ( )
1366 {
@@ -16,11 +69,12 @@ public void BasicPartitioning_ShouldPartitionCorrectly()
1669
1770 var algo = new DsmClassicPartitioningAlgorithm ( dsm ) ;
1871 var result = algo . Partition ( ) ;
19-
72+
2073 result . Should ( ) . NotBeNull ( "Partitioning result should not be null" ) ;
21-
74+
75+
2276 // Expected DSM after partitioning
23- float [ , ] expectedMatrix = {
77+ float [ , ] expectedMatrix = {
2478 { 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
2579 { 0 , 0 , 0 , 1 , 0 , 0 , 0 } ,
2680 { 0 , 1 , 0 , 0 , 0 , 0 , 0 } ,
@@ -30,25 +84,47 @@ public void BasicPartitioning_ShouldPartitionCorrectly()
3084 { 1 , 0 , 0 , 0 , 0 , 1 , 0 }
3185 } ;
3286
87+ // Expected row/column order after partitioning
88+ string [ ] expectedOrder = { "F" , "B" , "D" , "G" , "A" , "C" , "E" } ;
89+
90+ // Задаем ожидаемые группы
91+ var expectedGroups = new List < List < PSVertex > >
92+ {
93+ new ( ) { new PSVertex ( "F" ) , new PSVertex ( "E" ) } ,
94+ new ( ) { new PSVertex ( "B" ) , new PSVertex ( "D" ) , new PSVertex ( "G" ) } ,
95+ new ( ) { new PSVertex ( "A" ) , new PSVertex ( "C" ) }
96+ } ;
97+
98+ Console . WriteLine ( "Row indices:" ) ;
99+ foreach ( var kvp in result . RowIndex . OrderBy ( k => k . Value ) )
100+ {
101+ Console . WriteLine ( $ "{ kvp . Key . Name } => { kvp . Value } ") ;
102+ }
103+
104+ // Проверим, что группы упорядочены и внутри сжаты
105+ AssertVertexGroupsAreClustered ( result . RowIndex , expectedGroups ) ;
106+
107+ var reorderedActual = ReorderMatrix ( result . DsmMatrixView , result . RowIndex , expectedOrder ) ;
108+
33109 // Create expected matrix using MathNet
34110 var targetMatrix = Matrix < Single > . Build . DenseOfArray ( expectedMatrix ) ;
35111
36112 // Check if the partitioned DSM matrix matches the expected matrix
37- result . DsmMatrixView . Should ( ) . BeEquivalentTo ( targetMatrix , "The partitioned DSM matrix should match the expected matrix" ) ;
113+ reorderedActual . Should ( ) . BeEquivalentTo ( targetMatrix , "The partitioned DSM matrix should match the expected matrix" ) ;
38114
39- // Expected row/column order after partitioning
40- string [ ] expectedOrder = { "F" , "B" , "D" , "G" , "A" , "C" , "E" } ;
41115
42116 // Validate row and column indices match the expected vertex order
43117 for ( int idx = 0 ; idx < expectedOrder . Length ; idx ++ )
44118 {
45119 var vertex = new PSVertex ( expectedOrder [ idx ] ) ;
46120
47121 result . RowIndex . Should ( ) . ContainKey ( vertex ) ;
48- result . RowIndex [ vertex ] . Should ( ) . Be ( idx , $ "Row index for vertex { vertex . Name } should be { idx } ") ;
49-
50122 result . ColIndex . Should ( ) . ContainKey ( vertex ) ;
51- result . ColIndex [ vertex ] . Should ( ) . Be ( idx , $ "Column index for vertex { vertex . Name } should be { idx } ") ;
123+ }
124+
125+ for ( int i = 0 ; i < reorderedActual . RowCount ; i ++ )
126+ {
127+ reorderedActual [ i , i ] . Should ( ) . Be ( 0 , $ "self-dependency at index { i } should be zero") ;
52128 }
53129 }
54130 }
0 commit comments