/** * Graph Class */ import java.util.ArrayList; import java.util.Map; import java.util.List; public class Graph { private List vertices; private ArrayList edges; /** * @param vertices * @param edges */ public Graph(List vertices, ArrayList edges) { this.vertices = vertices; this.edges = edges; } /** * @return the vertices */ public List getVertices() { return vertices; } /** * @param vertices the vertices to set */ public void setVertices(List vertices) { this.vertices = vertices; } /** * @return the edges */ public ArrayList getEdges() { return edges; } /** * @param edges the edges to set */ public void setEdges(ArrayList edges) { this.edges = edges; } /** * @param v the vertex to search for * @param unvisitedNodes the set of all unvisited nodes * @return the edges which contain v */ public ArrayList getEdgesContaining( Vertex v ) { ArrayList list = new ArrayList(); for( Edge e : edges ) { if( (e.getStart().equals( v ) || e.getEnd().equals( v )) ) list.add( e ); } return list; } /** * @param unvisitedNodes the map of vertices and Integer (If a node has been already seen) * @param distances the map of Vertices to their distances * @return the vertex which contains the smallest distance */ public synchronized Vertex getSmallestNode( Map unvisitedNodes, Map distances ) { Vertex smallest = null; for( Vertex v : unvisitedNodes.keySet() ) { if( smallest == null ) smallest = v; else { if( distances.get( v ) < distances.get( smallest ) && unvisitedNodes.get( v ) == null ) smallest = v; } } return smallest; } }