private double getTotalWeight(Collection<E> edges) { double total = 0; for (E e : edges) { total += graph.getEdgeWeight(e); } return total; } }
/** * {@inheritDoc} */ @Override public double getEdgeWeight(E e) { return base.getEdgeWeight(e); }
/** * {@inheritDoc} */ @Override public double getEdgeWeight(E e) { return delegate.getEdgeWeight(e); }
private GraphPath<V, E> createGraphPath(List<E> edgeList, V startVertex, V endVertex) { double weight = 0; for (E edge : edgeList) { weight += originalGraph.getEdgeWeight(edge); } return new GraphWalk<>(originalGraph, startVertex, endVertex, edgeList, weight); }
/** * {@inheritDoc} */ @Override public double getEdgeWeight(E edge) { assert (edgeSet().contains(edge)); return base.getEdgeWeight(edge); }
private void assertNonNegativeEdge(E edge) { if (getGraph().getEdgeWeight(edge) < 0) { throw new IllegalArgumentException("negative edge weights not allowed"); } }
/** * Compute the sum of the weights entering a vertex * * @param v the vertex * @return the sum of the weights entering a vertex */ public double vertexWeight(Set<V> v) { double wsum = 0.0; for (DefaultWeightedEdge e : workingGraph.edgesOf(v)) { wsum += workingGraph.getEdgeWeight(e); } return wsum; }
/** * Build final walk * * @return the final walk */ private GraphWalk<V, E> buildWalk() { double totalWeight = 0d; List<E> result = new ArrayList<>(); EdgeNode it = eulerianHead; while (it != null) { result.add(it.e); totalWeight += g.getEdgeWeight(it.e); it = it.next; } return new GraphWalk<>(g, startVertex, startVertex, result, totalWeight); }
/** * Constructor. Constructs a new network on which we will calculate the maximum flow, using * Dinic algorithm. * * @param network the network on which we calculate the maximum flow. * @param epsilon the tolerance for the comparison of floating point values. */ public DinicMFImpl(Graph<V, E> network, double epsilon) { super(network, epsilon); this.vertexExtensionsFactory = VertexExtension::new; this.edgeExtensionsFactory = AnnotatedFlowEdge::new; if (epsilon <= 0) { throw new IllegalArgumentException("Epsilon must be positive!"); } for (E e : network.edgeSet()) { if (network.getEdgeWeight(e) < -epsilon) { throw new IllegalArgumentException("Capacity must be non-negative!"); } } }
/** * {@inheritDoc} */ @Override public double getEdgeWeight(E e) { if (g1.containsEdge(e) && g2.containsEdge(e)) { return operator.combine(g1.getEdgeWeight(e), g2.getEdgeWeight(e)); } if (g1.containsEdge(e)) { return g1.getEdgeWeight(e); } if (g2.containsEdge(e)) { return g2.getEdgeWeight(e); } throw new IllegalArgumentException("no such edge in the union"); }
/** * Transform an ordered list of edges into a GraphPath. * * The weight of the generated GraphPath is set to the sum of the weights of the edges. * * @param edges the edges * * @return the corresponding GraphPath */ private GraphPath<V, E> makePath(List<E> edges) { V source = graph.getEdgeSource(edges.get(0)); V target = graph.getEdgeTarget(edges.get(edges.size() - 1)); double weight = edges.stream().mapToDouble(edge -> graph.getEdgeWeight(edge)).sum(); return new GraphWalk<>(graph, source, target, edges, weight); } }
private AnnotatedFlowEdge createBackwardEdge(AnnotatedFlowEdge forwardEdge) { AnnotatedFlowEdge backwardEdge; E backwardPrototype = network.getEdge(forwardEdge.target.prototype, forwardEdge.source.prototype); if (directedGraph && backwardPrototype != null) { // if edge exists in directed input graph backwardEdge = createEdge( forwardEdge.target, forwardEdge.source, backwardPrototype, network.getEdgeWeight(backwardPrototype)); } else { backwardEdge = edgeExtensionManager.createExtension(); backwardEdge.source = forwardEdge.target; backwardEdge.target = forwardEdge.source; if (!directedGraph) { // Undirected graph: if (u,v) exists, then so much (v,u) backwardEdge.capacity = network.getEdgeWeight(backwardPrototype); backwardEdge.prototype = backwardPrototype; } } forwardEdge.inverse = backwardEdge; backwardEdge.inverse = forwardEdge; return backwardEdge; }
/** * Costs taken into account are the weights stored in <code>Edge</code> objects. * * @param pathElement * @param edge the edge via which the vertex was encountered. * * @return the cost obtained by concatenation. * * @see Graph#getEdgeWeight(E) */ private double calculatePathWeight(RankingPathElement<V, E> pathElement, E edge) { double pathWeight = this.graph.getEdgeWeight(edge); // otherwise it's the start vertex. if ((pathElement.getPrevEdge() != null)) { pathWeight += pathElement.getWeight(); } return pathWeight; }
public static <V, E> int hashCodeIgnoreAnnotations( Graph<V, E> graph ) { int hash = graph.vertexSet().hashCode(); for( E e : graph.edgeSet() ) { int part = e.hashCode(); int source = graph.getEdgeSource( e ).hashCode(); int target = graph.getEdgeTarget( e ).hashCode(); int pairing = pair( source, target ); part = ( 27 * part ) + pairing; long weight = (long) graph.getEdgeWeight( e ); part = ( 27 * part ) + (int) ( weight ^ ( weight >>> 32 ) ); hash += part; } return hash; }
private void exportAsEdgeList(Graph<V, E> g, PrintWriter out) { boolean exportEdgeWeights = parameters.contains(CSVFormat.Parameter.EDGE_WEIGHTS); for (E e : g.edgeSet()) { exportEscapedField(out, vertexIDProvider.getName(g.getEdgeSource(e))); out.print(delimiter); exportEscapedField(out, vertexIDProvider.getName(g.getEdgeTarget(e))); if (exportEdgeWeights) { out.print(delimiter); exportEscapedField(out, String.valueOf(g.getEdgeWeight(e))); } out.println(); } }
private void exportAsAdjacencyList(Graph<V, E> g, PrintWriter out) { boolean exportEdgeWeights = parameters.contains(CSVFormat.Parameter.EDGE_WEIGHTS); for (V v : g.vertexSet()) { exportEscapedField(out, vertexIDProvider.getName(v)); for (E e : g.outgoingEdgesOf(v)) { V w = Graphs.getOppositeVertex(g, e, v); out.print(delimiter); exportEscapedField(out, vertexIDProvider.getName(w)); if (exportEdgeWeights) { out.print(delimiter); exportEscapedField(out, String.valueOf(g.getEdgeWeight(e))); } } out.println(); } }
/** * {@inheritDoc} */ @Override public SpanningTree<E> getSpanningTree() { UnionFind<V> forest = new UnionFind<>(graph.vertexSet()); ArrayList<E> allEdges = new ArrayList<>(graph.edgeSet()); allEdges.sort(Comparator.comparingDouble(graph::getEdgeWeight)); double spanningTreeCost = 0; Set<E> edgeList = new HashSet<>(); for (E edge : allEdges) { V source = graph.getEdgeSource(edge); V target = graph.getEdgeTarget(edge); if (forest.find(source).equals(forest.find(target))) { continue; } forest.union(source, target); edgeList.add(edge); spanningTreeCost += graph.getEdgeWeight(edge); } return new SpanningTreeImpl<>(edgeList, spanningTreeCost); } }
/** * Relaxes the edge outgoing from u and updates the queue appropriately. * * @param u Vertex u. * @param e Edge e. * @param queue The queue. */ protected void relax(V startNode, V u, E e, PriorityQueue<V> queue) { // Get the target vertex. V v = Graphs.getOppositeVertex(graph, e, u); // Get the weight. double uvWeight = graph.getEdgeWeight(e); // If a smaller distance estimate is available, make the necessary // updates. if (v.getDistance() > u.getDistance() + uvWeight) { shortestPathSoFarUpdate(startNode, u, v, uvWeight, e, queue); } else if (Math.abs(v.getDistance() - (u.getDistance() + uvWeight)) < TOLERANCE) { multipleShortestPathUpdate(u, v, e); } }
/** * Determine weighted path length to a vertex via an edge, using the path length for the * opposite vertex. * * @param vertex the vertex for which to calculate the path length. * @param edge the edge via which the path is being extended. * * @return calculated path length. */ private double calculatePathLength(V vertex, E edge) { assertNonNegativeEdge(edge); V otherVertex = Graphs.getOppositeVertex(getGraph(), edge, vertex); FibonacciHeapNode<QueueEntry<V, E>> otherEntry = getSeenData(otherVertex); return otherEntry.getKey() + getGraph().getEdgeWeight(edge); }
@Override protected void transformGraph(List<E> previousPath) { V source, target; E reversedEdge; // replace previous path edges with reversed edges with negative weight for (E originalEdge : previousPath) { source = workingGraph.getEdgeSource(originalEdge); target = workingGraph.getEdgeTarget(originalEdge); double originalEdgeWeight = workingGraph.getEdgeWeight(originalEdge); workingGraph.removeEdge(originalEdge); workingGraph.addEdge(target, source); reversedEdge = workingGraph.getEdge(target, source); workingGraph.setEdgeWeight(reversedEdge, -originalEdgeWeight); } }