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

Inverse graph method was added

parent 0e4e6391
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;
public class Task9 {
public static void main(String[] args) {
Graph g = null;
try {
g = new Graph("noder.txt");
g = new Graph("noder Iceland.txt");
} catch (IOException e) {
e.printStackTrace();
}
Objects.requireNonNull(g).loadEdges("kanter.txt");
//g.loadNodes("noder.txt");
/*int startNumber = 0;
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(edge.getDestination());
}
System.out.println("----------------------- Dijkstra Done --------------------------------");
g.readNodeInformation("interessepkt.txt");
g.loadNodes("noder Iceland.txt");
g.readNodeInformation("interessepktIceland.txt");
Node[] listOfPlaces = g.getListOfPlaces(0,Byte.parseByte("2"),Byte.parseByte("8"));
for(Node node: listOfPlaces){
System.out.println(node.getLatitude()+","+node.getLongitude());
}*/
int[] array = g.fromLandmark(3430400);
/*try {
Utils.arrayToFile(array, "testOslo.txt");
} catch (Exception e) {
e.printStackTrace();
}*/
try {
int[] loadedArray = Utils.fileToArray("testOslo.txt", g.getSize());
System.out.println(loadedArray.length == g.getSize());
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -163,7 +154,6 @@ class Edge implements Comparable<Edge>{
}
class Graph{
private final int size;
private final HashMap<Integer, List<Edge>> adjacencyList = new HashMap<>();
private final List<Node> nodes = new ArrayList<>();
......@@ -172,14 +162,12 @@ class Graph{
}
public Graph(String inFile) throws IOException {
size = Utils.readFirstLine(inFile);
for(int i = 0; i < size; ++i) {
int numberOfNodes = Utils.readFirstLine(inFile);
for(int i = 0; i < numberOfNodes; ++i) {
adjacencyList.put(i, new ArrayList<>());
}
}
public int getSize() {return size;}
public void loadNodes(String inFile) {
List<String> data = Utils.readFile(inFile);
for(int i = 1; i < data.size(); ++i) {
......@@ -198,6 +186,15 @@ class Graph{
System.out.println("Edges loaded.");
}
public void loadInverseEdges(String inFile){
List<String> data = Utils.readFile(inFile);
for(int i = 1; i < data.size(); ++i) {
String[] splitText = Utils.dividedText(data.get(i), 3);
addEdge(Integer.parseInt(splitText[1]), Integer.parseInt(splitText[0]), Integer.parseInt(splitText[2]));
}
System.out.println("Edges loaded.");
}
public void readNodeInformation(String inFile){
List<String> data = Utils.readFile(inFile);
for(int i = 1; i < data.size(); ++i) {
......@@ -210,6 +207,7 @@ class Graph{
}
public void addEdge(int start, int destination, int drivingTime) {
adjacencyList.get(start).add(new Edge(start,destination, drivingTime));
}
......@@ -280,7 +278,6 @@ class Graph{
public int [] fromLandmark(int landMark){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
int nodeSize = adjacencyList.size();
System.out.println(nodeSize);
int[] pathWeight = new int[nodeSize];
Arrays.fill(pathWeight, Integer.MAX_VALUE);
pathWeight[landMark] = 0;
......@@ -296,11 +293,11 @@ class Graph{
for(Edge edge: adjacencyList.get(nextNode)){
edge.setCurrentWeight(pathWeight[start]);
priorityQueue.add(edge);
}
index ++;
}
priorityQueue.addAll(adjacencyList.get(nextNode));
}
if(index == size) return pathWeight;
if(index == nodeSize) return pathWeight;
}
return null;
}
......@@ -342,25 +339,14 @@ class Utils{
return currentDirectoryPath + System.getProperty("file.separator") +fileName;
}
public static void arrayToFile(int[] array, String fileName) throws IOException{
FileWriter writer = new FileWriter(fileName);
for (int j : array) {
writer.write(j + "\n");
}
writer.close();
}
public static int[] fileToArray(String fileName, int size) throws Exception {
int[] array = new int[size];
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String currentLine = null;
int index = 0;
while((currentLine = reader.readLine()) != null) {
array[index] = Integer.parseInt(currentLine);
++index;
}
public void arrayToFile(int[] array, String fileName) throws IOException{
FileOutputStream out= new FileOutputStream(fileName);
FileChannel file = out.getChannel();
ByteBuffer but = file.map(FileChannel.MapMode.READ_WRITE,0, 4L * array.length);
for(int i : array){
but.putInt(i);
}
return array;
file.close();
}
public static String[] dividedText(String line, int number){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment