Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.

Commit 77e31d2

Browse files
authored
Merge pull request #30 from NineNineFive/Lars
committing dijkstra
2 parents 4d538c5 + 19f090b commit 77e31d2

File tree

12 files changed

+335
-218
lines changed

12 files changed

+335
-218
lines changed
266 Bytes
Binary file not shown.
6.36 KB
Binary file not shown.
-3.41 KB
Binary file not shown.
0 Bytes
Binary file not shown.
821 Bytes
Binary file not shown.
895 Bytes
Binary file not shown.

src/files/models/AStar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package files.models;
2+
3+
public class AStar {
4+
}

src/files/models/Dijkstra.java

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package files.models;
2+
3+
import files.models.graph.Edge;
4+
import files.models.graph.Vertex;
5+
import javafx.util.Pair;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class Dijkstra {
12+
13+
14+
private Graph graph;
15+
16+
int counter = 0;
17+
18+
Map<Vertex,Vertex> predecessorMap= new HashMap<>(); // The key is a vertex, the value is the previous vertex
19+
Map<Vertex,Integer> distanceMap=new HashMap<>(); // The key is a vertex, the value is the shortest distance to it
20+
ArrayList<Vertex> queue = new ArrayList<Vertex>();
21+
int infinity = (int)Double.POSITIVE_INFINITY;
22+
int minimum = infinity;
23+
int weight = infinity;
24+
25+
26+
27+
public static void main(String[] args) {
28+
Dijkstra dijkstra = new Dijkstra();
29+
dijkstra.startDijkstra();
30+
}
31+
32+
public void startDijkstra(){
33+
// Create graph
34+
graph = this.makeSmallGraphB();
35+
// A
36+
//Vertex startNode = graph.getVertex("A");
37+
//Vertex endNode = graph.getVertex("E");
38+
39+
// B
40+
Vertex startNode = graph.getVertex("J");
41+
Vertex endNode = graph.getVertex("F");
42+
43+
Pair<Integer, Map<Vertex, Vertex>> results = dijkstra(startNode, endNode);
44+
Vertex current =endNode;
45+
ArrayList<Vertex> Path= new ArrayList<>();
46+
Path.add(endNode);
47+
48+
while ((current != startNode) && (results.getValue().get(current)!=null)) {
49+
current=results.getValue().get(current);
50+
Path.add(0,current);
51+
}
52+
53+
for(Vertex v : Path)
54+
{
55+
System.out.print( v.name);
56+
if (v!=endNode)
57+
System.out.print("->");
58+
}
59+
60+
}
61+
62+
public Graph makeSmallGraphA() {
63+
Graph myGraph= new Graph();
64+
final Vertex A= myGraph.addVertex("A");
65+
final Vertex B= myGraph.addVertex("B");
66+
final Vertex C = myGraph.addVertex("C");
67+
final Vertex D = myGraph.addVertex("D");
68+
final Vertex E = myGraph.addVertex("E");
69+
70+
myGraph.newEdge(A,B, 5, 3);
71+
myGraph.newEdge(A,C, 10, 3);
72+
myGraph.newEdge(B,C, 3, 3);
73+
myGraph.newEdge(B,D, 2, 3);
74+
myGraph.newEdge(B,E, 9, 3);
75+
myGraph.newEdge(C,B, 2, 3);
76+
myGraph.newEdge(C,E, 1, 3);
77+
myGraph.newEdge(D,E, 6, 3);
78+
myGraph.newEdge(E,D, 4, 3);
79+
80+
return myGraph;
81+
}
82+
83+
public Graph makeSmallGraphB() {
84+
Graph myGraph= new Graph();
85+
final Vertex A = myGraph.addVertex("A");
86+
final Vertex B = myGraph.addVertex("B");
87+
final Vertex C = myGraph.addVertex("C");
88+
final Vertex D = myGraph.addVertex("D");
89+
final Vertex E = myGraph.addVertex("E");
90+
final Vertex F = myGraph.addVertex("F");
91+
final Vertex G = myGraph.addVertex("G");
92+
final Vertex H = myGraph.addVertex("H");
93+
final Vertex I = myGraph.addVertex("I");
94+
final Vertex J = myGraph.addVertex("J");
95+
96+
myGraph.newEdge(A,B,10,0);
97+
myGraph.newEdge(A,D,20,0);
98+
myGraph.newEdge(A,E,20,0);
99+
myGraph.newEdge(A,F,5,0);
100+
myGraph.newEdge(A,G,15,0);
101+
myGraph.newEdge(B,C,7,0);
102+
myGraph.newEdge(B,D,10,0);
103+
myGraph.newEdge(C,B,15,0);
104+
myGraph.newEdge(C,D,5,0);
105+
myGraph.newEdge(D,E,10,0);
106+
myGraph.newEdge(E,F,5,0);
107+
myGraph.newEdge(G,F,10,0);
108+
myGraph.newEdge(H,A,5,0);
109+
myGraph.newEdge(H,B,20,0);
110+
myGraph.newEdge(H,G,5,0);
111+
myGraph.newEdge(I,B,15,0);
112+
myGraph.newEdge(I,H,20,0);
113+
myGraph.newEdge(I,J,10,0);
114+
myGraph.newEdge(J,B,5,0);
115+
myGraph.newEdge(J,C,15,0);
116+
117+
return myGraph;
118+
}
119+
120+
////////////////////////////////////////////////////////////////////////////////////////////
121+
///////////////////////////////// TODO: Implement new dijkstra 25-11-2019 //////////////////////////
122+
//////////////////////////////////////////////////////////////////////////////////////
123+
124+
public Pair<Integer, Map<Vertex,Vertex>> dijkstra(Vertex startNode, Vertex endNode) {
125+
126+
for(Vertex vertex: graph.getVertices()) {
127+
distanceMap.put(vertex,infinity); // This is the nodes and their weight
128+
predecessorMap.put(vertex, null); // we set the vertex to be null, which is meant to be "not visited"
129+
}
130+
131+
Vertex from = startNode; // Set previous to be start node
132+
Vertex next;
133+
134+
distanceMap.put(startNode,0); // Setting previous distance map to 0
135+
predecessorMap.put(startNode,new Vertex("Start")); // Initialize the start as "coming from previous" (which at start is start)
136+
137+
while(predecessorMap.containsValue(null)&&from!=null) {
138+
System.out.println(from.name + " is having the weight of: " + distanceMap.get(from));
139+
System.out.println("For: " + from.name + " {");
140+
for(Edge edge : from.getEdges()) {
141+
if(!edge.getToVertex().equals(from)){
142+
Vertex to = edge.getToVertex();
143+
System.out.print("\t From: " + from.name + " ");
144+
System.out.print("\t To: " + to.name + " ");
145+
System.out.print("\t Distance: " + edge.distance);
146+
System.out.println();
147+
weight = edge.distance+distanceMap.get(from);
148+
if (weight <= distanceMap.get(to)) {
149+
weight = edge.distance+distanceMap.get(from);
150+
distanceMap.put(to, weight); // Set the weight to distMap
151+
System.out.println("\t " + to.name + " is now: " + weight);
152+
} else {
153+
predecessorMap.remove(from); // Removing vertex
154+
System.out.println("\t removed " + from.name);
155+
// we are not setting a new previous here, the reason is that we instead had a loop that runs from the other vertices.
156+
}
157+
}
158+
}
159+
160+
next = findMin(from);
161+
System.out.println("set "+next.name+" to "+from.name);
162+
predecessorMap.put(next,from);
163+
from = next;
164+
165+
if(next.equals(endNode)){
166+
System.out.println("this is the end node!");
167+
from = predecessorMap.get(null);
168+
}
169+
}
170+
171+
return (new Pair<Integer,Map<Vertex,Vertex>> (distanceMap.get(startNode), predecessorMap));
172+
}
173+
174+
private Vertex findMin(Vertex vertex) {
175+
Vertex closestVertex = null;
176+
int lowestDistance = infinity;
177+
for (Edge edge : vertex.getEdges()) {
178+
179+
if(!edge.getToVertex().equals(vertex)){
180+
int distance = edge.distance;
181+
if (edge.distance <= lowestDistance) {
182+
lowestDistance = distance;
183+
closestVertex = edge.getToVertex();
184+
}
185+
}
186+
}
187+
return closestVertex;
188+
}
189+
}

