22
33import files .models .graph .Edge ;
44import files .models .graph .Vertex ;
5- import javafx .util .Pair ;
65
7- import java .util .ArrayList ;
8- import java .util .HashMap ;
9- import java .util .Map ;
6+ import java .util .*;
107
118public class Dijkstra {
129
13-
1410 private Graph graph ;
1511
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-
12+ int infinity = (int ) Double .POSITIVE_INFINITY ;
2513
2614
2715 public static void main (String [] args ) {
2816 Dijkstra dijkstra = new Dijkstra ();
2917 dijkstra .startDijkstra ();
3018 }
3119
32- public void startDijkstra (){
20+ public void startDijkstra () {
3321 // Create graph
22+ //graph = this.makeDijkstraGraph();
3423 graph = this .makeSmallGraphB ();
3524 // A
3625 //Vertex startNode = graph.getVertex("A");
@@ -40,48 +29,19 @@ public void startDijkstra(){
4029 Vertex startNode = graph .getVertex ("J" );
4130 Vertex endNode = graph .getVertex ("F" );
4231
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- }
32+ ArrayList <Vertex > result = dijkstra (startNode , endNode );
5233
53- for (Vertex v : Path )
54- {
55- System .out .print ( v .name );
34+ for (Vertex v : result ) {
35+ System .out .print ( v .name + " Dist:" + v .distance + " " );
5636 if (v !=endNode )
57- System .out .print ("->" );
37+ System .out .print ("-> " );
5838 }
5939
6040 }
6141
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- }
8242
8343 public Graph makeSmallGraphB () {
84- Graph myGraph = new Graph ();
44+ Graph myGraph = new Graph ();
8545 final Vertex A = myGraph .addVertex ("A" );
8646 final Vertex B = myGraph .addVertex ("B" );
8747 final Vertex C = myGraph .addVertex ("C" );
@@ -93,26 +53,26 @@ public Graph makeSmallGraphB() {
9353 final Vertex I = myGraph .addVertex ("I" );
9454 final Vertex J = myGraph .addVertex ("J" );
9555
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 );
56+ myGraph .newEdge (A , B , 10 , 0 );
57+ myGraph .newEdge (A , D , 20 , 0 );
58+ myGraph .newEdge (A , E , 20 , 0 );
59+ myGraph .newEdge (A , F , 5 , 0 );
60+ myGraph .newEdge (A , G , 15 , 0 );
61+ myGraph .newEdge (B , C , 7 , 0 );
62+ myGraph .newEdge (B , D , 10 , 0 );
63+ myGraph .newEdge (C , B , 15 , 0 );
64+ myGraph .newEdge (C , D , 5 , 0 );
65+ myGraph .newEdge (D , E , 10 , 0 );
66+ myGraph .newEdge (E , F , 5 , 0 );
67+ myGraph .newEdge (G , F , 10 , 0 );
68+ myGraph .newEdge (H , A , 5 , 0 );
69+ myGraph .newEdge (H , B , 20 , 0 );
70+ myGraph .newEdge (H , G , 5 , 0 );
71+ myGraph .newEdge (I , B , 15 , 0 );
72+ myGraph .newEdge (I , H , 20 , 0 );
73+ myGraph .newEdge (I , J , 10 , 0 );
74+ myGraph .newEdge (J , B , 5 , 0 );
75+ myGraph .newEdge (J , C , 15 , 0 );
11676
11777 return myGraph ;
11878 }
@@ -121,69 +81,47 @@ public Graph makeSmallGraphB() {
12181 ///////////////////////////////// TODO: Implement new dijkstra 25-11-2019 //////////////////////////
12282 //////////////////////////////////////////////////////////////////////////////////////
12383
124- public Pair <Integer , Map <Vertex ,Vertex >> dijkstra (Vertex startNode , Vertex endNode ) {
84+ public ArrayList <Vertex > dijkstra (Vertex startNode , Vertex endNode ) {
85+
86+ TreeSet <Vertex > graphTreeSet = new TreeSet <>(Comparator .comparingInt (Vertex ::getDistance ));
12587
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"
88+ for (Vertex vertex : graph .getVertices ()) {
89+ vertex . setDistance ( infinity );
90+ vertex . setPredecessor ( null );
12991 }
92+ startNode .setDistance (0 );
93+ graphTreeSet .addAll (graph .getVertices ());
94+ Vertex current ;
13095
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- }
96+ while (graphTreeSet .size () != 0 ) {
97+ current = graphTreeSet .first ();
98+ System .out .println (" CURRENT: " + current .name );
15999
160- next = findMin (from );
161- System .out .println ("set " +next .name +" to " +from .name );
162- predecessorMap .put (next ,from );
163- from = next ;
100+ for (Edge edge : current .edges ) {
164101
165- if (next .equals (endNode )){
166- System .out .println ("this is the end node!" );
167- from = predecessorMap .get (null );
102+ if (current .distance != infinity && edge .distance + current .distance < edge .getToVertex ().distance ) {
103+ edge .getToVertex ().setDistance (edge .distance + current .distance );
104+ edge .getToVertex ().setPredecessor (current );
105+ graphTreeSet .remove (edge .getToVertex ());
106+ graphTreeSet .add (edge .getToVertex ());
107+ }
108+
109+ System .out .println ("Evaluating: " + edge .getFromVertex ().name + " -> " + edge .getToVertex ().name
110+ + " dist: " + edge .getToVertex ().distance );
168111 }
169- }
170112
171- return (new Pair <Integer ,Map <Vertex ,Vertex >> (distanceMap .get (startNode ), predecessorMap ));
172- }
113+ graphTreeSet .remove (current );
114+ System .out .println ("removed: " +current .name );
115+ }
173116
174- private Vertex findMin (Vertex vertex ) {
175- Vertex closestVertex = null ;
176- int lowestDistance = infinity ;
177- for (Edge edge : vertex .getEdges ()) {
117+ Vertex resultCurrent = endNode ;
118+ ArrayList <Vertex > Path = new ArrayList <>();
119+ Path .add (endNode );
178120
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- }
121+ while ((resultCurrent != startNode ) && (resultCurrent .predecessor != null )) {
122+ resultCurrent = resultCurrent .predecessor ;
123+ Path .add (0 ,resultCurrent );
186124 }
187- return closestVertex ;
125+ return Path ;
188126 }
189127}
0 commit comments