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

Reworked adjacency list to use HashMap. Edges now load quickly.

parent bcc50e29
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
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
......@@ -6,7 +6,7 @@ import java.util.*;
public class Task9 {
public static void main(String[] args) {
Graph graph = new Graph();
/*Graph graph = new Graph();
graph.readAllNodes(Utils.readFile(Utils.generatePath("noder.txt")));
graph.readAllEdges(Utils.readFile(Utils.generatePath("kanter.txt")));
List<Edge> edges = Dijkstra.getShortestPath(graph,0,3);
......@@ -18,7 +18,9 @@ public class Task9 {
System.out.println(destination.getLongitude()+","+ destination.getLatitude());
sum+= edge.getDrivingTime();
}
System.out.println("Final driving time is " + sum);
System.out.println("Final driving time is " + sum);*/
Graph g = new Graph("noder.txt");
g.loadEdges("kanter.txt");
}
}
class Node{
......@@ -27,7 +29,8 @@ class Node{
private final double longitude;
private String[] typeArray;
private String name;
private final List<Edge> edges = new ArrayList<>();
//private final List<Edge> edges = new ArrayList<>();
public Node(int number, double latitude, double longitude) {
this.number = number;
this.latitude = latitude;
......@@ -54,10 +57,6 @@ class Node{
return name;
}
public List<Edge> getEdges() {
return edges;
}
public void setName(String name){
this.name=name;
}
......@@ -78,9 +77,9 @@ class Node{
}
}
public void addEdge(int destination, int drivingTime, int distance, int speedLimit){
/*public void addEdge(int destination, int drivingTime, int distance, int speedLimit){
edges.add(new Edge(this.getNumber(),destination,drivingTime,distance,speedLimit));
}
}*/
public boolean isTypeOf(String typeName){
return Arrays.stream(typeArray).anyMatch(t -> t.equalsIgnoreCase(typeName));
......@@ -99,90 +98,59 @@ class Node{
}
}
class Edge implements Comparable<Edge> {
private final int start;
private final int destination;
private final int drivingTime;
private final int distance;
private final int speedLimit;
public Edge(int start, int destination, int drivingTime, int distance, int speedLimit) {
this.start = start;
this.destination = destination;
this.drivingTime = drivingTime;
this.distance = distance;
this.speedLimit = speedLimit;
}
public int getStart() {
return start;
}
public int getDestination() {
return destination;
}
public int getDrivingTime() {
return drivingTime;
}
public int getDistance() {
return distance;
}
public int getSpeedLimit() {
return speedLimit;
}
record Edge(int destination, int drivingTime) implements Comparable<Edge> {
@Override
public int compareTo(Edge o) {
return Integer.compare(this.getDrivingTime(),o.getDrivingTime());
return Integer.compare(this.drivingTime(), o.drivingTime());
}
}
class Graph{
private final List<Node> nodes = new ArrayList<>();
private final List<Node> nodes;
private final HashMap<Integer, List<Edge>> adjacencyList = new HashMap<>();
public void setNodes(int number, double longitude, double latitude){
nodes.add(number, new Node(number,latitude,longitude));
public Graph(String inFile) {
nodes = loadNodes(inFile);
for(int i = 0; i < nodes.size(); ++i) {
adjacencyList.put(i, new ArrayList<>());
}
public Node getNode(int index){
return nodes.get(index);
}
public void readAllNodes(List<String> data){
for (int i = 1; i<data.size(); i++){
//System.out.println(data.get(i));
public List<Node> loadNodes(String inFile) {
List<Node> nodes = new ArrayList<>();
List<String> data = Utils.readFile(inFile);
for(int i = 1; i < data.size(); ++i) {
String[] splitText = Utils.dividedText(data.get(i), 3);
//System.out.println(Arrays.toString(splitText));
this.setNodes(Integer.parseInt(splitText[0]),Double.parseDouble(splitText[1]),Double.parseDouble(splitText[2]));
nodes.add(new Node(Integer.parseInt(splitText[0]), Double.parseDouble(splitText[1]), Double.parseDouble(splitText[2])));
}
System.out.println("Nodes loaded.");
return nodes;
}
/**Possible fail*/
public void readAllEdges(List<String> data){
for(int i = 1 ; i<data.size();i++){
String [] splitText = Utils.dividedText(data.get(i),5);
// System.out.println(Arrays.toString(splitText));
this.getNode(Integer.parseInt(splitText[0])).
addEdge(Integer.parseInt(splitText[1])
,Integer.parseInt(splitText[2]),Integer.parseInt(splitText[3])
,Integer.parseInt(splitText[4]));
public void loadEdges(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[0]), Integer.parseInt(splitText[1]), Integer.parseInt(splitText[2]));
}
System.out.println("Edges loaded.");
}
public void addEdge(int start, int destination, int drivingTime) {
adjacencyList.get(start).add(new Edge(destination, drivingTime));
}
public void readNodeInformation(List<String> data){
/*public void readNodeInformation(List<String> data){
for(String text : data){
if(!text.equals(data.get(0))){
String [] splitText = Utils.dividedText(text,3);
this.setTypes(Integer.parseInt(splitText[0]),Integer.parseInt(splitText[1]),splitText[2]);
}
}
}
}*/
private void setTypes(int nodeNumber,int typeValue, String name){
/*private void setTypes(int nodeNumber,int typeValue, String name){
Node node = this.getNode(nodeNumber);
if((typeValue&1) ==1) node.setType("Place name");
......@@ -193,7 +161,7 @@ class Graph{
if((typeValue&32) == 32) node.setType("Accommodation");
node.setName(name);
}
}*/
public int getNumberOfNodes(){
return nodes.size();
......@@ -213,7 +181,7 @@ class Graph{
}
class Dijkstra{
public static List<Edge> getShortestPath(Graph graph, int startNumber, int destinationNumber){
/*public static List<Edge> getShortestPath(Graph graph, int startNumber, int destinationNumber){
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
Node startNode = graph.getNode(startNumber);
//boolean[] visitedNodes = new boolean[graph.getNumberOfNodes()];
......@@ -227,18 +195,18 @@ class Dijkstra{
System.out.println("Debugging");
while (!priorityQueue.isEmpty()){
Edge nextEdge = priorityQueue.poll();
int nextNode = nextEdge.getDestination();
int currentSum = pathWeight[nextEdge.getStart()] + nextEdge.getDrivingTime();
int nextNode = nextEdge.destination();
int currentSum = pathWeight[nextEdge.start()] + nextEdge.drivingTime();
if(currentSum < pathWeight[nextNode]){
pathWeight[nextNode] = currentSum;
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[nextEdge.getStart()]);
pathToNodes[nextNode] = new ArrayList<>(pathToNodes[nextEdge.start()]);
//pathToNodes[nextNode.getNumber()].addAll(pathToNodes[nextEdge.getStart().getNumber()]);
pathToNodes[nextNode].add(nextEdge);
priorityQueue.addAll( graph.getNode(nextNode).getEdges());
}
}
return pathToNodes[destinationNumber];
}
}*/
/*
public static ListOfPlaces getClosePlaces(Graph graph, int startNumber, String typOfPlace, int numberOfPlaces){
ListOfPlaces listOfPlaces = new ListOfPlaces(numberOfPlaces);
......
import java.util.*;
public class Trying {
}
class NewNode {
private final int number;
private final double latitude;
private final double longitude;
private String[] typeArray;
private String name;
public NewNode(int number, double latitude, double longitude) {
this.number = number;
this.latitude = latitude;
this.longitude = longitude;
}
public int getNumber() {
return number;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public String[] getTypeArray() {
return typeArray;
}
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public void setType(String type){
if(typeArray == null){
this.typeArray = new String[6];
}
this.setInFreeSpace(type,this.getTypeArray());
}
private void setInFreeSpace(String type, String[] typeArray){
for(int i = 0; i<typeArray.length; i++){
if(typeArray[i]==null){
typeArray[i]=type;
i=typeArray.length;
}
}
}
public boolean isTypeOf(String typeName){
return Arrays.stream(typeArray).anyMatch(t -> t.equalsIgnoreCase(typeName));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NewNode node)) return false;
return number == ((NewNode) o).getNumber();
}
@Override
public int hashCode() {
return Objects.hash(number);
}
}
class NewEdge implements Comparable<NewEdge>{
private final int start;
private final int destination;
private final int drivingTime;
private final int distance;
private final int speedLimit;
public NewEdge(int start, int destination, int drivingTime, int distance, int speedLimit) {
this.start = start;
this.destination = destination;
this.drivingTime = drivingTime;
this.distance = distance;
this.speedLimit = speedLimit;
}
public int getStart() {
return start;
}
public int getDestination() {
return destination;
}
public int getDrivingTime() {
return drivingTime;
}
public int getDistance() {
return distance;
}
public int getSpeedLimit() {
return speedLimit;
}
@Override
public int compareTo(NewEdge o) {
return Integer.compare(this.getDrivingTime(),o.getDrivingTime());
}
}
class NewGraph{
private final List<Node> nodes = new ArrayList<>();
private final Map<Integer,List<Edge>> graph = new HashMap<>();
public void readAllNodes(List<String> data){
for (int i = 1; i<data.size(); i++){
//System.out.println(data.get(i));
String [] splitText = Utils.dividedText(data.get(i),3);
//System.out.println(Arrays.toString(splitText));
this.setNodes(Integer.parseInt(splitText[0]),Double.parseDouble(splitText[1]),Double.parseDouble(splitText[2]));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment