Skip to content
Snippets Groups Projects
Commit c6b8419e authored by Anders Austlid's avatar Anders Austlid
Browse files

Working

parent 701dae57
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
No preview for this file type
public class ALT {
}
......@@ -10,20 +10,32 @@ public class Task9 {
public static void main(String[] args) {
try {
ALTGraph ag = new ALTGraph("noder.txt");
ag.loadALtEdges("kanter.txt", "testOslo.txt");
List<ALTEdge> list = ag.getShortestPath(3420069, 3690420);
ag.loadALtEdges("kanter.txt", "testOsloNew.txt");
List<ALTEdge> list = ag.getShortestPath(3292784, 7352330);
System.out.println(3420069);
double sum = 0;
for(var edge : list) {
System.out.println(edge.destination);
sum += edge.drivingTime;
//System.out.println(edge.destination);
}
sum = ((sum / 100) / 3600) * 60;
System.out.println(sum);
} catch (Exception e) {
e.printStackTrace();
}
/*try {
Graph g = new Graph("noder.txt");
g.loadEdges("kanter.txt");
int[] array = g.fromLandmark(3430400);
Utils.arrayToFile(array, "testOslo.txt", 3430400);
List<Edge> list = g.getShortestPath(3292784, 7352330);
double sum = 0;
for(var edge : list) {
sum += edge.drivingTime;
//System.out.println(edge.destination);
}
sum = ((sum / 100) / 3600) * 60;
System.out.println(sum);
*//*int[] array = g.fromLandmark(3430400);
Utils.arrayToFile(array, "testOsloNew.txt", 3430400);*//*
} catch (Exception e) {
e.printStackTrace();
}*/
......@@ -196,7 +208,7 @@ class ALTEdge implements Comparable<ALTEdge>{
@Override
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());
}
}
......@@ -337,6 +349,7 @@ class Graph{
int currentSum = pathWeight[start] + nextEdge.getDrivingTime();
if(currentSum<pathWeight[nextNode]) {
pathWeight[nextNode] = currentSum;
//System.out.println(pathWeight[nextNode]);
//System.out.println(currentSum);
for(Edge edge: adjacencyList.get(nextNode)){
edge.setCurrentWeight(pathWeight[start]);
......@@ -362,12 +375,15 @@ class ALTGraph{
private final HashMap<Integer, List<ALTEdge>> adjacencyList = new HashMap<>();
private final List<Node> nodes = new ArrayList<>();
private final int size;
public List<Node> getNodes() {
return nodes;
}
public ALTGraph(String inFile) throws IOException {
int numberOfNodes = Utils.readFirstLine(inFile);
size = numberOfNodes;
for(int i = 0; i < numberOfNodes; ++i) {
adjacencyList.put(i, new ArrayList<>());
}
......@@ -384,14 +400,15 @@ class ALTGraph{
public void loadALtEdges(String inFileEdges, String distanceFromLandMark) throws Exception{
List<String> data = Utils.readFile(inFileEdges);
int[] distances = Utils.fileToArray(distanceFromLandMark,adjacencyList.size());
int[] distances = Utils.fileToArray(distanceFromLandMark,size);
for(int i = 1; i < data.size(); ++i) {
String[] splitText = Utils.dividedText(data.get(i), 3);
addEdge(Integer.parseInt(splitText[0]), Integer.parseInt(splitText[1]), Integer.parseInt(splitText[2]));
}
for(int i = 0; i<distances.length;i++){
for(ALTEdge edge: adjacencyList.get(i)){
edge.setDestinationDelta(distances[i]);
edge.setFromLandMark(distances[i]);
edge.setDestinationDelta(distances[3430400]);
}
}
System.out.println("Edges loaded.");
......@@ -463,9 +480,9 @@ class Utils{
public static void arrayToFile(int[] array, String fileName, int landmark) throws IOException{
try(FileWriter writer = new FileWriter(fileName)) {
writer.write(landmark + "\n");
for (int j : array) {
if(j == array[array.length - 1]) writer.write(j + "");
else writer.write(j + "\n");
for (int i = 0; i < array.length; ++i) {
if(i == array.length - 1) writer.write(i + "");
else writer.write(i + "\n");
}
}
}
......@@ -474,9 +491,11 @@ class Utils{
int[] array = new int[size];
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String currentLine = null;
int index = 0;
int index = -1;
while((currentLine = reader.readLine()) != null) {
if(index != -1) {
array[index] = Integer.parseInt(currentLine);
}
++index;
}
}
......
testOsloNew.txt 0 → 100644
NaN GiB (NaN%)
View file @ c6b8419e
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment