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.coloring; 006 007 import graphlab.library.BaseEdge; 008 import graphlab.library.BaseGraph; 009 import graphlab.library.BaseVertex; 010 import graphlab.library.algorithms.Algorithm; 011 import graphlab.library.algorithms.AutomatedAlgorithm; 012 import graphlab.library.event.GraphRequest; 013 import graphlab.library.event.VertexEvent; 014 import graphlab.library.exceptions.InvalidGraphException; 015 016 public class SampleColoring 017 <VertexType extends BaseVertex, EdgeType extends BaseEdge<VertexType>> 018 extends Algorithm 019 implements AutomatedAlgorithm 020 021 { 022 BaseGraph<VertexType, EdgeType> graph; 023 024 public SampleColoring(BaseGraph<VertexType, EdgeType> g) { 025 graph = g; 026 027 } 028 029 public SampleColoring() { 030 graph = null; 031 032 } 033 034 public void doColoring() { 035 if (graph == null) 036 throw new InvalidGraphException(); 037 038 for (VertexType v : graph) { 039 v.setColor(graph.getInDegree(v)); 040 dispatchEvent( 041 new VertexEvent<VertexType, EdgeType>(graph, v, VertexEvent.EventType.COLOR_CHANGE)); 042 } 043 } 044 045 public void doAlgorithm() { 046 GraphRequest<VertexType, EdgeType> gr = new GraphRequest<VertexType, EdgeType>(); 047 dispatchEvent(gr); 048 graph = gr.getGraph(); 049 doColoring(); 050 } 051 052 }