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

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

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

Graph.getEdge介绍

[英]Returns an edge connecting source vertex to target vertex if such vertices and such edge exist in this graph. Otherwise returns null. If any of the specified vertices is null returns null

In undirected graphs, the returned edge may have its source and target vertices in the opposite order.
[中]返回将源顶点连接到目标顶点的边(如果此图中存在此类顶点和此类边)。否则返回null。如果指定的任何顶点为null,则返回null
在无向图中,返回边的源顶点和目标顶点的顺序可能相反。

代码示例

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

/**
 * {@inheritDoc}
 */
@Override
public E getEdge(V sourceVertex, V targetVertex)
{
  return delegate.getEdge(sourceVertex, targetVertex);
}

代码示例来源: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);
  }
}

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

/**
 * Construct new instance
 * 
 * @param G the input graph
 * @param S first partition of the vertex set
 * @param T second partition of the vertex set
 */
public KuhnMunkresMatrixImplementation(
  final Graph<V, E> G, final List<? extends V> S, final List<? extends V> T)
{
  int partition = S.size();
  // Build an excess-matrix corresponding to the supplied weighted
  // complete bipartite graph
  costMatrix = new double[partition][];
  for (int i = 0; i < S.size(); ++i) {
    V source = S.get(i);
    costMatrix[i] = new double[partition];
    for (int j = 0; j < T.size(); ++j) {
      V target = T.get(j);
      if (source.equals(target)) {
        continue;
      }
      costMatrix[i][j] = G.getEdgeWeight(G.getEdge(source, target));
    }
  }
}

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

@Override
public E getEdgeCorrespondence(E currEdge, boolean forward)
{
  Graph<V, E> sourceGraph, targetGraph;
  if (forward) {
    sourceGraph = this.graph1;
    targetGraph = this.graph2;
  } else {
    sourceGraph = this.graph2;
    targetGraph = this.graph1;
  }
  V mappedSourceVertex =
    getVertexCorrespondence(sourceGraph.getEdgeSource(currEdge), forward);
  V mappedTargetVertex =
    getVertexCorrespondence(sourceGraph.getEdgeTarget(currEdge), forward);
  if ((mappedSourceVertex == null) || (mappedTargetVertex == null)) {
    return null;
  } else {
    return targetGraph.getEdge(mappedSourceVertex, mappedTargetVertex);
  }
}

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

@Override
protected void transformGraph(List<E> previousPath)
{
  for (E edge : this.workingGraph.edgeSet()) {
    V source = workingGraph.getEdgeSource(edge);
    V target = workingGraph.getEdgeTarget(edge);
    double modifiedWeight = this.workingGraph.getEdgeWeight(edge)
      - singleSourcePaths.getWeight(target) + singleSourcePaths.getWeight(source);
    this.workingGraph.setEdgeWeight(edge, modifiedWeight);
  }
  E reversedEdge;
  for (E originalEdge : previousPath) {
    double zeroWeight = workingGraph.getEdgeWeight(originalEdge);
    if (zeroWeight != 0) {
      throw new IllegalStateException("Expected zero weight edge along the path");
    }
    V source = workingGraph.getEdgeSource(originalEdge);
    V target = workingGraph.getEdgeTarget(originalEdge);
    workingGraph.removeEdge(originalEdge);
    workingGraph.addEdge(target, source);
    reversedEdge = workingGraph.getEdge(target, source);
    workingGraph.setEdgeWeight(reversedEdge, zeroWeight);
  }
}

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

@Override
public E getEdgeCorrespondence(E e, boolean forward)
{
  Graph<V, E> fromGraph;
  Graph<V, E> toGraph;
  if (forward) {
    fromGraph = graph1;
    toGraph = graph2;
  } else {
    fromGraph = graph2;
    toGraph = graph1;
  }
  V u = fromGraph.getEdgeSource(e);
  V v = fromGraph.getEdgeTarget(e);
  V uu = getVertexCorrespondence(u, forward);
  if (uu == null) {
    return null;
  }
  V vv = getVertexCorrespondence(v, forward);
  if (vv == null) {
    return null;
  }
  return toGraph.getEdge(uu, vv);
}

代码示例来源: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: cwensel/cascading

public Scope getEdge( FlowElement sourceVertex, FlowElement targetVertex )
 {
 return graph.getEdge( sourceVertex, targetVertex );
 }

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

/**
 * Transform from an array representation to a graph path.
 * 
 * @param tour an array containing the index of the vertices of the tour
 * @return a graph path
 */
private GraphPath<V, E> tourToPath(int[] tour)
{
  List<E> tourEdges = new ArrayList<E>(n);
  List<V> tourVertices = new ArrayList<>(n + 1);
  double tourWeight = 0d;
  V start = revIndex.get(tour[0]);
  tourVertices.add(start);
  for (int i = 1; i < n + 1; i++) {
    V u = revIndex.get(tour[i - 1]);
    V v = revIndex.get(tour[i]);
    tourVertices.add(v);
    E e = graph.getEdge(u, v);
    tourEdges.add(e);
    tourWeight += graph.getEdgeWeight(e);
  }
  return new GraphWalk<>(graph, start, start, tourVertices, tourEdges, tourWeight);
}

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

/**
 * Check whether the subgraph of <code>graph</code> induced by the given <code>vertices</code>
 * is complete, i.e. a clique.
 *
 * @param graph the graph.
 * @param vertices the vertices to induce the subgraph from.
 *
 * @return true if the induced subgraph is a clique.
 */
private static <V, E> boolean isClique(Graph<V, E> graph, Set<V> vertices)
{
  for (V v1 : vertices) {
    for (V v2 : vertices) {
      if (!v1.equals(v2) && (graph.getEdge(v1, v2) == null)) {
        return false;
      }
    }
  }
  return true;
}

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

/**
 * Returns a matching of maximum cardinality. Each time this method is invoked, the matching is
 * computed from scratch. Consequently, it is possible to make changes to the graph and to
 * re-invoke this method on the altered graph.
 * 
 * @return a matching of maximum cardinality.
 */
@Override
public Matching<V, E> getMatching()
{
  this.init();
  if (initializer != null)
    this.warmStart(initializer);
  // Continuously augment the matching until augmentation is no longer possible.
  while (matchedVertices < graph.vertexSet().size() - 1 && augment()) {
    matchedVertices += 2;
  }
  Set<E> edges = new LinkedHashSet<>();
  double cost = 0;
  for (int vx = 0; vx < vertices.size(); vx++) {
    if (matching.isExposed(vx))
      continue;
    V v = vertices.get(vx);
    V w = vertices.get(matching.opposite(vx));
    E edge = graph.getEdge(v, w);
    edges.add(edge);
    cost += 0.5 * graph.getEdgeWeight(edge);
  }
  return new MatchingImpl<>(graph, edges, cost);
}

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

/**
 * Assigns a weight to an edge between <code>sourceVertex</code> and <code>targetVertex</code>.
 * If no edge exists between <code>sourceVertex</code> and <code>targetVertex</code> or either
 * of these vertices is <code>null</code>, a <code>NullPointerException</code> is thrown.
 * <p>
 * When there exist multiple edges between <code>sourceVertex</code> and
 * <code>targetVertex</code>, consider using {@link #setEdgeWeight(Object, double)} instead.
 *
 * @param sourceVertex source vertex of the edge
 * @param targetVertex target vertex of the edge
 * @param weight new weight for edge
 * @throws UnsupportedOperationException if the graph does not support weights
 */
default void setEdgeWeight(V sourceVertex, V targetVertex, double weight)
{
  this.setEdgeWeight(this.getEdge(sourceVertex, targetVertex), weight);
}

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

/**
 * @param v1Number the number identifying the vertex $v_1$
 * @param v2Number the number identifying the vertex $v_2$
 *
 * @return the edge from $v_1$ to $v_2$
 */
public E getEdge(int v1Number, int v2Number)
{
  V v1 = getVertex(v1Number), v2 = getVertex(v2Number);
  return graph.getEdge(v1, v2);
}

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

/**
 * Returns the edges making up the path. The first edge in this path is incident to the start
 * vertex. The last edge is incident to the end vertex. The vertices along the path can be
 * obtained by traversing from the start vertex, finding its opposite across the first edge, and
 * then doing the same successively across subsequent edges; see {@link #getVertexList()}.
 *
 * <p>
 * Whether or not the returned edge list is modifiable depends on the path implementation.
 *
 * @return list of edges traversed by the path
 */
default List<E> getEdgeList()
{
  List<V> vertexList = this.getVertexList();
  if (vertexList.size() < 2)
    return Collections.emptyList();
  Graph<V, E> g = this.getGraph();
  List<E> edgeList = new ArrayList<>();
  Iterator<V> vertexIterator = vertexList.iterator();
  V u = vertexIterator.next();
  while (vertexIterator.hasNext()) {
    V v = vertexIterator.next();
    edgeList.add(g.getEdge(u, v));
    u = v;
  }
  return edgeList;
}

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

/**
 * {@inheritDoc}
 */
@Override
public E getEdge(V sourceVertex, V targetVertex)
{
  E res = null;
  if (g1.containsVertex(sourceVertex) && g1.containsVertex(targetVertex)) {
    res = g1.getEdge(sourceVertex, targetVertex);
  }
  if ((res == null) && g2.containsVertex(sourceVertex) && g2.containsVertex(targetVertex)) {
    res = g2.getEdge(sourceVertex, targetVertex);
  }
  return res;
}

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

@Override
  public Matching<V, E> getMatching()
  {
    this.init();
    this.warmStart();

    while (matchedVertices < partition1.size() && bfs()) {
      // Greedily search for vertex disjoint augmenting paths
      for (int v = 1; v <= partition1.size() && matchedVertices < partition1.size(); v++)
        if (matching[v] == DUMMY) // v is unmatched
          if (dfs(v))
            matchedVertices++;
    }
    assert matchedVertices <= partition1.size();

    Set<E> edges = new HashSet<>();
    for (int i = 0; i < vertices.size(); i++) {
      if (matching[i] != DUMMY) {
        edges.add(graph.getEdge(vertices.get(i), vertices.get(matching[i])));
      }
    }
    return new MatchingImpl<>(graph, edges, edges.size());
  }
}

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

public Scope addEdge( FlowElement sourceVertex, FlowElement targetVertex )
  {
//     prevent multiple edges from head or to tail
  if( !allowMultipleExtentEdges() && ( sourceVertex == Extent.head || targetVertex == Extent.tail ) && graph.containsEdge( sourceVertex, targetVertex ) )
   return graph.getEdge( sourceVertex, targetVertex );

  return graph.addEdge( sourceVertex, targetVertex );
  }

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

public Edge getEdge( int lhsVertex, int rhsVertex )
 {
 Node lhsNode = getVertex( lhsVertex );
 Node rhsNode = getVertex( rhsVertex );
 return getDelegate().getEdge( lhsNode, rhsNode );
 }

代码示例来源:origin: de.unistuttgart.ims/de.unistuttgart.ims.drama.graph

public void export(JCas jcas, Graph<Figure, ?> graph) throws IOException {
    StringWriter sw = new StringWriter();
    sw.write(" \n");
    for (Figure figure1 : graph.vertexSet()) {
      for (Figure figure2 : graph.vertexSet()) {
        if (graph.containsEdge(figure1, figure2)) {
          sw.write(figure1.getBegin() + " " + figure2.getBegin());
          Object edge = graph.getEdge(figure1, figure2);
          try {
            @SuppressWarnings("unchecked")
            double w = ((WeightedGraph<Figure, Object>) graph).getEdgeWeight(edge);
            sw.write(" " + w);
          } catch (ClassCastException e) {
            // we try to cast, but ignore it if impossible
          }
          sw.write("\n");
        }
      }
    }
    sw.flush();
    sw.close();
    jcas.setDocumentText(sw.toString());
    jcas.setDocumentLanguage("");
    GraphMetaData graphAnnotation = AnnotationFactory.createAnnotation(jcas, 0, 1, GraphMetaData.class);
    graphAnnotation.setGraphClassName(graph.getClass().getCanonicalName());
    if (!graph.edgeSet().isEmpty())
      graphAnnotation.setEdgeClassName(graph.edgeSet().iterator().next().getClass().getCanonicalName());

  }
}

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

/**
   * Visit the given node, updating its predecessor and discovery and
   * finishing times.
   *
   * @param node The node.
   */
  protected void visit(V node) {

    time++;

    node.setDiscoveryTime(time);

    for (V neighbor : successorListOf(node)) {
      if (neighbor.getDiscoveryTime() < 0) {
        neighbor.addPredecessor(node);
        neighbor.addPredecessorEdge(graph.getEdge(node, neighbor));
        visit(neighbor);
      }
    }

    time++;

    node.setFinishingTime(time);
  }
}

相关文章