Example #1
0
 public void initializePQ(MyGraph graph) {
   for (String vertex : graph.getVertices()) {
     pq.add(vertex, Double.POSITIVE_INFINITY);
     toVisit.add(vertex);
     distance.put(vertex, Double.POSITIVE_INFINITY);
     parent.put(vertex, null);
   }
 }
Example #2
0
  public void executeDijkstra(MyGraph graph, String start) {

    initializePQ(graph);

    source = start;
    pq.changePriority("a", 0.0);
    distance.put(source, 0.0);
    parent.put(source, source);
    visited.add(source);
    toVisit.remove(source);

    String last_added = pq.removeMin();
    HashMap<String, Double> neighbouring_edges = graph.getEdgesFrom(last_added);

    while (!toVisit.isEmpty()) {
      /*for(String vertex : visited){
      	System.out.print(vertex + " ");
      }
      System.out.println();*/
      // First change the priorities considering the new crossing edges from last_added.
      // last_added is u and vertex is v in the edge (u,v). weight(u,v) is graph.getEdge(u,v)
      for (String vertex : neighbouring_edges.keySet()) {
        if (toVisit.contains(vertex)) {
          if (parent.get(vertex) == null) {
            parent.put(vertex, last_added);
            distance.put(vertex, distance.get(last_added) + graph.getEdge(last_added, vertex));
            pq.changePriority(vertex, distance.get(vertex));
          } else {
            // if the new crossing edges allow for a shorter path to vertex
            if (distance.get(last_added) + graph.getEdge(last_added, vertex)
                < distance.get(vertex)) {
              parent.put(vertex, last_added);
              distance.put(vertex, distance.get(last_added) + graph.getEdge(last_added, vertex));
              pq.changePriority(vertex, distance.get(vertex));
            }
          }
        }
      }
      // Once the pq has been updated, remove the min vertex
      last_added = pq.removeMin();
      neighbouring_edges = graph.getEdgesFrom(last_added);
      toVisit.remove(last_added);
      visited.add(last_added);
    }
  }
Example #3
0
  public static void main(String args[]) throws Exception {
    HashMap<Integer, Integer> fileToGraphVIDMap = new HashMap<Integer, Integer>();
    MyGraph<Coordinate, Street> g =
        GraphFactory.loadGraph("./graphs/distanceedjohnDijkstra.txt", true, fileToGraphVIDMap);

    g.removeEdge(7);
    g.removeEdge(8);
    g.removeEdge(14);
    printList(doTopologicalSort(g));

    MyDijkstra<Coordinate, Street> dijkstra = new MyDijkstra<Coordinate, Street>();
    dijkstra.setGraph(g);
    dijkstra.setStart(5);
    dijkstra.setWeighing(new MyWeighing(g));

    dijkstra.computeShortestPath();

    GuiGraphDriver gui = new GuiGraphDriver(g);

    List<Integer> path = dijkstra.getPath(8);
    gui.addPath(path, Color.yellow);
    printList(path);

    g.removeEdge(dijkstra.getConnectingEID(2));
    dijkstra.computeShortestPath();

    path = dijkstra.getPath(8);
    gui.addPath(path, Color.cyan);
    printList(path);

    /*        g.removeEdge(dijkstra.getConnectingEID(7));
    dijkstra.computeShortestPath();
    printList(dijkstra.getPath(7));
    */

  }