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

Boolean array was added to Dikjstra

parent 6c31859b
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
No preview for this file type
......@@ -18,17 +18,12 @@ public class Task9 {
Objects.requireNonNull(g).loadEdges("kanterIceland.txt");
int startNumber = 0;
List<Edge> edges = g.getShortestPath(0,3);
System.out.println(startNumber);
for (Edge edge: edges) {
System.out.println("From " + edge.start() + " to "+ edge.destination() + " in " + edge.drivingTime() + " s/100");
System.out.println(edge.destination());
}
System.out.println("----------------------- Dijkstra Done --------------------------------");
g.loadNodes(Utils.generatePath("noder Iceland.txt"));
List<Node> nodes = g.getNodes();
System.out.println(nodes.get(startNumber).getLatitude()+","+nodes.get(startNumber).getLongitude());
for(Edge edge : edges){
Node destination = nodes.get(edge.destination());
System.out.println(destination.getLatitude() +","+destination.getLongitude());
}
g.loadNodes(Utils.generatePath("noder.txt"));
}
}
......@@ -115,7 +110,7 @@ record Edge(int start, int destination, int drivingTime) implements Comparable<E
}
class Graph{
private final HashMap<Integer, List<Edge>> adjacencyList = new HashMap<>();
private HashMap<Integer, List<Edge>> adjacencyList = new HashMap<>();
private List<Node> nodes = new ArrayList<>();
public List<Node> getNodes() {
......@@ -155,15 +150,18 @@ class Graph{
public List<Edge> getShortestPath(int startNumber, int destinationNumber){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int nodeSize = adjacencyList.size();
int[] pathWeight = new int[adjacencyList.size()];
int[] pathWeight = new int[nodeSize];
boolean[] visited = new boolean[nodeSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
List<Edge>[] pathToNodes = new List[nodeSize];
ArrayList<Edge>[] pathToNodes = new ArrayList[nodeSize];
pathWeight[startNumber] = 0;
visited[startNumber] = true;
Arrays.fill(pathToNodes,new ArrayList<>());
priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.destination();
if(!visited[nextNode]){
int start = nextEdge.start();
int currentSum = pathWeight[start] + nextEdge.drivingTime();
if(currentSum<pathWeight[nextNode]) {
......@@ -173,6 +171,7 @@ class Graph{
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