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.extensions.reports;
006    
007    import graphlab.graph.graph.GraphColoring;
008    import graphlab.graph.graph.VertexModel;
009    import graphlab.platform.parameter.Parameter;
010    import graphlab.platform.parameter.Parametrizable;
011    import graphlab.plugins.main.GraphData;
012    import graphlab.plugins.reports.extension.GraphReportExtension;
013    
014    import java.util.Vector;
015    
016    /**
017     * @author Azin Azadi
018     */
019    public class ColoringReport implements GraphReportExtension, ColoringListener, Parametrizable {
020        Partitioner p;
021        Vector<GraphColoring> colorings;
022        boolean found;
023    
024        @Parameter(name = "Lower Bound", description = "Lower Bound for the number of colors, This will make the search Interval smaller")
025        public Integer lowerBound = 1;
026    
027        @Parameter(name = "All Colorings", description = "Create a list of all colorings of graph using minimum number of colors")
028        public Boolean allColorings=false;
029    
030        public Object calculate(GraphData gd) {
031            p = new Partitioner(gd.getGraph());
032            colorings = new Vector<GraphColoring>(1);
033            int ct = lowerBound;
034            found = false;
035            while (!found) {
036                tryToColor(ct++);
037            }
038            return colorings;
039        }
040    
041        public void tryToColor(int t) {
042            p.findAllPartitionings(t, this);
043        }
044    
045        public boolean coloringFound(final int t) {
046            found = true;
047            GraphColoring coloring = new GraphColoring();
048            for (int i = 0; i < p.vertices.length; i++) {
049                coloring.vertexColors.put((VertexModel) p.vertices[i], p.color[i]);
050            }
051            colorings.add(coloring);
052            return !allColorings;
053        }
054    
055        public String getName() {
056            return "Graph Coloring";
057        }
058    
059        public String getDescription() {
060            return "Graph Vertex Coloring";
061        }
062    
063        public String checkParameters() {
064            return lowerBound < 0 ? "Lower Bound should be positive" : null;
065        }
066    }