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 General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.graph.graph; 006 007 import java.util.ArrayList; 008 import java.util.HashSet; 009 import java.util.Iterator; 010 011 /** 012 * @author azin azadi 013 * @email 014 */ 015 public class SubGraph { 016 public GraphModel graph; 017 018 public SubGraph(GraphModel graph) { 019 this.graph = graph; 020 } 021 022 public HashSet<VertexModel> vertices = new HashSet<VertexModel>(); 023 public HashSet<EdgeModel> edges = new HashSet<EdgeModel>(); 024 public String label = ""; 025 026 027 /** 028 * using this constructor the selected graph will be considered as the default graph in the blackboard 029 */ 030 public SubGraph() { 031 graph = null; 032 } 033 034 public ArrayList<VertexModel> getNeighbors(VertexModel v) { 035 ArrayList<VertexModel> ret = new ArrayList<VertexModel>(); 036 for (VertexModel nv : graph.getNeighbors(v)) { 037 if (vertices.contains(nv)) 038 ret.add(nv); 039 } 040 return ret; 041 } 042 043 @Override 044 public String toString() { 045 String txt = ""; 046 if (label != null && !label.equals("")) { 047 txt += label + ": \n"; 048 } 049 if (vertices != null && vertices.size() > 0) { 050 txt = txt + "V: {"; 051 for (VertexModel v : vertices) { 052 txt = txt + v.getLabel() + ", "; 053 } 054 txt = txt.substring(0, txt.length() - 2) + "}"; 055 } 056 if (edges != null && edges.size() > 0) { 057 txt += "\nE: {"; 058 for (EdgeModel e : edges) { 059 txt = txt + e.getLabel() + ", "; 060 } 061 txt = txt.substring(0, txt.length() - 2) + "}"; 062 } 063 return txt; 064 } 065 066 }