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(); }
public void calculateLatestTime() { ArrayList<Vertex> endVertices = getEndVertices(); for (Vertex vertex : endVertices) { vertex.setLatestTime(vertex.getEarliestTime()); getLatestTimeOfVertex(vertex); } }
/** * 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; }
// 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); }
/** * 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; }
public boolean containsValue(T value) { for (Vertex v : vertices) { if (v.getValue().equals(value)) return true; } return false; }
public Vertex getVertex(T value) { for (Vertex v : vertices) { if (v.getValue().equals(value)) return v; } return null; }