2013-01-17 242 views
1

我已经在Java中实现了两种算法,并且在测试深度优先搜索时,当有12个节点时,似乎需要花费大量的时间,当使用A *它在几秒钟内完成时,我只是想知道这是预期还是我做错了什么?它在我输入这个内容后在后台运行搜索并进行了几分钟。 我通常不介意,但我必须测试多达500个节点,这可能需要几天的时间,这是我应该期望的,还是我做错了什么?Java - 深度优先搜索

谢谢!


import java.util.*; 


@SuppressWarnings({ "rawtypes", "unchecked" }) 

public class DepthFirstSearch { 

    Routes distances; 
    static Routes routes; 

    int firstNode; 
    String result = new String(); 

    ArrayList firstRoute, bestRoute; 
    int nodes = 0; 
    int routeCost = 0; 
    int bestCost = Integer.MAX_VALUE; 

    public DepthFirstSearch(Routes matrix, int firstNode) { //new instance 

     distances = matrix; 
     this.firstNode = firstNode; 
    } 


    public void run() { //run algorithm 
     long startTime = System.nanoTime(); 
     firstRoute = new ArrayList(); 
     firstRoute.add(firstNode); 
     bestRoute = new ArrayList(); 
     nodes++; 
     long endTime = System.nanoTime(); 

     System.out.println("Depth First Search\n"); 
     search(firstNode, firstRoute); 
     System.out.println(result); 
     System.out.println("Visited Nodes: "+nodes); 
     System.out.println("\nBest solution: "+bestRoute.toString() + "\nCost: "+bestCost); 
     System.out.println("\nElapsed Time: "+(endTime-startTime)+" ns\n"); 
    } 


    /** 
    * @param from node where we start the search. 
    * @param route followed route for arriving to node "from". 
    */ 
    public void search (int from, ArrayList chosenRoute) { 

     // we've found a new solution 
     if (chosenRoute.size() == distances.getCitiesCount()) { 

      chosenRoute.add(firstNode); 
      nodes++; 

      // update the route's cost 
      routeCost += distances.getCost(from, firstNode); 

      if (routeCost < bestCost) { 
       bestCost = routeCost; 
       bestRoute = (ArrayList)chosenRoute.clone(); 
      } 

      result += chosenRoute.toString() + " - Cost: "+routeCost + "\n"; 

      // update the route's cost (back to the previous value) 
      routeCost -= distances.getCost(from, firstNode); 
     } 
     else { 
      for (int to=0; to<distances.getCitiesCount(); to++){ 
       if (!chosenRoute.contains(to)) { 

        ArrayList increasedRoute = (ArrayList)chosenRoute.clone(); 
        increasedRoute.add(to); 
        nodes++; 

        // update the route's cost 
        routeCost += distances.getCost(from, to); 

        search(to, increasedRoute); 

        // update the route's cost (back to the previous value) 
        routeCost -= distances.getCost(from, to); 
       } 
      } 
     } 

    } 

} 
+4

您是否将当前节点标记为已访问?它不应该那么长时间。请在这里发布你的代码。 – nhahtdh

+0

“节点”是什么意思?应该在毫秒内搜索一个包含500个节点的搜索树。或者,如果目标被发现极其复杂,那么你的测试是? – MrSmith42

+0

感谢您的回复,我不知道,我不这么认为;即时通讯不太确定身份证如何去做!编辑原来的问题,其中包括代码 @ MrSmith42它的TSP,它运行良好,直到我添加了第9个节点时,它似乎停止/它需要那么长时间我没有收到最佳路线,正如我所说当我需要测试多达500个时,令人讨厌。 – thrash

回答

1

你没有正确更新chosenRoute;你总是添加“firstNode”与你的ArrayList具有相同的值,我想你应该添加访问节点。 我会尽力检查以后

+0

好的那么如果你能做到这一点会很出色,我真的不知道它出了什么问题。我在测试时只测试了很少的数字,所以它看起来很好,但现在在最后阶段我意识到它根本就不是! – thrash