package jimDij; import java.util.*; /***** * a Node object for graph problems.

* * Each Node contains *

* * @author Jim Mahoney (mahoney@marlboro.edu) * @version 0.1 Nov 25 2002 ***/ public class Node { /** * White nodes are still being worked on by the search algorithm. **/ public static final int WHITE=0; /** * Black nodes have had their shortest path computed. **/ public static final int BLACK=1; private static final int INFINITY=100000; // much bigger than distances private static final int DEFAULT_EDGE_COUNT = 10; private String name; private Node parent; private int color; private int pathDistance; // An ArrayList is a Java class in the utilities package // that's like an array, but with more power and flexibility. private ArrayList edges; // --- These are in the applet, but I'm not using them. //private int x; // only for drawing it on the screen //private int y; /*** * @param initialEdgeCount is how many Edges start in the ArrayList. ***/ Node(int initialEdgeCount){ edges = new ArrayList(initialEdgeCount); color = WHITE; parent= null; pathDistance = INFINITY; } /*** * By default the Node starts with ten edges. **/ Node(){ this(DEFAULT_EDGE_COUNT); } /*** * @param s is the name of the Node. ***/ Node(String s){ this(10); setName(s); } /**** * displayAsText prints the name, color and pathDistance of a Node. **/ public void displayAsText(){ Node p = getParent(); String s = p==null ? "" : ""+p.getName(); System.out.println(" Node " + this + " '" + getName() + "' (" + getColorAsString() + ", d="+ getPathDistance() + ") parent="+s); //// This shows the edges, too. A bit too much overkill, usually. // for (Iterator i = edgeIterator(); i.hasNext();){ // Edge e = (Edge)i.next(); // System.out.println(" Edge " + e.getFromNode() + " -> " + // e.getToNode() + " , distance=" + // e.getDistance() + "."); // } } public String getColorAsString(){ if (color == WHITE) { return "White"; } if (color == BLACK) { return "Black"; } return ""; } public void addEdge(Edge e){ edges.add(e); } public Iterator edgeIterator(){ return edges.iterator(); } public void setName(String n){ name=n; } public String getName(){ return name; } public void setParent(Node n){ parent=n; } public Node getParent(){ return parent; } public boolean hasParent(){ return parent != null; } public boolean isBlack(){ return color==BLACK; } public void setColor(int c){ color=c; } public int getColor(){ return color; } public int getPathDistance(){ return pathDistance; } public void setPathDistance(int d){ pathDistance = d; } //public void setX(int x){ // this.x=x; //} //public int getX(){ // return x; //} //public void setY(int y){ // this.y=y; //} //public int getY(){ // return y; //} }