public static void main(String args[]) { Grafo teste = new Grafo(); teste.setVertices(lerGrafo(args[0])); Vertice i1 = new Vertice(); Vertice i2 = new Vertice(); i1 = teste.encontrarVertice(args[1]); i2 = teste.encontrarVertice(args[2]); List<Vertice> resultado = new ArrayList<Vertice>(); Dijkstra algoritmo = new Dijkstra(); resultado = algoritmo.encontrarMenorCaminhoDijkstra(teste, i1, i2); System.out.println("Esse é o menor caminho feito pelo algoritmo:" + resultado); }
private double[][] getMinDistances() { double[][] minDistances = new double[clients.length][clients.length]; for (int i = 0; i < clients.length; i++) { double[] result = Dijkstra.getMinDistance(i, clients); minDistances[i] = result; } return minDistances; }
public static void main(String[] args) { Vertex v0 = new Vertex("Harrisburg"); Vertex v1 = new Vertex("Baltimore"); Vertex v2 = new Vertex("Washington"); Vertex v3 = new Vertex("Philadelphia"); Vertex v4 = new Vertex("Binghamton"); Vertex v5 = new Vertex("Allentown"); Vertex v6 = new Vertex("New York"); ArrayList<Vertex> vertexes = new ArrayList<>(); vertexes.add(v0); vertexes.add(v1); vertexes.add(v2); vertexes.add(v3); vertexes.add(v4); vertexes.add(v5); vertexes.add(v6); ArrayList<Edge> edges = new ArrayList<>(); edges.add(new Edge(v2, v1, 10)); edges.add(new Edge(v3, v0, 3)); edges.add(new Edge(v2, v3, 1)); edges.add(new Edge(v4, v2, 100)); edges.add(new Edge(v5, v4, 6)); edges.add(new Edge(v6, v4, 30)); edges.add(new Edge(v0, v6, 20)); edges.add(new Edge(v3, v4, 5)); // Dijkstra DJ = new Dijkstra(); Dijkstra.computePaths(v5); System.out.println("Vzdalenost do v0: " + v0.minDistance); List<Vertex> path = Dijkstra.getShortestPathTo(v0); System.out.println("Cesta přes: " + path); }
@Override public void run() { try { System.out.println("Processing current input status message"); StatusInputMsg receivedNewMsg = queue.take(); // ------------------------------------------------------------------------- ArrayList<Path> currPathList = new ArrayList<Path>(receivedNewMsg.getCurrEdge()); Map<String, Gate> currGateStatus = new HashMap<String, Gate>(); Map<String, Gate> finalCurrGateStatus = new HashMap<String, Gate>(); Map<Gate, ArrayList<Edge>> adjacencyStatus = getCurrentAdjacencyStatus(currPathList); for (Path path : currPathList) { Gate tempGate = new Gate(path.getStartNode()); currGateStatus.put(tempGate.toString(), tempGate); } // At this instant, form the graph and find the shorted path with the current edges for (Map.Entry<String, Gate> e1 : currGateStatus.entrySet()) { String key = e1.getKey(); Gate gate = e1.getValue(); ArrayList<Edge> a = adjacencyStatus.get(gate); Edge[] b = new Edge[a.size()]; // Update the adjecencies with current input. for (int i = 0; i < a.size(); i++) { Edge e = new Edge(gate, a.get(i).travelTime); b[i] = e; } gate.adjacencies = (Edge[]) b.clone(); finalCurrGateStatus.put(key, gate); } System.out.println("Succcessfully processed current Input Status"); /* for(Map.Entry<String, Gate> entry : finalCurrGateStatus.entrySet()){ System.out.println("key "+entry.getKey()); System.out.println("value "+entry.getValue().shortestTime); } */ // ------------------------------------------------------------------------- ArrayList<DepartureFlight> currDeptFlightList = new ArrayList<DepartureFlight>(receivedNewMsg.getCurrFlightStatus()); Map<String, String> currFlightIdGateNameStatus = new HashMap<String, String>(); for (DepartureFlight deptFlight : currDeptFlightList) { currFlightIdGateNameStatus.put(deptFlight.getFlightId(), deptFlight.getFlightGate()); } currFlightIdGateNameStatus.put("ARRIVAL", "BaggageClaim"); System.out.println("Succcessfully processed current flight Status"); // ------------------------------------------------------------------------- ArrayList<Baggage> currBaggageList = new ArrayList<Baggage>(receivedNewMsg.getCurrBaggage()); for (Baggage bg : currBaggageList) { String currBagNumber = bg.getBagNumber(); String currStartNode = bg.getEntryPoint(); String currEndNode = currFlightIdGateNameStatus.get(bg.getFlightId()); // This sleep can be varied to see the effect of processing time on the input buffer // management. sleep(1); Dijkstra.computeShortestPath(finalCurrGateStatus.get(currStartNode)); // run Dijkstra System.out.println( "For bag " + currBagNumber + " Time to reach " + currEndNode + ": " + finalCurrGateStatus.get(currEndNode).shortestTime); List<Gate> path = Dijkstra.getShortestPathTo(finalCurrGateStatus.get(currEndNode)); System.out.println("Path: " + path.toString()); } System.out.println( "Succcessfully generated shortest duration paths to each baggage in input message"); } catch (InterruptedException e) { e.printStackTrace(); } }
/** * Loads the graph from the input file. Runs Dijktra's algorithm on every origin node and for each * destination node, compares the returned path with the correct path in the resultsFile. * * @param inputFile * @param resultsFile * @return * @throws IOException */ public static boolean testDijkstra(String inputFile, String resultsFile) throws IOException { Dijkstra dijkstra = new Dijkstra(inputFile); MapGraph gr = dijkstra.getGraph(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(resultsFile)); String s; for (int i = 0; i < gr.numNodes(); i++) { CityNode originNode = gr.getNode(i); String originCity = originNode.getCity(); s = br.readLine(); if (s == null) { System.out.println("Error reading from the resultsFile"); return false; } assert (s.equals(originCity)); // Run Dijkstra's algorithm to compute the paths from originCity // to all other nodes dijkstra.computePaths(originNode); // Go along all the destinations for (int j = 0; j < gr.numNodes(); j++) { if (j != i) { CityNode destNode = gr.getNode(j); String destCity = destNode.getCity(); // get the path between originCity and destCity ArrayList<Integer> path = dijkstra.shortestPath(destNode); String pathString = br.readLine(); if (pathString == null) { System.out.println("Error reading from the resultsFile"); return false; } String[] expectedPath = pathString.split(" "); if (path.size() != expectedPath.length) { System.out.println("Your path's size = " + path.size()); System.out.println("Expected the path of size: " + expectedPath.length); DijkstraTest.printInfo(originCity, destCity, path, expectedPath, gr); return false; } assert (expectedPath[expectedPath.length - 1].equals(destCity)); // comparing the cities on the returned path and the expected path for (int k = 0; k < path.size(); k++) { Integer num = path.get(k); CityNode nodeOnPath = gr.getNode(num); String cityString = nodeOnPath.getCity(); if (!cityString.equals(expectedPath[k])) { DijkstraTest.printInfo(originCity, destCity, path, expectedPath, gr); return false; } } // for loop on nodes on the path } // if i!=j } // for destinations } // for origins } catch (IOException e) { e.getMessage(); } return true; }
@Test public void testReplaceLongerPath() { assertEquals(5, dijkstra.findMinimumPath(1, 4)); }
@Test public void testDoNotReplaceShorterPath() { assertEquals(5, dijkstra.findMinimumPath(1, 5)); }
@Test public void testConnectedThroughCommonNode() { assertEquals(3, dijkstra.findMinimumPath(1, 2)); }
@Test public void testConnectedNeighbors() { assertEquals(1, dijkstra.findMinimumPath(0, 1)); }
@Test public void testTwoUnconnectedComponentsHaveDistanceOfInfinity() { assertEquals(Dijkstra.INFINITY, dijkstra.findMinimumPath(0, 9)); }