Java Program to Find Chromatic Index of Cyclic Graphs


Chromatic Index of a graph is the parameter which indicates the minimum number of colours needed to colour all the edges of graph such that no two edges sharing the common vertex have same coloured edge. In this article, we will discuss how to find the chromatic index of cyclic graphs using the Java programming language.

What is a Cyclic Graph?

Cyclic Graph is a graph which consists of at least one cycle path in that particular graph. Cyclic path is a path that starts from a specific node and ends at the same node.

What is Graph Colouring?

The process which is used to colour graph’s edges or graph’s vertices is known as “Graph Colouring”.

The chromatic index of a graph is one of the most important topic in graph theory as it has many practical applications in real-life scenarios such as the scheduling of tasks, frequency assignment in wireless communication networks, register allocation and many more. In this article, we will discuss Vizing’s theory for detecting the chromatic index of a cyclic graph and implement using Java programming language.

What is Degree of a Vertex?

The number of edges connecting a vertex is known as “Degree of a Vertex”.

Vizing’s Theorem

Vizing’s theorem states that if a simple graph ‘G’ has a maximum degree of ‘d’ then the chromatic index of graph ‘G’ can be either ‘d’ or ‘d+1’. You can go through in detail about algorithm at vizing’s theorem.

Algorithm

STEP 1 − Initialize a 2d-array with represents a graph.

STEP 2 − Initialize a variable with zero which indicates the chromatic index of the graph.

STEP 3 − Find the degrees of each vertex of the Graph ‘G’.

STEP 4 − Calculate the maxDegree of the Graph ‘G’.

STEP 5 − If the maxDegree of the Graph ‘G’ is even, then the Chromatic Index of graph is maxDegree.

STEP 6 − Else if the maxDegree of Graph ‘G’ is odd then theChromatic Index of Graph is maxDegree + 1.

Example

In this below example the Vizing’s algorithm is implemented in Java Programming Language to find the Chromatic Index of Graph.

We initialize a 2-d array which represents a Graph and the graph is passed to a function “chromaticIndexOfGraph”. This function returns the chromatic index of the graph. The function uses vizing’s theorem in calculating the chromatic index of the graph which generally calculates the degree of each vertex of the Graph. After finding the degree of each vertex it finds the maxDegree from the degree[] array. If the maxDegree is even, then the function returns the maxDegree else if maxDegree is odd it returns the value maxDegree + 1.

import java.util.*;
public class Main {
   // returns the chromatic index of the graph
   public static int chromaticIndexOfGraph(int[][] graph, int n) {
      int maxDegree = 0;
      int degree[] = new int[n];
      for (int i = 0; i < graph.length; i++) {
         for (int j = 0; j < graph[i].length; j++) {
            if (graph[i][j] == 1) {
               degree[j]++;
            }
         }
      }
      for(int i=0 ; i<degree.length; i++) {
         if(degree[i] > maxDegree) {
            maxDegree = degree[i];
         }
      }
      System.out.println("Max Degree:" + maxDegree);
      if (maxDegree % 2 == 0) {

         return maxDegree; //if maxDegree is even then chromaticIndex is maxDegree
      } else {

         return maxDegree + 1; //if maxDegree is odd then chromaticIndex is maxDegree+1
      }
   }
   public static void main(String[] args) {
      int n = 4; // defines the number of vertices in Graph
      int[][] graph = {
         {0, 1, 1, 1},{1, 0, 1, 0},{1, 1, 0, 1},{1, 0, 1, 0}
      };
      int chromatic_index = chromaticIndexOfGraph(graph, n);
      System.out.println("Chromatic index: " + chromatic_index);
    }
}

Output

Max Degree:3
Chromatic index: 4

Time Complexity: O(N2) Auxiliary Space: O(N)

In this article, we have learned how to find the Chromatic Index of Cyclic Graph using Java Programming Language.

Updated on: 10-Apr-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements