使用贪婪算法进行图着色

时间:2019-05-06 17:17:41

标签: java algorithm graph greedy graph-coloring

我正在尝试在Java上为图形着色。

例如,我有一个这样的图形:

An example

现在,我想用颜色0(红色),1(蓝色)或2(绿色)填充顶点。可能的结果之一是:

Vertex 1 ---> Color 1 
Vertex 2 ---> Color 1 
Vertex 3 ---> Color 2
Vertex 4 ---> Color 0
Vertex 5 ---> Color 0
Vertex 6 ---> Color 2

这是我的代码,其中发现了一种使用贪婪算法为顶点着色的方法

public class Graph {
            int V;
            int[] verticleColor;
            boolean[] colorAvailable;
            ArrayList<ArrayList<Integer> > adjList;

            Graph(int v) { 
                V = v; 
                adjList = new ArrayList<ArrayList<Integer> >(V); 
                for (int i = 0; i < V+1; i++) {
                    adjList.add(new ArrayList<Integer>()); 
                }
            } 

            public void add(int x, int y) { 
                adjList.get(x).add(y); 
                adjList.get(y).add(x); 
            }

            public void colorTheVerticle() {
                 verticleColor = new int[V]; 

                for (int a = 0; a < verticleColor.length; a++) {
                    if (a == 0) {
                        verticleColor[a] = 0;
                    } else {
                        verticleColor[a] = -1;
                    }
                }

                colorAvailable = new boolean[V]; 
                for (int b = 0; b < colorAvailable.length; b++) {
                    colorAvailable[b] = true;
                }



                for (int c = 1; c < V; c++) {
                    Iterator<Integer> it = adjList.get(c).iterator() ; 
                    while (it.hasNext()) { 
                        int i = it.next();
                        if (verticleColor[i-1] != -1)  {
                            colorAvailable[verticleColor[i]] = false; 
                        }
                    } 


                    int color; 
                    for (color = 0; color < V; color++){ 
                        if (colorAvailable[color]) {
                            break; 
                        }
                    } 

                    verticleColor[c] = color; 

                    for (int d = 0; d <  colorAvailable.length; d++) {
                        colorAvailable[d] = true;
                    } 
                } 

                for (int u = 1; u < V+1; u++) {
                    System.out.println("Vertex " + u + " ---> Color " + verticleColor[u-1]);
                }
}

问题是,我得到的结果与预期不同,这是

Vertex 1 ---> Color 0
Vertex 2 ---> Color 0
Vertex 3 ---> Color 0
Vertex 4 ---> Color 1
Vertex 5 ---> Color 0
Vertex 6 ---> Color 2

此外,更改方法的一些方法也会导致ArrayIndexOutOfBoundsException错误。

稍作解释会有所帮助。

1 个答案:

答案 0 :(得分:0)

目前无法使用Java,但我可以理解代码。

代码取决于两个事实

  1. 对于n个顶点的图形,最多只能使用n种颜色。
  2. 遍历每个顶点并根据未在相邻顶点的颜色上使用的可用颜色列表分配可用颜色。

以上事实表明,使用贪婪算法最多可使用n种颜色,但通常少于n种颜色(除非每个顶点相互连接),尽管通常不是最佳选择。

分析计算颜色的方法:

初始化步骤

verticleColor = new int[V]; // initialise the colors assigned to each vertex to a list

for (int a = 0; a < verticleColor.length; a++) {
   if (a == 0) {
      verticleColor[a] = 0; // we can assign the first color to first vertex, no problem
   } else {
      verticleColor[a] = -1; // else for rest vertices, no assigned color yet
   }
}


colorAvailable = new boolean[V]; // initialise a list of available colors to assign to vertices, at most n
for (int b = 0; b < colorAvailable.length; b++) {
   colorAvailable[b] = true; // initially all colors are available for assignment
}

主计算循环

            for (int c = 1; c < V; c++) { // for all vertices, except first
                Iterator<Integer> it = adjList.get(c).iterator() ; // get iterator that loops through current vertex's adjacent vertices
                while (it.hasNext()) { 
                    int i = it.next(); // adjacent vertex
                    if (verticleColor[i-1] != -1)  { // if assigned color
                        colorAvailable[verticleColor[i]] = false; // this color is not available anymore
                    }
                } 


                int color; 
                for (color = 0; color < V; color++){ // loop through all colors
                    if (colorAvailable[color]) {
                        break; // find first available color, we can always find an available color since we have at most n possible colors
                    }
                } 
                /* effectively availableColors list holds the available and
                 used colors for each vertex and its adjacent/connected 
                 vertices, but we do not need to store multiple 
                 availableColors for each vertex, we can re-use same, no problem
                */
                verticleColor[c] = color; // color the vertex with this color

                // for next round, all colors are again available
                for (int d = 0; d <  colorAvailable.length; d++) {
                    colorAvailable[d] = true; // available color
                } 
            }