[英]Graph Coloring with greedy algorithm
我正在嘗試在Java上為圖表着色。
例如,我有一個這樣的圖形:
現在我想用顏色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錯誤。
一點點解釋會有所幫助。
目前不使用Java,但我可以理解代碼。
代碼取決於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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.