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

ArraylistAgain

parent 727ffe6b
No related branches found
No related tags found
No related merge requests found
......@@ -229,35 +229,57 @@ class Graph{
}
*/
public int getShortestPathTwo( int startNumber, int destinationNumber){
public List<Edge> getShortestPathTwo( int startNumber, int destinationNumber){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int adjacencyListSize = adjacencyList.size();
int[] pathWeight = new int[adjacencyListSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
String[] pathToNodes = new String[adjacencyListSize];
List<Edge>[] pathToNodes = new List[adjacencyListSize];
pathWeight[startNumber] = 0;
Arrays.fill(pathToNodes,"");
Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll();
int start = nextEdge.start();
int nextNode = nextEdge.destination();
int currentSum = pathWeight[nextEdge.start()] + nextEdge.drivingTime();
int currentSum = pathWeight[start] + nextEdge.drivingTime();
if(currentSum<pathWeight[nextNode]){
pathWeight[nextNode] = currentSum;
//pathToNodes[nextNode] = "";
//pathToNodes[nextNode] =
//pathToNodes[nextNode].add(nextEdge);
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[start]);
pathToNodes[nextNode].add(nextEdge);
priorityQueue.addAll(adjacencyList.get(startNumber));
}
}
return pathWeight[destinationNumber];//pathToNodes[destinationNumber];
return pathToNodes[destinationNumber];
}
public int getAdjacencyListSize(){
return adjacencyList.size();
}
public int getShortestPath(int startNumber, int destinationNumber){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int[] pathWeight = new int[this.getAdjacencyListSize()];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
List<Edge>[] pathToNodes = new List[this.getAdjacencyListSize()];
pathWeight[startNumber] = 0;
Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(this.getAllEdgesFromNode(startNumber));
while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.destination();
int currentSum = pathWeight[nextEdge.start()] + nextEdge.drivingTime();
if(currentSum<pathWeight[nextNode]){
pathWeight[nextNode] = currentSum;
//pathToNodes[nextNode] = new ArrayList<>(pathToNodes[nextEdge.start()]);
//pathToNodes[nextNode].add(nextEdge);
priorityQueue.addAll(this.getAllEdgesFromNode(nextNode));
}
}
return pathWeight[destinationNumber];//pathToNodes[destinationNumber];
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment