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);
208 public static void visit(java.io.OutputStream output, Collection nodes) {
209 DOTVisitor visitor = new DOTVisitor(output);
210 visitor.nodes = nodes;
215 private void make() {
216 output.println("digraph dotvisitor {");
217 output.println("\trotate=90;");
218 /* output.println("\tpage=\"8.5,11\";");
219 output.println("\tnslimit=1000.0;");
220 output.println("\tnslimit1=1000.0;");
221 output.println("\tmclimit=1000.0;");
222 output.println("\tremincross=true;");*/
223 output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
224 output.println("\tedge [fontsize=6];");
226 output.println("}\n");
229 private void traverse() {
230 Set cycleset=GraphNode.findcycles(nodes);
232 Iterator i = nodes.iterator();
233 while (i.hasNext()) {
234 GraphNode gn = (GraphNode) i.next();
235 Iterator edges = gn.edges();
236 String label = gn.getTextLabel(); // + " [" + gn.discoverytime + "," + gn.finishingtime + "];";
238 if (cycleset.contains(gn))
239 option=",style=bold";
240 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + option+"];");
242 while (edges.hasNext()) {
243 Edge edge = (Edge) edges.next();
244 GraphNode node = edge.getTarget();
245 if (nodes.contains(node)) {
246 String edgelabel = useEdgeLabels ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
247 output.println("\t" + gn.getLabel() + " -> " + node.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
254 /** This function returns the set of nodes involved in cycles.
255 * It only considers cycles containing nodes in the set 'nodes'.
257 public static Set findcycles(Collection nodes) {
258 HashSet cycleset=new HashSet();
259 SCC scc=DFS.computeSCC(nodes);
260 if (!scc.hasCycles())
262 for(int i=0;i<scc.numSCC();i++) {
264 cycleset.addAll(scc.getSCC(i));
269 public static class SCC {
273 public SCC(boolean acyclic, HashMap map,HashMap revmap,int numscc) {
274 this.acyclic=acyclic;
280 /** Returns whether the graph has any cycles */
281 public boolean hasCycles() {
285 /** Returns the number of Strongly Connected Components */
286 public int numSCC() {
290 /** Returns the strongly connected component number for the GraphNode gn*/
291 public int getComponent(GraphNode gn) {
292 return ((Integer)revmap.get(gn)).intValue();
295 /** Returns the set of nodes in the strongly connected component i*/
296 public Set getSCC(int i) {
297 Integer scc=new Integer(i);
298 return (Set)map.get(scc);
301 /** Returns whether the strongly connected component i contains a cycle */
302 boolean hasCycle(int i) {
303 Integer scc=new Integer(i);
304 Set s=(Set)map.get(scc);
307 Object [] array=s.toArray();
308 GraphNode gn=(GraphNode)array[0];
309 for(Iterator it=gn.edges();it.hasNext();) {
310 Edge e=(Edge)it.next();
311 if (e.getTarget()==gn)
312 return true; /* Self Cycle */
319 * DFS encapsulates the depth first search algorithm
321 public static class DFS {
326 Vector finishingorder=null;
330 private DFS(Collection nodes) {
333 /** Calculates the strong connected components for the graph composed
334 * of the set of nodes 'nodes'*/
335 public static SCC computeSCC(Collection nodes) {
337 throw new NullPointerException();
339 DFS dfs=new DFS(nodes);
340 dfs.sccmap=new HashMap();
341 dfs.sccmaprev=new HashMap();
342 dfs.finishingorder=new Vector();
343 boolean acyclic=dfs.go();
344 for (Iterator it = nodes.iterator();it.hasNext();) {
345 GraphNode gn = (GraphNode) it.next();
348 for(int i=dfs.finishingorder.size()-1;i>=0;i--) {
349 GraphNode gn=(GraphNode)dfs.finishingorder.get(i);
350 if (gn.getStatus() == UNVISITED) {
352 dfs.sccindex++; /* Increment scc index */
355 return new SCC(acyclic,dfs.sccmap,dfs.sccmaprev,dfs.sccindex);
358 void dfsprev(GraphNode gn) {
359 if (gn.getStatus()==FINISHED||!nodes.contains(gn))
361 gn.setStatus(FINISHED);
362 Integer i=new Integer(sccindex);
363 if (!sccmap.containsKey(i))
364 sccmap.put(i,new HashSet());
365 ((Set)sccmap.get(i)).add(gn);
367 for(Iterator edgeit=gn.inedges();edgeit.hasNext();) {
368 Edge e=(Edge)edgeit.next();
369 GraphNode gn2=e.getSource();
374 public static boolean depthFirstSearch(Collection nodes) {
376 throw new NullPointerException();
379 DFS dfs = new DFS(nodes);
383 private boolean go() {
386 boolean acyclic=true;
387 i = nodes.iterator();
388 while (i.hasNext()) {
389 GraphNode gn = (GraphNode) i.next();
393 i = nodes.iterator();
394 while (i.hasNext()) {
395 GraphNode gn = (GraphNode) i.next();
396 assert gn.getStatus() != PROCESSING;
397 if (gn.getStatus() == UNVISITED) {
405 private boolean dfs(GraphNode gn) {
406 boolean acyclic=true;
408 Iterator edges = gn.edges();
410 while (edges.hasNext()) {
411 Edge edge = (Edge) edges.next();
412 GraphNode node = edge.getTarget();
413 if (!nodes.contains(node)) /* Skip nodes which aren't in the set */
415 if (node.getStatus() == UNVISITED) {
418 } else if (node.getStatus()==PROCESSING) {
422 if (finishingorder!=null)
423 finishingorder.add(gn);