Java Program to Implement Graph Coloring Algorithm

This is a Java Program to Implement Graph Coloring Algorithm. Graph Coloring is a way of coloring the vertices of a undirected graph such that no two adjacent vertices share the same color.

Here is the source code of the Java Program to Implement Graph Coloring Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **  Java Program to Implement Graph Coloring Algorithm
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class GraphColoring **/
  8. public class GraphColoring
  9. {    
  10.     private int V, numOfColors;
  11.     private int[] color; 
  12.     private int[][] graph;
  13.  
  14.     /** Function to assign color **/
  15.     public void graphColor(int[][] g, int noc)
  16.     {
  17.         V = g.length;
  18.         numOfColors = noc;
  19.         color = new int[V];
  20.         graph = g;
  21.  
  22.         try
  23.         {
  24.             solve(0);
  25.             System.out.println("No solution");
  26.         }
  27.         catch (Exception e)
  28.         {
  29.             System.out.println("\nSolution exists ");
  30.             display();
  31.         }
  32.     }
  33.     /** function to assign colors recursively **/
  34.     public void solve(int v) throws Exception
  35.     {
  36.         /** base case - solution found **/
  37.         if (v == V)
  38.             throw new Exception("Solution found");
  39.         /** try all colours **/
  40.         for (int c = 1; c <= numOfColors; c++)
  41.         {
  42.             if (isPossible(v, c))
  43.             {
  44.                 /** assign and proceed with next vertex **/
  45.                 color[v] = c;
  46.                 solve(v + 1);
  47.                 /** wrong assignement **/
  48.                 color[v] = 0;
  49.             }
  50.         }    
  51.     }
  52.     /** function to check if it is valid to allot that color to vertex **/
  53.     public boolean isPossible(int v, int c)
  54.     {
  55.         for (int i = 0; i < V; i++)
  56.             if (graph[v][i] == 1 && c == color[i])
  57.                 return false;
  58.         return true;
  59.     }
  60.     /** display solution **/
  61.     public void display()
  62.     {
  63.         System.out.print("\nColors : ");
  64.         for (int i = 0; i < V; i++)
  65.             System.out.print(color[i] +" ");
  66.         System.out.println();
  67.     }    
  68.     /** Main function **/
  69.     public static void main (String[] args) 
  70.     {
  71.         Scanner scan = new Scanner(System.in);
  72.         System.out.println("Graph Coloring Algorithm Test\n");
  73.         /** Make an object of GraphColoring class **/
  74.         GraphColoring gc = new GraphColoring();
  75.  
  76.         /** Accept number of vertices **/
  77.         System.out.println("Enter number of verticesz\n");
  78.         int V = scan.nextInt();
  79.  
  80.         /** get graph **/
  81.         System.out.println("\nEnter matrix\n");
  82.         int[][] graph = new int[V][V];
  83.         for (int i = 0; i < V; i++)
  84.             for (int j = 0; j < V; j++)
  85.                 graph[i][j] = scan.nextInt();
  86.  
  87.         System.out.println("\nEnter number of colors");
  88.         int c = scan.nextInt();
  89.  
  90.         gc.graphColor(graph, c);
  91.  
  92.     }
  93. }

Graph Coloring Algorithm Test
 
Enter number of vertices
 
10
 
Enter matrix
 
0 1 0 0 0 1 0 0 0 0
1 0 1 0 0 0 1 0 0 0
0 1 0 1 0 0 0 1 0 0
0 0 1 0 1 0 0 0 1 0
1 0 0 1 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 1 1
0 0 1 0 0 1 0 0 0 1
0 0 0 1 0 1 1 0 0 0
0 0 0 0 1 0 1 1 0 0
 
Enter number of colors
3
 
Solution exists
 
Colors : 1 2 1 2 3 2 1 3 3 2

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.