I. Description of the problem
A given undirected connected graph G and m different colors. These colors are used to color each vertex of figure g, one color for each vertex. Is there a coloring method that makes 2 vertices of each edge in g different colors. This problem is the M-colorable determinant of graphs. If a diagram requires a minimum of M color in order to make the 2 vertices of each side of the graph connected with different colors, it is said that the number m is the color number of the graph. The problem of finding the color number m of a graph is called the M-colorable optimization problem of graphs.
Given figure g= (v,e) and M colors, if the graph is not M colorable, give a negative answer; If the figure is M colorable, find out all the different coloring methods.
Two. Problem analysis
1>: (x1, x2, ..., xn) represents the color of vertex i x[i]
2>: A subset tree, a spatial tree for n=3,m=3 problems
3> pruning function: Vertex i and shaded adjacent vertex colors are not duplicated. Constraint functions
Three. Algorithm implementation
/** m coloring Problem Input Example 5 8 * 5*/#include <iostream>using namespace std;class color{friend int Mcolorin G (int,int,int**);p rivate:bool ok (int k), void backtrack (int t), int n,//number of vertices m,//available color number **a,//graph adjacency Matrix *x,//Current solution sum;//currently found Number of M coloring schemes};bool color::ok (int k) {//Check color availability for (int j=1; j<k; J + +) {if ((a[k][j]==1) && (X[j]==x[k])) return false;} return true;} /* Backtracking algorithm subset tree */void color::backtrack (int t) {if (t>n)//Search to leaf node {sum++;for (int i=1; i<=n; i++) {cout << x[i] < < "";} cout << Endl;} else{for (int i=1; i<=m; i++) {x[t] = I;if (ok (t)) {backtrack (t+1);} X[t] = 0;}}} int mcoloring (int n, int m, int** a) {Color c;//initialize CC.N = n; C.M = m; C.A = A; C.sum = 0;int *x = new int[n+1];for (int i=0; i<=n; i++) {x[i] = 0;} c.x = x; C.backtrack (1);d elete []x;return c.sum;} void Main () {int e,m,n,u,v; int** a = new int*[100]; for (int k=0; k<100; k++) {a[k] = new int[100];} while (1) {cout << input vertices n, number of sides e and number of colors M: "<< endl;cin >> N >>E >> m;cout << "Enter an edge of the non-graphic (for example, enter 1 2):" << endl;for (int i=0; i<e; i++) {cin >> u >> v;a [U] [v] = 1;a[v][u] = 1;} cout << "Viable coloring method:" << endl;int sum = mcoloring (n,m,a); cout << "Total" << sum << "Coloring method" << Endl;}}
M coloring problem for graph