Java 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)

目录

[使用三元搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)](#使用三元搜索——时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1))

示例代码

[使用黄金分割搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)](#使用黄金分割搜索——时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1))

示例代码


给定一个大小为 2 * n 的点集 points \[\]\[\] ,其中每个点表示为 p, q,以及一条 由方程 ax + by + c = 0 表示的直线L\[\] 。

在直线L\[\]上找到一点 ,使得该点到所有给定点的欧氏距离之和最小。返回最小可能的距离之和,保留两位小数。

例如:

输入: n = 5,L\[\] = 1, -1, -3,points\[\]\[\] = \[-3, 2, -1, 0, -1, 2, 1, 2, 3, 4]输出: 20.77

说明:在给定的图中,x - y - 3 = 0 直线上的点的最佳位置是 (2, -1),它与其他点的总距离为 20.77,这是可获得的最小总距离。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

输入: n = 3,L\[\] = 2, 1, 4,points\[\]\[\] = \[-1, 2, 1, 3, 2, 4]

输出: 11.20

说明: L\[\] 表示的直线为 2x + y + 4 = 0。该直线上的最优解约为 (-2.64, 1.27)。该最优解到所有给定点的欧氏距离之和约为 11.20,这是可能的最小值。

使用三元搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)

因此,直接线性搜索并不合适,因为我们需要沿着直线采样许多点,而且没有固定的步长可以保证找到精确的最小值。

相反,我们可以使用单个变量 t 对线上的每个点进行参数化。这样就将原来的二维优化问题转化为一维问题。

如果 (x0, y0) 是直线上的任意一点,(dx, dy) 是沿直线方向的单位向量,则直线上的每一点都可以表示为:

对于给定的 t 值,我们计算从该点到所有给定点的距离之和。

所得函数是凸函数(单峰函数),这意味着它先递减直至达到最小值,然后递增。因此,我们无需检查所有可能的 t 值,即可使用三元搜索高效地找到最小值。

1、取直线 ax + by + c = 0,并找到该直线上的任意一点 (x0, y0)。

2、使用 (b, -a) 作为直线的方向向量,并将其归一化为单位向量 (dirX, dirY)。

3、将所有给定的点投影到线上,以确定参数 t 的搜索范围 低,高。

4、由于欧氏距离之和是凸函数,因此对 low, high 应用三元搜索。

5、对于每次迭代,计算 mid1 和 mid2 之间的总距离,并舍弃不能包含最小值的三分之一。

6、收敛后,取最终范围的中点,将其转换回 (x, y),并返回距离之和最小值。

示例代码

import java.util.*;

class GFG {

static double getDistanceSum(int\[\]\[\] points, double x,

double y)

{

double sum = 0.0;

for (int\[\] point : points) {

double dx = x - point0;

double dy = y - point1;

sum += Math.sqrt(dx * dx + dy * dy);

}

return sum;

}

static double findOptimumCost(int\[\] L, int\[\]\[\] points)

{

double a = L0;

double b = L1;

double c = L2;

// Find any point (x0, y0) lying on the line:

// ax + by + c = 0

double x0 = 0.0, y0 = 0.0;

if (b != 0)

y0 = -c / b;

else

x0 = -c / a;

// (b, -a) is a direction vector parallel to the

// line. Normalize it to get a unit direction

// vector.

double len = Math.sqrt(a * a + b * b);

double dirX = b / len;

double dirY = -a / len;

// We represent every point on the line as:

//

// (x, y) = (x0 + t * dirX, y0 + t * dirY)

//

// Find the range of t values corresponding to the

// projections of all given points onto the line.

double low = 1e18;

double high = -1e18;

for (int\[\] point : points) {

// Projection of (point - (x0, y0)) onto

// the unit direction vector.

double t = (point0 - x0) * dirX

  • (point1 - y0) * dirY;

low = Math.min(low, t);

high = Math.max(high, t);

}

// The sum-of-distances function is convex, so

// ternary search can be used to find its minimum.

double eps = 1e-7;

while (high - low > eps) {

// Divide the current range into three parts.

double mid1 = low + (high - low) / 3.0;

double mid2 = high - (high - low) / 3.0;

// Point corresponding to mid1.

double x1 = x0 + mid1 * dirX;

double y1 = y0 + mid1 * dirY;

// Point corresponding to mid2.

double x2 = x0 + mid2 * dirX;

double y2 = y0 + mid2 * dirY;

// Calculate the total distance at both points.

double cost1 = getDistanceSum(points, x1, y1);

double cost2 = getDistanceSum(points, x2, y2);

// If cost1 is smaller, the minimum lies

// in the left two-thirds.

if (cost1 < cost2) {

high = mid2;

}

// Otherwise, the minimum lies in the

// right two-thirds.

else {

low = mid1;

}

}

// Take the middle of the final range as the

// approximate optimal value of t.

double t = (low + high) / 2.0;

// Convert t back to the corresponding point on the

// line.

double x = x0 + t * dirX;

double y = y0 + t * dirY;

// Return the minimum possible sum of distances.

return getDistanceSum(points, x, y);

}

public static void main(String\[\] args)

{

int\[\] L = { 1, -1, -3 };

int\[\]\[\] points = { { -3, -2 },

{ -1, 0 },

{ -1, 2 },

{ 1, 2 },

{ 3, 4 } };

double ans = findOptimumCost(L, points);

System.out.printf("%.2f%n", ans);

}

}

输出

20.77

