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

New implementation of Dijkstra

parent bca22cc6
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -20,7 +20,7 @@ public class Task9 { ...@@ -20,7 +20,7 @@ public class Task9 {
List<Edge> edges = g.getShortestPath(0,3); List<Edge> edges = g.getShortestPath(0,3);
System.out.println(startNumber); System.out.println(startNumber);
for (Edge edge: edges) { for (Edge edge: edges) {
System.out.println(edge.destination()); System.out.println(edge.getDestination());
} }
System.out.println("----------------------- Dijkstra Done --------------------------------"); System.out.println("----------------------- Dijkstra Done --------------------------------");
g.loadNodes(Utils.generatePath("noder.txt")); g.loadNodes(Utils.generatePath("noder.txt"));
...@@ -97,15 +97,41 @@ class Node{ ...@@ -97,15 +97,41 @@ class Node{
} }
} }
record Edge(int start, int destination, int drivingTime) implements Comparable<Edge> { class Edge implements Comparable<Edge>{
int start;
int destination;
int drivingTime;
int currentWeight = 0;
@Override Edge(int start, int destination, int drivingTime){
public int compareTo(Edge o) { this.start = start;
return Integer.compare(this.drivingTime(), o.drivingTime()); this.destination = destination;
this.drivingTime = drivingTime;
} }
public int start() { public int getStart() {
return this.start; return start;
}
public int getDestination() {
return destination;
}
public int getDrivingTime() {
return drivingTime;
}
public int getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(int currentWeight) {
this.currentWeight = currentWeight;
}
@Override
public int compareTo(Edge o) {
return Integer.compare(drivingTime+currentWeight,o.drivingTime+o.currentWeight);
} }
} }
...@@ -153,28 +179,27 @@ class Graph{ ...@@ -153,28 +179,27 @@ class Graph{
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(); PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int nodeSize = adjacencyList.size(); int nodeSize = adjacencyList.size();
int[] pathWeight = new int[nodeSize]; int[] pathWeight = new int[nodeSize];
boolean[] visited = new boolean[nodeSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE); Arrays.fill(pathWeight, Integer.MAX_VALUE);
ArrayList<Edge>[] pathToNodes = new ArrayList[nodeSize]; ArrayList<Edge>[] pathToNodes = new ArrayList[nodeSize];
pathWeight[startNumber] = 0; pathWeight[startNumber] = 0;
visited[startNumber] = true;
Arrays.fill(pathToNodes,new ArrayList<>()); Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(adjacencyList.get(startNumber)); priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()){ while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll(); Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.destination(); int nextNode = nextEdge.getDestination();
if(!visited[nextNode]){ int start = nextEdge.getStart();
int start = nextEdge.start(); int currentSum = pathWeight[start] + nextEdge.getDrivingTime();
int currentSum = pathWeight[start] + nextEdge.drivingTime();
if(currentSum<pathWeight[nextNode]) { if(currentSum<pathWeight[nextNode]) {
pathWeight[nextNode] = currentSum; pathWeight[nextNode] = currentSum;
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[start]); pathToNodes[nextNode] = new ArrayList<>(pathToNodes[start]);
pathToNodes[nextNode].add(nextEdge); pathToNodes[nextNode].add(nextEdge);
priorityQueue.addAll(adjacencyList.get(nextNode)); for(Edge edge: adjacencyList.get(nextNode)){
edge.setCurrentWeight(pathWeight[start]);
priorityQueue.add(edge);
} }
priorityQueue.addAll(adjacencyList.get(nextNode));
} }
if(visited[destinationNumber]) return pathToNodes[destinationNumber]; if(nextNode == destinationNumber) return pathToNodes[destinationNumber];
} }
return pathToNodes[destinationNumber]; return pathToNodes[destinationNumber];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment