
- 資料結構與演算法
- DSA - 首頁
- DSA - 概述
- DSA - 環境搭建
- DSA - 演算法基礎
- DSA - 漸近分析
- 資料結構
- DSA - 資料結構基礎
- DSA - 資料結構和型別
- DSA - 陣列資料結構
- 連結串列
- DSA - 連結串列資料結構
- DSA - 雙向連結串列資料結構
- DSA - 迴圈連結串列資料結構
- 棧與佇列
- DSA - 棧資料結構
- DSA - 表示式解析
- DSA - 佇列資料結構
- 搜尋演算法
- DSA - 搜尋演算法
- DSA - 線性搜尋演算法
- DSA - 二分搜尋演算法
- DSA - 插值搜尋
- DSA - 跳躍搜尋演算法
- DSA - 指數搜尋
- DSA - 斐波那契搜尋
- DSA - 子列表搜尋
- DSA - 雜湊表
- 排序演算法
- DSA - 排序演算法
- DSA - 氣泡排序演算法
- DSA - 插入排序演算法
- DSA - 選擇排序演算法
- DSA - 歸併排序演算法
- DSA - 希爾排序演算法
- DSA - 堆排序
- DSA - 桶排序演算法
- DSA - 計數排序演算法
- DSA - 基數排序演算法
- DSA - 快速排序演算法
- 圖資料結構
- DSA - 圖資料結構
- DSA - 深度優先遍歷
- DSA - 廣度優先遍歷
- DSA - 生成樹
- 樹資料結構
- DSA - 樹資料結構
- DSA - 樹的遍歷
- DSA - 二叉搜尋樹
- DSA - AVL樹
- DSA - 紅黑樹
- DSA - B樹
- DSA - B+樹
- DSA - 伸展樹
- DSA - 字典樹
- DSA - 堆資料結構
- 遞迴
- DSA - 遞迴演算法
- DSA - 使用遞迴的漢諾塔
- DSA - 使用遞迴的斐波那契數列
- 分治法
- DSA - 分治法
- DSA - 最大最小問題
- DSA - Strassen矩陣乘法
- DSA - Karatsuba演算法
- 貪心演算法
- DSA - 貪心演算法
- DSA - 旅行商問題(貪心法)
- DSA - Prim最小生成樹
- DSA - Kruskal最小生成樹
- DSA - Dijkstra最短路徑演算法
- DSA - 地圖著色演算法
- DSA - 分數揹包問題
- DSA - 帶截止日期的作業排序
- DSA - 最優合併模式演算法
- 動態規劃
- DSA - 動態規劃
- DSA - 矩陣鏈乘法
- DSA - Floyd-Warshall演算法
- DSA - 0-1揹包問題
- DSA - 最長公共子序列演算法
- DSA - 旅行商問題(動態規劃法)
- 近似演算法
- DSA - 近似演算法
- DSA - 頂點覆蓋演算法
- DSA - 集合覆蓋問題
- DSA - 旅行商問題(近似演算法)
- 隨機化演算法
- DSA - 隨機化演算法
- DSA - 隨機化快速排序演算法
- DSA - Karger最小割演算法
- DSA - Fisher-Yates洗牌演算法
- DSA有用資源
- DSA - 問答
- DSA - 快速指南
- DSA - 有用資源
- DSA - 討論
地圖著色演算法
地圖著色問題是指,給定一個圖 G {V, E},其中 V 和 E 分別是圖的頂點集和邊集,需要對 V 中的所有頂點進行著色,使得任何兩個相鄰的頂點不能具有相同的顏色。
該演算法的現實應用包括:分配移動無線電頻率、制定時間表、設計數獨、分配暫存器等。
地圖著色演算法
使用地圖著色演算法,將圖 G 和要新增到圖中的顏色作為輸入,最終得到一個著色圖,其中任何兩個相鄰頂點都沒有相同的顏色。
演算法
初始化圖中的所有頂點。
選擇度數最高的節點,並用任何顏色對其進行著色。
使用選擇顏色函式選擇要用於圖的顏色,以確保沒有相鄰頂點具有相同的顏色。
檢查是否可以新增顏色,如果可以,則將其新增到解集中。
從步驟 2 重複此過程,直到輸出集準備好。
示例

步驟 1
找到所有頂點的度數 -
A – 4 B – 2 C – 2 D – 3 E – 3
步驟 2
選擇度數最高的頂點首先著色,即 A,並使用選擇顏色函式選擇顏色。檢查顏色是否可以新增到頂點,如果可以,則將其新增到解集中。

步驟 3
從剩餘頂點中選擇任何具有次高度數的頂點,並使用選擇顏色函式對其進行著色。
D 和 E 都有次高度數 3,因此在兩者之間選擇一個,例如 D。

D 與 A 相鄰,因此不能與 A 使用相同的顏色。因此,使用選擇顏色函式選擇不同的顏色。
步驟 4
下一個度數最高的頂點是 E,因此選擇 E。

E 與 A 和 D 都相鄰,因此不能與 A 和 D 使用相同的顏色。使用選擇顏色函式選擇不同的顏色。
步驟 5
下一個度數最高的頂點是 B 和 C。因此,隨機選擇一個。

B 與 A 和 E 相鄰,因此不允許使用 A 和 E 的顏色,但它與 D 不相鄰,因此可以使用 D 的顏色。
步驟 6
剩下的最後一個頂點是 C,它與 A 和 D 相鄰,不允許使用 A 和 D 的顏色。但它與 E 不相鄰,因此可以使用 E 的顏色。

示例
以下是地圖著色演算法在各種程式語言中的完整實現,其中圖的著色方式使得任何兩個相鄰頂點都沒有相同的顏色。
#include<stdio.h> #include<stdbool.h> #define V 4 bool graph[V][V] = { {0, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0}, }; bool isValid(int v,int color[], int c){ //check whether putting a color valid for v for (int i = 0; i < V; i++) if (graph[v][i] && c == color[i]) return false; return true; } bool mColoring(int colors, int color[], int vertex){ if (vertex == V) //when all vertices are considered return true; for (int col = 1; col <= colors; col++) { if (isValid(vertex,color, col)) { //check whether color col is valid or not color[vertex] = col; if (mColoring (colors, color, vertex+1) == true) //go for additional vertices return true; color[vertex] = 0; } } return false; //when no colors can be assigned } int main(){ int colors = 3; // Number of colors int color[V]; //make color matrix for each vertex for (int i = 0; i < V; i++) color[i] = 0; //initially set to 0 if (mColoring(colors, color, 0) == false) { //for vertex 0 check graph coloring printf("Solution does not exist."); } printf("Assigned Colors are: \n"); for (int i = 0; i < V; i++) printf("%d ", color[i]); return 0; }
輸出
Assigned Colors are: 1 2 3 1
#include<iostream> using namespace std; #define V 4 bool graph[V][V] = { {0, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0}, }; bool isValid(int v,int color[], int c){ //check whether putting a color valid for v for (int i = 0; i < V; i++) if (graph[v][i] && c == color[i]) return false; return true; } bool mColoring(int colors, int color[], int vertex){ if (vertex == V) //when all vertices are considered return true; for (int col = 1; col <= colors; col++) { if (isValid(vertex,color, col)) { //check whether color col is valid or not color[vertex] = col; if (mColoring (colors, color, vertex+1) == true) //go for additional vertices return true; color[vertex] = 0; } } return false; //when no colors can be assigned } int main(){ int colors = 3; // Number of colors int color[V]; //make color matrix for each vertex for (int i = 0; i < V; i++) color[i] = 0; //initially set to 0 if (mColoring(colors, color, 0) == false) { //for vertex 0 check graph coloring cout << "Solution does not exist."; } cout << "Assigned Colors are: \n"; for (int i = 0; i < V; i++) cout << color[i] << " "; return 0; }
輸出
Assigned Colors are: 1 2 3 1
public class mcolouring { static int V = 4; static int graph[][] = { {0, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0}, }; static boolean isValid(int v,int color[], int c) { //check whether putting a color valid for v for (int i = 0; i < V; i++) if (graph[v][i] != 0 && c == color[i]) return false; return true; } static boolean mColoring(int colors, int color[], int vertex) { if (vertex == V) //when all vertices are considered return true; for (int col = 1; col <= colors; col++) { if (isValid(vertex,color, col)) { //check whether color col is valid or not color[vertex] = col; if (mColoring (colors, color, vertex+1) == true) //go for additional vertices return true; color[vertex] = 0; } } return false; //when no colors can be assigned } public static void main(String args[]) { int colors = 3; // Number of colors int color[] = new int[V]; //make color matrix for each vertex for (int i = 0; i < V; i++) color[i] = 0; //initially set to 0 if (mColoring(colors, color, 0) == false) { //for vertex 0 check graph coloring System.out.println("Solution does not exist."); } System.out.println("Assigned Colors are: "); for (int i = 0; i < V; i++) System.out.print(color[i] + " "); } }
輸出
Assigned Colors are: 1 2 3 1
V = 4 graph = [[0, 1, 1, 0], [1, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 0]] def isValid(v, color, c): # check whether putting a color valid for v for i in range(V): if graph[v][i] and c == color[i]: return False return True def mColoring(colors, color, vertex): if vertex == V: # when all vertices are considered return True for col in range(1, colors + 1): if isValid(vertex, color, col): # check whether color col is valid or not color[vertex] = col if mColoring(colors, color, vertex + 1): return True # go for additional vertices color[vertex] = 0 return False # when no colors can be assigned colors = 3 # Number of colors color = [0] * V # make color matrix for each vertex if not mColoring( colors, color, 0): # initially set to 0 and for Vertex 0 check graph coloring print("Solution does not exist.") else: print("Assigned Colors are:") for i in range(V): print(color[i], end=" ")
輸出
Assigned Colors are: 1 2 3 1