/** Solve problem posed in https://stackoverflow.com/q/52046650/16582
Search for a permuted column in the input matrix
which matches each given column
@param[out] assign the first node assignment which creates a match
@param[in] distance the distances between nodes, given and input
Mean time to find match ( milliseconds )
<pre>
Cities Search1 Search2
10 20 0.01
100 ??? 4
</pre2>
*/
void Find(
cNodeAssign& assign,
cNodeDistance& distance )
{
raven::set::cRunWatch R("Search");
assign.Clear();
// loop over rows in given distances
for( int given = 0; given < distance.Size(); given++ )
{
// loop over rows in input distances
for( int input = 0; input < distance.Size(); input++ )
{
// check if the input row has already been assigned
if( assign.Find( input ) )
continue;
// check if row and column are permutations of each other
if( distance.IsPermutation( given, input ))
{
// found a match
assign.Add( input );
// no need to search further for this row
break;
}
}
}
}
int main()
{
cout << "Original Problem:\n";
vector<vector<double>> GivenDistances = // A B C D
{
/*A*/ {0, 10, 5, 7.5},
/*B*/ {10, 0, 12, 17 },
/*C*/ {5, 12, 0, 5 },
/*D*/ {7.5,17, 5, 0 }
};
vector<vector<double>> InputDistances = // K L M N
{
/*K*/{ 0, 10, 12, 17 },
/*L*/{ 10, 0, 5, 7.5},
/*M*/{ 12, 5, 0, 5 },
/*N*/{ 17, 7.5,5, 0 }
};
cNodeDistance dop( GivenDistances, InputDistances );
cNodeAssign assign( dop.Size() );
dop.Display();
Find( assign, dop );
assign.Display();
Demo( 4 );
Demo( 10 );
Timer( 10 );
Timer( 100 );
}
available here