/**
   * 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;
  }
 @Before
 public void onSetUp() {
   SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph =
       new SimpleDirectedWeightedGraph<>(DefaultWeightedEdge.class);
   graph.addVertex(A);
   graph.addVertex(B);
   algorithmFactory = new AlgorithmFactory<>(graph);
 }
Exemple #3
0
  /** @param args */
  public static void main(String[] args) {

    SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g =
        new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    String v1 = "1", v2 = "2", v3 = "3", v4 = "4", v5 = "5";
    g.addVertex(v1);
    g.addVertex(v2);
    g.addVertex(v3);
    g.addVertex(v4);
    g.addVertex(v5);

    g.addEdge(v1, v2);
    g.addEdge(v2, v3);
    g.addEdge(v3, v2);
    g.addEdge(v4, v2);
    g.addEdge(v3, v5);
    g.addEdge(v4, v5);

    System.out.println("������ȣ�");
    BreadthFirstIterator<String, DefaultWeightedEdge> bit =
        new BreadthFirstIterator<String, DefaultWeightedEdge>(g);
    bit.setCrossComponentTraversal(true);
    while (bit.hasNext()) System.out.println(bit.next());
    System.out.println("�������");

    System.out.println(g.incomingEdgesOf(v2));
    System.out.println(g.outgoingEdgesOf(v2));
  }