Exemplo n.º 1
0
  /**
   * to performe the relaxation between two vertices
   *
   * @param firstVertex the first vertex
   * @param neighbour the neighbouring vertex of the first vertex
   * @param weightOfEdge the weight of the edge between the first vertex and the neighbouring vertex
   */
  private static void relax(Vertex firstVertex, Vertex neighbour, int weightOfEdge) {

    if (neighbour.getDistance() > firstVertex.getDistance() + weightOfEdge) {
      neighbour.setDistance(firstVertex.getDistance() + weightOfEdge);
      neighbour.setPredecessor(firstVertex);
      neighbour.setState(EndStateOfPoint.REACHABLE);
    }
  }
Exemplo n.º 2
0
  /**
   * initializes the Dijkstra algorithm by setting the start vertex, setting a queue to help
   * performing Dijkstra
   *
   * @param graph the graph in which the Dijkstra algorithm should be performed
   * @param startVertexID the start vertex
   * @param destinationVertexID the end vertex
   */
  public static void initializeDijkstra(
      Graph<Vertex, Edge<Vertex>> graph, int startVertexID, int destinationVertexID) {

    for (int i = 0; i < graph.getVertices().size(); i++) {
      graph.getVertex(i).setDistance(Integer.MAX_VALUE);
      graph.getVertex(i).setPredecessor(null);
    }

    Vertex startVertex = graph.getVertex(startVertexID);
    startVertex.setDistance(0);
    startVertex.setPredecessor(startVertex);
    startVertex.setState(EndStateOfPoint.STARTPOINT);

    ArrayList<Vertex> queue = new ArrayList<Vertex>();

    for (int i = 0; i < graph.getVertices().size(); i++) {
      queue.add(graph.getVertex(i));
    }

    performeDijkstra(graph, queue, startVertexID, destinationVertexID);
  }