Java Program to Find Chromatic Index of Cyclic Graphs
Last Updated :
12 Apr, 2023
Chromatic Index of a graph is the minimum number of colours required to colour the edges of the graph such that any two edges that share the same vertex have different colours.
Whereas, the cyclic graph is a graph that contains at least one graph cycle i.e. cyclic means a path from at least one node back to itself. Here given, the cyclic graph we have to find the chromatic index of that graph.
Examples:
Input: e = 12
edges = {{ 1, 2}, { 2, 3}, { 3, 4},
{ 4, 1}, { 5, 6}, { 6, 7},
{ 7, 8}, { 8, 5}, { 1, 8},
{ 2, 5}, { 3, 6}, { 4, 7}}
Output: Chromatic Index = 3
Explanation:
Approach:
By applying Vizing’s Theorem we can prove that a given graph can have a chromatic index of ‘d’ or ‘d’+1, where d is the maximum degree of the graph.
Below is the step-by-step approach of the algorithm:-
- Initialize the number of edges and the edge list.
- Color the graph according to the Vizing’s Theorem.
- Assign a color to an edge and check if any adjacent edges have the same color or not.
- If any adjacent edge has the same color, then increment the color to try the next color for that edge.
- Repeat till all the edges get it’s color according to the theorem.
- Once done print the maximum value of color for all the edges and the colors of every edge.
Below is the implementation of the above approach:
Java
import java.util.*;
public class chromaticIndex {
public void edgeColoring( int [][] edges, int e)
{
int i = 0 , color = 1 ;
while (i < e) {
edges[i][ 2 ] = color;
boolean flag = false ;
for ( int j = 0 ; j < e; j++) {
if (j == i)
continue ;
if ((edges[i][ 0 ] == edges[j][ 0 ])
|| (edges[i][ 1 ] == edges[j][ 0 ])
|| (edges[i][ 0 ] == edges[j][ 1 ])
|| (edges[i][ 1 ] == edges[j][ 1 ])) {
if (edges[i][ 2 ] == edges[j][ 2 ]) {
color++;
flag = true ;
break ;
}
}
}
if (flag == true ) {
continue ;
}
color = 1 ;
i++;
}
int maxColor = - 1 ;
for (i = 0 ; i < e; i++) {
maxColor = Math.max(maxColor, edges[i][ 2 ]);
}
System.out.println( "Chromatic Index = " + maxColor);
}
public static void main(String[] args)
{
int e = 4 ;
int [][] edges = new int [e][ 3 ];
for ( int i = 0 ; i < e; i++) {
edges[i][ 2 ] = - 1 ;
}
edges[ 0 ][ 0 ] = 1 ;
edges[ 0 ][ 1 ] = 2 ;
edges[ 1 ][ 0 ] = 2 ;
edges[ 1 ][ 1 ] = 3 ;
edges[ 2 ][ 0 ] = 3 ;
edges[ 2 ][ 1 ] = 4 ;
edges[ 3 ][ 0 ] = 4 ;
edges[ 3 ][ 1 ] = 1 ;
chromaticIndex c = new chromaticIndex();
c.edgeColoring(edges, e);
}
}
|
Output
Chromatic Index = 2
Time Complexity: O(e2)
Auxiliary Space: O(1)
As constant extra space is used.
References:
- vizings-theorem
- https://en.wikipedia.org/wiki/Vizing%27s_theorem
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...