6 public class GraphNode {
7 /* NodeStatus enumeration pattern ***********/
9 public static final NodeStatus UNVISITED = new NodeStatus("UNVISITED");
10 public static final NodeStatus PROCESSING = new NodeStatus("PROCESSING");
11 public static final NodeStatus FINISHED = new NodeStatus("FINISHED");
13 public static class NodeStatus {
14 private static String name;
15 private NodeStatus(String name) { this.name = name; }
16 public String toString() { return name; }
19 int discoverytime = -1;
20 int finishingtime = -1; /* used for searches */
22 protected Vector edges = new Vector();
23 protected Vector inedges = new Vector();
25 NodeStatus status = UNVISITED;
26 String dotnodeparams = new String();
27 public boolean merge=false;
29 public void setMerge() {
33 public static void computeclosure(Collection nodes, Collection removed) {
34 Stack tovisit=new Stack();
35 tovisit.addAll(nodes);
36 while(!tovisit.isEmpty()) {
37 GraphNode gn=(GraphNode)tovisit.pop();
38 for(Iterator it=gn.edges();it.hasNext();) {
39 Edge edge=(Edge)it.next();
40 GraphNode target=edge.getTarget();
41 if (!nodes.contains(target)) {
43 (!removed.contains(target))) {
52 public static void boundedcomputeclosure(Collection nodes, Collection removed,int depth) {
53 Stack tovisit=new Stack();
54 Stack newvisit=new Stack();
55 tovisit.addAll(nodes);
56 for(int i=0;i<depth&&!tovisit.isEmpty();i++) {
57 while(!tovisit.isEmpty()) {
58 GraphNode gn=(GraphNode)tovisit.pop();
59 for(Iterator it=gn.edges();it.hasNext();) {
60 Edge edge=(Edge)it.next();
61 GraphNode target=edge.getTarget();
62 if (!nodes.contains(target)) {
64 (!removed.contains(target))) {
66 newvisit.push(target);
76 public void setDotNodeParameters(String param) {
78 throw new NullPointerException();
80 if (dotnodeparams.length() > 0) {
81 dotnodeparams += "," + param;
83 dotnodeparams = param;
87 public void setStatus(NodeStatus status) {
89 throw new NullPointerException();
94 public String getLabel() {
98 public String getTextLabel() {
102 public String getName(){
106 public NodeStatus getStatus() {
110 public int numedges() {
114 public int numinedges() {
115 return inedges.size();
118 public Edge getedge(int i) {
119 return (Edge) edges.get(i);
122 public Edge getinedge(int i) {
123 return (Edge) inedges.get(i);
126 public Vector getEdgeVector() {
130 public Vector getInedgeVector() {
134 public Iterator edges() {
135 return edges.iterator();
138 public Iterator inedges() {
139 return inedges.iterator();
142 public void addEdge(Edge newedge) {
143 newedge.setSource(this);
144 edges.addElement(newedge);
145 GraphNode tonode=newedge.getTarget();
146 tonode.inedges.addElement(newedge);
149 public void addEdge(Vector v) {
150 for (Iterator it = v.iterator(); it.hasNext();)
151 addEdge((Edge)it.next());
154 public void removeEdge(Edge edge) {
158 public void removeInedge(Edge edge) {
159 inedges.remove(edge);
162 public void removeAllEdges() {
163 edges.removeAllElements();
166 public void removeAllInedges() {
167 inedges.removeAllElements();
179 void discover(int time) {
180 discoverytime = time;
184 void finish(int time) {
185 assert status == PROCESSING;
186 finishingtime = time;
190 /** Returns finishing time for dfs */
192 public int getFinishingTime() {
193 return finishingtime;
197 public static class DOTVisitor {
199 java.io.PrintWriter output;
204 private DOTVisitor(java.io.OutputStream output, Vector namers) {
207 this.output = new java.io.PrintWriter(output, true);
211 private String getNewID(String name) {
212 tokennumber = tokennumber + 1;
213 return new String (name+tokennumber);
219 public static void visit(java.io.OutputStream output, Collection nodes, Vector namers) {
220 DOTVisitor visitor = new DOTVisitor(output, namers);
221 visitor.nodes = nodes;
225 public static void visit(java.io.OutputStream output, Collection nodes) {
226 Vector v=new Vector();
228 DOTVisitor visitor = new DOTVisitor(output, v);
229 visitor.nodes = nodes;
233 private void make() {
234 output.println("digraph dotvisitor {");
235 /* output.println("\tpage=\"8.5,11\";");
236 output.println("\tnslimit=1000.0;");
237 output.println("\tnslimit1=1000.0;");
238 output.println("\tmclimit=1000.0;");
239 output.println("\tremincross=true;");*/
240 output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
241 output.println("\tedge [fontsize=6];");
243 output.println("}\n");
248 private void traverse() {
249 Set cycleset=GraphNode.findcycles(nodes);
251 Iterator it = nodes.iterator();
252 while (it.hasNext()) {
253 GraphNode gn = (GraphNode) it.next();
254 Iterator edges = gn.edges();
256 String dotnodeparams="";
258 for(int i=0;i<namers.size();i++) {
259 Namer name=(Namer) namers.get(i);
260 String newlabel=name.nodeLabel(gn);
261 String newparams=name.nodeOption(gn);
263 if (!newlabel.equals("") && !label.equals("")) {
266 if (!newparams.equals("")) {
267 dotnodeparams+=", " + name.nodeOption(gn);
269 label+=name.nodeLabel(gn);
273 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + dotnodeparams + "];");
276 while (edges.hasNext()) {
277 Edge edge = (Edge) edges.next();
278 GraphNode node = edge.getTarget();
279 if (nodes.contains(node)) {
280 for(Iterator nodeit=nonmerge(node).iterator();nodeit.hasNext();) {
281 GraphNode node2=(GraphNode)nodeit.next();
282 String edgelabel = "";
283 String edgedotnodeparams="";
285 for(int i=0;i<namers.size();i++) {
286 Namer name=(Namer) namers.get(i);
287 String newlabel=name.edgeLabel(edge);
288 String newoption=name.edgeOption(edge);
289 if (!newlabel.equals("")&& ! edgelabel.equals(""))
292 if (!newoption.equals(""))
293 edgedotnodeparams+=", "+newoption;
296 output.println("\t" + gn.getLabel() + " -> " + node2.getLabel() + " [" + "label=\"" + edgelabel + "\"" + edgedotnodeparams + "];");
303 Set nonmerge(GraphNode gn) {
304 HashSet newset=new HashSet();
305 HashSet toprocess=new HashSet();
307 while(!toprocess.isEmpty()) {
308 GraphNode gn2=(GraphNode)toprocess.iterator().next();
309 toprocess.remove(gn2);
313 Iterator edges = gn2.edges();
314 while (edges.hasNext()) {
315 Edge edge = (Edge) edges.next();
316 GraphNode node = edge.getTarget();
317 if (!newset.contains(node)&&nodes.contains(node))
327 /** This function returns the set of nodes involved in cycles.
328 * It only considers cycles containing nodes in the set 'nodes'.
330 public static Set findcycles(Collection nodes) {
331 HashSet cycleset=new HashSet();
332 SCC scc=DFS.computeSCC(nodes);
333 if (!scc.hasCycles())
335 for(int i=0;i<scc.numSCC();i++) {
337 cycleset.addAll(scc.getSCC(i));
342 public static class SCC {
346 public SCC(boolean acyclic, HashMap map,HashMap revmap,int numscc) {
347 this.acyclic=acyclic;
353 /** Returns whether the graph has any cycles */
354 public boolean hasCycles() {
358 /** Returns the number of Strongly Connected Components */
359 public int numSCC() {
363 /** Returns the strongly connected component number for the GraphNode gn*/
364 public int getComponent(GraphNode gn) {
365 return ((Integer)revmap.get(gn)).intValue();
368 /** Returns the set of nodes in the strongly connected component i*/
369 public Set getSCC(int i) {
370 Integer scc=new Integer(i);
371 return (Set)map.get(scc);
374 /** Returns whether the strongly connected component i contains a cycle */
375 boolean hasCycle(int i) {
376 Integer scc=new Integer(i);
377 Set s=(Set)map.get(scc);
380 Object [] array=s.toArray();
381 GraphNode gn=(GraphNode)array[0];
382 for(Iterator it=gn.edges();it.hasNext();) {
383 Edge e=(Edge)it.next();
384 if (e.getTarget()==gn)
385 return true; /* Self Cycle */
392 * DFS encapsulates the depth first search algorithm
394 public static class DFS {
399 Vector finishingorder=null;
400 Vector finishingorder_edge = null;
405 private DFS(Collection nodes) {
408 /** Calculates the strong connected components for the graph composed
409 * of the set of nodes 'nodes'*/
410 public static SCC computeSCC(Collection nodes) {
412 throw new NullPointerException();
414 DFS dfs=new DFS(nodes);
415 dfs.sccmap=new HashMap();
416 dfs.sccmaprev=new HashMap();
417 dfs.finishingorder=new Vector();
418 boolean acyclic=dfs.go();
419 for (Iterator it = nodes.iterator();it.hasNext();) {
420 GraphNode gn = (GraphNode) it.next();
423 for(int i=dfs.finishingorder.size()-1;i>=0;i--) {
424 GraphNode gn=(GraphNode)dfs.finishingorder.get(i);
425 if (gn.getStatus() == UNVISITED) {
427 dfs.sccindex++; /* Increment scc index */
430 return new SCC(acyclic,dfs.sccmap,dfs.sccmaprev,dfs.sccindex);
433 void dfsprev(GraphNode gn) {
434 if (gn.getStatus()==FINISHED||!nodes.contains(gn))
436 gn.setStatus(FINISHED);
437 Integer i=new Integer(sccindex);
438 if (!sccmap.containsKey(i))
439 sccmap.put(i,new HashSet());
440 ((Set)sccmap.get(i)).add(gn);
442 for(Iterator edgeit=gn.inedges();edgeit.hasNext();) {
443 Edge e=(Edge)edgeit.next();
444 GraphNode gn2=e.getSource();
449 public static boolean depthFirstSearch(Collection nodes) {
451 throw new NullPointerException();
454 DFS dfs = new DFS(nodes);
458 private boolean go() {
462 boolean acyclic=true;
463 i = nodes.iterator();
464 while (i.hasNext()) {
465 GraphNode gn = (GraphNode) i.next();
469 i = nodes.iterator();
470 while (i.hasNext()) {
471 GraphNode gn = (GraphNode) i.next();
472 assert gn.getStatus() != PROCESSING;
473 if (gn.getStatus() == UNVISITED) {
481 private boolean dfs(GraphNode gn) {
482 boolean acyclic=true;
484 Iterator edges = gn.edges();
486 while (edges.hasNext()) {
487 Edge edge = (Edge) edges.next();
488 edge.discover(edgetime++);
489 GraphNode node = edge.getTarget();
490 if (!nodes.contains(node)) /* Skip nodes which aren't in the set */ {
491 if(finishingorder_edge != null)
492 finishingorder_edge.add(edge);
493 edge.finish(edgetime++);
496 if (node.getStatus() == UNVISITED) {
499 } else if (node.getStatus()==PROCESSING) {
502 if(finishingorder_edge != null)
503 finishingorder_edge.add(edge);
504 edge.finish(edgetime++);
506 if (finishingorder!=null)
507 finishingorder.add(gn);
512 public static Vector topology(Collection nodes, Vector finishingorder_edge) {
514 throw new NullPointerException();
516 DFS dfs=new DFS(nodes);
517 dfs.finishingorder=new Vector();
518 if(finishingorder_edge != null) {
519 dfs.finishingorder_edge = new Vector();
521 boolean acyclic=dfs.go();
522 Vector topology = new Vector();
523 for(int i=dfs.finishingorder.size()-1;i>=0;i--) {
524 GraphNode gn=(GraphNode)dfs.finishingorder.get(i);
527 if(finishingorder_edge != null) {
528 for(int i=dfs.finishingorder_edge.size()-1;i>=0;i--) {
529 Edge gn=(Edge)dfs.finishingorder_edge.get(i);
530 finishingorder_edge.add(gn);