knn计算方法 一条线_计算同一条线上的最大点

knn计算方法 一条线

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

Problem statement:

问题陈述:

From a given set of input points find a maximum no of points on the same line.

从给定的一组输入点中找到同一条线上的最大点数。

Example:

例:

Points are represented as a list of lists where the inner list is [x, y] representing a single point.

点表示为列表列表,其中内部列表为[x,y],表示单个点。

Input list:
[1,1]   ->  1st point
[3,3]   ->  2nd point
[-1,-1] ->  3rd point
[4, 4]  ->  4th point
[5, 6]  ->  5th point
[7,4]   ->  6th point

The graph plotted is like below,

绘制的图形如下所示,

Count maximum points on same line

From the above input images, it’s visible that max no of point in the same line is 4. As there is 4 point in one line and the other line there are 2 points. So maximum points on the same line are 4.

从上面的输入图像中,可以看到同一行中的最大点数为4。由于一行中有4点,另一行中有2点。 所以同一条线上的最大点是4。

Edge cases:

边缘情况:

The edges cases for the problem is:

该问题的极端情况是:

  1. In the inputs, there can be similar points which will be counted separately

    在输入中,可以有相似的点,这些点将分别计算

Solution:

解:

To find whether three or more points are on the same line or not we need gradient (m) to check.

为了确定三个或更多点是否在同一条线上,我们需要进行梯度( m )检查。

If two points are [x1,y1] and [x2,y2] then the gradient, m is calculated as: m=(y1-y2)/(x1-x2) or 1/m=(x1-x2)/(y1-y2)/

如果两个点分别为[x1,y1][x2,y2],则梯度m的计算公式为: m =(y1-y2)/(x1-x2)或1 / m =(x1-x2)/(y1 -y2)/

Now, m can be 0 when y1=y2 and can be infinity when x1=x2. While computing we need to take care of the infinity case as well.

现在,当y1 = y2m可以为0,而当x1 = x2m可以为无穷大。 在计算时,我们还需要注意无穷大的情况。

So, the basic idea is, take two points as reference points for the line initially and check with other points how many points will fall on that line, i.e., will have the same gradient.
But this will take O(n^3) as we will require three loops to runs. Outer two loops will for two reference point selection and the inner one will be for other points selection.

因此,基本思想是,最初将两个点作为该线的参考点,然后与其他点一起检查该线上有多少个点,即具有相同的坡度。
但这需要O(n ^ 3),因为我们需要运行三个循环。 外两个循环用于选择两个参考点,内循环用于选择其他点。

Hashing can improve the complexity of keeping the logic the same. So what will be our hash function and how would we construct our hash table.

散列可以提高保持逻辑不变的复杂性。 那么我们的哈希函数将是什么,以及我们将如何构建哈希表。

The idea is, we will have one point as reference and we will compute gradient with all the other points  and store the gradient in our hash table and ill increment the same hash values.

这个想法是,我们将有一个点作为参考,我们将与所有其他点一起计算梯度并将梯度存储在哈希表中,并增加相同的哈希值。

So the hashing function, h(point)=gradient(x, reference point).

因此,哈希函数h(point)= gradient(x,参考点)

So it will have complexity only O(n^2) as the outer loop will be to choose the reference point and the inner loop to pick other points to compute the gradient. After completion of each inner loop, we will get the number of points falling in the same line with the reference point.

因此它仅具有O(n ^ 2)的复杂度,因为外部回路将选择参考点,而内部回路将选择其他点以计算梯度。 每个内部循环完成后,我们将获得与参考点在同一行中的点数。

Finally, we will keep updating our maximum number of points after each outer loop iteration.

最后,我们将在每次外循环迭代后继续更新最大点数。

How to handle edge case of repeating points?

如何处理重复点的边缘情况?

As mentioned earlier we may have the same points in the input list and both need to be considered separately. If we compute gradient we will find 0/0 which is indeterminate. So we need to keep a check for whether the other point is the same or not. If the same increment the count and finally add the count in max no of points for the reference point.

如前所述,我们在输入列表中可能有相同的观点,并且都需要分别考虑。 如果我们计算梯度,我们将发现0/0是不确定的。 因此,我们需要检查其他点是否相同。 如果相同,则增加计数,最后将计数加到最大参考点数之内。

Check the detailed algorithm to understand fully and check the code to get the implementation.

检查详细算法以充分理解并检查代码以获取实现。

Algorithm:

算法:

Input array of points=points
1.	Set max_point=0
2.	For each point o taking as reference point:
		Initialize an hash table to store the gradients
		Set temp_max_point =0
		Set count=1 which will track same no of points 
        for other points(edge case handling)
		For each other point p
			If p is not same as o
			   
			Find gradient m b/w p and o
			Add gradient to the hash table like below
				mymap[m]++;
			update temp_max_point=max(temp_max_point,mymap[m]);
				
			else if p and o are same point
				increment count
			end if-else
		end for
		update max_point=max(max_point,count+ temp_max_point)
        (consider the same points too)
		
	end for
max_point has the final result

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

//to find gradient
long double slope(vector<int> a, vector<int> b)
{
    //infinity case
    if (a[0] == b[0])
        return DBL_MAX;
    return (long double)((long double)(a[1] - b[1]) / (long double)(a[0] - b[0]));
}

//whether two points are same or not
bool same(vector<int> a, vector<int> b)
{
    if (a[0] == b[0] && a[1] == b[1])
        return true;
    return false;
}

//function to find maximum points in the same line
int maxPoints(vector<vector<int> >& points)
{
    if (points.size() < 2)
        return points.size();
    int maxl = 0;
    int n = points.size();
    for (int i = 0; i < n; i++) {
        map<long double, int> mymap;
        int temp = 0;
        int count = 1;
        for (int j = 0; j < n; j++) {
            if (j != i && !same(points[i], points[j])) {
                long double p;
                p = slope(points[i], points[j]);
                //cout<<p<<endl;
                mymap[p]++;
                temp = max(temp, mymap[p]);
                //cout<<temp<<endl;
            }
            else if (j != i && same(points[i], points[j])) {
                count++;
            }
        }
        maxl = max(maxl, count + temp);
    }

    return maxl;
}

int main()
{
    int n;

    cout << "Enter number of points\n";
    cin >> n;

    vector<vector<int> > arr;
    cout << "Enter points as <x,y> pair\n";
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        arr.push_back(vector<int>{ x, y });
    }

    cout << "maximum no of points on the same line: " << maxPoints(arr) << endl;
    return 0;
}

Output:

输出:

Enter number of points
6
Enter points as 
   
   
    
     pair
1 1
3 3
5 6
7 4
4 4
-1 -1
maximum no of points on the same line: 4

   
   


Also tagged in: Amazon

还标记在: 亚马逊

翻译自: https://www.includehelp.com/data-structure-tutorial/count-maximum-points-on-same-line.aspx

knn计算方法 一条线

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值