allow partial coloring

This commit is contained in:
ksrinivs 2013-11-26 15:04:09 -05:00
parent 8888d77f97
commit c1a923d0ba
2 changed files with 72 additions and 44 deletions

View File

@ -17,10 +17,10 @@ import junit.framework.Assert;
import org.junit.Test;
import com.ibm.wala.util.collections.Pair;
import com.ibm.wala.util.graph.Graph;
import com.ibm.wala.util.graph.impl.SlowSparseNumberedGraph;
import com.ibm.wala.util.graph.traverse.WelshPowell;
import com.ibm.wala.util.graph.traverse.WelshPowell.ColoredVertices;
public class WelshPowellTest {
@ -61,10 +61,10 @@ public class WelshPowellTest {
new Integer[]{6, 3, 1, 4},
new Integer[]{7, 1, 2, 4},
new Integer[]{8, 1, 2, 3}});
Pair<Map<Integer, Integer>,Integer> colors = new WelshPowell<Integer>().color(G);
ColoredVertices<Integer> colors = new WelshPowell<Integer>().color(G);
System.err.println(colors);
assertColoring(G, colors.fst);
Assert.assertTrue(colors.snd.intValue() <= 4);
assertColoring(G, colors.getColors());
Assert.assertTrue(colors.getNumColors() <= 4);
}
@Test
@ -81,9 +81,9 @@ public class WelshPowellTest {
new String[]{"star3", "poly3", "star1", "star5"},
new String[]{"star4", "poly4", "star1", "star2"},
new String[]{"star5", "poly5", "star2", "star3"}});
Pair<Map<String, Integer>,Integer> colors = new WelshPowell<String>().color(G);
ColoredVertices<String> colors = new WelshPowell<String>().color(G);
System.err.println(colors);
assertColoring(G, colors.fst);
Assert.assertTrue(colors.snd.intValue() == 3);
assertColoring(G, colors.getColors());
Assert.assertTrue(colors.getNumColors() == 3);
}
}

View File

@ -17,11 +17,35 @@ import java.util.SortedSet;
import java.util.TreeSet;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.collections.Pair;
import com.ibm.wala.util.graph.Graph;
public class WelshPowell<T> {
public static class ColoredVertices<T> {
private final boolean fullColoring;
private final Map<T, Integer> colors;
private final int numColors;
public boolean isFullColoring() {
return fullColoring;
}
public Map<T, Integer> getColors() {
return colors;
}
public int getNumColors() {
return numColors;
}
public ColoredVertices(boolean fullColoring, Map<T, Integer> colors, int numColors) {
this.fullColoring = fullColoring;
this.colors = colors;
this.numColors = numColors;
}
}
public static <T> Comparator<T> defaultComparator(final Graph<T> G) {
return new Comparator<T>() {
@ -37,55 +61,59 @@ public class WelshPowell<T> {
}
};
}
public Pair<Map<T,Integer>, Integer> color(final Graph<T> G) {
return color(G, defaultComparator(G));
public ColoredVertices<T> color(final Graph<T> G) {
return color(G, defaultComparator(G), Integer.MAX_VALUE);
}
public Pair<Map<T,Integer>, Integer> color(final Graph<T> G, Comparator<T> order) {
public ColoredVertices<T> color(final Graph<T> G, int maxColors) {
return color(G, defaultComparator(G), maxColors);
}
public ColoredVertices<T> color(final Graph<T> G, Comparator<T> order, int maxColors) {
Map<T, Integer> colors = HashMapFactory.make();
SortedSet<T> vertices = new TreeSet<T>(order);
for(T n : G) {
for (T n : G) {
vertices.add(n);
}
int currentColor = 0;
while(colors.size() < G.getNumberOfNodes()) {
for(T n : vertices) {
if (! colors.containsKey(n)) {
colors.put(n, currentColor);
for(T m : vertices) {
if (! colors.containsKey(m)) {
color_me: {
for(Iterator<T> ps = G.getPredNodes(m); ps.hasNext(); ) {
T p = ps.next();
if (colors.containsKey(p) && colors.get(p) == currentColor) {
break color_me;
}
}
for(Iterator<T> ss = G.getSuccNodes(m); ss.hasNext(); ) {
T s = ss.next();
if (colors.containsKey(s) && colors.get(s) == currentColor) {
break color_me;
}
}
colors.put(m, currentColor);
for (T n : vertices) {
if (!colors.containsKey(n)) {
colors.put(n, currentColor);
for (T m : vertices) {
if (!colors.containsKey(m)) {
color_me: {
for (Iterator<T> ps = G.getPredNodes(m); ps.hasNext();) {
T p = ps.next();
if (colors.containsKey(p) && colors.get(p) == currentColor) {
break color_me;
}
}
for (Iterator<T> ss = G.getSuccNodes(m); ss.hasNext();) {
T s = ss.next();
if (colors.containsKey(s) && colors.get(s) == currentColor) {
break color_me;
}
}
colors.put(m, currentColor);
}
}
currentColor++;
}
if (currentColor == maxColors - 1) {
return new ColoredVertices<T>(false, colors, currentColor);
}
currentColor++;
}
}
return Pair.make(colors, currentColor);
assert colors.size() == G.getNumberOfNodes();
return new ColoredVertices<T>(true, colors, currentColor);
}
}