繁体   English   中英

最近邻居算法将元素(城市)复制到输出数组java

[英]nearest neighbor algorithm copy element (city) to output array java

因此,到目前为止,我有一个程序可以读取以下格式的城市和距离的csv文件:

阿拉斯加里程表,安克雷奇,安德森,坎特韦尔,安克雷奇,0,284,210,安德森,284,0,74,坎特韦尔,210,74,0,

因此,该算法起作用并按照应使用最短邻居算法按照最短路径访问它们的顺序输出城市,而最近邻居算法始终以始发地为锚定城市或起始城市为起点。

使用此数据,该算法的示例输出为:1,3,2。 我用27个元素的图表进行了分析,结果也很好。 我正在使用这一小工具进行编写和调试。

理想情况下,我要查找的输出是城市名称和累积里程。

现在,我正在努力将城市划分为可以打印的城市。 这两个部分的帮助将不胜感激,或者也要记住,这也是最终目标。

我的想法是,最终我可能想创建一个{string,int}的数组

所以我的输出看起来像这样。

锚地0

坎特威尔210

安德森284

我能够将数组的第一个元素设置为1,但是无法获取新输出数组的第二个和第三个元素来更正

这是我有问题的代码:

public class TSPNearestNeighbor {

    private int numberOfNodes;
    private Stack<Integer> stack;

    public TSPNearestNeighbor()
    {
        stack = new Stack<>();
    }

     public void tsp(int adjacencyMatrix[][])
    {

        numberOfNodes = adjacencyMatrix[1].length;
//        System.out.print(numberOfNodes);


//        System.out.print(Arrays.deepToString(adjacencyMatrix));


        int[] visited = new int[numberOfNodes];
 //       System.out.print(Arrays.toString(visited));

        visited[1] = 1;
 //       System.out.print(Arrays.toString(visited));

        stack.push(1);
        int element, dst = 0, i;
        int min = Integer.MAX_VALUE;
        boolean minFlag = false;
        System.out.print(1 + "\n");

        //System.arraycopy(arr_cities, 0, arr_final, 0, 1); // Copies Anchorage to Pos 1 always

        //System.out.print(Arrays.deepToString(arr_final)+ "\n");

        while (!stack.isEmpty())
        {
            element = stack.peek();
            i = 1;
            min = Integer.MAX_VALUE;
            while (i <= numberOfNodes-1)
            {
                if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
                {
                    if (min > adjacencyMatrix[element][i])
                    {
                        min = adjacencyMatrix[element][i];
                        dst = i;
                        minFlag = true;

                    }
                }
                i++;
            }
            if (minFlag)
            {
                visited[dst] = 1;
                stack.push(dst);
                System.out.print(dst + "\n");

                minFlag = false;
                continue;
            }
            stack.pop();

        }
    }

我想间接地回答您的问题。 您通过使用对象数组使自己的生活变得艰难。 它们使代码难以阅读且难以访问。 如果您使用适当的方法创建一个City类来帮助您进行输出,事情将会变得更加容易。

例如:

class City {
    private final String name;
    private final Map<City,Integer> connections = new HashMap<>();

    public static addConnection(City from, City to, int distance) {
        from.connections.put(to, distance);
        to.connections.put(from, distance);
    }

    public int getDistanceTo(City other) {
        if (connections.containsKey(other))
            return connections.get(other);
        else
            throw new IllegalArgumentException("Non connection error");
    }
}

为了清楚起见,我省略了构造函数,getter和setter。

现在,输出路径变得相当简单:

public void outputPath(List<City> cities) {
    int cumulativeDistance = 0;
    City previous = null;
    for (City current: cities) {
        if (previous != null)
            cumulativeDistance += previous.getDistanceTo(current);
        System.out.println(current.getName + " " + cumulativeDistance);
        previous = current;
    }            
}

给定您正在使用的现有结构,您可以使用以下命令输出路径中的城市:

public void printCities(Stack<Integer> path, int[][] distances, List<String> names) {
    int cumulativeDistance = 0;
    int previous = -1;
    for (int city: path) {
        if (previous != -1)
            cumulativeDistance += distances[previous][city];
        System.out.println(names.get(city) + " " + cumulativeDistance);
        previous = city;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM