Example #1
0
  /**
   * Returns a new graph with the same structure as the one wrapped here, and with vertices
   * generated by the given {@link Function1}. Edges are copied in direction and weight.
   *
   * @param factory the vertex factory used to instantiate new vertices in the new graph
   * @param function the function used to set values of a new vertex in the new graph, from the
   *     matching spot
   * @param mappings a map that will receive mappings from {@link Spot} to the new vertices. Can be
   *     <code>null</code> if you do not want to get the mappings
   * @return a new {@link SimpleDirectedWeightedGraph}.
   */
  public <V> SimpleDirectedWeightedGraph<V, DefaultWeightedEdge> copy(
      final VertexFactory<V> factory,
      final Function1<Spot, V> function,
      final Map<Spot, V> mappings) {
    final SimpleDirectedWeightedGraph<V, DefaultWeightedEdge> copy =
        new SimpleDirectedWeightedGraph<V, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    final Set<Spot> spots = graph.vertexSet();
    // To store mapping of old graph vs new graph
    Map<Spot, V> map;
    if (null == mappings) {
      map = new HashMap<Spot, V>(spots.size());
    } else {
      map = mappings;
    }

    // Generate new vertices
    for (final Spot spot : Collections.unmodifiableCollection(spots)) {
      final V vertex = factory.createVertex();
      function.compute(spot, vertex);
      map.put(spot, vertex);
      copy.addVertex(vertex);
    }

    // Generate new edges
    for (final DefaultWeightedEdge edge : graph.edgeSet()) {
      final DefaultWeightedEdge newEdge =
          copy.addEdge(map.get(graph.getEdgeSource(edge)), map.get(graph.getEdgeTarget(edge)));
      copy.setEdgeWeight(newEdge, graph.getEdgeWeight(edge));
    }

    return copy;
  }
Example #2
0
 /** @see Graph#getEdgeWeight(Object) */
 public double getEdgeWeight(final DefaultWeightedEdge edge) {
   return graph.getEdgeWeight(edge);
 }