/** * Implementation of Node for graphs uusing an adjacency list * The node definition has been extended to support Dijkstra's shorted path * calculations be the addition of the cost, priorNodes and visited instance variables. * * @author geoffreytowell * date Dec 2019 */ import java.util.ArrayList; public class Node implements Comparable> { // the "payload" of the node. For simple implementations this is just an identifier private E element; // the list of edges from the node to implement the adjacency list representation private ArrayList> edges; // The cost of getting to the node. Note that cost is only defined with respect to a starting node. private int cost; // the minimum cost path to get to the node -- from a starting node. private ArrayList> priorEdges; // whether this node has yet been "visited" private boolean visited; /** * Basic contstructor. Just initializes everything. * @param e */ public Node(E e) { edges = new ArrayList<>(); element=e; cost = 0; visited=false; priorEdges=new ArrayList<>();; } /** * Clear variables to get ready for running dijkstra's algorithm */ public void reset() { setCost(Integer.MAX_VALUE); clearPriorEdges(); setVisited(false); } /** * * @param v */ public void setVisited(boolean v) { this.visited=v; } public boolean getVisited() { return visited; } public int getCost() { return cost; } public void setCost(int c) { this.cost=c; } public ArrayList> getPriorEdges() { return priorEdges; } public void addPriorEdge(Edge ed) { priorEdges.add(ed); } public void setPriorEdges(ArrayList> edges) { clearPriorEdges(); priorEdges.addAll(edges); } public void clearPriorEdges() { priorEdges.clear(); } public ArrayList> getEdges() { return edges; } public E getValue() { return element; } public void addEdgeTo(Node too, String id, int cost) { edges.add(new Edge(this, too, id, cost)); } public void addEdgeTo(Node too, String id) { edges.add(new Edge(this, too, id)); } public String toString() { return element.toString(); } @Override public int compareTo(Node node) { return cost-node.getCost(); } }