STL:在大量数据中进行分类和搜索
STL: sorting and searching in enormous data
我有两组点(source
和target
(。目的是为每个source
点找到唯一1:1最近的邻居 target
中的点。
我的尝试是按预期工作,但慢慢慢。我已经测试了几千分,但是在实际情况下,将是数百万。我不是 stl 的专家。有什么建议我如何优化它?
std::vector<UT_Vector3> targetPosVector;
for (auto i = 0; i < targetNumPoints; i++)
{
auto pos = target->getPos3(i);
targetPosVector.push_back(pos);
}
std::vector<int> uniqueNeighborVector;
for (auto ptoff = 0; ptoff < sourceNumPoints; ptoff++)
{
std::vector<std::pair<int, fpreal>> nearpointVector; // neighbor vector in form of "(idx, dist)"
auto pos = source->getPos3(ptoff);
for (auto j = 0; j < targetNumPoints; j++)
{
fpreal dist = pos.distance(targetPosVector[j]);
std::pair<int, fpreal> neighbor {j, dist};
nearpointVector.push_back(neighbor);
}
std::sort(nearpointVector.begin(), nearpointVector.end(), [](const std::pair<int, fpreal> &left,
const std::pair<int, fpreal> &right)
{ return left.second < right.second; });
std::vector<int> neighborVector;
for (auto i : nearpointVector)
{
neighborVector.push_back(i.first);
}
// trying to imitate Python's next() function
// uniqueNeighborList[]
// uneighbor = next(i for i in neighborVector if i not in uniqueNeighborVector)
// uniqueNeighborVector = set(uniqueNeighborList.append(uneighbor))
for (auto i : neighborVector)
{
if (std::find(uniqueNeighborVector.begin(), uniqueNeighborVector.end(), i) == uniqueNeighborVector.end())
{
int uneighbor = i; // later on binds to the geometry attribute
uniqueNeighborVector.push_back(i);
break;
}
}
}
其中:
-
source
和target
是详细信息几何数据 -
distance
是计算两个向量之间距离的函数 -
getPos3
是获得3-float vector
点位置的函数 -
fpreal
aka64-bit float
-
UT_Vector3
是3-float vector
-
sourceNumPoints
和targetNumPoints
是source
和target
几何形状。
如评论中提到的,二次复杂性是在尝试以数百万点计算此点时的衰落。即使您优化了代码,如果方法不更改,则二次复杂性也将保持。
在我看来,这就像R3中的经典NN问题。一种众所周知的方法是使用k-d树,它们允许在O(n log n(构造时间和线性空间内使用O(log n(查询时间。可以通过各种库来寻求实现:nanoflann,kdtree(这些是从快速搜索中,我敢肯定也有包含k-d树的精心库。(
简短回顾:我将使用3-D树并将其构建在您的目标点集中。然后将每个源点(一一(拿到O(log n(时间的3-D树中的每个源点(| source | log | target |(和o(|(目标|(大小。
一个相关的问题。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 向量上的线性搜索
- 如何在动态数组上使用搜索函数
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- cmake:添加要搜索头文件的目录
- 使用C++创建特殊的二叉搜索树
- 在C++的字符串中搜索和删除某些字符
- std::unordered_map 搜索算法是如何实现的?
- 使用不变量来确定二分搜索中的边界条件
- 二叉搜索如何比线性搜索更快?
- STL:在大量数据中进行分类和搜索
- 如何使用二进制搜索将元素插入分类的向量
- 在C 中搜索和分类向量
- 深度优先搜索树边分类