【问题标题】:Traveling Salesman in JavaJava 中的旅行推销员
【发布时间】:2015-05-16 11:41:36
【问题描述】:

我正在尝试为旅行商问题制作一个 Java 实现。我已经阅读了很多关于不同最优算法的内容,我发现 Held-Karp 算法是一个很好的实现。现在我的问题是我正在尝试制作一个邻接矩阵来存储我的边缘值,然后使用这些值来实现算法,我找不到一个好的逻辑来制作邻接矩阵。

我的代码从具有边缘及其相关成本的文件(graphFile)中读取。文件如下:

A : B : 10
A : C : 3
A : D : 30
etc

现在我的代码读取文件,遍历每一行并将顶点(节点)以 [A, B, C, D, ...] 的形式存储在 SortedSet 中。同时,边以 {AB = 10, AC = 3, ...} 的形式连同其相关成本一起存储在 TreeMap 中。我使用我的顶点集来定义我的邻接矩阵的大小,这就是我卡住的地方。

在邻接矩阵中插入边的关联值的最佳方法是什么?这甚至是解决 TSP 的好方法还是我走错了路?

static int[][] adjacencyMatrix;
static SortedSet<String> vertices  = new TreeSet<String>();
static TreeMap<String, Integer> edgeCost = new TreeMap<String, Integer>();

这是一个应该有帮助的代码 sn-p。这段代码读取文件并将值相应地存储在 Map 和 Set 中:

while (graphFile.hasNextLine()) {
    line = graphFile.nextLine();
    tokenizer = line.split(" : "); 
    try {
        if (tokenizer.length != 3) {
            continue;
        }
        String source = tokenizer[0];
        String dest = tokenizer[1];
        String cost = tokenizer[2];
        addVertex(source, dest); //Add Vertex to set of vertices. Duplicates are removed.
        addEdge(source+dest, Integer.parseInt(cost)); //Add edges to an array then add array to list of edges.
    } catch (ArrayIndexOutOfBoundsException e) {
        System.err.println("Wrongly Formatted Line " + line);
    }
}

现在我有一个计算邻接矩阵的代码。到目前为止,我设法只制作了矩阵的对角线部分(值 INF = 99999)。但是我有一个逻辑问题,即如何将其余的 edgeCost 值存储在矩阵中。

private static void createAdjacencyMatrix() {

   for(int i=0; i<adjacencyMatrix.length; i++){
       for(int j=0; j<adjacencyMatrix.length; j++){
           if(i == j){
                adjacencyMatrix[i][j] = INF;
           }
       }
   }
   //Part where values of edgeCost should be added in matrix. Logic problem here.
}
}

现在我在想,我不使用邻接矩阵,而是使用邻接列表。哪一个更适合使用 Held-Karp 算法的 TSP?

【问题讨论】:

  • 怎么样:TreeMap&lt;String, TreeMap&lt;String, Integer&gt;&gt;
  • @ReutSharabani 那么它的目的是什么?
  • 我假设你想要关系 a->b->5(从ab 的旅行成本是5),所以如果你使用这个结构,你就有那个确切的映射。
  • @ReutSharabani 我明白了。但在那种情况下不应该是 TreeMap, Integer> 吗?
  • 查看我的回答了解更多详情。如果您有任何问题,请在此处发表评论

标签: java algorithm traveling-salesman


【解决方案1】:

这是我在 cmets 中建议的一个小例子。它不会从文件中解析,但我认为您可以处理它:-)

public static void main(String[] args){
    TreeMap<String, Map<String, Integer>> routes = new TreeMap<String, Map<String, Integer>>();

    // create some input
    String[] routeStrings ={"a : b : 10", "a : c : 3", "a : d : 30", "d : a : 7", "d : b : 5"};

    // parse and populate map
    for (String route: routeStrings){
        String[] sourceDestinationCost = route.split(" : ");
        String source = sourceDestinationCost[0];
        String destination = sourceDestinationCost[1];
        Integer cost = Integer.parseInt(sourceDestinationCost[2]);

        // create entry for source if needed
        if (!routes.containsKey(source)){
            routes.put(source, new TreeMap<String, Integer>());
        }
        routes.get(source).put(destination, cost);
    }

    // see result
    System.out.println(routes.toString());

}

这会为每个源创建一个到其目的地的映射,该映射映射到成本。

简而言之,可以将地图视为表单(源、目的地、成本)的元组列表。

【讨论】:

  • 我明白了。那么现在我们可以说它是一个邻接表吗?或者我应该根据这些制作一个邻接矩阵(如果我这样做,我该怎么做)。
猜你喜欢
  • 2015-04-28
  • 2011-09-08
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多