Skip to content
Snippets Groups Projects
Commit 194f4e5b authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Small change

parent 5fa19d91
No related branches found
No related tags found
No related merge requests found
...@@ -185,6 +185,31 @@ class Graph{ ...@@ -185,6 +185,31 @@ class Graph{
} }
return -1; return -1;
} }
public List<Edge> getShortestPath(int startNumber, int destinationNumber){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int nodeSize = nodes.size();
int[] pathWeight = new int[nodes.size()];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
List<Edge>[] pathToNodes = new List[nodeSize];
pathWeight[startNumber] = 0;
Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.destination();
int start = nextEdge.start();
int currentSum = pathWeight[start] + nextEdge.drivingTime();
if(currentSum<pathWeight[nextNode]){
pathWeight[nextNode] = currentSum;
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[start]);
pathToNodes[nextNode].add(nextEdge);
priorityQueue.addAll(adjacencyList.get(nextNode));
}
}
return pathToNodes[destinationNumber];
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment