mercredi 18 mars 2015

Dijkstra's algorithm in recursion in Java


Vote count:

0




I am computing travelling salesman problem. I have function as follow:



public static double dijkstry(city source, city[] cities){
double min = Double.MAX_VALUE, k;
city[] tmp = new city[2]; //temporary array of city's
tmp[0]=source;
city zm = null;
for(city c : cities){
tmp[1]=c;
k = cost(tmp);
if(k<min&&k!=0){
min=k;
zm=c;
}
}

if(zm!=null||city!=null)
min+=dijkstry(zm,remain(zm,cities));
else
min=0;

return min;
}


where cost function is a function to compute length beetwen two cities (in km), remain function return collection of the other cities to visit. It's look good, but even I have if statement preventing from Null pointer exception, I still have this exception.


How to use dijkstra's algorithm in one simple Java function where I already have data like collection of the other cities?



asked 40 secs ago







Dijkstra's algorithm in recursion in Java

Aucun commentaire:

Enregistrer un commentaire