コード例 #1
0
float ClosestPair(Point* pX, int n){
	if(n <= 3) return bruteForce(pX, n);
	
	//Chia mang thanh hai phan bang nhau
	int mid = n/2;
	Point midPoint = pX[mid];

	float dL = ClosestPair(pX, mid);
	printf("ClosestPairL %f\n", dL);
	float dR = ClosestPair(pX + mid,n-mid);
	printf("ClosestPairR %f\n", dR);
	
	float d = min(dL, dR);
	printf("ClosestPair %f\n", d);

	Point* pLR = (Point*) malloc (n * sizeof(Point));
	int k = 0;
	for (int i = 0; i < n; ++i){
		if (abs(pX[i].x - midPoint.x) <= d){
			pLR[k] = pX[i];
			k++;
		}
	}
	if (dmin > min(stripClosest(pLR, k, d), d)){
		dmin = min(stripClosest(pLR, k, d), d);
	}
	return min(stripClosest(pLR, k, d), d);;
}
コード例 #2
0
double ClosestPair(int l, int r, double *dis) {
	if(l < r) {
		if(l == r-1) {
			return (D[l].x-D[r].x)*(D[l].x-D[r].x) + (D[l].y-D[r].y)*(D[l].y-D[r].y);
		}
		int m = (l+r)>>1;
		*dis = Min(*dis, ClosestPair(l, m, dis));
		*dis = Min(*dis, ClosestPair(m+1, r, dis));
		*dis = Min(*dis, Merge(l, m, r, *dis));
		return *dis;
	}
	return oo;
}
コード例 #3
0
ファイル: closestpair.cpp プロジェクト: Kolkir/algo
double ClosestPair(std::vector<Point>::const_iterator start,
                   std::vector<Point>::const_iterator end,
                   size_t& count,
                   Point& p1, Point& p2)
{
    auto len = std::distance(start, end);
    if (len <= 3)
    {
        return ClosestPairB(start, end, count, p1, p2);
    }
    else
    {
        auto splitPos = len / 2;
        auto l = start;
        auto r = start;
        std::advance(r, splitPos);

        Point l1, l2, r1, r2;
        double dl = ClosestPair(l, r, count, l1, l2);
        double dr = ClosestPair(r, end, count, r1, r2);

        double minD = std::min(dl, dr);
        if (minD == dl)
        {
            p1 = l1;
            p2 = l2;
        }
        else
        {
            p1 = r1;
            p2 = r2;
        }

        auto middleX = (std::prev(r)->x + r->x) / 2;
        l = std::lower_bound(start, end, Point{ middleX - minD, 0 });
        r = std::upper_bound(start, end, Point{ middleX + minD, 0 });

        auto d = ClosestPairB(l, r, count, l1, l2);

        if (d < minD)
        {
            minD = d;
            p1 = l1;
            p2 = l2;
        }
        return minD;
    }
}
コード例 #4
0
ファイル: closestpair.cpp プロジェクト: Kolkir/algo
void closestpair_test()
{
    std::vector<Point> m = { { 2,  7  },
                             { 4,  13 },
                             { 5,  7  },
                             { 10, 5  },
                             { 13, 9  },
                             { 15, 5  },
                             { 17, 7  },
                             { 19, 10 },
                             { 22, 7  },
                             { 25, 10 },
                             { 29, 14 },
                             { 30, 2  } };
    
    size_t count = 0;

    Point p1, p2;
    double minD = ClosestPairB(m.begin(), m.end(), count, p1, p2);

    std::cout << "Bruteforce: " << count << std::endl;
    std::cout << "Min = " << minD << std::endl;

    std::sort(m.begin(), m.end());

    Point p3, p4;
    count = 0;
    minD = 0;

    minD = ClosestPair(m.begin(), m.end(), count, p3, p4);
    
    std::cout << "Divide: " << count << std::endl;
    std::cout << "Min = " << minD << std::endl;
}
コード例 #5
0
int main() {
	int N, i, x, y;
	ReadInt(&N);
    for(i = 0; i < N; i++) {
        ReadInt(&x), ReadInt(&y);
        D[i].x = x, D[i].y = y;
    }
    double distance = oo;
    distance = ClosestPair(0, N-1, &distance);
    distance = sqrt(distance);
    printf("%.4lf\n", distance);
    return 0;
}
コード例 #6
0
int main(int argc, char const *argv[]){
	readFile();
	Point* pX = (Point*) malloc(count * sizeof(Point));
	mergeSort(p,0,count-1, 1);
	printPoint(p);
	mergeSort(p,0,count-1, 0);
	setPoint(p, pX);
	printf("Co %d diem tren mat phang.\n", count);
	printPoint(p);
//	Timer ti;
//	bruteForce(p, count);
//	double y = ti.getElapsedTime();
//	printf("Thoi gian chay: %f\n",y);
//	printf("Hai diem gan nhau nhat la:\nA(%d, %d), B(%d, %d)\n",a.x,a.y,b.x,b.y);
	float min = ClosestPair(pX, count);
	printf("Khoang cach gan nhat la: %f\n", min);
	printf("Hai diem gan nhau nhat la:\nA(%d, %d), B(%d, %d)\n",a.x,a.y,b.x,b.y);
	
	return 0;
}