I am building a weighted graph of roads, in java jgrapht.
I am trying to find the shortest path using Dijkstra algorithm, but the function returns that there is no path between the vertexes. But as for roads concern the graph is connected.
it seems that the line path = new dijkstra... does nothing. the debugger just run over it to the next line, any ideas?
can you help figure out why?
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
public String getDistancesBEtweenCars(List<ConcreteData> placedCars){
ArrayList<DijkstraShortestPath<RoadData, DefaultWeightedEdge>> current1;
current1 = new ArrayList<>();
Map<ConcreteData, ConcreteData> m;
m = new HashMap<>();
double maxLength = 0;
List <Double> distances;
distances = new ArrayList<>();
StringBuilder str = new StringBuilder();
int i = 0;
for (ConcreteData p : placedCars){
for (ConcreteData p1 : placedCars){
if ((m.get(p) != null && m.get(p).equals(p1))){
continue;}
if ((m.get(p1) != null && m.get(p1).equals(p)))
continue;
if (p.equals(p1))
continue;
DijkstraShortestPath path = new DijkstraShortestPath(rg.getGrpah(), ConversionBetweenPlacemnetandRoadData((PoliceCarsPlacement)p),
ConversionBetweenPlacemnetandRoadData((PoliceCarsPlacement) p1));
current1.add(path);
double dist = current1.get(i).getPathLength();
if (dist == Double.POSITIVE_INFINITY)
continue;
distances.add(dist);
if (dist > maxLength)
maxLength = dist;
i++;
m.put(p, p1);
}
}
str.append("max distance between cars: ")
.append(String.valueOf(Math.round(maxLength))).append(" average distance: ").
append(String.valueOf(Math.round(this.s.getMean(distances)))).append(" standard deviation: ").
append(String.valueOf(Math.round(this.s.getStdDev(distances))));
numberOfCollisions(current1);
return str.toString();
}
the class of the graph:
public class RoadGraph {
private WeightedGraph<RoadData, DefaultWeightedEdge> graph;
/**
* Class Constructor
*/
public RoadGraph(){
graph = new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class);
}
/**
* this function adds vertexes
* @param r - road data from the list
*/
public void AddVertex(RoadData r){
this.graph.addVertex(r);
}
/**
* this function adds edges
* i/e connection between roads
* @param r - from road segment
* @param t - to road segment
*/
public void AddeEdge(RoadData r, RoadData t){
if (this.graph.getAllEdges(r,t).isEmpty()==true)
this.graph.addEdge(r, t);
this.graph.setEdgeWeight(this.graph.getEdge(r, t), returnMin(r.getSegmentStartingPoint(),
r.getSegementEndingPoint(), t.getSegmentStartingPoint(), t.getSegementEndingPoint()));
}
the function that adds veterxes and edges to the graph:
/**
*
* @param rd - RoadData list - to save all the roads segments
*/
protected void createGraph(List<RoadData> rd){
rg = new RoadGraph();
int i,j;
for (i = 0; i < roadList.get(0).size(); i++)
rg.AddVertex((RoadData) roadList.get(0).get(i));
for (i = 0; i < roadList.get(0).size(); i++){
RoadData r1 = (RoadData) roadList.get(0).get(i);
rd.add(r1);
for (j = 0; j < roadList.get(0).size(); j++){
if (i != j){
RoadData r2 = (RoadData) roadList.get(0).get(j);
if (r1.getFrom().equals(r2.getFrom()) || r1.getTo().equals(r2.getFrom())
|| r1.getFrom().equals(r2.getTo()) || r1.getTo().equals(r2.getTo()))
rg.AddeEdge(r1, r2);
}
}
}
}