1 /**
2 * Copyright (c) 2008, The JUNG Authors
3 *
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9 * Created on Jun 7, 2008
10 *
11 */
12 package edu.uci.ics.jung.algorithms.filters;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import edu.uci.ics.jung.graph.Hypergraph;
18
19 /**
20 * Utility methods relating to filtering.
21 */
22 public class FilterUtils
23 {
24 /**
25 * Creates the induced subgraph from <code>graph</code> whose vertex set
26 * is equal to <code>vertices</code>. The graph returned has
27 * <code>vertices</code> as its vertex set, and includes all edges from
28 * <code>graph</code> which are incident only to elements of
29 * <code>vertices</code>.
30 *
31 * @param <V> the vertex type
32 * @param <E> the edge type
33 * @param <G> the graph type
34 * @param vertices the subset of <code>graph</code>'s vertices around
35 * which the subgraph is to be constructed
36 * @param graph the graph whose subgraph is to be constructed
37 * @return the subgraph induced by <code>vertices</code>
38 * @throws IllegalArgumentException if any vertex in
39 * <code>vertices</code> is not in <code>graph</code>
40 */
41 @SuppressWarnings("unchecked")
42 public static <V,E,G extends Hypergraph<V,E>> G createInducedSubgraph(Collection<V>
43 vertices, G graph)
44 {
45 G subgraph = null;
46 try
47 {
48 subgraph = (G)graph.getClass().newInstance();
49
50 for (V v : vertices)
51 {
52 if (!graph.containsVertex(v))
53 throw new IllegalArgumentException("Vertex " + v +
54 " is not an element of " + graph);
55 subgraph.addVertex(v);
56 }
57
58 for (E e : graph.getEdges())
59 {
60 Collection<V> incident = graph.getIncidentVertices(e);
61 if (vertices.containsAll(incident))
62 subgraph.addEdge(e, incident, graph.getEdgeType(e));
63 }
64 }
65 catch (InstantiationException e)
66 {
67 throw new RuntimeException("Unable to create copy of existing graph: ", e);
68 }
69 catch (IllegalAccessException e)
70 {
71 throw new RuntimeException("Unable to create copy of existing graph: ", e);
72 }
73 return subgraph;
74 }
75
76 /**
77 * Creates the induced subgraphs of <code>graph</code> associated with each
78 * element of <code>vertex_collections</code>.
79 * Note that these vertex collections need not be disjoint.
80 * @param <V> the vertex type
81 * @param <E> the edge type
82 * @param <G> the graph type
83 * @param vertex_collections the collections of vertex collections to be
84 * used to induce the subgraphs
85 * @param graph the graph whose subgraphs are to be created
86 * @return the induced subgraphs of <code>graph</code> associated with each
87 * element of <code>vertex_collections</code>
88 */
89 public static <V,E,G extends Hypergraph<V,E>> Collection<G>
90 createAllInducedSubgraphs(Collection<? extends Collection<V>>
91 vertex_collections, G graph)
92 {
93 Collection<G> subgraphs = new ArrayList<G>();
94
95 for (Collection<V> vertex_set : vertex_collections)
96 subgraphs.add(createInducedSubgraph(vertex_set, graph));
97
98 return subgraphs;
99 }
100 }