List of usage examples for org.apache.commons.collections15 Factory create
public T create();
From source file:edu.uci.ics.jung.algorithms.transformation.DirectionTransformer.java
/** * Transforms <code>graph</code> (which may be of any directionality) * into an undirected graph. (This may be useful for * visualization tasks).// w w w . jav a2 s .com * Specifically: * <ul> * <li/>Vertices are copied from <code>graph</code>. * <li/>Directed edges are 'converted' into a single new undirected edge in the new graph. * <li/>Each undirected edge (if any) in <code>graph</code> is 'recreated' with a new undirected edge in the new * graph if <code>create_new</code> is true, or copied from <code>graph</code> otherwise. * </ul> * * @param graph the graph to be transformed * @param create_new specifies whether existing undirected edges are to be copied or recreated * @param graph_factory used to create the new graph object * @param edge_factory used to create new edges * @return the transformed <code>Graph</code> */ public static <V, E> UndirectedGraph<V, E> toUndirected(Graph<V, E> graph, Factory<UndirectedGraph<V, E>> graph_factory, Factory<E> edge_factory, boolean create_new) { UndirectedGraph<V, E> out = graph_factory.create(); for (V v : graph.getVertices()) out.addVertex(v); for (E e : graph.getEdges()) { Pair<V> endpoints = graph.getEndpoints(e); V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); E to_add; if (graph.getEdgeType(e) == EdgeType.DIRECTED || create_new) to_add = edge_factory.create(); else to_add = e; out.addEdge(to_add, v1, v2, EdgeType.UNDIRECTED); } return out; }
From source file:edu.uci.ics.jung.algorithms.transformation.DirectionTransformer.java
/** * Transforms <code>graph</code> (which may be of any directionality) * into a directed graph. /*from w w w . ja v a 2s . c o m*/ * Specifically: * <ul> * <li/>Vertices are copied from <code>graph</code>. * <li/>Undirected edges are 'converted' into two new antiparallel directed edges in the new graph. * <li/>Each directed edge (if any) in <code>graph</code> is 'recreated' with a new edge in the new * graph if <code>create_new</code> is true, or copied from <code>graph</code> otherwise. * </ul> * * @param graph the graph to be transformed * @param create_new specifies whether existing directed edges are to be copied or recreated * @param graph_factory used to create the new graph object * @param edge_factory used to create new edges * @return the transformed <code>Graph</code> */ public static <V, E> Graph<V, E> toDirected(Graph<V, E> graph, Factory<DirectedGraph<V, E>> graph_factory, Factory<E> edge_factory, boolean create_new) { DirectedGraph<V, E> out = graph_factory.create(); for (V v : graph.getVertices()) out.addVertex(v); for (E e : graph.getEdges()) { Pair<V> endpoints = graph.getEndpoints(e); if (graph.getEdgeType(e) == EdgeType.UNDIRECTED) { V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); out.addEdge(edge_factory.create(), v1, v2, EdgeType.DIRECTED); out.addEdge(edge_factory.create(), v2, v1, EdgeType.DIRECTED); } else // if the edge is directed, just add it { V source = graph.getSource(e); V dest = graph.getDest(e); E to_add = create_new ? edge_factory.create() : e; out.addEdge(to_add, source, dest, EdgeType.DIRECTED); } } return out; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java
/** * Creates a <code>Graph</code> which is an edge-folded version of <code>h</code>, where * hyperedges are replaced by k-cliques in the output graph. * /* w w w. j a va 2s . co m*/ * <p>The vertices of the new graph are the same objects as the vertices of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding vertices * in <code>h</code> are connected by a hyperedge. Thus, each hyperedge with * <i>k</i> vertices in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph consist of collections of each hyperedge that connected * the corresponding vertex pair in the original graph.</p> * * @param <V> vertex type * @param <E> input edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @return a copy of the input graph where hyperedges are replaced by cliques */ public static <V, E> Graph<V, Collection<E>> foldHypergraphEdges(Hypergraph<V, E> h, Factory<Graph<V, Collection<E>>> graph_factory) { Graph<V, Collection<E>> target = graph_factory.create(); for (V v : h.getVertices()) target.addVertex(v); for (E e : h.getEdges()) { ArrayList<V> incident = new ArrayList<V>(h.getIncidentVertices(e)); populateTarget(target, e, incident); } return target; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java
/** * Creates a <code>Graph</code> which is a vertex-folded version of <code>h</code>, whose * vertices are the input's hyperedges and whose edges are induced by adjacent hyperedges * in the input.// ww w . ja v a 2 s .c om * * <p>The vertices of the new graph are the same objects as the hyperedges of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding edges * in <code>h</code> have a vertex in common. Thus, each vertex incident to * <i>k</i> edges in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph are created by the specified factory.</p> * * @param <V> vertex type * @param <E> input edge type * @param <F> output edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @param edge_factory factory used to generate the output edges * @return a transformation of the input graph whose vertices correspond to the input's hyperedges * and edges are induced by hyperedges sharing vertices in the input */ public static <V, E, F> Graph<E, F> foldHypergraphVertices(Hypergraph<V, E> h, Factory<Graph<E, F>> graph_factory, Factory<F> edge_factory) { Graph<E, F> target = graph_factory.create(); for (E e : h.getEdges()) target.addVertex(e); for (V v : h.getVertices()) { ArrayList<E> incident = new ArrayList<E>(h.getIncidentEdges(v)); for (int i = 0; i < incident.size(); i++) for (int j = i + 1; j < incident.size(); j++) target.addEdge(edge_factory.create(), incident.get(i), incident.get(j)); } return target; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java
/** * Converts <code>g</code> into a unipartite graph whose vertex set is the * vertices of <code>g</code>'s partition <code>p</code>. For vertices * <code>a</code> and <code>b</code> in this partition, the resultant * graph will include the edge <code>(a,b)</code> if the original graph * contains edges <code>(a,c)</code> and <code>(c,b)</code> for at least * one vertex <code>c</code>. * /* w ww. ja v a 2s .c o m*/ * <p>The vertices of the new graph are the same as the vertices of the * appropriate partition in the old graph; the edges in the new graph are * created by the input edge <code>Factory</code>.</p> * * <p>If there is more than 1 such vertex <code>c</code> for a given pair * <code>(a,b)</code>, the type of the output graph will determine whether * it will contain parallel edges or not.</p> * * <p>This function will not create self-loops.</p> * * @param <V> vertex type * @param <E> input edge type * @param g input k-partite graph * @param p predicate specifying vertex partition * @param graph_factory factory used to create the output graph * @param edge_factory factory used to create the edges in the new graph * @return a copy of the input graph folded with respect to the input partition */ public static <V, E> Graph<V, E> foldKPartiteGraph(KPartiteGraph<V, E> g, Predicate<V> p, Factory<Graph<V, E>> graph_factory, Factory<E> edge_factory) { Graph<V, E> newGraph = graph_factory.create(); // get vertices for the specified partition Collection<V> vertices = g.getVertices(p); for (V v : vertices) { newGraph.addVertex(v); for (V s : g.getSuccessors(v)) { for (V t : g.getSuccessors(s)) { if (!vertices.contains(t) || t.equals(v)) continue; newGraph.addVertex(t); newGraph.addEdge(edge_factory.create(), v, t); } } } return newGraph; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java
/** * Converts <code>g</code> into a unipartite graph whose vertices are the * vertices of <code>g</code>'s partition <code>p</code>, and whose edges * consist of collections of the intermediate vertices from other partitions. * For vertices//from w w w .ja v a2 s . c o m * <code>a</code> and <code>b</code> in this partition, the resultant * graph will include the edge <code>(a,b)</code> if the original graph * contains edges <code>(a,c)</code> and <code>(c,b)</code> for at least * one vertex <code>c</code>. * * <p>The vertices of the new graph are the same as the vertices of the * appropriate partition in the old graph; the edges in the new graph are * collections of the intermediate vertices <code>c</code>.</p> * * <p>This function will not create self-loops.</p> * * @param <V> vertex type * @param <E> input edge type * @param g input k-partite graph * @param p predicate specifying vertex partition * @param graph_factory factory used to create the output graph * @return the result of folding g into unipartite graph whose vertices * are those of the <code>p</code> partition of g */ public static <V, E> Graph<V, Collection<V>> foldKPartiteGraph(KPartiteGraph<V, E> g, Predicate<V> p, Factory<Graph<V, Collection<V>>> graph_factory) { Graph<V, Collection<V>> newGraph = graph_factory.create(); // get vertices for the specified partition, copy into new graph Collection<V> vertices = g.getVertices(p); for (V v : vertices) { newGraph.addVertex(v); for (V s : g.getSuccessors(v)) { for (V t : g.getSuccessors(s)) { if (!vertices.contains(t) || t.equals(v)) continue; newGraph.addVertex(t); Collection<V> v_coll = newGraph.findEdge(v, t); if (v_coll == null) { v_coll = new ArrayList<V>(); newGraph.addEdge(v_coll, v, t); } v_coll.add(s); } } } return newGraph; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformer.java
/** * Creates a <code>Graph</code> which is an edge-folded version of <code>h</code>, where * hyperedges are replaced by k-cliques in the output graph. * /*from www .j a v a 2 s. co m*/ * <p>The vertices of the new graph are the same objects as the vertices of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding vertices * in <code>h</code> are connected by a hyperedge. Thus, each hyperedge with * <i>k</i> vertices in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph are generated by the specified edge factory.</p> * * @param <V> vertex type * @param <E> input edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @param edge_factory factory used to create the new edges * @return a copy of the input graph where hyperedges are replaced by cliques */ public static <V, E> Graph<V, E> foldHypergraphEdges(Hypergraph<V, E> h, Factory<Graph<V, E>> graph_factory, Factory<E> edge_factory) { Graph<V, E> target = graph_factory.create(); for (V v : h.getVertices()) target.addVertex(v); for (E e : h.getEdges()) { ArrayList<V> incident = new ArrayList<V>(h.getIncidentVertices(e)); for (int i = 0; i < incident.size(); i++) for (int j = i + 1; j < incident.size(); j++) target.addEdge(edge_factory.create(), incident.get(i), incident.get(j)); } return target; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformerFixed.java
/** * Creates a <code>Graph</code> which is an edge-folded version of <code>h</code>, where * hyperedges are replaced by k-cliques in the output graph. * /* w ww . j a v a 2s . c o m*/ * <p>The vertices of the new graph are the same objects as the vertices of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding vertices * in <code>h</code> are connected by a hyperedge. Thus, each hyperedge with * <i>k</i> vertices in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph are generated by the specified edge factory.</p> * * @param <V> vertex type * @param <E> input edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @param edge_factory factory used to create the new edges * @return a copy of the input graph where hyperedges are replaced by cliques */ public static <V, E> UndirectedGraph<V, E> foldHypergraphEdges(Hypergraph<V, E> h, Factory<UndirectedGraph<V, E>> graph_factory, Factory<E> edge_factory) { UndirectedGraph<V, E> target = graph_factory.create(); for (V v : h.getVertices()) target.addVertex(v); for (E e : h.getEdges()) { ArrayList<V> incident = new ArrayList<V>(h.getIncidentVertices(e)); for (int i = 0; i < incident.size(); i++) for (int j = i + 1; j < incident.size(); j++) target.addEdge(edge_factory.create(), incident.get(i), incident.get(j)); } return target; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformerFixed.java
/** * Creates a <code>Graph</code> which is a vertex-folded version of <code>h</code>, whose * vertices are the input's hyperedges and whose edges are induced by adjacent hyperedges * in the input./*from ww w . j a v a 2 s . c o m*/ * * <p>The vertices of the new graph are the same objects as the hyperedges of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding edges * in <code>h</code> have a vertex in common. Thus, each vertex incident to * <i>k</i> edges in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph are created by the specified factory.</p> * * @param <V> vertex type * @param <E> input edge type * @param <F> output edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @param edge_factory factory used to generate the output edges * @return a transformation of the input graph whose vertices correspond to the input's hyperedges * and edges are induced by hyperedges sharing vertices in the input */ public static <V, E, F> UndirectedGraph<E, F> foldHypergraphVertices(Hypergraph<V, E> h, Factory<UndirectedGraph<E, F>> graph_factory, Factory<F> edge_factory) { UndirectedGraph<E, F> target = graph_factory.create(); for (E e : h.getEdges()) target.addVertex(e); for (V v : h.getVertices()) { ArrayList<E> incident = new ArrayList<E>(h.getIncidentEdges(v)); for (int i = 0; i < incident.size(); i++) for (int j = i + 1; j < incident.size(); j++) target.addEdge(edge_factory.create(), incident.get(i), incident.get(j)); } return target; }
From source file:edu.uci.ics.jung.algorithms.transformation.FoldingTransformerFixed.java
/** * Creates a <code>Graph</code> which is an edge-folded version of <code>h</code>, where * hyperedges are replaced by k-cliques in the output graph. * //w ww . j av a 2 s. c o m * <p>The vertices of the new graph are the same objects as the vertices of * <code>h</code>, and <code>a</code> * is connected to <code>b</code> in the new graph if the corresponding vertices * in <code>h</code> are connected by a hyperedge. Thus, each hyperedge with * <i>k</i> vertices in <code>h</code> induces a <i>k</i>-clique in the new graph.</p> * * <p>The edges of the new graph consist of collections of each hyperedge that connected * the corresponding vertex pair in the original graph.</p> * * @param <V> vertex type * @param <E> input edge type * @param h hypergraph to be folded * @param graph_factory factory used to generate the output graph * @return a copy of the input graph where hyperedges are replaced by cliques */ public static <V, E> UndirectedGraph<V, FoldedEdge<V, E>> foldHypergraphEdges(Hypergraph<V, E> h, Factory<UndirectedGraph<V, FoldedEdge<V, E>>> graph_factory) { UndirectedGraph<V, FoldedEdge<V, E>> target = graph_factory.create(); for (V v : h.getVertices()) target.addVertex(v); for (E e : h.getEdges()) { ArrayList<V> incident = new ArrayList<V>(h.getIncidentVertices(e)); populateTarget(target, e, incident); } return target; }