Exemplo n.º 1
0
  public String toDOTString() {
    StringBuilder sb = new StringBuilder();
    sb.append("digraph onzeMooieGraph {\nrankdir=LR;\n");

    for (Vertex vertex : vertices) {
      sb.append(
          String.format(
              "n%d [label=\"%s ("
                  + vertex.getEarliestTime()
                  + ","
                  + vertex.getLatestTime()
                  + ")\" shape=\"square\"];\n",
              vertices.indexOf(vertex),
              vertex.getValue()));
    }

    for (Edge edge : edges) {
      sb.append(
          String.format(
              "n%d -> n%d [ label = \"%d\" ];\n",
              vertices.indexOf(edge.getFrom()), vertices.indexOf(edge.getTo()), edge.getWeight()));
    }

    sb.append("}");
    return sb.toString();
  }
Exemplo n.º 2
0
 public void calculateLatestTime() {
   ArrayList<Vertex> endVertices = getEndVertices();
   for (Vertex vertex : endVertices) {
     vertex.setLatestTime(vertex.getEarliestTime());
     getLatestTimeOfVertex(vertex);
   }
 }
Exemplo n.º 3
0
 /**
  * This methode allows us to set the earliest time in a vertex
  *
  * @param from starting vertex
  * @param to end vertex
  * @param edgeList the list of connections available
  * @return the edge that connects the two vertices
  */
 private Edge setEarliestTimeOfVertex(Vertex from, Vertex to, ArrayList<Edge> edgeList) {
   for (Edge edge : edgeList) {
     if (edge.getFrom().equals(from) && edge.getTo().equals(to)) {
       if ((edge.getWeight() + from.getEarliestTime()) > to.getEarliestTime()) {
         to.setEarliestTime(edge.getWeight() + from.getEarliestTime());
       }
       return edge;
     }
   }
   return null;
 }
Exemplo n.º 4
0
 // Make the vertex object comparable to each other so they can be used in a heap
 @Override
 public int compareTo(Vertex arg0) {
   // TODO Auto-generated method stub
   Integer distance1 = this.distance;
   Integer distance2 = arg0.getDistance();
   return distance1.compareTo(distance2);
 }
Exemplo n.º 5
0
  /**
   * A method to check if there are no cycles created
   *
   * @param from The from node
   * @param to The to node
   * @return Returns true if a cycle was created else false
   */
  @SuppressWarnings("rawtypes")
  public boolean checkCycles(Vertex from, Vertex to) {
    ArrayList<Vertex> visited = new ArrayList<>();
    ArrayList<Vertex> neighbours = new ArrayList<>();

    neighbours.add(from);
    visited.add(from);

    while (!neighbours.isEmpty()) {
      Vertex currentNode = neighbours.remove(0);
      for (Vertex neighbour : getNeighbours(currentNode)) {
        if (!visited.contains(neighbour)) {
          if (neighbour.getValue().equals(to.getValue())) {
            return true;
          }
          neighbours.add(neighbour);
          visited.add(neighbour);
        }
      }
    }
    return false;
  }
Exemplo n.º 6
0
 public boolean containsValue(T value) {
   for (Vertex v : vertices) {
     if (v.getValue().equals(value)) return true;
   }
   return false;
 }
Exemplo n.º 7
0
 public Vertex getVertex(T value) {
   for (Vertex v : vertices) {
     if (v.getValue().equals(value)) return v;
   }
   return null;
 }