001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU Lesser General Public License (LGPL): http://www.gnu.org/licenses/
004    
005    package graphlab.library.algorithms.subgraphs;
006    
007    import graphlab.library.BaseEdge;
008    import graphlab.library.BaseGraph;
009    import graphlab.library.BaseVertex;
010    
011    import java.util.AbstractList;
012    import java.util.Comparator;
013    import java.util.TreeSet;
014    
015    public class InducedSubgraphs {
016    
017        public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>>
018        BaseGraph<VertexType, EdgeType>
019        getVertexInducedSubgraph(BaseGraph<VertexType, EdgeType> graph, AbstractList<VertexType> inducedVertices) {
020            BaseGraph<VertexType, EdgeType> newGraph = graph.createEmptyGraph();
021            newGraph.registerSubgraph(graph);
022            newGraph.setSubGraphIndex(graph.getNewSubgraphIndex());
023    
024            for (VertexType v : inducedVertices) {
025                graph.checkVertex(v);
026                newGraph.insertVertex(v);
027            }
028    
029            for (int i = 0; i < inducedVertices.size(); ++i) {
030                for (int j = i + 1; j < inducedVertices.size(); ++j) {
031                    AbstractList<EdgeType> edges = graph.getEdges(inducedVertices.get(i), inducedVertices.get(j));
032    
033                    for (EdgeType edge : edges)
034                        newGraph.insertEdge(edge);
035                }
036            }
037    
038            return newGraph;
039        }
040    
041        public static <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>>
042        BaseGraph<VertexType, EdgeType>
043        getEdgeInducedSubgraph(BaseGraph<VertexType, EdgeType> graph, AbstractList<EdgeType> inducedEdges) {
044            BaseGraph<VertexType, EdgeType> newGraph = graph.createEmptyGraph();
045            newGraph.registerSubgraph(graph);
046            newGraph.setSubGraphIndex(graph.getNewSubgraphIndex());
047    
048            TreeSet<VertexType> vertices = new TreeSet<VertexType>(
049                    new Comparator<VertexType>() {
050                        public int compare(VertexType o1, VertexType o2) {
051                            if (o1.getId() < o2.getId())
052                                return -1;
053    
054                            if (o1.getId() == o2.getId())
055                                return 0;
056    
057                            return 1;
058                        }
059                    }
060            );
061    
062            //Removing duplicate vertices by adding them to a TreeSet
063            for (EdgeType e : inducedEdges) {
064                graph.checkVertex(e.source);
065                graph.checkVertex(e.target);
066                vertices.add(e.source);
067                vertices.add(e.target);
068            }
069    
070            for (VertexType v : vertices)
071                newGraph.insertVertex(v);
072    
073            for (EdgeType e : inducedEdges)
074                newGraph.insertEdge(e);
075    
076            return newGraph;
077        }
078    
079    
080    }