What is graph coloring
A coloring of a simple graph is the assignment of a color to each vertex of the graph so that no two adjacent vertices are assigned the same color. The chromatic number of a graph is the least number of colors needed for coloring of the graph. The problem here is to color a graph with its chromatic number.
Suppose that we are coloring a simple undirected graph.
Given a graph with n vertices. This algorithm will color each vertex with a number. F(x) is the color of vertex x.
In this article, I am using the Recursive-Largest-First algorithm by F. T. Leighton. Here is the pseudo code:
colornumber = 0; while (number_of_uncolored_vertex > 0){
determine a vertex x of maximal degree in G;
colornumber = colornumber + 1;
F(x) = colornumber;
NN = set of non-neighbors of x;
while (cardinality_of_NN > 0){ maxcn = 1; ydegree = 1; for every vertex z in NN{
cn=number of common neighbors of z and x;
if (cn > maxcn or (cn == maxcn and degree(z) < ydegree)){
y = z;
ydegree = degree(y);
maxcn = cn;
}
}
if (maxcn == 0){ y=vertex of maximal degree in NN;
}
F(y) = colornumber;
contract y into x;
update the set NN of non-neighbors of x;
}
G = G x; }
Data structure
We can use an adjacency matrix to store the adjacent status of the vertices. If the graph has n vertices, there will be an n-degree square adjacency matrix.
a[i][j] will be 1 if the ith and jth vertices are adjacent, and 0 otherwise. For example:

// below is the adjacency matrix
7 // number of vertices
0 1 1 0 1 1 0
1 0 0 1 0 1 1
1 0 0 1 1 1 0
0 1 1 0 0 1 1
1 0 1 0 0 0 1
1 1 1 1 0 0 0
0 1 0 1 1 0 0
We will color the vertices with numbers instead of color, uUsing an array color[n] to store the numbers. For example: color[0] stores the color number of the first vertex, so color[0] = 3 means we colored vertex 1 with color 3.
C/C++ code
Here is the ariables declaration:
const int MAX = 100;
int n;
int a[MAX][MAX];
int color[MAX];
int degree[MAX];
int NN[MAX];
int NNCount;
int unprocessed;
Here is the coloring function:
void Coloring()
{
int x,y;
int ColorNumber = 0;
int VerticesInCommon = 0;
while (unprocessed>0) {
x = MaxDegreeVertex(); ColorNumber ++;
color[x] = ColorNumber; unprocessed --;
UpdateNN(ColorNumber); while (NNCount>0)
{
y = FindSuitableY(ColorNumber, VerticesInCommon);
if (VerticesInCommon == 0)
y = MaxDegreeInNN();
color[y] = ColorNumber;
unprocessed --;
UpdateNN(ColorNumber);
}
}
}
Other necessary functions:
void GetInput()
{
ifstream in("input.txt");
in >> n; for (int i=0; i < n; i++) for (int j=0; j < n; j++)
in >> a[i][j]; in.close();
}
void Init()
{
for (int i=0; i < n; i++)
{
color[i] = 0; degree[i] = 0; for (int j = 0; j < n; j++)
if (a[i][j] == 1) degree[i] ++; }
NNCount = 0; unprocessed = n;
}
int MaxDegreeVertex()
{
int max = -1;
int max_i;
for (int i =0; i < n; i++)
if (color[i] == 0)
if (degree[i]>max)
{
max = degree[i];
max_i = i;
}
return max_i;
}
void scannedInit(int scanned[MAX])
{
for (int i=0; i < n; i++)
scanned[i] = 0;
}
void UpdateNN(int ColorNumber)
{
NNCount = 0;
for (int i=0; i < n; i++)
if (color[i] == 0)
{
NN[NNCount] = i;
NNCount ++; }
for (int i=0; i < n; i++)
if (color[i] == ColorNumber) for (int j=0; j < NNCount; j++)
while (a[i][NN[j]] == 1)
{
NN[j] = NN[NNCount - 1];
NNCount --; }
}
int FindSuitableY(int ColorNumber, int& VerticesInCommon)
{
int temp,tmp_y,y;
int scanned[MAX];
VerticesInCommon = 0;
for (int i=0; i < NNCount; i++) {
tmp_y = NN[i];
temp = 0;
scannedInit(scanned);
for (int x=0; x < n; x++)
if (color[x] == ColorNumber) for (int k=0; k < n; k++)
if (color[k] == 0 && scanned[k] == 0)
if (a[x][k] == 1 && a[tmp_y][k] == 1)
{
temp ++;
scanned[k] = 1; }
if (temp > VerticesInCommon)
{
VerticesInCommon = temp;
y = tmp_y;
}
}
return y;
}
int MaxDegreeInNN()
{
int tmp_y; int temp, max = 0;
for (int i=0; i < NNCount; i++)
{
temp = 0;
for (int j=0; j < n; j++)
if (color[j] == 0 && a[NN[i]][j] == 1)
temp ++;
if (temp>max) {
max = temp; tmp_y = NN[i];
}
}
if (max == 0) return NN[0];
else return tmp_y;
}
void PrintOutput()
{
for (int i=0; i < n; i++)
cout << "Vertex " << i+1
<< " is colored " << color[i] << endl;
}
void main()
{
GetInput(); Init(); Coloring(); PrintOutput(); getch();
}
Result
This is the result for the above example:


Reference
- Graph coloring in Wiki
- Leighton, F. T., A graph coloring algorithm for large scheduling problems, Journal of Research of the National Bureau of Standards 84 (1979).