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

Alt mk1

parent 9beb0157
Branches
No related tags found
No related merge requests found
...@@ -6,5 +6,19 @@ public class Comparators { ...@@ -6,5 +6,19 @@ public class Comparators {
return Integer.compare(left.drivingTime + left.currentWeight, right.drivingTime + right.currentWeight); return Integer.compare(left.drivingTime + left.currentWeight, right.drivingTime + right.currentWeight);
} }
} }
//TODO: ALT comparator static class AltComparator implements Comparator<Edge> {
int[] landMark;
int landMarkNumber;
public AltComparator(int[] landMarkValues, int landMarkNumber){
landMark = landMarkValues;
this.landMarkNumber = landMarkNumber;
}
@Override
public int compare(Edge o1, Edge o2) {
return Integer.compare(o1.drivingTime + o1.currentWeight+getDelta(o1.destination), o2.drivingTime + o2.currentWeight +getDelta(o2.destination));
}
private int getDelta(int destination){
return landMark[destination] - landMark[landMarkNumber];
}
}
} }
...@@ -85,6 +85,32 @@ class Graph { ...@@ -85,6 +85,32 @@ class Graph {
return null; return null;
} }
public Edge getShortestPathALT(int startNumber, int destinationNumber) throws Exception {
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(new Comparators.AltComparator(Utils.fileToArray("testOslo.txt",adjacencyList.size()),3430400));
int nodeSize = adjacencyList.size();
int[] pathWeight = new int[nodeSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
pathWeight[startNumber] = 0;
priorityQueue.addAll(adjacencyList.get(startNumber));
while (!priorityQueue.isEmpty()) {
Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.getDestination();
int start = nextEdge.getStart();
int currentSum = pathWeight[start] + nextEdge.getDrivingTime();
if (currentSum < pathWeight[nextNode]) {
pathWeight[nextNode] = currentSum;
for (Edge edge : adjacencyList.get(nextNode)) {
edge.setCurrentWeight(pathWeight[start]);
priorityQueue.add(edge);
edge.setPreviousEdge(nextEdge);
}
priorityQueue.addAll(adjacencyList.get(nextNode));
}
if (nextNode == destinationNumber) return nextEdge;
}
return null;
}
public Node[] getListOfPlaces(int startNumber, byte typeNumber, byte numberPlaces) { public Node[] getListOfPlaces(int startNumber, byte typeNumber, byte numberPlaces) {
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(); PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
Node[] listOfPlaces = new Node[numberPlaces]; Node[] listOfPlaces = new Node[numberPlaces];
...@@ -119,6 +145,7 @@ class Graph { ...@@ -119,6 +145,7 @@ class Graph {
return listOfPlaces; return listOfPlaces;
} }
public int[] fromLandmark(int landMark) { public int[] fromLandmark(int landMark) {
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(); PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int nodeSize = adjacencyList.size(); int nodeSize = adjacencyList.size();
......
...@@ -82,6 +82,7 @@ class ALTEdge implements Comparable<ALTEdge>{ ...@@ -82,6 +82,7 @@ class ALTEdge implements Comparable<ALTEdge>{
public int compareTo(ALTEdge o) { public int compareTo(ALTEdge o) {
return Integer.compare(drivingTime+currentWeight + getDelta(), o.drivingTime + o.currentWeight + o.getDelta()); return Integer.compare(drivingTime+currentWeight + getDelta(), o.drivingTime + o.currentWeight + o.getDelta());
} }
} }
class ALTGraph{ class ALTGraph{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment