Java Program to Implement the Vizing's Theorem


In this problem, we need to implement Vizing's Theorem. Vizing's Theorem is used with graphs.

Theorem statement - For any undirected graph G, the value of the Chromatic index is equal to the d or d + 1, where d is the maximum degree of the graph.

The degree for any vertex is the total number of incoming or outgoing edges.

Problem statement - We have given a graph and need to implement Vizing's Theorem to find the Chromatic index of the graph.

Note - The chromatic index is a positive integer, requiring a total number of different colors to color all edges so that the same vertex never shares the edges with the same color.

Sample examples

Input

totalEdges = 6, 1 -> 2, 2 -> 3, 3 -> 4, 4 -> 1, 4 -> 5, 3 -> 5

Output


Explanation

1 -> 2 : Color is 1

2 -> 3 : Color is 2

3 -> 4 : Color is 1

4 -> 1 : Color is 2

3 -> 5 : Color is 3

0 -> 0 : Color is 1

Input

totalEdges = 3, 1 -> 2, 2 -> 3, 3 -> 1

Output


1 -> 2 : Color is 1

2 -> 3 : Color is 2

3 -> 1 : Color is 3

Input

totalEdges = 10, 1 -> 5, 1 -> 2, 1 -> 4, 1 -> 3,  2 -> 3, 2 -> 4, 2 -> 5, 3 -> 4, 3 -> 5, 4 -> 5

Output

6

Explanation

1 -> 5 : Color is 1

1 -> 2 : Color is 2

1 -> 4 : Color is 3

1 -> 3 : Color is 4

2 -> 3 : Color is 1

2 -> 4 : Color is 4

2 -> 5 : Color is 3

3 -> 4 : Color is 2

3 -> 5 : Color is 5

4 -> 5 : Color is 6

Approach 1

This approach will use the 2D array to store the graph edges. Also, we will iterate each edge and assign color to the edge according to the color of other edges connected to the same vertex.

Algorithm

Step 1 - Define the 'totalEdges' variable and initialize it with the total edges in the graph.

Step 2 - Define the 'edgeMatrix' array, which stores the starting vertex, ending vertex, and color of the edge. Initially, the color of all edges is -1. After that, initialize the matrix and add edges.

Step 3 - Execute the findChrometicIndex() function. In the function, define the currentEdge, and currentColor variable, representing the color of the edge.

Step 4 - Use the loop to assign a color to each edge. Assign currentColor to the edgeMatrix[currentEdge][2].

Step 5 - Now, we need to check any edges associated with both vertexes of the current edges containing the same color. So, define the 'isSameColor' variable and initialize with false.

Step 6 - Traverse all edges. If q is equal to currentEdge, move to the next iteration.

Step 7 - If any edge shares a vertex with the current edge and has the same color, increase the value of currentColor by 1 and update isSameColor to true.

Step 8 - If isSameColor is true, continue to the next iteration. Else, update currentColor to 1 and move to the next edge.

Step 9 - Find the maximum color value among all edges, which is the chromatic index for the graph.

Example

import java.util.*;
public class Main {
   public static void findChrometicIndex(int[][] edgeMatrix, int 
totalEdges) {
      // start color with 1
      int currentEdge = 0, currentColor = 1;
      // Making iterations
      while (currentEdge < totalEdges) {
         // Assign a current color to the current edge
         edgeMatrix[currentEdge][2] = currentColor;
         // variable to track the same color vertex on two different edges
         boolean isSameColor = false;
         // Traverse edges
         for (int q = 0; q < totalEdges; q++) {
            if (q == currentEdge)
               continue;
            // Check for the same vertex of two edges
            if ((edgeMatrix[currentEdge][0] == edgeMatrix[q][0]) || (edgeMatrix[currentEdge][1] == edgeMatrix[q][0])
                  || (edgeMatrix[currentEdge][0] == edgeMatrix[q][1])
                  || (edgeMatrix[currentEdge][1] == edgeMatrix[q][1])) {
               // If two edges share a vertex and the color is the same, increase the color by 1 and update isSameColor
               if (edgeMatrix[currentEdge][2] == edgeMatrix[q][2]) {
                  // Increment the color by 1
                  currentColor++;
                  isSameColor = true;
                  break;
               }
            }
         }
         // start a new iteration for the same color
         if (isSameColor == true) {
            continue;
         }
         // reset color to 1
         currentColor = 1;
         currentEdge++;
      }
      // Find the maximum color
      int MaxColorValue = -1;
      for (currentEdge = 0; currentEdge < totalEdges; currentEdge++) {
         MaxColorValue = Math.max(MaxColorValue, edgeMatrix[currentEdge]
         [2]);
      }
      System.out.println("Chromatic Index for the given graph is = " + 
      MaxColorValue);
   }
   public static void main(String[] args) {
      // variable to store total edges
      int totalEdges = 6;
      // array to store edges
      int[][] edgeMatrix = new int[totalEdges][3];
      // edgeMatrix[i][2] represents the color of edge, Initially -1
      for (int i = 0; i < totalEdges; i++) {
         edgeMatrix[i][2] = -1;
      }
      // add edges
      edgeMatrix[0][0] = 1;
      edgeMatrix[0][1] = 2;
      edgeMatrix[1][0] = 2;
      edgeMatrix[1][1] = 3;
      edgeMatrix[2][0] = 3;
      edgeMatrix[2][1] = 4;
      edgeMatrix[3][0] = 4;
      edgeMatrix[3][1] = 1;
      edgeMatrix[4][0] = 4;
      edgeMatrix[4][1] = 5;   
      edgeMatrix[4][0] = 3;
      edgeMatrix[4][1] = 5;
      findChrometicIndex(edgeMatrix, totalEdges);
   }
}

Output

Chromatic Index for the given graph is = 3

Time complexity - O(N*N), where N is the total number of edges.

Space complexity - O(1) as we don't use extra space to find the chromatic index.

In the above code, we proved that the graph's chromatic index can either be d or d + 1, where d is the maximum degree. Programmers can take any input and observe the chromatic index for that.

Updated on: 24-Aug-2023

40 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements