package org.simantics.scl.compiler.parser.generator.compression; public class GraphColoring { public static interface ColGraph { int size(); boolean areConnected(int a, int b); } /** * Finds a coloring of the graph so that all connected vertices * have a different color. Returns the number of colors used. */ public static int color(int[] color, ColGraph graph) { int size = graph.size(); // unassigned/unassignedCount is a list of unassigned vertex ids int[] unassigned = new int[size]; for(int i=0;i 0) { // Choose a vertex is bestId from unassigned list so that // bestBlockedCount=blockedCount[bestId] is maximized int bestId = 0; int bestUnassignedTableId = 0; int bestBlockedCount = -1; for(int i=0;i bestBlockedCount) { bestUnassignedTableId = i; bestId = id; bestBlockedCount = tempCount; } } // Remove from unassigned table unassigned[bestUnassignedTableId] = unassigned[--unassignedCount]; // Choose color int chosenColor = 0; if(bestBlockedCount == colorCount) // All colors are blocked chosenColor = colorCount++; else { // There is unblocked color for(int i=0;i