6 public class GraphNode {
8 public static boolean useEdgeLabels;
10 /* NodeStatus enumeration pattern ***********/
12 public static final NodeStatus UNVISITED = new NodeStatus("UNVISITED");
13 public static final NodeStatus PROCESSING = new NodeStatus("PROCESSING");
14 public static final NodeStatus FINISHED = new NodeStatus("FINISHED");
16 public static class NodeStatus {
17 private static String name;
18 private NodeStatus(String name) { this.name = name; }
19 public String toString() { return name; }
22 /* Edge *****************/
24 public static class Edge {
27 private GraphNode target;
28 private GraphNode source;
29 private String dotnodeparams = new String();
31 public Edge(String label, GraphNode target) {
36 public String getLabel() {
40 public void setSource(GraphNode s) {
44 public GraphNode getSource() {
48 public GraphNode getTarget() {
52 public void setDotNodeParameters(String param) {
54 throw new NullPointerException();
56 if (param.length() > 0) {
57 dotnodeparams = "," + param;
59 dotnodeparams = new String();
65 int discoverytime = -1;
66 int finishingtime = -1; /* used for searches */
68 Vector edges = new Vector();
69 Vector inedges = new Vector();
72 NodeStatus status = UNVISITED;
73 String dotnodeparams = new String();
76 public GraphNode(String label) {
77 this.nodelabel = label;
78 this.textlabel = label;
81 public GraphNode(String label, Object owner) {
82 this.nodelabel = label;
83 this.textlabel = label;
87 public GraphNode(String label, String textlabel, Object owner) {
88 this.nodelabel = label;
89 this.textlabel = textlabel;
93 public Object getOwner() {
97 public static void computeclosure(Collection nodes, Collection removed) {
98 Stack tovisit=new Stack();
99 tovisit.addAll(nodes);
100 while(!tovisit.isEmpty()) {
101 GraphNode gn=(GraphNode)tovisit.pop();
102 for(Iterator it=gn.edges();it.hasNext();) {
103 Edge edge=(Edge)it.next();
104 GraphNode target=edge.getTarget();
105 if (!nodes.contains(target)) {
106 if ((removed==null)||
107 (!removed.contains(target))) {
109 tovisit.push(target);
116 public void setDotNodeParameters(String param) {
118 throw new NullPointerException();
120 if (param.length() > 0) {
121 dotnodeparams = "," + param;
123 dotnodeparams = new String();
127 public void setStatus(NodeStatus status) {
128 if (status == null) {
129 throw new NullPointerException();
131 this.status = status;
134 public String getLabel() {
138 public String getTextLabel() {
142 public NodeStatus getStatus() {
146 public Iterator edges() {
147 return edges.iterator();
150 public Iterator inedges() {
151 return inedges.iterator();
154 public void addEdge(Edge newedge) {
155 newedge.setSource(this);
156 edges.addElement(newedge);
157 GraphNode tonode=newedge.getTarget();
158 tonode.inedges.addElement(newedge);
171 void discover(int time) {
172 discoverytime = time;
176 void finish(int time) {
177 assert status == PROCESSING;
178 finishingtime = time;
182 /** Returns finishing time for dfs */
184 public int getFinishingTime() {
185 return finishingtime;
189 public static class DOTVisitor {
191 java.io.PrintWriter output;
195 private DOTVisitor(java.io.OutputStream output) {
198 this.output = new java.io.PrintWriter(output, true);
201 private String getNewID(String name) {
202 tokennumber = tokennumber + 1;
203 return new String (name+tokennumber);
209 public static void visit(java.io.OutputStream output, Collection nodes) {
210 visit(output,nodes,null);
213 public static void visit(java.io.OutputStream output, Collection nodes, Collection special) {
214 DOTVisitor visitor = new DOTVisitor(output);
215 visitor.special=special;
216 visitor.nodes = nodes;
220 private void make() {
221 output.println("digraph dotvisitor {");
222 output.println("\trotate=90;");
223 /* output.println("\tpage=\"8.5,11\";");
224 output.println("\tnslimit=1000.0;");
225 output.println("\tnslimit1=1000.0;");
226 output.println("\tmclimit=1000.0;");
227 output.println("\tremincross=true;");*/
228 output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
229 output.println("\tedge [fontsize=6];");
231 output.println("}\n");
234 private void traverse() {
235 Set cycleset=GraphNode.findcycles(nodes);
237 Iterator i = nodes.iterator();
238 while (i.hasNext()) {
239 GraphNode gn = (GraphNode) i.next();
240 Iterator edges = gn.edges();
241 String label = gn.getTextLabel(); // + " [" + gn.discoverytime + "," + gn.finishingtime + "];";
243 if (cycleset.contains(gn))
244 option=",style=bold";
245 if (special!=null&&special.contains(gn))
246 option+=",shape=box";
247 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + option+"];");
249 while (edges.hasNext()) {
250 Edge edge = (Edge) edges.next();
251 GraphNode node = edge.getTarget();
252 if (nodes.contains(node)) {
253 String edgelabel = useEdgeLabels ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
254 output.println("\t" + gn.getLabel() + " -> " + node.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
261 /** This function returns the set of nodes involved in cycles.
262 * It only considers cycles containing nodes in the set 'nodes'.
264 public static Set findcycles(Collection nodes) {
265 HashSet cycleset=new HashSet();
266 SCC scc=DFS.computeSCC(nodes);
267 if (!scc.hasCycles())
269 for(int i=0;i<scc.numSCC();i++) {
271 cycleset.addAll(scc.getSCC(i));
276 public static class SCC {
280 public SCC(boolean acyclic, HashMap map,HashMap revmap,int numscc) {
281 this.acyclic=acyclic;
287 /** Returns whether the graph has any cycles */
288 public boolean hasCycles() {
292 /** Returns the number of Strongly Connected Components */
293 public int numSCC() {
297 /** Returns the strongly connected component number for the GraphNode gn*/
298 public int getComponent(GraphNode gn) {
299 return ((Integer)revmap.get(gn)).intValue();
302 /** Returns the set of nodes in the strongly connected component i*/
303 public Set getSCC(int i) {
304 Integer scc=new Integer(i);
305 return (Set)map.get(scc);
308 /** Returns whether the strongly connected component i contains a cycle */
309 boolean hasCycle(int i) {
310 Integer scc=new Integer(i);
311 Set s=(Set)map.get(scc);
314 Object [] array=s.toArray();
315 GraphNode gn=(GraphNode)array[0];
316 for(Iterator it=gn.edges();it.hasNext();) {
317 Edge e=(Edge)it.next();
318 if (e.getTarget()==gn)
319 return true; /* Self Cycle */
326 * DFS encapsulates the depth first search algorithm
328 public static class DFS {
333 Vector finishingorder=null;
337 private DFS(Collection nodes) {
340 /** Calculates the strong connected components for the graph composed
341 * of the set of nodes 'nodes'*/
342 public static SCC computeSCC(Collection nodes) {
344 throw new NullPointerException();
346 DFS dfs=new DFS(nodes);
347 dfs.sccmap=new HashMap();
348 dfs.sccmaprev=new HashMap();
349 dfs.finishingorder=new Vector();
350 boolean acyclic=dfs.go();
351 for (Iterator it = nodes.iterator();it.hasNext();) {
352 GraphNode gn = (GraphNode) it.next();
355 for(int i=dfs.finishingorder.size()-1;i>=0;i--) {
356 GraphNode gn=(GraphNode)dfs.finishingorder.get(i);
357 if (gn.getStatus() == UNVISITED) {
359 dfs.sccindex++; /* Increment scc index */
362 return new SCC(acyclic,dfs.sccmap,dfs.sccmaprev,dfs.sccindex);
365 void dfsprev(GraphNode gn) {
366 if (gn.getStatus()==FINISHED||!nodes.contains(gn))
368 gn.setStatus(FINISHED);
369 Integer i=new Integer(sccindex);
370 if (!sccmap.containsKey(i))
371 sccmap.put(i,new HashSet());
372 ((Set)sccmap.get(i)).add(gn);
374 for(Iterator edgeit=gn.inedges();edgeit.hasNext();) {
375 Edge e=(Edge)edgeit.next();
376 GraphNode gn2=e.getSource();
381 public static boolean depthFirstSearch(Collection nodes) {
383 throw new NullPointerException();
386 DFS dfs = new DFS(nodes);
390 private boolean go() {
393 boolean acyclic=true;
394 i = nodes.iterator();
395 while (i.hasNext()) {
396 GraphNode gn = (GraphNode) i.next();
400 i = nodes.iterator();
401 while (i.hasNext()) {
402 GraphNode gn = (GraphNode) i.next();
403 assert gn.getStatus() != PROCESSING;
404 if (gn.getStatus() == UNVISITED) {
412 private boolean dfs(GraphNode gn) {
413 boolean acyclic=true;
415 Iterator edges = gn.edges();
417 while (edges.hasNext()) {
418 Edge edge = (Edge) edges.next();
419 GraphNode node = edge.getTarget();
420 if (!nodes.contains(node)) /* Skip nodes which aren't in the set */
422 if (node.getStatus() == UNVISITED) {
425 } else if (node.getStatus()==PROCESSING) {
429 if (finishingorder!=null)
430 finishingorder.add(gn);