n维中最接近对的错误
errors in closest pair in n dimension
我试着写出n维中最接近的一对,我使用分治法。首先我把它分成左右两部分,分别找到它们的最短路径然后我要找到它们之间的距离,当我这样做的时候,就会产生误差。我已经试过了,但是找不到错误,我的方法哪里出错了。
请帮我找找看!谢谢。
#include <vector>
#include <cmath>
#include <cfloat>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> ClosestPair(const vector< vector<double> >& points){
vector<int> closest_pair;
closest_pair.resize(2);
closest_pair[0] = 0;
closest_pair[1] = 1;
return closest_pair;
}
//utility function to calculate two point distance
double dist(vector<double> &p1, vector<double> &p2){
double sum = 0;
//n dimension point
for(int i = 0; i < p1.size(); i ++){
//cout << "i:" << i << endl;
//cout << "1:" << p1[i] << ",2:" << p2[i] << endl;
sum += (p1[i] - p2[i]) * (p1[i] - p2[i]);
}
//cout << "dist:" << sqrt(sum) << endl;
return sqrt(sum);
}
//direct calculate the distance
double bruteForce(vector<vector<double> > &p, int min_d, int max_d, int & a, int & b){
//set sum to the max_d value of double to test minimum
double sum = DBL_MAX;
//compare each distance
for(int i = min_d; i < max_d; i ++){
for(int j = i + 1; j < max_d + 1; j ++){
if(dist(p[i], p[j]) < sum){
a = i;
b = j;
sum = dist(p[i], p[j]);
}
}
}
cout << "brute:" << sum << endl;
return sum;
}
double closest(vector<vector<double> > &p, int min_d, int max_d, int& a, int& b){
//terminal condition
if(max_d - min_d <= 3){
//cout << "QQ" << endl;
return bruteForce(p, min_d, max_d, a, b);
}
//divide and conquer
else{
int a1 = 0, b1 = 0;
//cout << "haha" << endl;
int median = (max_d - min_d) / 2;
double left = closest(p, min_d, median, a1, b1);
double right = closest(p, median + 1, max_d, a, b);
if(left < right){
a = a1;
b = b1;
}
//minimun of left and right
double d = min(left, right);
//record index
int first = 0;
int last = 0;
int count = 0;
vector<vector<double> > temp;
for(int i = min_d; i < max_d; i ++){
//== d already exist
if(abs(p[i][0] - p[median][0]) < d){
if(count == 0)first = i;
count ++;
//cout << "inside:" << i << "count:" << count << endl;
temp.push_back(p[i]);
}
}
last = first + count - 1;
//cout << "first" << first << "last" << last << endl;
return min(d, closest(temp, first, last, a, b));
}
}
int main(){
int a = 0, b = 0;
//3 dimension
vector<vector<double> >test(7,vector<double>(3));
//(10,20,30)
test[0][0]= 10;
test[0][1]= 20;
test[0][2]= 30;
//(1,2,3)
test[1][0]= 1;
test[1][1]= 2;
test[1][2]= 3;
//(1000,2000,3000)
test[2][0]= 1000;
test[2][1]= 2000;
test[2][2]= 3000;
//(100,200,300)
test[3][0]= 100;
test[3][1]= 200;
test[3][2]= 300;
//(100,200,300)
test[4][0]= 100;
test[4][1]= 200;
test[4][2]= 301;
//(100,200,300)
test[5][1]= 200000;
test[5][0]= 100000;
test[5][2]= 300000;
//(100,200,300)
test[6][0]= 1000000;
test[6][1]= 2000000;
test[6][2]= 3000000;
//sort X
sort (test.begin(), test.end());
for (int i = 0; i < test.size(); i ++){
for(int j = 0; j < test[i].size(); j ++){
cout << test[i][j] << endl;
}
}
cout << "result:" << closest(test, 0, 6, a, b);
cout << "a" << a << "b" << b << endl;
return 0;
}
将temp设置为:
vector<vector<double> > temp(7,vector<double>(3));
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 查找索引的最接近的真实值
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- C++ lower_bound() 搜索最接近目标值的元素
- C++有效地找到向量中第一个最接近的匹配值?
- 在结构向量中查找最接近的值
- 在排序数组中搜索最接近的 int 值 <= 从右起给定值
- 最接近"std::atomic<std::vector>"是什么?
- C++中最接近 Lua 表操作的东西是什么?
- 在std::集中获取与给定元素最接近的元素
- 查找整数数组中最接近的数字
- Systemc - 将sc_time四舍五入到最接近的第 10 SC_NS
- 我是 c++ 的新手,有没有一种算法可以找到 3d 数组中最接近的 0 的距离?
- 我的程序不断四舍五入到最接近的整数
- 3 与错误最接近的总和:字符串常量之前的预期非限定 id
- 最接近Direct2D/DirectWrite的跨平台替代方案?
- 最接近的字符串:在字符串阵列中找到2个字符串之间的最小距离
- n维中最接近对的错误
- C++11:`错误:代码中')'标记之前应有主表达式',用于行扫描以查找最接近的对