org.jgrapht.Graph.getEdgeWeight()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(164)

本文整理了Java中org.jgrapht.Graph.getEdgeWeight()方法的一些代码示例,展示了Graph.getEdgeWeight()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph.getEdgeWeight()方法的具体详情如下:
包路径:org.jgrapht.Graph
类名称:Graph
方法名:getEdgeWeight

Graph.getEdgeWeight介绍

[英]Returns the weight assigned to a given edge. Unweighted graphs return 1.0 (as defined by #DEFAULT_EDGE_WEIGHT), allowing weighted-graph algorithms to apply to them when meaningful.
[中]返回指定给给定边的权重。未加权图返回1.0(由#DEFAULT_EDGE_WEIGHT定义),允许在有意义时对其应用加权图算法。

代码示例

代码示例来源:origin: org.jgrapht/jgrapht-core

private double getTotalWeight(Collection<E> edges)
  {
    double total = 0;
    for (E e : edges) {
      total += graph.getEdgeWeight(e);
    }
    return total;
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * {@inheritDoc}
 */
@Override
public double getEdgeWeight(E e)
{
  return base.getEdgeWeight(e);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * {@inheritDoc}
 */
@Override
public double getEdgeWeight(E e)
{
  return delegate.getEdgeWeight(e);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

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);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * {@inheritDoc}
 */
@Override
public double getEdgeWeight(E edge)
{
  assert (edgeSet().contains(edge));
  return base.getEdgeWeight(edge);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

private void assertNonNegativeEdge(E edge)
{
  if (getGraph().getEdgeWeight(edge) < 0) {
    throw new IllegalArgumentException("negative edge weights not allowed");
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * 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;
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * 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);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * 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!");
    }
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * {@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");
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
   * 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);
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

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;
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * 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;
}

代码示例来源:origin: cwensel/cascading

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;
 }

代码示例来源:origin: org.jgrapht/jgrapht-io

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();
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-io

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();
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
   * {@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);
  }
}

代码示例来源:origin: org.orbisgis/java-network-analyzer

/**
 * 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);
  }
}

代码示例来源:origin: org.jgrapht/jgrapht-core

/**
 * 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);
}

代码示例来源:origin: org.jgrapht/jgrapht-core

@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);
  }
}

相关文章