public boolean equals(Object o) { if (!(o instanceof IGraph)) { return false; } IGraph<Node> that = (IGraph) o; // Note: Order of checks is important for speed. // check if they have the same sets of nodes. Set<Node> thisNodes = this.getNodes(); Set<Node> thatNodes = that.getNodes(); if (!thisNodes.equals(thatNodes)) { return false; } // check if they have the same sets of edges. for (Node v : thisNodes) { Set<Node> thisPreds = this.getPreds(v); Set<Node> thatPreds = that.getPreds(v); if (!thisPreds.equals(thatPreds)) { return false; } } // check if they have the same sets of roots. Set<Node> thisRoots = this.getRoots(); Set<Node> thatRoots = that.getRoots(); if (!thisRoots.equals(thatRoots)) { return false; } return true; }
public FordaBellmana(IGraph<Integer> graph) { this.graph = graph; numberofvertices = graph.getVerticesCount(); distances = new int[numberofvertices + 1]; predecesor = new Integer[numberofvertices + 1]; this.firstIndexId = 0; }
public int run(int source, int target) { for (int node = firstIndexId; node < numberofvertices + firstIndexId; node++) { distances[node] = MAX_VALUE; } distances[source] = 0; // predecesor[source] = null; for (int node = firstIndexId; node < numberofvertices + firstIndexId; node++) { for (int sourcenode = firstIndexId; sourcenode < numberofvertices + firstIndexId; sourcenode++) { for (int destinationnode = firstIndexId; destinationnode < numberofvertices + firstIndexId; destinationnode++) { Integer weight = graph.getWeight(sourcenode, destinationnode); if (weight != null && weight != MAX_VALUE) { if (distances[destinationnode] > distances[sourcenode] + weight) { distances[destinationnode] = distances[sourcenode] + weight; predecesor[destinationnode] = sourcenode; } } } } } for (int sourcenode = firstIndexId; sourcenode < numberofvertices + firstIndexId; sourcenode++) { for (int destinationnode = firstIndexId; destinationnode < numberofvertices + firstIndexId; destinationnode++) { Integer weight = graph.getWeight(sourcenode, destinationnode); if (weight != null && weight != MAX_VALUE) { if (distances[destinationnode] > distances[sourcenode] + graph.getWeight(sourcenode, destinationnode)) System.out.println("The Graph contains negative egde cycle"); } } } return distances[target]; }
@Deprecated @operator( value = "rewire_p", category = {IOperatorCategory.GRAPH}) @doc( value = "Rewires a graph (in the Watts-Strogatz meaning)", deprecated = "Does not work now", examples = { @example(value = "graph graphEpidemio <- graph([]);", isTestOnly = true), @example(value = "graphEpidemio rewire_p 0.2", test = false) }, see = "rewire_p") public static IGraph rewireGraph(final IScope scope, final IGraph g, final Double probability) { GraphAlgorithmsHandmade.rewireGraphProbability(scope, g, probability); g.incVersion(); return g; }
@Override public IVertexSequence<V> next() { if (!hasNext()) throw new NoSuchElementException(); // Generate a weighted random walk starting at vertex order[current] int currVertexIdx = order[position++]; int[] indices = new int[walkLength + 1]; indices[0] = currVertexIdx; if (walkLength == 0) return new VertexSequence<>(graph, indices); for (int i = 1; i <= walkLength; i++) { List<? extends Edge<? extends Number>> edgeList = graph.getEdgesOut(currVertexIdx); // First: check if there are any outgoing edges from this vertex. If not: handle the situation if (edgeList == null || edgeList.size() == 0) { switch (mode) { case SELF_LOOP_ON_DISCONNECTED: for (int j = i; j < walkLength; j++) indices[j] = currVertexIdx; return new VertexSequence<>(graph, indices); case EXCEPTION_ON_DISCONNECTED: throw new NoEdgesException( "Cannot conduct random walk: vertex " + currVertexIdx + " has no outgoing edges. " + " Set NoEdgeHandling mode to NoEdgeHandlingMode.SELF_LOOP_ON_DISCONNECTED to self loop instead of " + "throwing an exception in this situation."); default: throw new RuntimeException("Unknown/not implemented NoEdgeHandling mode: " + mode); } } // To do a weighted random walk: we need to know total weight of all outgoing edges double totalWeight = 0.0; for (Edge<? extends Number> edge : edgeList) { totalWeight += edge.getValue().doubleValue(); } double d = rng.nextDouble(); double threshold = d * totalWeight; double sumWeight = 0.0; for (Edge<? extends Number> edge : edgeList) { sumWeight += edge.getValue().doubleValue(); if (sumWeight >= threshold) { if (edge.isDirected()) { currVertexIdx = edge.getTo(); } else { if (edge.getFrom() == currVertexIdx) { currVertexIdx = edge.getTo(); } else { currVertexIdx = edge .getFrom(); // Undirected edge: might be next--currVertexIdx instead of // currVertexIdx--next } } indices[i] = currVertexIdx; break; } } } return new VertexSequence<>(graph, indices); }
/** * @param graph IGraph to conduct walks on * @param walkLength length of each walk. Walk of length 0 includes 1 vertex, walk of 1 includes 2 * vertices etc * @param rngSeed seed for randomization * @param mode mode for handling random walks from vertices with either no edges, or no outgoing * edges (for directed graphs) */ public WeightedRandomWalkIterator( IGraph<V, ? extends Number> graph, int walkLength, long rngSeed, NoEdgeHandling mode) { this(graph, walkLength, rngSeed, mode, 0, graph.numVertices()); }