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 String dotnodeparams = new String();
30 public Edge(String label, GraphNode target) {
35 public String getLabel() {
39 public GraphNode getTarget() {
43 public void setDotNodeParameters(String param) {
45 throw new NullPointerException();
47 if (param.length() > 0) {
48 dotnodeparams = "," + param;
50 dotnodeparams = new String();
56 int discoverytime = -1;
57 int finishingtime = -1; /* used for searches */
58 Vector edges = new Vector();
61 NodeStatus status = UNVISITED;
62 String dotnodeparams = new String();
65 public GraphNode(String label) {
66 this.nodelabel = label;
67 this.textlabel = label;
70 public GraphNode(String label, Object owner) {
71 this.nodelabel = label;
72 this.textlabel = label;
76 public GraphNode(String label, String textlabel, Object owner) {
77 this.nodelabel = label;
78 this.textlabel = textlabel;
82 public Object getOwner() {
86 public void setDotNodeParameters(String param) {
88 throw new NullPointerException();
90 if (param.length() > 0) {
91 dotnodeparams = "," + param;
93 dotnodeparams = new String();
97 public void setStatus(NodeStatus status) {
99 throw new NullPointerException();
101 this.status = status;
104 public String getLabel() {
108 public String getTextLabel() {
112 public NodeStatus getStatus() {
116 public Iterator edges() {
117 return edges.iterator();
120 public void addEdge(Edge newedge) {
121 edges.addElement(newedge);
124 public void reset() {
130 public void discover(int time) {
131 discoverytime = time++;
135 public void finish(int time) {
136 assert status == PROCESSING;
137 finishingtime = time++;
141 public int getFinishingTime() {
142 return finishingtime;
145 public static class DOTVisitor {
147 java.io.PrintWriter output;
151 private DOTVisitor(java.io.OutputStream output) {
154 this.output = new java.io.PrintWriter(output, true);
157 private String getNewID(String name) {
158 tokennumber = tokennumber + 1;
159 return new String (name+tokennumber);
164 public static void visit(java.io.OutputStream output, Collection nodes) {
165 DOTVisitor visitor = new DOTVisitor(output);
166 visitor.nodes = nodes;
171 private void make() {
172 output.println("digraph dotvisitor {");
173 output.println("\trotate=90;");
174 output.println("\tpage=\"8.5,11\";");
175 output.println("\tnslimit=1000.0;");
176 output.println("\tnslimit1=1000.0;");
177 output.println("\tmclimit=1000.0;");
178 output.println("\tremincross=true;");
179 output.println("\tnode [fontsize=10,height=\"0.1\", width=\"0.1\"];");
180 output.println("\tedge [fontsize=6];");
184 output.println("}\n");
187 private void traverse() {
188 Iterator i = nodes.iterator();
189 while (i.hasNext()) {
190 GraphNode gn = (GraphNode) i.next();
191 Iterator edges = gn.edges();
192 String label = gn.getTextLabel(); // + " [" + gn.discoverytime + "," + gn.finishingtime + "];";
193 output.println("\t" + gn.getLabel() + " [label=\"" + label + "\"" + gn.dotnodeparams + "];");
195 while (edges.hasNext()) {
196 Edge edge = (Edge) edges.next();
197 GraphNode node = edge.getTarget();
198 String edgelabel = useEdgeLabels ? "label=\"" + edge.getLabel() + "\"" : "label=\"\"";
199 output.println("\t" + gn.getLabel() + " -> " + node.getLabel() + " [" + edgelabel + edge.dotnodeparams + "];");
206 * DFS encapsulates the depth first search algorithm
208 public static class DFS {
213 private DFS(Collection nodes) {
217 public static void depthFirstSearch(Collection nodes) {
219 throw new NullPointerException();
222 DFS dfs = new DFS(nodes);
230 i = nodes.iterator();
231 while (i.hasNext()) {
232 GraphNode gn = (GraphNode) i.next();
236 i = nodes.iterator();
237 while (i.hasNext()) {
238 GraphNode gn = (GraphNode) i.next();
239 assert gn.getStatus() != PROCESSING;
240 if (gn.getStatus() == UNVISITED) {
246 private void dfs(GraphNode gn) {
248 Iterator edges = gn.edges();
250 while (edges.hasNext()) {
251 Edge edge = (Edge) edges.next();
252 GraphNode node = edge.getTarget();
253 if (node.getStatus() == UNVISITED) {