/**
* Copyright (c) 2008, the JUNG Project and the Regents of the University
* of California
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* http://jung.sourceforge.net/license.txt for a description.
* Created on Jun 7, 2008
*
*/
package edu.uci.ics.jung.algorithms.filters;
import java.util.ArrayList;
import java.util.Collection;
import edu.uci.ics.jung.graph.Hypergraph;
/**
* Utility methods relating to filtering.
*/
public class FilterUtils
{
/**
* Creates the induced subgraph from graph
whose vertex set
* is equal to vertices
. The graph returned has
* vertices
as its vertex set, and includes all edges from
* graph
which are incident only to elements of
* vertices
.
*
* @param the vertex type
* @param the edge type
* @param vertices the subset of graph
's vertices around
* which the subgraph is to be constructed
* @param graph the graph whose subgraph is to be constructed
* @return the subgraph induced by vertices
* @throws IllegalArgumentException if any vertex in
* vertices
is not in graph
*/
@SuppressWarnings("unchecked")
public static > G createInducedSubgraph(Collection
vertices, G graph)
{
G subgraph = null;
try
{
subgraph = (G)graph.getClass().newInstance();
for (V v : vertices)
{
if (!graph.containsVertex(v))
throw new IllegalArgumentException("Vertex " + v +
" is not an element of " + graph);
subgraph.addVertex(v);
}
for (E e : graph.getEdges())
{
Collection incident = graph.getIncidentVertices(e);
if (vertices.containsAll(incident))
subgraph.addEdge(e, incident, graph.getEdgeType(e));
}
}
catch (InstantiationException e)
{
throw new RuntimeException("Unable to create copy of existing graph: ", e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException("Unable to create copy of existing graph: ", e);
}
return subgraph;
}
/**
* Creates the induced subgraphs of graph
associated with each
* element of vertex_collections
.
* Note that these vertex collections need not be disjoint.
* @param the vertex type
* @param the edge type
* @param vertex_collections the collections of vertex collections to be
* used to induce the subgraphs
* @param graph the graph whose subgraphs are to be created
* @return the induced subgraphs of graph
associated with each
* element of vertex_collections
*/
public static > Collection
createAllInducedSubgraphs(Collection extends Collection>
vertex_collections, G graph)
{
Collection subgraphs = new ArrayList();
for (Collection vertex_set : vertex_collections)
subgraphs.add(createInducedSubgraph(vertex_set, graph));
return subgraphs;
}
}