2016-04-12 104 views
1

您好,我目前正在研究Dijkstras算法。我想要計算所選特定邊的數量,以便更改下一個邊的成本等。添加特定邊的次數有問題。我鏈接了我的部分代碼。計算Dijkstra算法的特定邊緣

Edge dirC = new Edge("d" + nodes.get(i * ds.columns + j), nodes.get(i * ds.columns + j), nodes.get((i + 1) * ds.columns + j), downcost1); 
 
edges.add(dirC);

在這一部分的特定邊緣 「DIRC」 被添加。我想添加一個「dicCcount」,然後獲取計數器的值,以便在該部分運行後在if語句中使用它。 Edge類看起來像這樣。

package autonavigate; 
 

 
public class Edge { 
 
    private final String id; 
 
    private final Vertex source; 
 
    private final Vertex destination; 
 
    private final double weight; 
 
    
 
    public Edge(String id, Vertex source, Vertex destination, double weight) { 
 
    this.id = id; 
 
    this.source = source; 
 
    this.destination = destination; 
 
    this.weight = weight; 
 
    } 
 
    
 
    public String getId() { 
 
    return id; 
 
    } 
 
    
 
    public Vertex getDestination() { 
 
    return destination; 
 
    } 
 

 
    public Vertex getSource() { 
 
    return source; 
 
    } 
 
    public double getWeight() { 
 
    return weight; 
 
    } 
 
    
 
    @Override 
 
    public String toString() { 
 
    return source + " " + destination; 
 
    } 
 
}

我試圖解決這個問題了幾個小時,但我無法弄清楚如何做到這一點。它不是一個家庭作業,它是我爲遙控車所做的一個程序:)。

任何建議如何做到這一點?

回答

0

如果我理解正確的話你的問題,你可能會考慮下列操作之一:

  1. 字典數據結構(邊緣數據類型的外部定義)來跟蹤選定的邊,裝櫃(如值)每次選擇邊緣時都會增加;這將在你跟蹤你的圖表時更新(它應該在跟蹤算法的末尾處理)
  2. 對你的邊緣數據類型的擴展,它實現了「被選中」的行爲並且在內部跟蹤了多少在跟蹤運行期間訪問過的次數(可選擇重置其內部計數器)