【问题标题】:nearest neighbor algorithm copy element (city) to output array java最近邻算法复制元素(城市)到输出数组java
【发布时间】:2015-12-30 02:35:05
【问题描述】:

所以到目前为止,我编写了一个程序,它以以下格式读取城市和距离的 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();

        }
    }

【问题讨论】:

    标签: java arrays algorithm csv element


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

    • 你的意思是public void printCities(Stack&lt;Integer&gt; path, int[][] distances, ArrayList&lt;String&gt; names)
    • 我知道它是如何工作的,但我不知道如何将它插入到我已经拥有的东西中。然后将其包含在 TSPNeighbor 类中,我将主要通过 temp.printCities(city) 请求该输出;或 temp.printCities(names);
    【解决方案2】:

    我想稍微间接地回答你的问题。通过使用对象数组,您使自己的生活变得艰难。它们使代码难以阅读并且难以访问。如果您使用适当的方法创建一个 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;
        }            
    }
    

    【讨论】:

    • 好的,这很有帮助。我将发布第二个答案,其中包含使用当前结构打印输出的解决方案。但是,我建议您尽早习惯使用类来正确表示结构。数组在 Java 中很少是正确的方法。
    • 关于这个问题的任何更新?感谢您迄今为止的意见。我仍然遇到这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2012-12-16
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多