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.extensions.reports; 006 007 import graphlab.graph.graph.GraphModel; 008 import graphlab.graph.graph.SubGraph; 009 import graphlab.graph.graph.VertexModel; 010 import graphlab.library.BaseVertex; 011 import graphlab.platform.lang.CommandAttitude; 012 import graphlab.platform.parameter.Parameter; 013 import graphlab.plugins.main.GraphData; 014 import graphlab.plugins.reports.extension.GraphReportExtension; 015 016 import java.util.ArrayDeque; 017 import java.util.HashSet; 018 import java.util.Vector; 019 020 /** 021 * @author Azin Azadi 022 */ 023 024 @CommandAttitude(name = "maximum_independent_set", abbreviation = "_mis") 025 public class MaxIndependentSetReport implements GraphReportExtension { 026 //todo: impelemnt these: 027 // @Parameter(name = "Lower Bound", description = "Lower Bound for the number of independent set members, This will make the search Interval smaller") 028 // public Integer lowerBound = 1; 029 // 030 // @Parameter(name = "All Independent Sets", description = "Create a list of all independent sets of graph using minimum number of members") 031 // public Boolean allColorings = false; 032 // 033 034 public String getName() { 035 return "Max Independent Set"; 036 } 037 038 public String getDescription() { 039 return "Maximum independent set of graph vertices"; 040 } 041 042 043 public Object calculate(GraphData gd) { 044 GraphModel graph = gd.getGraph(); 045 Vector<ArrayDeque<BaseVertex>> maxsets = getMaxIndependentSet(graph); 046 Vector<SubGraph> ret = new Vector<SubGraph>(); 047 for (ArrayDeque<BaseVertex> maxset : maxsets) { 048 SubGraph sd = new SubGraph(graph); 049 sd.vertices = new HashSet<VertexModel>(); 050 for (BaseVertex v : maxset) { 051 sd.vertices.add((VertexModel) v); 052 } 053 ret.add(sd); 054 } 055 return ret; 056 } 057 058 public static Vector<ArrayDeque<BaseVertex>> getMaxIndependentSet(GraphModel graph) { 059 Partitioner p = new Partitioner(graph); 060 MaxIndSetSubSetListener l = new MaxIndSetSubSetListener(); 061 p.findAllSubsets(l); 062 Vector<ArrayDeque<BaseVertex>> ret = new Vector<ArrayDeque<BaseVertex>>(); 063 for (ArrayDeque<BaseVertex> set : l.maxsets) { 064 if (set.size() == l.max) { 065 ret.add(set); 066 } 067 } 068 return ret; 069 } 070 071 public static int getMaxIndependentSetSize(GraphModel graph, boolean putFirstVertexInSet) { 072 System.out.println(graph); 073 Partitioner p = new Partitioner(graph); 074 // max = -1; 075 // MaxIndSetSubSetSizeListener l = new MaxIndSetSubSetSizeListener(); 076 return p.findMaxIndSet(putFirstVertexInSet); 077 // System.out.println("\n sets checked"+l.nn); 078 // return l.max; 079 } 080 081 } 082 083 class MaxIndSetSubSetListener implements SubSetListener { 084 Vector<ArrayDeque<BaseVertex>> maxsets = new Vector<ArrayDeque<BaseVertex>>(); 085 // ArrayDeque<BaseVertex> maxset = new ArrayDeque<BaseVertex>(); 086 int max = -1; 087 088 public boolean subsetFound(int t, ArrayDeque<BaseVertex> complement, ArrayDeque<BaseVertex> set) { 089 if (max <= set.size()) { 090 max = set.size(); 091 maxsets.add(new ArrayDeque<BaseVertex>(set)); 092 } 093 return false; 094 } 095 } 096 097 class MaxIndSetSubSetSizeListener implements SubSetListener { 098 int max = -1; 099 int nn = 0; 100 101 public boolean subsetFound(int t, ArrayDeque<BaseVertex> complement, ArrayDeque<BaseVertex> set) { 102 nn++; 103 if (max < set.size()) { 104 max = set.size(); 105 } 106 return false; 107 } 108 }