src/files/models/Graph.java

Lines changed: 4 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package files.models;
2-
import javafx.util.Pair;
2+
import files.models.graph.Edge;
3+
import files.models.graph.Vertex;
34
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.Map;
65

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

35-
/////////////////////////////////////
36-
int infinity = (int)Double.POSITIVE_INFINITY;
37-
38-
public Pair<Integer, Map<Vertex,Vertex>> dijkstra(Vertex startNode, Vertex endNode)
39-
{
40-
Map<Vertex,Vertex> predecessorMap= new HashMap<>(); // The key is a vertex, the value is the previous vertex
41-
Map<Vertex,Integer> distanceMap=new HashMap<>(); // The key is a vertex, the value is the shortest distance to it
42-
// initialize arrays
43-
for(Vertex vertex: vertices) {
44-
distanceMap.put(vertex,infinity); // This is the nodes and their weight
45-
predecessorMap.put(vertex, null); // we set the vertex to be null, which is meant to be "not visited"
46-
}
47-
48-
/////////////////////////////implement Dijkstra /////////////////////////////
49-
// Initialize values
50-
51-
52-
//Vertex previous = new Vertex("Start"); // Set start to previous
53-
54-
Vertex previous = startNode; // Set previous to be start node
55-
Vertex current = null;
56-
Vertex next = null;
57-
58-
distanceMap.put(previous,0); // Setting previous distance map to 0
59-
predecessorMap.put(startNode,new Vertex("Start")); // Initialize the start as "coming from previous" (which at start is start)
60-
61-
ArrayList<Vertex> queue = new ArrayList<Vertex>();
62-
63-
int minimum = infinity;
64-
int weight = infinity;
65-
66-
while(predecessorMap.containsValue(null)){
67-
//TODO: what would happen if we start at a point, where we cant evaluate everything?
68-
// TODO: FIX QUEUE, Make a foreach node check after evaluating every network from the startNode
69-
// TODO: print a message if the system couldnt find endNode, fix the remove script
70-
System.out.println(previous.name+" is having the weight of: "+distanceMap.get(previous));
71-
System.out.println("For: " + previous.name + " {");
72-
73-
for (Edge edge : previous.getEdges()) {
74-
if (!previous.equals(edge.getToVertex())) {
75-
current = edge.getToVertex(); // Current is set to the end of the edge, if the relation arrow is pointing outwards
76-
if (predecessorMap.get(current) == null) { // check if the end of the edge is null (available)
77-
System.out.print("\t From: " + previous.name + " "); System.out.print("\t To: " + current.name + " ");
78-
System.out.print("\t Distance: " + edge.distance); System.out.println();
79-
80-
if (weight <= distanceMap.get(current)) {
81-
weight = edge.distance+distanceMap.get(previous);
82-
distanceMap.put(current, weight); // Set the weight to distMap
83-
System.out.println("\t " + current.name + " is now: " + weight);
84-
} else {
85-
predecessorMap.remove(previous); // Removing vertex
86-
System.out.println("\t removed " + previous.name);
87-
// we are not setting a new previous here, the reason is that we instead had a loop that runs from the other vertices.
88-
}
89-
90-
if (minimum >= edge.distance) {
91-
minimum = edge.distance; // Setting the minimum to be the lowest of the edge distances in this for loop
92-
next = current;
93-
} else if(predecessorMap.get(current)==null&&!current.equals(endNode)) {
94-
queue.add(current); // This is the Queue of the other nodes, that is higher than the minimum
95-
}
96-
} else {
97-
System.out.println("not evaluating: " + current.name);
98-
}
99-
}
100-
}
101-
102-
System.out.println("Queue size: "+queue.size());
103-
for(Vertex q : queue){
104-
System.out.println("In Queue-> "+q.name);
105-
}
106-
107-
108-
if(previous==next&&!next.equals(endNode)){
109-
110-
System.out.println("We jump (queue)");
111-
112-
next = queue.get(0);
113-
114-
System.out.println("Jumper: i put key: "+next.name+" the value: "+predecessorMap.get(previous).name);
115-
predecessorMap.put(next,predecessorMap.get(previous));
116-
current = queue.get(0);
117-
previous = queue.get(0);
118-
queue.remove(0);
119-
120-
} else {
121-
System.out.println("i put key: "+next.name+" the value: "+previous.name);
122-
predecessorMap.put(next,previous);
123-
previous = next;
124-
System.out.println("\t Minimum is: " + minimum + " So we select: " + next.name);
125-
System.out.println("}");
126-
System.out.println();
127-
128-
}
129-
}
130-
131-
return (new Pair<Integer,Map<Vertex,Vertex>> (distanceMap.get(startNode), predecessorMap));
132-
}
133-
134-
}
135-
136-
class Vertex {
137-
public String name;
138-
public ArrayList<Edge> edges = new ArrayList<>();
139-
public Vertex(String name){
140-
this.name = name;
141-
}
142-
public void addEdge(Edge edge){
143-
edges.add(edge);
144-
}
145-
public ArrayList<Edge> getEdges(){
146-
return edges;
34+
public ArrayList<Vertex> getVertices(){
35+
return vertices;
14736
}
14837
}
14938

150-
class Edge {
151-
private Vertex from;
152-
private Vertex to;
153-
public int distance=0;
154-
public int time=0;
155-
156-
public Vertex getToVertex() {
157-
return this.to;
158-
}
159-
160-
public Edge(Vertex from, Vertex to){
161-
this.from=from;
162-
this.to=to;
163-
this.from.addEdge(this);
164-
this.to.addEdge(this); //If not directional
165-
}
166-
}

0 commit comments

Comments
 (0)