|
| 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 | +} |
0 commit comments