找到最近的邻居K-NN

时间:2014-03-19 15:58:51

标签: java arrays algorithm

嘿伙计我在编写10个点的代码时遇到问题,为1到500之间的每个点分配一个距离,然后为每个点找到最近的两个邻居。到目前为止,我有两个类我的PointD类负责数学并分配邻居和我的主类运行程序并计算距离:

PointD Class:

    package points;
/**
 *
 * @Ashley Caldwell
 */
public class PointD {
    private final int x;
    private final String name;
    //Creates an int named x for the math and a string called name for the point

    public PointD(int x, String name) {
        this.x = x;
        this.name = name;
    }//Used to create the Point

    public int distanceTo(PointD other) {
        return Math.abs(other.x - this.x);
    }//Math used to work out the distance to the next point

    /* extensions for your 2 neighbors */
    private PointD neighbor1 = null;
    private PointD neighbor2 = null;

    public String toString() {
        return  name +" "+ "at " + x + ", "
             + "with neighbors: " + (neighbor1 != null? neighbor1.name + 
             " Distance(" + distanceTo(neighbor1) + "), " : "") 
             + (neighbor2 != null? neighbor2.name + 
             " Distance(" + distanceTo(neighbor2) + ")" : ""); 
    }// sets the values to string for both the points

    public void setNeighbors(PointD p1, PointD p2) {
        neighbor1 = p1;
        neighbor2 = p2;
        //Assigns the neighbours to the point
    }

}

主类:

package points;
/**
 *
 * @Ashley Caldwell
 */
import java.util.Random;
public class Main {
 // better into a seperate MathLibrary
    public static int distance(PointD p1, PointD p2) {
        return p1.distanceTo(p2);
    }

    public static void main(String[] args) {
        Random rng = new Random();
        //used to create distances for points
        int numberOfPoints = 10+1;
        // generate points
        PointD[] points = new PointD[numberOfPoints];

        for(int i = 0; i < numberOfPoints; ++i) {
            points[i] = new PointD(rng.nextInt(500) + 1, "Points " + i);

            //Loop assigns value between 1 and 500 to each point
        }

        for(int i = 0; i < numberOfPoints; ++i) {
            PointD currentPoint = points[i];
            PointD closestPoint = null;
            PointD secondClosestPoint = null;
            for(int j = 0; j < numberOfPoints; ++j) {
                if(i == j) continue;
                if(closestPoint == null || currentPoint.distanceTo(points[j]) < currentPoint.distanceTo(closestPoint)) {
                    secondClosestPoint = closestPoint;
                    closestPoint = points[j];
                } else if(secondClosestPoint == null || currentPoint.distanceTo(points[j]) < currentPoint.distanceTo(secondClosestPoint)) {
                    secondClosestPoint = points[j];
                }//Loop used to calculate the distance between each point by using current distance of point
            }
            currentPoint.setNeighbors(closestPoint, secondClosestPoint);
        }

        for(int i = 0; i < numberOfPoints; ++i)
        {
            System.out.println(points[i]);

        }

    }// Prints out the points and the strings
}

这是我跑步时的输出:

Points 0 at 80, with neighbors: Points 6 Distance(39), Points 3 Distance(40)
Points 1 at 212, with neighbors: Points 7 Distance(29), Points 9 Distance(64)
Points 2 at 308, with neighbors: Points 5 Distance(12), Points 8 Distance(19)
Points 3 at 40, with neighbors: Points 6 Distance(1), Points 0 Distance(40)
Points 4 at 489, with neighbors: Points 10 Distance(35), Points 8 Distance(162)
Points 5 at 296, with neighbors: Points 2 Distance(12), Points 9 Distance(20)
Points 6 at 41, with neighbors: Points 3 Distance(1), Points 0 Distance(39)
Points 7 at 241, with neighbors: Points 1 Distance(29), Points 9 Distance(35)
Points 8 at 327, with neighbors: Points 2 Distance(19), Points 5 Distance(31)
Points 9 at 276, with neighbors: Points 5 Distance(20), Points 2 Distance(32)
Points 10 at 454, with neighbors: Points 4 Distance(35), Points 8 Distance(127)

我遇到的主要问题是有些点使用相同的点超过两次,所以有些点没有连接,我需要尝试对其进行排序,以便它只找到两个最接近但尚未存在的点使用,我遇到的另一个问题是我希望我的Point 0始终设置为距离0,但不知道如何将此元素的值设置为零,因为它引用了另一个类,具有随机距离是不是必要的,如果我更容易改变设定值,那么我会这样做,但只是想知道是否有人有任何建议可以给他们?

0 个答案:

没有答案