2015-05-14 71 views
1

我想實現一個使用遞歸方法的搜索算法。執行算法的Java遞歸

該算法應擴大startnode到其相鄰節點,然後選擇具有最小成本的相鄰節點,然後將該成本添加到pathcost(其最初是0),並且至少所選擇的成本的節點將變爲startnode和繼續搜索再次遞歸直到找到goalnode

下面是我實現這種遞歸的代碼。這不是給我任何錯誤,但它並沒有給我預期的解決方案。 INSTEAD它增加了每個相鄰節點的成本(我只需要它添加最小成本節點)。我一直在努力去做,但似乎無法找到任何線索如何去做。

Queue<String> frontierNodes = new PriorityQueue<String>(); 
Queue<Node1> frontierCosts = new PriorityQueue<Node1>(); // here Node1 is class storing the start, end and cost of the map. 

public void Search(Node1[] nodes, String startnode, String goalnode, int size, double pathcost){ 

    for(int i=0; i<size;i++) {    
     if(startnode.equalsIgnoreCase(nodes[i].getStartNode())) {    
      frontierNodes.add(nodes[i].getEndNode()); 
      frontierCosts.add(new Node1(nodes[i].getCost())); 

      System.out.println("Frontier Nodes are " +frontierNodes); 
      System.out.println("Path cost till now "+pathcost); 
      // Something should be implemented here to add only the least cost 
      pathcost += frontierCosts.peek().toCostString(); 
      System.out.println("Path cost till now "+pathcost); 

     }  
    } 
    System.out.println("Expanding node... " +frontierNodes.peek()); 
    //Recursive call 
    Search(nodes, frontierNodes.poll(), goalnode, nodes.length-(frontierNodes.size()), pathcost); 

} 
+1

actuallly什麼應該是發生在這裏,探索的StartNode的所有鄰居,對它們進行比較,皮卡最低成本節點,並將它們添加到您的最終排名,這是回答你的問題 但不知何故,這並不看作是一個分cost tour algorithm – Vihar

+0

比較成本的條件檢查在哪裏?您聲明您希望選擇最低成本,但我看不到任何比較節點成本的代碼。 – MadConan

+0

其實我正在使用優先級隊列而不是檢查成本,以便我可以poll()/ peek()隊列頭(這是最便宜的)。這不工作嗎? :/ – Dee

回答

0

我不確定爲什麼要使用PriorityQueue。您應該將所有內容都放在遞歸方法的範圍內。對於樹的遍歷遞歸,你要遵循的一般模式(僞代碼)

int sumOfLeastCost(Node node){ 
    if(node == null){ return 0; } 
    int sum = 0; 
    Node min = null; 
    for each child{ 
     if(min == null){ 
      min = currentChild; 
     }else if(currentChild.getCost() < min.getCost()){ 
      min = currentChild; 
      sum = min.getCost(); 
     } 
    } 

    return sum + sumOfLeastCost(min); 
} 

這隻會遵循最低成本節點的分支。

0

這更貼近您的需求嗎?

public double Search(ArrayList<Node1> nodes, String startnode, String goalnode, double pathcost){ 

    //Probably needs a better data structure to hold these 
    ArrayList<String> neighbours = new ArrayList<>(); 
    ArrayList<Double> neighbourPathCosts = new ArrayList<>(); 

    int i; 
    for(i=0; i<nodes.size();i++) {    
     if(startnode.equalsIgnoreCase(nodes.get(i).getStartNode())) {    
      neighbours.add(nodes.get(i).getEndNode()); 
      neighbourPathCosts.add(nodes.get(i).getCost()); 
     } 
    } 

    int indexOfCheapest = -1; 
    double cheapest = Double.MAX_VALUE; 
    for(int j = 0; j < neighbours.size(); j++){ 
     if(cheapest > neighbourPathCosts.get(i)){ 
      cheapest = neighbourPathCosts.get(i); 
      indexOfCheapest = i; 
     } 
    } 

    pathcost += neighbourPathCosts.get(indexOfCheapest); 
    System.out.println("Path cost till now " + pathcost); 

    String nextNode = neighbours.get(indexOfCheapest); 
    System.out.println("Expanding node... " + nextNode); 

    if(startNode.equals(goalNode)){ 
     return pathcost; 
    }else{ 
     nodes.remove(i); 
     //Recursive call 
     return Search(nodes, nextNode, goalnode, pathcost); 
    } 
} 
+0

是的,我已經嘗試過這一點,而運行它給了我錯誤的結果與stackoverflow錯誤以及許多奇怪的錯誤:/(在運行時) – Dee

+0

什麼其實collections.min在這裏做什麼?它選擇成本最低的節點嗎? – Dee

+0

是Collections.min()獲得最小值,然後我使用索引來知道它是哪一個。 – George