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.

/**
 **  Java Program to Implement Graph Coloring Algorithm
 **/
 
import java.util.Scanner;
 
/** Class GraphColoring **/
public class GraphColoring
{    
    private int V, numOfColors;
    private int[] color; 
    private int[][] graph;
 
    /** Function to assign color **/
    public void graphColor(int[][] g, int noc)
    {
        V = g.length;
        numOfColors = noc;
        color = new int[V];
        graph = g;
 
        try
        {
            solve(0);
            System.out.println("No solution");
        }
        catch (Exception e)
        {
            System.out.println("\nSolution exists ");
            display();
        }
    }
    /** function to assign colors recursively **/
    public void solve(int v) throws Exception
    {
        /** base case - solution found **/
        if (v == V)
            throw new Exception("Solution found");
        /** try all colours **/
        for (int c = 1; c <= numOfColors; c++)
        {
            if (isPossible(v, c))
            {
                /** assign and proceed with next vertex **/
                color[v] = c;
                solve(v + 1);
                /** wrong assignement **/
                color[v] = 0;
            }
        }    
    }
    /** function to check if it is valid to allot that color to vertex **/
    public boolean isPossible(int v, int c)
    {
        for (int i = 0; i < V; i++)
            if (graph[v][i] == 1 && c == color[i])
                return false;
        return true;
    }
    /** display solution **/
    public void display()
    {
        System.out.print("\nColors : ");
        for (int i = 0; i < V; i++)
            System.out.print(color[i] +" ");
        System.out.println();
    }    
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Graph Coloring Algorithm Test\n");
        /** Make an object of GraphColoring class **/
        GraphColoring gc = new GraphColoring();
 
        /** Accept number of vertices **/
        System.out.println("Enter number of verticesz\n");
        int V = scan.nextInt();
 
        /** get graph **/
        System.out.println("\nEnter matrix\n");
        int[][] graph = new int[V][V];
        for (int i = 0; i < V; i++)
            for (int j = 0; j < V; j++)
                graph[i][j] = scan.nextInt();
 
        System.out.println("\nEnter number of colors");
        int c = scan.nextInt();
 
        gc.graphColor(graph, c);
 
    }
}
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

Related posts:

Spring MVC Content Negotiation
Spring @RequestMapping New Shortcut Annotations
Simple Single Sign-On with Spring Security OAuth2
Apache Commons Collections SetUtils
Serve Static Resources with Spring
Composition, Aggregation, and Association in Java
Introduction to Thread Pools in Java
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to add two large numbers using Linked List
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Implement Bloom Filter
Java Program to Generate Random Hexadecimal Byte
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Introduction to Spring Data REST
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Implement Knapsack Algorithm
Registration – Activate a New Account by Email
Java Program to Implement Adjacency Matrix
Batch Processing with Spring Cloud Data Flow
Java Program to Implement Min Heap
Handling Errors in Spring WebFlux
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Hướng dẫn Java Design Pattern – Command
Java Program to Perform the Sorting Using Counting Sort
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Threaded Binary Tree
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Simplify the DAO with Spring and Java Generics
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Implement Stack using Linked List