如何根据距离矩阵确定节点?
How to determine nodes based on distances matrix?
假设我有一个给定的:
A -> B10
A -> C5
A -> D7.5
B -> C12
B -> D17
C -> D5
然后,我收到一个未排序的输入,如下所示:
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
我必须确定(对于任何类型的输入 - 任何顺序(哪个节点(K,L,M & N(实际上是A,B,C和D。
对于上面的示例输入,这里的情况是A is L
,B is K
,C is M
&D is N
。
所以我已经开始了一些事情,但我仍然不确定如何继续。下面为我提供了一个 std::map 输入的哪一行,是给定的行。但是,即使我知道存在这种组合,我也不确定如何知道未知(城市的顺序(。有人可以帮我对输入进行排序以匹配给定的输入吗?
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
bool checkForSimilar(vector<double> Vec1, vector<double> Vec2)
{
std::sort(Vec1.begin(), Vec1.end());
std::sort(Vec2.begin(), Vec2.end());
return std::equal(Vec1.begin(), Vec1.end(), Vec1.begin(), Vec2.end());
}
int main()
{
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 }};
std::map<int, int> RowMatches;
for (int i = 0; i < InputDistances.size(); i++)
{
for (int j = 0; j < InputDistances[i].size(); j++)
{
// check if current row is any combination if GivenDistances
if (checkForSimilar(InputDistances[i], GivenDistances[j]))
{
RowMatches[i] = j;
}
}
}
// How to order then them??
int pause;
cin >> pause;
return 0;
}
一个解决问题的函数:
/** 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 );
}
此处提供了用于构建演示应用程序的代码。
相关文章:
- 如何递归删除Qt3DWindow根实体中的所有节点?
- 计算路径根到节点的总和等于一定数字的数量
- 想要以最新的添加节点完整返回树节点的根
- 插入功能保持重新创建根节点
- 交换树数据结构中根的 2 个子节点指针
- 根节点应具有分配的节点,但仍为 NULL。为什么我无法将节点分配给根?
- 具有最大不同节点的根到叶路径
- 检查节点是否是二进制搜索树的根
- 为什么我的原始二叉搜索树被修改,即使我在另一个节点中复制了根节点
- 二进制搜索树中的根节点为空
- BST 删除/删除节点 - 根
- C++二叉树,修改根节点,指针到指针到根节点
- 二叉搜索树,如何将节点连接到根
- 是被视为列表一部分的单链列表的根节点
- 迭代地向二叉树中添加一个新节点,根被覆盖
- 递归函数插入二叉搜索树无法按根访问节点
- 二叉搜索树插入c++根在当前节点
- 删除二叉搜索树的根节点
- 将根节点替换为Document
- 树类根节点未更新