使用黄金分割搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)

三元搜索每次迭代都会检查两个新点,因此它会计算两次距离和。黄金分割搜索通过使用黄金分割率选择点来改进这一点。

这样,缩小范围后,就可以重复使用之前的某个点。因此,在每次迭代中,我们只需计算一个新点的距离和,从而在找到凸函数最小值的同时,提高了搜索效率。

1、找到直线上的一点(x0,y0),并求出单位方向向量(dirX,dirY)。

2、将所有给定的点投影到线上,以确定搜索范围低,高。

3、利用黄金分割率将范围分成两个点 x1 和 x2。

4、计算这两点之间的距离之和。

5、比较成本并缩短时间间隔;尽可能重复使用先前计算的成本。

6、继续进行,直到区间足够小,然后评估中点以获得最小成本。

示例代码

import java.util.*;

class GFG {

static double getDistanceSum(int\[\]\[\] points, double x, double y)

{

double sum = 0.0;

for (int\[\] point : points) {

double dx = x - point0;

double dy = y - point1;

sum += Math.sqrt(dx * dx + dy * dy);

}

return sum;

}

static double findOptimumCost(int\[\] L, int\[\]\[\] points)

{

double a = L0;

double b = L1;

double c = L2;

// Find any point (x0, y0) lying on the line:

// ax + by + c = 0

double x0 = 0.0;

double y0 = 0.0;

if (b != 0)

y0 = -c / b;

else

x0 = -c / a;

// (b, -a) is a direction vector parallel to the

// line. Normalize it to get a unit direction

// vector.

double len = Math.sqrt(a * a + b * b);

double dirX = b / len;

double dirY = -a / len;

// We represent every point on the line as:

//

// (x, y) = (x0 + t * dirX, y0 + t * dirY)

//

// Find the range of t values corresponding to the

// projections of all given points onto the line.

double low = 1e18;

double high = -1e18;

for (int\[\] point : points) {

// Projection of (point - (x0, y0)) onto

// the unit direction vector.

double t = (point0 - x0) * dirX

  • (point1 - y0) * dirY;

low = Math.min(low, t);

high = Math.max(high, t);

}

// The sum-of-distances function is convex, so

// Golden Section Search can be used to find its

// minimum.

// Golden ratio value.

double phi = (Math.sqrt(5.0) - 1.0) / 2.0;

double eps = 1e-7;

// Initial two points using the golden ratio.

double mid1 = high - phi * (high - low);

double mid2 = low + phi * (high - low);

// Calculate the total distance at both points.

double cost1 = getDistanceSum(

points, x0 + mid1 * dirX, y0 + mid1 * dirY);

double cost2 = getDistanceSum(

points, x0 + mid2 * dirX, y0 + mid2 * dirY);

while (high - low > eps) {

// If cost1 is smaller, the minimum lies

// in the left part of the range.

if (cost1 < cost2) {

high = mid2;

// Reuse the previous point and its cost.

mid2 = mid1;

cost2 = cost1;

// Calculate only one new point.

mid1 = high - phi * (high - low);

cost1 = getDistanceSum(points,

x0 + mid1 * dirX,

y0 + mid1 * dirY);

}

// Otherwise, the minimum lies in the

// right part of the range.

else {

low = mid1;

// Reuse the previous point and its cost.

mid1 = mid2;

cost1 = cost2;

// Calculate only one new point.

mid2 = low + phi * (high - low);

cost2 = getDistanceSum(points,

x0 + mid2 * dirX,

y0 + mid2 * dirY);

}

}

// Take the middle of the final range as the

// approximate optimal value of t.

double t = (low + high) / 2.0;

// Convert t back to the corresponding point on the

// line.

double x = x0 + t * dirX;

double y = y0 + t * dirY;

// Return the minimum possible sum of distances.

return getDistanceSum(points, x, y);

}

public static void main(String\[\] args)

{

int\[\] L = { 1, -1, -3 };

int\[\]\[\] points = { { -3, -2 },

{ -1, 0 },

{ -1, 2 },

{ 1, 2 },

{ 3, 4 } };

double ans = findOptimumCost(L, points);

System.out.printf("%.2f%n", ans);

}

}

输出

20.77

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
源码_空间13 分钟前
基于Springboot的在线编程练习判题平台(源码+文档)
java·spring boot·后端
专注仿真1 小时前
仿真智能体训练(评估指标篇):任务完成率算法详解与实战
人工智能·算法·机器学习·afsim智能体·仿真训练·afsim智能体训练·任务完成率算法
小手cool2 小时前
二分查找:在有序数组中查找最接近 target 的元素索引
java·数据结构·算法
zmzb01032 小时前
C++课后习题训练记录Day216
数据结构·c++·算法
李白的手机2 小时前
深入理解 Spring @RefreshScope:从入门用法到源码原理,再到举一反三实战扩展
java·后端·spring
深入云栈2 小时前
Netty 4.2.x 源码深度解析 (二十五):PoolThreadCache 与引用计数 —— 线程缓存与内存泄漏检测
java·后端·架构
INGNIGHT2 小时前
leetcode尊享100题
算法·哈希算法
砚底藏山河2 小时前
量化行情横截面中性化闭式与回归实现
android·java·python·金融·kotlin
Helix2503 小时前
JDK单元测试实战:JUnit 5与Mockito注解、断言、参数化测试、Mock注入完整指南
java·junit·单元测试·junit 5·mockito·mockmvc·参数化测试