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.HashMap;
008    import java.util.Iterator;
009    import java.util.Map;
010    
011    /**
012     * @author Azin Azadi
013     */
014    public class GraphColoring {
015        public GraphModel graph;
016        public HashMap<VertexModel, Integer> vertexColors = new HashMap<VertexModel, Integer>();
017        public HashMap<EdgeModel, Integer> edgeColors = new HashMap<EdgeModel, Integer>();
018        public String label = "";
019    
020        public GraphColoring(GraphModel graph) {
021            this.graph = graph;
022        }
023    
024        /**
025         * using this constructor the selected graph will be considered as the default graph in the blackboard
026         */
027        public GraphColoring() {
028            graph = null;
029        }
030    
031        @Override
032        public String toString() {
033            String txt = "";
034            if (label != null && !label.equals("")) {
035                txt = txt + label + ":  \n";
036            }
037            if (vertexColors != null && vertexColors.size() > 0) {
038                txt = txt + "Vertex colors: ";
039                for (Map.Entry<VertexModel, Integer> p : vertexColors.entrySet()) {
040                    txt = txt + p.getKey().getLabel() + ":" + p.getValue() + " , ";
041                }
042            }
043            if (edgeColors != null && edgeColors.size() > 0) {
044                txt = txt + "\nEdge colors: ";
045                for (Map.Entry<EdgeModel, Integer> p : edgeColors.entrySet()) {
046                    txt = txt + p.getKey().getLabel() + ":" + p.getValue() + " , ";
047                }
048            }
049            return txt;
050        }
051    
052        public void applyColoring() {
053            if (vertexColors != null && vertexColors.size() > 0) {
054                for (Map.Entry<VertexModel, Integer> p : vertexColors.entrySet()) {
055                    p.getKey().setColor(p.getValue());
056                }
057            }
058            if (edgeColors != null && edgeColors.size() > 0) {
059                for (Map.Entry<EdgeModel, Integer> p : edgeColors.entrySet()) {
060                    p.getKey().setColor(p.getValue());
061                }
062            }
063    
064        }
065    
066        /**
067         * resets and stores all colorings of g
068         */
069        public void backupColoring() {
070            vertexColors = new HashMap<VertexModel, Integer>();
071            for (VertexModel v : graph) {
072                vertexColors.put(v, v.getColor());
073            }
074            edgeColors = new HashMap<EdgeModel, Integer>();
075            for (Iterator<EdgeModel> ite = graph.edgeIterator(); ite.hasNext();) {
076                EdgeModel e = ite.next();
077                edgeColors.put(e, e.getColor());
078            }
079        }
080    }