/** * Dijkstra doesn't allow negative edge weights so throw an exception if one was found. * * @throws IllegalArgumentException if the graph contains negative weights */ private void checkForNegativeWeights() { for (Edge e : graph.getEdges()) { if (e.getWeight() < 0) { throw new IllegalArgumentException("graph contains negative weights!"); } } }
private void executeDijkstra() { // determine minimum distance vertex and add it to the path. while (!this.priorityQueue.isEmpty()) { Vertex vertex = this.priorityQueue.extractMin(); for (Edge e : graph.getIncidentEdgesOf(vertex)) { relax(vertex, e.getVertexB(), e.getWeight(), dist, pred); } } }
/** * Set every distance to int.max and sets the start vertex by setting distance to 0 * * @throws IllegalArgumentException if the graph contains negative weights * @param start */ private void init(Vertex start) throws IllegalArgumentException { checkForNegativeWeights(); for (Vertex v : graph.getVertices()) { dist.put(v, Double.MAX_VALUE); } // sets start Vertex by setting distance to 0 dist.put(start, 0.0); }