Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file modified out/production/PathfindingProject/Files/models/Graph.class
Binary file not shown.
Binary file modified out/production/PathfindingProject/Files/models/Pathfinding.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions src/files/models/AStar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package files.models;

public class AStar {
}
189 changes: 189 additions & 0 deletions src/files/models/Dijkstra.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package files.models;

import files.models.graph.Edge;
import files.models.graph.Vertex;
import javafx.util.Pair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Dijkstra {


private Graph graph;

int counter = 0;

Map<Vertex,Vertex> predecessorMap= new HashMap<>(); // The key is a vertex, the value is the previous vertex
Map<Vertex,Integer> distanceMap=new HashMap<>(); // The key is a vertex, the value is the shortest distance to it
ArrayList<Vertex> queue = new ArrayList<Vertex>();
int infinity = (int)Double.POSITIVE_INFINITY;
int minimum = infinity;
int weight = infinity;



public static void main(String[] args) {
Dijkstra dijkstra = new Dijkstra();
dijkstra.startDijkstra();
}

public void startDijkstra(){
// Create graph
graph = this.makeSmallGraphB();
// A
//Vertex startNode = graph.getVertex("A");
//Vertex endNode = graph.getVertex("E");

// B
Vertex startNode = graph.getVertex("J");
Vertex endNode = graph.getVertex("F");

Pair<Integer, Map<Vertex, Vertex>> results = dijkstra(startNode, endNode);
Vertex current =endNode;
ArrayList<Vertex> Path= new ArrayList<>();
Path.add(endNode);

while ((current != startNode) && (results.getValue().get(current)!=null)) {
current=results.getValue().get(current);
Path.add(0,current);
}

for(Vertex v : Path)
{
System.out.print( v.name);
if (v!=endNode)
System.out.print("->");
}

}

public Graph makeSmallGraphA() {
Graph myGraph= new Graph();
final Vertex A= myGraph.addVertex("A");
final Vertex B= myGraph.addVertex("B");
final Vertex C = myGraph.addVertex("C");
final Vertex D = myGraph.addVertex("D");
final Vertex E = myGraph.addVertex("E");

myGraph.newEdge(A,B, 5, 3);
myGraph.newEdge(A,C, 10, 3);
myGraph.newEdge(B,C, 3, 3);
myGraph.newEdge(B,D, 2, 3);
myGraph.newEdge(B,E, 9, 3);
myGraph.newEdge(C,B, 2, 3);
myGraph.newEdge(C,E, 1, 3);
myGraph.newEdge(D,E, 6, 3);
myGraph.newEdge(E,D, 4, 3);

return myGraph;
}

public Graph makeSmallGraphB() {
Graph myGraph= new Graph();
final Vertex A = myGraph.addVertex("A");
final Vertex B = myGraph.addVertex("B");
final Vertex C = myGraph.addVertex("C");
final Vertex D = myGraph.addVertex("D");
final Vertex E = myGraph.addVertex("E");
final Vertex F = myGraph.addVertex("F");
final Vertex G = myGraph.addVertex("G");
final Vertex H = myGraph.addVertex("H");
final Vertex I = myGraph.addVertex("I");
final Vertex J = myGraph.addVertex("J");

myGraph.newEdge(A,B,10,0);
myGraph.newEdge(A,D,20,0);
myGraph.newEdge(A,E,20,0);
myGraph.newEdge(A,F,5,0);
myGraph.newEdge(A,G,15,0);
myGraph.newEdge(B,C,7,0);
myGraph.newEdge(B,D,10,0);
myGraph.newEdge(C,B,15,0);
myGraph.newEdge(C,D,5,0);
myGraph.newEdge(D,E,10,0);
myGraph.newEdge(E,F,5,0);
myGraph.newEdge(G,F,10,0);
myGraph.newEdge(H,A,5,0);
myGraph.newEdge(H,B,20,0);
myGraph.newEdge(H,G,5,0);
myGraph.newEdge(I,B,15,0);
myGraph.newEdge(I,H,20,0);
myGraph.newEdge(I,J,10,0);
myGraph.newEdge(J,B,5,0);
myGraph.newEdge(J,C,15,0);

return myGraph;
}

////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// TODO: Implement new dijkstra 25-11-2019 //////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

public Pair<Integer, Map<Vertex,Vertex>> dijkstra(Vertex startNode, Vertex endNode) {

for(Vertex vertex: graph.getVertices()) {
distanceMap.put(vertex,infinity); // This is the nodes and their weight
predecessorMap.put(vertex, null); // we set the vertex to be null, which is meant to be "not visited"
}

Vertex from = startNode; // Set previous to be start node
Vertex next;

distanceMap.put(startNode,0); // Setting previous distance map to 0
predecessorMap.put(startNode,new Vertex("Start")); // Initialize the start as "coming from previous" (which at start is start)

while(predecessorMap.containsValue(null)&&from!=null) {
System.out.println(from.name + " is having the weight of: " + distanceMap.get(from));
System.out.println("For: " + from.name + " {");
for(Edge edge : from.getEdges()) {
if(!edge.getToVertex().equals(from)){
Vertex to = edge.getToVertex();
System.out.print("\t From: " + from.name + " ");
System.out.print("\t To: " + to.name + " ");
System.out.print("\t Distance: " + edge.distance);
System.out.println();
weight = edge.distance+distanceMap.get(from);
if (weight <= distanceMap.get(to)) {
weight = edge.distance+distanceMap.get(from);
distanceMap.put(to, weight); // Set the weight to distMap
System.out.println("\t " + to.name + " is now: " + weight);
} else {
predecessorMap.remove(from); // Removing vertex
System.out.println("\t removed " + from.name);
// we are not setting a new previous here, the reason is that we instead had a loop that runs from the other vertices.
}
}
}

next = findMin(from);
System.out.println("set "+next.name+" to "+from.name);
predecessorMap.put(next,from);
from = next;

if(next.equals(endNode)){
System.out.println("this is the end node!");
from = predecessorMap.get(null);
}
}

return (new Pair<Integer,Map<Vertex,Vertex>> (distanceMap.get(startNode), predecessorMap));
}

private Vertex findMin(Vertex vertex) {
Vertex closestVertex = null;
int lowestDistance = infinity;
for (Edge edge : vertex.getEdges()) {

if(!edge.getToVertex().equals(vertex)){
int distance = edge.distance;
if (edge.distance <= lowestDistance) {
lowestDistance = distance;
closestVertex = edge.getToVertex();
}
}
}
return closestVertex;
}
}
136 changes: 4 additions & 132 deletions src/files/models/Graph.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package files.models;
import javafx.util.Pair;
import files.models.graph.Edge;
import files.models.graph.Vertex;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Graph {
private ArrayList<Vertex> vertices = new ArrayList<>();
Expand Down Expand Up @@ -32,135 +31,8 @@ public void newEdge(Vertex from, Vertex to, int distance, int time) {
edge.time = time;
}

/////////////////////////////////////
int infinity = (int)Double.POSITIVE_INFINITY;

public Pair<Integer, Map<Vertex,Vertex>> dijkstra(Vertex startNode, Vertex endNode)
{
Map<Vertex,Vertex> predecessorMap= new HashMap<>(); // The key is a vertex, the value is the previous vertex
Map<Vertex,Integer> distanceMap=new HashMap<>(); // The key is a vertex, the value is the shortest distance to it
// initialize arrays
for(Vertex vertex: vertices) {
distanceMap.put(vertex,infinity); // This is the nodes and their weight
predecessorMap.put(vertex, null); // we set the vertex to be null, which is meant to be "not visited"
}

/////////////////////////////implement Dijkstra /////////////////////////////
// Initialize values


//Vertex previous = new Vertex("Start"); // Set start to previous

Vertex previous = startNode; // Set previous to be start node
Vertex current = null;
Vertex next = null;

distanceMap.put(previous,0); // Setting previous distance map to 0
predecessorMap.put(startNode,new Vertex("Start")); // Initialize the start as "coming from previous" (which at start is start)

ArrayList<Vertex> queue = new ArrayList<Vertex>();

int minimum = infinity;
int weight = infinity;

while(predecessorMap.containsValue(null)){
//TODO: what would happen if we start at a point, where we cant evaluate everything?
// TODO: FIX QUEUE, Make a foreach node check after evaluating every network from the startNode
// TODO: print a message if the system couldnt find endNode, fix the remove script
System.out.println(previous.name+" is having the weight of: "+distanceMap.get(previous));
System.out.println("For: " + previous.name + " {");

for (Edge edge : previous.getEdges()) {
if (!previous.equals(edge.getToVertex())) {
current = edge.getToVertex(); // Current is set to the end of the edge, if the relation arrow is pointing outwards
if (predecessorMap.get(current) == null) { // check if the end of the edge is null (available)
System.out.print("\t From: " + previous.name + " "); System.out.print("\t To: " + current.name + " ");
System.out.print("\t Distance: " + edge.distance); System.out.println();

if (weight <= distanceMap.get(current)) {
weight = edge.distance+distanceMap.get(previous);
distanceMap.put(current, weight); // Set the weight to distMap
System.out.println("\t " + current.name + " is now: " + weight);
} else {
predecessorMap.remove(previous); // Removing vertex
System.out.println("\t removed " + previous.name);
// we are not setting a new previous here, the reason is that we instead had a loop that runs from the other vertices.
}

if (minimum >= edge.distance) {
minimum = edge.distance; // Setting the minimum to be the lowest of the edge distances in this for loop
next = current;
} else if(predecessorMap.get(current)==null&&!current.equals(endNode)) {
queue.add(current); // This is the Queue of the other nodes, that is higher than the minimum
}
} else {
System.out.println("not evaluating: " + current.name);
}
}
}

System.out.println("Queue size: "+queue.size());
for(Vertex q : queue){
System.out.println("In Queue-> "+q.name);
}


if(previous==next&&!next.equals(endNode)){

System.out.println("We jump (queue)");

next = queue.get(0);

System.out.println("Jumper: i put key: "+next.name+" the value: "+predecessorMap.get(previous).name);
predecessorMap.put(next,predecessorMap.get(previous));
current = queue.get(0);
previous = queue.get(0);
queue.remove(0);

} else {
System.out.println("i put key: "+next.name+" the value: "+previous.name);
predecessorMap.put(next,previous);
previous = next;
System.out.println("\t Minimum is: " + minimum + " So we select: " + next.name);
System.out.println("}");
System.out.println();

}
}

return (new Pair<Integer,Map<Vertex,Vertex>> (distanceMap.get(startNode), predecessorMap));
}

}

class Vertex {
public String name;
public ArrayList<Edge> edges = new ArrayList<>();
public Vertex(String name){
this.name = name;
}
public void addEdge(Edge edge){
edges.add(edge);
}
public ArrayList<Edge> getEdges(){
return edges;
public ArrayList<Vertex> getVertices(){
return vertices;
}
}

class Edge {
private Vertex from;
private Vertex to;
public int distance=0;
public int time=0;

public Vertex getToVertex() {
return this.to;
}

public Edge(Vertex from, Vertex to){
this.from=from;
this.to=to;
this.from.addEdge(this);
this.to.addEdge(this); //If not directional
}
}
Loading