Implement a graph coloring algorithm using Welsh-Powell to minimize colors. Sort vertices by degree and return the chromatic number. Optimize adjacency matrix storage using a BitSet for memory efficiency.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Produced Java code using BitSet[] for memory-efficient adjacency storage, implementing Welsh-Powell coloring by sorting vertices by degree and greedily assigning colors while tracking forbidden sets with BitSet. The code returns an approximate chromatic number.
A straightforward way to implement the Welsh–Powell heuristic is to keep your graph in a compact, dense format, sort the vertices by non‑increasing degree and then greedily build independent sets of that ordering.
In Java you can take advantage of java.util.BitSet to avoid a full n×n boolean matrix. Each row of the “matrix” becomes a BitSet of length n, and an or of those bit sets gives you fast set‑union and membership tests when you build a color class:
java
import java.util.Arrays;
import java.util.BitSet;
public class WelshPowellColoring {
private final int n;
private final BitSet[] adj;
public WelshPowellColoring(int n) {
this.n = n;
this.adj = new BitSet[n];
for (int i = 0; i < n; i++) {
adj[i] = new BitSet(n);
}
}
/** Add an undirected edge between u and v. */
public void addEdge(int u, int v) {
adj[u].set(v);
adj[v].set(u);
}
/**
* Run the Welsh–Powell heuristic and return the number of colors used.
* Note: this is an upper bound on the true chromatic number.
*/
public int color() {
// build an array of vertex indices
Integer[] vertices = new Integer[n];
for (int i = 0; i < n; i++) {
vertices[i] = i;
}
// sort by descending degree
Arrays.sort(vertices, (a, b) ->
Integer.compare(adj[b].cardinality(), adj[a].cardinality()));
int[] color = new int[n];
int colorCount = 0;
for (int i = 0; i < n; i++) {
int v = vertices[i];
if (color[v] != 0) {
continue; // already colored
}
colorCount++;
color[v] = colorCount;
// maintain a BitSet of all vertices forbidden for this color
BitSet forbidden = (BitSet) adj[v].clone();
for (int j = i + 1; j < n; j++) {
int w = vertices[j];
if (color[w] == 0 && !forbidden.get(w)) {
color[w] = colorCount;
// any neighbor of w cannot receive this color
forbidden.or(adj[w]);
}
}
}
return colorCount;
}
public static void main(String[] args) {
WelshPowellColoring g = new WelshPowellColoring(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(2, 4);
int chromatic = g.color();
System.out.println("Colors used: " + chromatic);
}
}
Here adj[v] is a BitSet with bits set at the indices of vertices adjacent to v. The call to cardinality() gives the degree for sorting. The inner loop uses a running forbidden bitset for each color class so you can test !forbidden.get(w) in O(1) and update it with or in O(n/wordSize), which is much faster than scanning all previously colored vertices. The color method returns the number of colors assigned by the heuristic.