Debian Codegraph @

src/com/google/javascript/jscomp/graph/GraphColoring.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.graph;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.graph.Annotation;
22 import com.google.javascript.jscomp.graph.GraphNode;
23 import com.google.javascript.jscomp.graph.SubGraph;
24
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Iterator;
28 import java.util.List;
29
30 /**
31 * Annotates the graph with a color in a way that no connected node will have
32 * the same color. Nodes of the same color cab then be partitioned together and
33 * be represented by a super node. This class will merely annotate the nodes
34 * with a color using {@link GraphNode#setAnnotation(Annotation)} and provide
35 * a node to super node mapping with {@link #getPartitionSuperNode(Object)}. The
36 * give graph itself will not be modified.
37 *
38 * <p>This algorithm is <b>NOT</b> deterministic by default. Passes that
39 * requires deterministic output should provide a {@code Comparator} in the
40 * constructor as a tie-breaker. This tie-break will be used when deciding
41 * which node should be colored first when multiple nodes have the same degree.
42 *
43 * @param <N> Value type that the graph node stores.
44 * @param <E> Value type that the graph edge stores.
45 *
46 */
47 public abstract class GraphColoring<N, E> {
48 // Maps a color (represented by an integer) to a variable. If, for example,
49 // the color 5 is mapped to "foo". Then any other variables colored with the
50 // color 5 will now use the name "foo".
51 protected N[] colorToNodeMap;
52 protected final AdjacencyGraph<N, E> graph;
53
54 public GraphColoring(AdjacencyGraph<N, E> graph) {
55 this.graph = graph;
56 }
57
58 /**
59 * Annotates the graph with {@link Color} objects using
60 * {@link GraphNode#setAnnotation(Annotation)}.
61 *
62 * @return The number of unique colors need.
63 */
64 public abstract int color();
65
66 /**
67 * Using the coloring as partitions, finds the node that represents that
68 * partition as the super node. The first to retrieve its partition will
69 * become the super node.
70 */
71 public N getPartitionSuperNode(N node) {
72 Preconditions.checkNotNull(colorToNodeMap,
73 "No coloring founded. color() should be called first.");
74 Color color = graph.getNode(node).getAnnotation();
75 N headNode = colorToNodeMap[color.value];
76 if (headNode == null) {
77 colorToNodeMap[color.value] = node;
78 return node;
79 } else {
80 return headNode;
81 }
82 }
83
84 public AdjacencyGraph<N, E> getGraph() {
85 return graph;
86 }
87
88 public static class Color implements Annotation {
89 int value = 0;
90
91 Color(int value) {
92 this.value = value;
93 }
94
95 @Override
96 public boolean equals(Object other) {
97 if (!(other instanceof Color)) {
98 return false;
99 } else {
100 return value == ((Color) other).value;
101 }
102 }
103
104 @Override
105 public int hashCode() {
106 return value;
107 }
108 }
109
110 /**
111 * Greedily assign nodes with high degree unique colors.
112 */
113 public static class GreedyGraphColoring<N, E> extends GraphColoring<N, E> {
114
115 private final Comparator<N> tieBreaker;
116 public GreedyGraphColoring(AdjacencyGraph<N, E> graph) {
117 this(graph, null);
118 }
119
120 /**
121 * @param tieBreaker In case of a tie between two nodes of the same degree,
122 * this comparator will determine which node should be colored first.
123 */
124 public GreedyGraphColoring(
125 AdjacencyGraph<N, E> graph, Comparator<N> tieBreaker) {
126 super(graph);
127 this.tieBreaker = tieBreaker;
128 }
129
130 @Override
131 public int color() {
132 graph.clearNodeAnnotations();
133 List<GraphNode<N, E>> worklist = Lists.newArrayList(graph.getNodes());
134
135 // Sort nodes by degree.
136 Collections.sort(worklist, new Comparator<GraphNode<N, E>>() {
137 @Override
138 public int compare(GraphNode<N, E> o1, GraphNode<N, E> o2) {
139 int result = graph.getWeight(o2.getValue())
140 - graph.getWeight(o1.getValue());
141 return result == 0 && tieBreaker != null ?
142 tieBreaker.compare(o1.getValue(), o2.getValue()) : result;
143 }
144 });
145
146 // Idea: From the highest to lowest degree, assign any uncolored node with
147 // a unique color if none of its neighbor has been assigned that color.
148 int count = 0;
149 do {
150 Color color = new Color(count);
151 SubGraph<N, E> subgraph = graph.newSubGraph();
152 for (Iterator<GraphNode<N, E>> i = worklist.iterator(); i.hasNext();) {
153 GraphNode<N, E> node = i.next();
154 if (subgraph.isIndependentOf(node.getValue())) {
155 subgraph.addNode(node.getValue());
156 node.setAnnotation(color);
157 i.remove();
158 }
159 }
160 count++;
161 } while (!worklist.isEmpty());
162 @SuppressWarnings("unchecked")
163 N[] map = (N[]) new Object[count];
164 colorToNodeMap = map;
165 return count;
166 }
167 }
168 }