圖的着色算法


圖着色算法描述:

https://www.jianshu.com/p/6a52b390f5fa

給定無向連通圖和m種不同的顏色。用這些顏色為圖G的各頂點着色,每個頂點着一種顏色。是否有一種着色法使G中每條邊的兩個頂點有不同的顏色。

這個問題是圖的m可着色判定問題。若一個圖最少需要m種顏色才能使圖中每條邊相連接的兩個頂點着不同顏色,稱這個數m為這個圖的色數。

求一個圖的色數m稱為圖的m可着色優化問題。 給定一個圖以及m種顏色,請計算出塗色方案數。


 
 

圖的着色算法分析:

  1. Color存儲着色方案。
  2. 從第一個頂點開始着色,判斷是否安全。
  3. 安全則繼續着色直到頂點全部被着色,輸出可行的着色方案
  4. 若不安全則停止着色方案,回溯,測試下一方案

 

本人使用的是C#,以下是完整代碼,輸入為頂點數,顏色數和圖的連接矩陣。IsSafe函數判斷安全與否,ColorGraph函數具體着色。遞歸實現回溯。

using System;
namespace graphColoring
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入頂點數:");
            string numberN = Console.ReadLine();
            int N = Int32.Parse(numberN);

            Console.WriteLine("請輸入顏色數:");
            string numberColor = Console.ReadLine();
            int M= Int32.Parse(numberColor);

            int[, ] matN = new int[N, N];           //無向圖的鄰接矩陣
            Console.WriteLine("請輸入無向圖的鄰接矩陣:");
            for(int i = 0; i < N; i++)
            {
                string str = Console.ReadLine();
                string[] temp = str.Split(" ".ToCharArray());
                for(int j = 0; j < N; j++)
                {
                    matN[i, j] = Int32.Parse(temp[j]);
                }
            }
            int[,] MatN = matN;
            graph myGraph = new graph(N,M,MatN);
            Console.WriteLine("所有的方案:");
            myGraph.graphDeal();

        }
    }
    class graph
    {
        private int n;                           //頂點數
        private int m;                           //顏色數
        private int[,] matN;                     //鄰接矩陣
        private int[] color;                     //着色方案
        public graph(int N,int M,int[,] MatN)
        {
            n = N;
            m = M;
            matN = MatN;
            color = new int[n];
            for (int i = 0; i < n; i++)
                color[i] = 0;
        }
        public void graphDeal()
        {
            ColorGraph(matN, m, color, 0);
        }
        private bool IsSafe(int[, ] matN,int[] color, int n1, int i)
        {
            for(int j = 0; j < n; j++)
                if (matN[n1, j] == 1 && i == color[j])
                    return false;
            return true;
        }
        private void ColorGraph(int[, ] matN,int m,int[] color,int n1)
        {
            if (n == n1)
            {
                printSolution(color);
                return;
            }
            for(int i = 1; i <= m; i++)
            {
                if (IsSafe(matN, color, n1, i))
                {
                    color[n1] = i;
                    ColorGraph(matN, m, color, n1 + 1);
                    color[n1] = 0;                           //如果缺少則color無法在每個正確或者錯誤的方案完成后重置
                }
            }
        }
        void printSolution(int[] color)
        {
            foreach(int i in color)
            {
                Console.Write(i+" ");
            }
            Console.WriteLine();
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM