/** Calculate the shortest paths (not done per default) */ private void lazyCalculatePaths() { // already we have calculated it once. if (paths != null) { return; } lazyCalculateMatrix(); Map<VertexPair<V>, GraphPath<V, E>> sps = new HashMap<VertexPair<V>, GraphPath<V, E>>(); int n = vertices.size(); nShortestPaths = 0; for (int i = 0; i < n; i++) { V v_i = vertices.get(i); for (int j = 0; j < n; j++) { // don't count this. if (i == j) { continue; } V v_j = vertices.get(j); GraphPath<V, E> path = getShortestPathImpl(v_i, v_j); // we got a path if (path != null) { sps.put(new VertexPair<V>(v_i, v_j), path); nShortestPaths++; } } } this.paths = sps; }
private void shortestPathRecur(List<E> edges, int v_a, int v_b) { int k = backtrace[v_a][v_b]; if (k == -1) { E edge = graph.getEdge(vertices.get(v_a), vertices.get(v_b)); if (edge != null) { edges.add(edge); } } else { shortestPathRecur(edges, v_a, k); shortestPathRecur(edges, k, v_b); } }
/** {@inheritDoc} */ protected void encounterVertexAgain(V vertex, E edge) { super.encounterVertexAgain(vertex, edge); int i; if (root != null) { // For rooted detection, the path must either // double back to the root, or to a node of a cycle // which has already been detected. if (vertex.equals(root)) { i = 0; } else if ((cycleSet != null) && cycleSet.contains(vertex)) { i = 0; } else { return; } } else { i = path.indexOf(vertex); } if (i > -1) { if (cycleSet == null) { // we're doing yes/no cycle detection throw new CycleDetectedException(); } else { for (; i < path.size(); ++i) { cycleSet.add(path.get(i)); } } } }
public void testNonSimplePath() { List<Integer> vertexList = Arrays.asList(0, 1, 2, 3, 2, 3, 4); List<DefaultEdge> edgeList = new ArrayList<>(); for (int i = 0; i < vertexList.size() - 1; i++) edgeList.add(completeGraph.getEdge(vertexList.get(i), vertexList.get(i + 1))); GraphPath<Integer, DefaultEdge> p1 = new GraphWalk<>(completeGraph, 0, 4, edgeList, 10); assertEquals(0, p1.getStartVertex().intValue()); assertEquals(4, p1.getEndVertex().intValue()); assertEquals(vertexList, p1.getVertexList()); assertEquals(edgeList.size(), p1.getLength()); assertEquals(10.0, p1.getWeight()); GraphPath<Integer, DefaultEdge> p2 = new GraphWalk<>(completeGraph, vertexList, 10); assertEquals(0, p2.getStartVertex().intValue()); assertEquals(4, p2.getEndVertex().intValue()); assertEquals(edgeList, p2.getEdgeList()); assertEquals(edgeList.size(), p2.getLength()); assertEquals(10.0, p2.getWeight()); }
/** {@inheritDoc} */ protected V provideNextVertex() { V v = super.provideNextVertex(); // backtrack for (int i = path.size() - 1; i >= 0; --i) { if (graph.containsEdge(path.get(i), v)) { break; } path.remove(i); } path.add(v); return v; }
/** * Reads the geometry and connectivity. * * @param filename the location of the gjf file */ public GJFfile(String filename) { super(filename); // read geometry String name = ""; List<Atom> contents = new ArrayList<>(); SimpleWeightedGraph<Atom, DefaultWeightedEdge> connectivity = new SimpleWeightedGraph<>(DefaultWeightedEdge.class); int blanks = 0; boolean lastBlank = false; boolean inGeometryBlock = false; for (List<String> line : fileContents) { // keep track of how many blanks we have seen if (line.size() == 1 && line.get(0).length() == 0) { if (lastBlank == false) { blanks++; lastBlank = true; } continue; } else lastBlank = false; // read the metadata if (blanks == 1) { for (String s : line) { String[] fields = s.split("@"); if (fields.length != 3) continue; String identifier = fields[1].toLowerCase(); String value = fields[2]; // System.out.println(s); // System.out.println(identifier + " : " + value); if (identifier.equals("o1")) O1Number = Integer.parseInt(value); else if (identifier.equals("o2")) O2Number = Integer.parseInt(value); else if (identifier.equals("n3")) N3Number = Integer.parseInt(value); else if (identifier.equals("cl1")) Cl1Number = Integer.parseInt(value); else if (identifier.equals("su2")) Su2Number = Integer.parseInt(value); else if (identifier.equals("ol3")) Ol3Number = Integer.parseInt(value); else if (identifier.equals("mem")) mem = Integer.parseInt(value); else if (identifier.equals("nprocshared")) nprocshared = Integer.parseInt(value); else if (identifier.equals("method")) method = value; else if (identifier.equals("basis")) basis = value; else System.out.println("unrecognized entry: " + s); } continue; } else if (blanks != 2) continue; // deal with the charge and multiplicity card (by ignoring it) if (line.size() == 2 && inGeometryBlock == false) { inGeometryBlock = true; continue; } if (line.size() != 4 && inGeometryBlock == false) throw new IllegalArgumentException( "unexpected text in geometry block in " + filename + ":\n" + line.toString()); // create atom // tinker atom types will be nonsense, of course Atom newAtom = new Atom( line.get(0), new Vector3D( Double.parseDouble(line.get(1)), Double.parseDouble(line.get(2)), Double.parseDouble(line.get(3))), 1); contents.add(newAtom); connectivity.addVertex(newAtom); } // read connectivity blanks = 0; lastBlank = false; for (List<String> line : fileContents) { // read the fourth block of text if (line.size() == 1 && line.get(0).length() == 0) { if (lastBlank == false) { blanks++; lastBlank = true; } continue; } else lastBlank = false; // only read connectivity lines if (blanks != 3) continue; Atom fromAtom = contents.get(Integer.parseInt(line.get(0)) - 1); for (int i = 1; i < line.size(); i += 2) { int toAtomIndex = Integer.parseInt(line.get(i)) - 1; Atom toAtom = contents.get(toAtomIndex); double bondOrder = Double.parseDouble(line.get(i + 1)); DefaultWeightedEdge thisEdge = connectivity.addEdge(fromAtom, toAtom); connectivity.setEdgeWeight(thisEdge, bondOrder); } } // create the molecule molecule = new Molecule(name, contents, connectivity, 0.0); }