📜  实现 Vizing 定理的Java程序

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

实现 Vizing 定理的Java程序

图论的 Vizing 定理指出,每个简单的无向图的色指数都比图的最大度数 'd' 大 1。简单来说,该定理指出色度指数可以是 'd' 或 'd'+1。

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

例子:

算法:

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

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

上述方法的实现:

Java
// Java program to Implement
// Vizing's Theorem
  
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);
        
        for (i = 0; i < e; i++)
        {
            System.out.println("Edge from " + edges[i][0]
                               + " to " + edges[i][1]
                               + " : Color " + edges[i][2]);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Number of edges
        int e = 5;
  
        // 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;
  
        edges[4][0] = 1;
        edges[4][1] = 3;
  
        // Run the function
        chromaticIndex c = new chromaticIndex();
        c.edgeColoring(edges, e);
    }
}


输出
Chromatic Index = 3
Edge from 1 to 2 : Color 1
Edge from 2 to 3 : Color 2
Edge from 3 to 4 : Color 1
Edge from 4 to 1 : Color 2
Edge from 1 to 3 : Color 3