📜  查找循环图的色指数的Java程序

📅  最后修改于: 2022-05-13 01:55:42.274000             🧑  作者: Mango

查找循环图的色指数的Java程序

图的色度指数是为图的边着色所需的最小颜色数,以便共享相同顶点的任何两条边具有不同的颜色。

而循环图是包含至少一个图循环的图,即循环意味着从至少一个节点回到其自身的路径。这里给出了循环图,我们必须找到该图的色指数。

例子:

方法:

通过应用 Vizing 定理,我们可以证明给定的图可以具有 'd' 或 'd'+1 的色指数,其中 d 是图的最大度数。

以下是该算法的分步方法:-

  1. 初始化边数和边列表。
  2. 根据 Vizing 定理为图形着色。
  3. 为边指定颜色并检查相邻边是否具有相同的颜色。
  4. 如果任何相邻边具有相同的颜色,则增加颜色以尝试该边的下一种颜色。
  5. 重复直到所有边根据定理得到它的颜色。
  6. 完成后打印所有边缘的颜色最大值和每个边缘的颜色。

下面是上述方法的实现:

Java
// Java program to find the chromatic
// index of a cyclic graph
import java.util.*;
  
public class chromaticIndex {
  
    // Function to find the chromatic index
    public void edgeColoring(int[][] edges, int e)
    {
        // Initialize edge to first
        // edge and color to color 1
        int i = 0, color = 1;
  
        // Repeat until all edges are done coloring
        while (i < e) {
            // Give the selected edge a color
            edges[i][2] = color;
            boolean flag = false;
            // Iterate through all others edges to check
            for (int j = 0; j < e; j++) {
                // Ignore if same edge
                if (j == i)
                    continue;
                // Check if one vertex is similar
                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])) {
                    // Check if color is similar
                    if (edges[i][2] == edges[j][2]) {
                        // Increment the color by 1
                        color++;
                        flag = true;
                        break;
                    }
                }
            }
  
            // If same color faced then repeat again
            if (flag == true) {
                continue;
            }
  
            // Or else proceed to a new vertex with color 1
            color = 1;
            i++;
        }
  
        // Check the maximum color from all the edge colors
        int maxColor = -1;
        for (i = 0; i < e; i++) {
            maxColor = Math.max(maxColor, edges[i][2]);
        }
  
        // Print the chromatic index
        System.out.println("Chromatic Index = " + maxColor);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Number of edges
        int e = 4;
  
        // Edge list
        int[][] edges = new int[e][3];
  
        // Initialize all edge colors to 0
        for (int i = 0; i < e; i++) {
            edges[i][2] = -1;
        }
  
        // Edges
        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;
  
        // Run the function
        chromaticIndex c = new chromaticIndex();
        c.edgeColoring(edges, e);
    }
}


输出
Chromatic Index = 2

输出:

Chromatic Index = 2

参考:

  1. 视觉定理
  2. https://en.wikipedia.org/wiki/Vizing%27s_theorem