为什么Dijkstra的实施不起作用?

时间:2013-08-03 14:45:34

标签: java loops dijkstra

这是我用来实现Dijkstra最短路径算法的方法...当我运行它时,第二个for循环从未到达,但我无法弄清楚如何解决这个问题。我知道这与将mincost设置为MAX_VALUE有关,但我不知道如何进行初始化。

// method to find shortest path between source village,s, and destination village, d 
public ArrayList<Village> shortestPath(Village s, Village d){
    int[] villageCosts= new int[villages.size()];
    boolean[] wasVisited= new boolean[villages.size()];
    shortestPath = new ArrayList<Village>();
    int counter= wasVisited.length;
    System.out.println("the value of the counter is: "+ counter);

    for(int i=0; i<villageCosts.length; i++){ //initialize to infinity
        villageCosts[i]= Integer.MAX_VALUE;
    }

    //villageCosts[s.getVillageName()] = 0; while(counter > 0){
        System.out.println("in the while loop! the value of the counter is: "+ counter);
        int mincost = Integer.MAX_VALUE;
        int minindex= 0;

        //if the minimum cost in villageCosts i still infinity
        for(int i=0; i<villageCosts.length && wasVisited[i]==false; i++){
            System.out.println("in the first for loop!");
            if (mincost <= villageCosts[i]){
                System.out.println("in the first if statement!");
                mincost = villageCosts[i];
                minindex= i;
                wasVisited[i]= true;
                counter--;
                System.out.println("the value of the counter after the first if statement: " + counter);
                System.out.println("min index: " + minindex);
            }   
            shortestPath.add(villages.get(i));
        }   System.out.println("out of the first for loop!");


        //if minimum cost in villegeCost is still infinity
        if(villageCosts[minindex] == Integer.MAX_VALUE){
            System.out.println("in the if statement that returns null if true!");
            return null;
        }

        //for min index road loop through adjVillages,and calculate village cost of minindex + cost of road between minindex and each adjVillage
        for(int i=0; i< villages.get(minindex).adjVillages.size(); i++){
            System.out.println("in the second for loop!");
            Road b= getRoadBetween(villages.get(minindex), villages.get(i));
            int toll=b.getToll();
            int alt= villageCosts[minindex] + toll;
            if(alt < toll){
                System.out.println("in the if statement in the second for loop!");
                toll=alt;
                wasVisited[toll]= true;
                counter--;
            } shortestPath.add(villages.get(alt));
        }   
    }           System.out.println("out of the while loop!"); //ends while loop 
    return shortestPath;
}

2 个答案:

答案 0 :(得分:1)

此行应更改

int mincost = Integer.MAX_VALUE;
if (mincost <= villageCosts[i]){

它应该是反过来的,你想找到最低的mincost。它应该是

if (mincost >= villageCosts[i]){ 

修改 回复评论

如果没有通过第二个循环,请检查villages.get(minindex)的大小。我不确定它是什么样的dijkstra。如果发现最短路径,我会更加惊讶。您可能应该退一步并查看您的代码。

答案 1 :(得分:0)

the second for loop is never reached

第一个循环后if中的条件始终为真,因为您没有覆盖villageCosts[minindex] ...

if(villageCosts[minindex] == Integer.MAX_VALUE){  
   System.out.println("in the if statement that returns null if true!");   
   return null  
}