Skip to content

Instantly share code, notes, and snippets.

@soundsmitten
Created December 12, 2012 22:34
Show Gist options
  • Save soundsmitten/4272283 to your computer and use it in GitHub Desktop.
Save soundsmitten/4272283 to your computer and use it in GitHub Desktop.
Java M-Coloring Graph algorithm.
// Wikipedia: a way of coloring the vertices of a graph such that no two
// adjacent vertices share the same color; this is called a vertex coloring.
// Similarly, an edge coloring assigns a color to each edge so that no two adjacent
// edges share the same color, and a face coloring of a planar graph assigns a color to
// each face or region so that no two faces that share a boundary have the same color.
// In NP-complete
boolean mcolor(int i, int m, int n, bool [][] W, int [] vcolor, int n)
{
// i: last colored vertex
// m: number of colors
// n: number of vertices
if (promising(i, vcolor, W)) {
if(i ==n)
{
for (k=1;k<=n;k++)
System.out.println(k + ": " + vcolor[k]);
}
else
for (k=1; k<=m; k++)
{
vcolor[i+1] = k;
mcolor (i+1,m,n,vcolor)
}
}
}
boolean promising (int i, int[] vcolor, bool[][]W)
{
for (int k; k<i; k++)
if(W[i][k] && vcolor[i] == vcolor[k])
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment