/** * 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; }
/** @see Graph#edgesOf(Object) */ public Set<DefaultWeightedEdge> edgesOf(final Spot spot) { if (graph.containsVertex(spot)) { return graph.edgesOf(spot); } else { return Collections.emptySet(); } }
DefaultWeightedEdge addEdge(final Spot source, final Spot target, final double weight) { if (!graph.containsVertex(source)) { graph.addVertex(source); } if (!graph.containsVertex(target)) { graph.addVertex(target); } final DefaultWeightedEdge edge = graph.addEdge(source, target); graph.setEdgeWeight(edge, weight); return edge; }
/** @see Graph#getEdgeWeight(Object) */ public double getEdgeWeight(final DefaultWeightedEdge edge) { return graph.getEdgeWeight(edge); }
/** @see Graph#getEdgeTarget(Object) */ public Spot getEdgeTarget(final DefaultWeightedEdge e) { return graph.getEdgeTarget(e); }
/** @see Graph#getEdgeSource(Object) */ public Spot getEdgeSource(final DefaultWeightedEdge e) { return graph.getEdgeSource(e); }
/** @see Graph#vertexSet() */ public Set<Spot> vertexSet() { return graph.vertexSet(); }
/** @see Graph#edgeSet() */ public Set<DefaultWeightedEdge> edgeSet() { return graph.edgeSet(); }
/** @see Graph#getEdge(Object, Object) */ public DefaultWeightedEdge getEdge(final Spot source, final Spot target) { return graph.getEdge(source, target); }
/** @see Graph#containsEdge(Object, Object) */ public boolean containsEdge(final Spot source, final Spot target) { return graph.containsEdge(source, target); }
void setEdgeWeight(final DefaultWeightedEdge edge, final double weight) { graph.setEdgeWeight(edge, weight); edgesModified.add(edge); }
boolean removeEdge(final DefaultWeightedEdge edge) { return graph.removeEdge(edge); }
DefaultWeightedEdge removeEdge(final Spot source, final Spot target) { return graph.removeEdge(source, target); }
void removeSpot(final Spot spotToRemove) { graph.removeVertex(spotToRemove); }
void addSpot(final Spot spotToAdd) { graph.addVertex(spotToAdd); }
@Test public void testSubgraph2() { ListenableUndirectedGraph<Integer, DefaultEdge> g1 = new ListenableUndirectedGraph<Integer, DefaultEdge>(DefaultEdge.class); g1.addVertex(0); g1.addVertex(1); g1.addVertex(2); g1.addVertex(3); g1.addVertex(4); g1.addEdge(0, 1); g1.addEdge(0, 4); g1.addEdge(1, 2); g1.addEdge(1, 4); g1.addEdge(2, 3); g1.addEdge(2, 4); g1.addEdge(3, 4); ListenableUndirectedGraph<Integer, DefaultEdge> g2 = new ListenableUndirectedGraph<Integer, DefaultEdge>(DefaultEdge.class); g2.addVertex(0); g2.addVertex(1); g2.addVertex(2); g2.addVertex(3); g2.addVertex(4); g2.addVertex(5); g2.addEdge(4, 2); g2.addEdge(2, 0); g2.addEdge(0, 1); g2.addEdge(1, 3); g2.addEdge(3, 4); g2.addEdge(4, 0); g2.addEdge(1, 4); ListenableUndirectedGraph<Integer, DefaultEdge> g3 = new ListenableUndirectedGraph<Integer, DefaultEdge>(DefaultEdge.class); g3.addVertex(0); g3.addVertex(1); g3.addVertex(2); g3.addVertex(3); g3.addVertex(4); g3.addVertex(5); g3.addEdge(0, 1); g3.addEdge(1, 2); g3.addEdge(2, 3); g3.addEdge(3, 4); g3.addEdge(5, 2); g3.addEdge(5, 3); g3.addEdge(5, 4); ListenableUndirectedGraph<Integer, DefaultEdge> g4 = new ListenableUndirectedGraph<Integer, DefaultEdge>(DefaultEdge.class); g4.addVertex(0); g4.addVertex(1); g4.addVertex(2); g4.addVertex(3); g4.addVertex(4); g4.addVertex(5); g4.addEdge(0, 1); g4.addEdge(1, 2); g4.addEdge(2, 3); g4.addEdge(4, 5); g4.addEdge(4, 2); g4.addEdge(5, 2); g4.addEdge(5, 3); VF2GraphIsomorphismInspector<Integer, DefaultEdge> vf2 = new VF2GraphIsomorphismInspector<Integer, DefaultEdge>(g2, g1), vf3 = new VF2GraphIsomorphismInspector<Integer, DefaultEdge>(g1, g2), vf4 = new VF2GraphIsomorphismInspector<Integer, DefaultEdge>(g3, g4); assertEquals(false, vf2.isomorphismExists()); assertEquals(false, vf3.isomorphismExists()); assertEquals(false, vf4.isomorphismExists()); }