Question: #I am strcut at writing data to the file Graph.java import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import
#I am strcut at writing data to the file
Graph.java import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; public class Graph implements GraphInterface{ private Set towns; private Map> connections; private Map paths; /*private List vertices;//Store vertices private Map adjacencyList ; // [vertices] -> [edge] private List edges;//store edges //private List > list;//adjacency list //creates an empty graph */ /** * non-arg Constructor. */ public Graph(){ towns = new TreeSet(); connections = new HashMap>(); } /** * Returns an edge connecting source vertex to target vertex if such * vertices and such edge exist in this graph. Otherwise returns * null. If any of the specified vertices is null * returns null * * In undirected graphs, the returned edge may have its source and target * vertices in the opposite order. * * @param sourceVertex source vertex of the edge. * @param destinationVertex target vertex of the edge. * * @return an edge connecting source vertex to target vertex. */ @Override public Road getEdge(Town sourceVertex, Town destinationVertex) { Iterator edges = connections.get(sourceVertex).iterator(); Road destination; while (edges.hasNext()) { destination = edges.next(); if(destination.getDestination().compareTo(destinationVertex) == 0) return destination; } return null; } /** * Tests the given vertices for null and for membership in the graph. * * @param v1 First vertex to examine. * @param v2 Second vertex to examine. * @throws IllegalArgumentException If any vertex is not part of this graph. * @throws NullPointerException If any vertex is null. */ protected void checkVertices(Town v1, Town v2) { if (!this.containsVertex(v1)) { throw new IllegalArgumentException(); } if (!this.containsVertex(v2)) { throw new IllegalArgumentException(); } } /** * Creates a new edge in this graph, going from the source vertex to the * target vertex, and returns the created edge. * * The source and target vertices must already be contained in this * graph. If they are not found in graph IllegalArgumentException is * thrown. * * * @param sourceVertex source vertex of the edge. * @param destinationVertex target vertex of the edge. * @param weight weight of the edge * @param description description for edge * * @return The newly created edge if added to the graph, otherwise null. * * @throws IllegalArgumentException if source or target vertices are not * found in the graph. * @throws NullPointerException if any of the specified vertices is null. */ @Override public Road addEdge(Town sourceVertex, Town destinationVertex, int weight, String description) { if (sourceVertex==null || destinationVertex==null) throw new NullPointerException(); else if (!containsVertex(sourceVertex) || !containsVertex(destinationVertex)) throw new IllegalArgumentException(); else if(getEdge(sourceVertex, destinationVertex)==null&&getEdge(destinationVertex, sourceVertex)==null){ Road connection1= new Road(sourceVertex, destinationVertex, 1, description); Road connection2= new Road(destinationVertex, sourceVertex, 1, description); connections.get(sourceVertex).add(connection1); connections.get(destinationVertex).add(connection2); return connection1; } return null; } /** * Adds the specified vertex to this graph if not already present. More * formally, adds the specified vertex, v, to this graph if * this graph contains no vertex u such that * u.equals(v). If this graph already contains such vertex, the call * leaves this graph unchanged and returns false. In combination * with the restriction on constructors, this ensures that graphs never * contain duplicate vertices. * * @param v vertex to be added to this graph. * * @return true if this graph did not already contain the specified * vertex. * * @throws NullPointerException if the specified vertex is null. */ @Override public boolean addVertex(Town v) { if(v==null) throw new NullPointerException(); if (!containsVertex(v)){ towns.add(v); return true; } else return false; } @Override public boolean containsEdge(Town sourceVertex, Town destinationVertex) { /** * Returns true if and only if this graph contains an edge going * from the source vertex to the target vertex. In undirected graphs the * same result is obtained when source and target are inverted. If any of * the specified vertices does not exist in the graph, or if is * null, returns false. * * @param sourceVertex source vertex of the edge. * @param destinationVertex target vertex of the edge. * * @return true if this graph contains the specified edge. */ Iterator e = connections.get(sourceVertex).iterator(); while(e.hasNext()) { if(e.next().getDestination().compareTo(destinationVertex) == 0) return false; } return true; } /** * Returns true if this graph contains the specified vertex. More * formally, returns true if and only if this graph contains a * vertex u such that u.equals(v). If the * specified vertex is null returns false. * * @param v vertex whose presence in this graph is to be tested. * * @return true if this graph contains the specified vertex. */ @Override public boolean containsVertex(Town v) { for (Town a: towns){ if (a.compareTo(v) == 0) return true; } return false; } @Override public Set edgeSet() { return (Set) connections; } /** * Returns a set of all edges touching the specified vertex (also * referred to as adjacent vertices). If no edges are * touching the specified vertex returns an empty set. * * @param vertex the vertex for which a set of touching edges is to be * returned. * * @return a set of all edges touching the specified vertex. * * @throws IllegalArgumentException if vertex is not found in the graph. * @throws NullPointerException if vertex is null. */ @Override public Set edgesOf(Town vertex) { Set edges= new TreeSet(); if (vertex==null) throw new NullPointerException(); else if(!containsVertex(vertex)) throw new IllegalArgumentException(); else{ if(((Road) connections).getSource().compareTo(vertex)==0||((Road) connections).getDestination().compareTo(vertex)==0){ edges.add((Road) connections); } } return edges; } @Override public Road removeEdge(Town sourceVertex, Town destinationVertex, int weight, String description) { if(containsEdge(sourceVertex, destinationVertex)== true){ connections.remove(getEdge(sourceVertex, destinationVertex)); return getEdge(sourceVertex, destinationVertex); } else { return null; } } @Override public boolean removeVertex(Town v) { try{ towns.remove(v); } catch(NullPointerException e){ return false; } return true; } /** * Returns a set of the vertices contained in this graph. The set is backed * by the graph, so changes to the graph are reflected in the set. If the * graph is modified while an iteration over the set is in progress, the * results of the iteration are undefined. * * @return a set view of the vertices contained in this graph. */ @Override public Set vertexSet() { Set returnSet = new HashSet(); for(Town a : towns) returnSet.add(a); return returnSet; } @Override public ArrayList shortestPath(Town sourceVertex, Town destinationVertex) { ArrayList asPath = new ArrayList(); // holds the output Road edge; // holds iterations of LinkedList stored in Path object paths = new HashMap(this.towns.size()); // Map of Actors to Path Objects for(Town a : towns) paths.put(a, new Path()); paths.put(sourceVertex, new Path()); // set path length to self to zero. This is // the base path all other paths start from in // dijkstra's. paths.get(sourceVertex).pathStart(); dijkstraShortestPath(sourceVertex); // invoke dijkstra's algorithm on sourceVertex if(paths.get(destinationVertex).getPathLength() != Integer.MAX_VALUE) { Iterator iEdge = (Iterator) paths.get(destinationVertex); // iterate minPath linked to destinationVertex // generate ArrayList output while(iEdge.hasNext()){ edge = iEdge.next(); asPath.add(edge.getSource().toString() + " via " + edge.getName() + " to " + edge.getDestination().toString()); } // return output return asPath; } else return null; } /** * Dijkstra's Shortest Path Method. Internal structures are built which * hold the ability to retrieve the path, shortest distance from the * sourceVertex to all the other vertices in the graph, etc. * @param sourceVertex the vertex to find shortest path from * */ @Override public void dijkstraShortestPath(Town sourceVertex) { // Path newPath; // One inefficiency of recursion is that I will check going back the direction I just came from, adding one iteration of the for // loop per recursion. Rewriting with sets of resolved vertices and unresolved vertices would eliminate that. for(Road e: edgesOf(sourceVertex)) { // add edge e to the path arriving at e.getDestination in new minimum Path candidate Path newPath = new Path(e, paths.get(e.getSource())); // if the new path is shorter than the one currently in the map // replace with new path. If it is not a shorter path, do not replace // and we also cease recursion down this path. if ( newPath.getPathLength() < paths.get(e.getDestination()).getPathLength() ) { paths.put(e.getDestination(), newPath); // map new path to that actor if(edgesOf(e.getDestination()).size() > 1) // only recurse at this point if degree of the destination node > 1 dijkstraShortestPath(e.getDestination()); // recursive call on the destination of e } } } public Town getVertex(String town1) { for ( Town act : towns ){ if(((String) act.getName()).compareTo(town1) == 0) return act; } return null; } } GraphTest.java import static org.junit.Assert.*; import org.junit.Test; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.Set; import org.junit.After; import org.junit.Before; import org.junit.Test; public class GraphTest { private GraphInterface graph; private Town[] town; @Before public void setUp() throws Exception { graph = new Graph(); town = new Town[12]; for (int i = 1; i < 12; i++) { town[i] = new Town("Town_" + i); graph.addVertex(town[i]); } graph.addEdge(town[1], town[2], 2, "Road_1"); graph.addEdge(town[1], town[3], 4, "Road_2"); graph.addEdge(town[1], town[5], 6, "Road_3"); graph.addEdge(town[3], town[7], 1, "Road_4"); graph.addEdge(town[3], town[8], 2, "Road_5"); graph.addEdge(town[4], town[8], 3, "Road_6"); graph.addEdge(town[6], town[9], 3, "Road_7"); graph.addEdge(town[9], town[10], 4, "Road_8"); graph.addEdge(town[8], town[10], 2, "Road_9"); graph.addEdge(town[5], town[10], 5, "Road_10"); graph.addEdge(town[10], town[11], 3, "Road_11"); graph.addEdge(town[2], town[11], 6, "Road_12"); } @After public void tearDown() throws Exception { graph = null; } @Test public void testGetEdge() { assertEquals(new Road(town[2], town[11],6, "Road_12"), graph.getEdge(town[2], town[11])); assertEquals(new Road(town[3], town[7],1, "Road_4"), graph.getEdge(town[3], town[7])); } @Test public void testAddEdge() { assertEquals(false, graph.containsEdge(town[3], town[5])); graph.addEdge(town[3], town[5], 1, "Road_13"); assertEquals(true, graph.containsEdge(town[3], town[5])); } @Test public void testAddVertex() { Town newTown = new Town("Town_12"); assertEquals(false, graph.containsVertex(newTown)); graph.addVertex(newTown); assertEquals(true, graph.containsVertex(newTown)); } @Test public void testContainsEdge() { assertEquals(true, graph.containsEdge(town[2], town[11])); assertEquals(false, graph.containsEdge(town[3], town[5])); } @Test public void testContainsVertex() { assertEquals(true, graph.containsVertex(new Town("Town_2"))); assertEquals(false, graph.containsVertex(new Town("Town_12"))); } @Test public void testEdgeSet() { Set roads = graph.edgeSet(); ArrayList roadArrayList = new ArrayList(); for(Road road : roads) roadArrayList.add(road.getName()); Collections.sort(roadArrayList); assertEquals("Road_1", roadArrayList.get(0)); assertEquals("Road_10", roadArrayList.get(1)); assertEquals("Road_11", roadArrayList.get(2)); assertEquals("Road_12", roadArrayList.get(3)); assertEquals("Road_2", roadArrayList.get(4)); assertEquals("Road_8", roadArrayList.get(10)); } @Test public void testEdgesOf() { Set roads = graph.edgesOf(town[1]); ArrayList roadArrayList = new ArrayList(); for(Road road : roads) roadArrayList.add(road.getName()); Collections.sort(roadArrayList); assertEquals("Road_1", roadArrayList.get(0)); assertEquals("Road_2", roadArrayList.get(1)); assertEquals("Road_3", roadArrayList.get(2)); } @Test public void testEdgesOfSTUDENT() { fail("Test not implemented yet"); } @Test public void testRemoveEdge() { assertEquals(true, graph.containsEdge(town[2], town[11])); graph.removeEdge(town[2], town[11], 6, "Road_12"); assertEquals(false, graph.containsEdge(town[2], town[11])); } @Test public void testRemoveEdgeSTUDENT() { fail("Test not implemented yet"); } @Test public void testRemoveVertex() { assertEquals(true, graph.containsVertex(town[2])); graph.removeVertex(town[2]); assertEquals(false, graph.containsVertex(town[2])); } @Test public void testVertexSet() { Set roads = graph.vertexSet(); assertEquals(true,roads.contains(town[1])); assertEquals(true, roads.contains(town[10])); assertEquals(true, roads.contains(town[11])); assertEquals(true, roads.contains(town[2])); assertEquals(true, roads.contains(town[3])); } @Test public void testTown_1ToTown_11() { String beginTown = "Town_1", endTown = "Town_11"; Town beginIndex=null, endIndex=null; Set towns = graph.vertexSet(); Iterator iterator = towns.iterator(); while(iterator.hasNext()) { Town town = iterator.next(); if(town.getName().equals(beginTown)) beginIndex = town; if(town.getName().equals(endTown)) endIndex = town; } if(beginIndex != null && endIndex != null) { ArrayList path = graph.shortestPath(beginIndex,endIndex); assertNotNull(path); assertTrue(path.size() > 0); assertEquals("Town_1 via Road_1 to Town_2 2 mi",path.get(0).trim()); assertEquals("Town_2 via Road_12 to Town_11 6 mi",path.get(1).trim()); } else fail("Town names are not valid"); } @Test public void testTown1ToTown_10() { String beginTown = "Town_1", endTown = "Town_10"; Town beginIndex=null, endIndex=null; Set towns = graph.vertexSet(); Iterator iterator = towns.iterator(); while(iterator.hasNext()) { Town town = iterator.next(); if(town.getName().equals(beginTown)) beginIndex = town; if(town.getName().equals(endTown)) endIndex = town; } if(beginIndex != null && endIndex != null) { ArrayList path = graph.shortestPath(beginIndex,endIndex); assertNotNull(path); assertTrue(path.size() > 0); assertEquals("Town_1 via Road_2 to Town_3 4 mi",path.get(0).trim()); assertEquals("Town_3 via Road_5 to Town_8 2 mi",path.get(1).trim()); assertEquals("Town_8 via Road_9 to Town_10 2 mi",path.get(2).trim()); } else fail("Town names are not valid"); } @Test public void testTown_4ToTown_11() { String beginTown = "Town_4", endTown = "Town_11"; Town beginIndex=null, endIndex=null; Set towns = graph.vertexSet(); Iterator iterator = towns.iterator(); while(iterator.hasNext()) { Town town = iterator.next(); if(town.getName().equals(beginTown)) beginIndex = town; if(town.getName().equals(endTown)) endIndex = town; } if(beginIndex != null && endIndex != null) { ArrayList path = graph.shortestPath(beginIndex,endIndex); assertNotNull(path); assertTrue(path.size() > 0); assertEquals("Town_4 via Road_6 to Town_8 3 mi",path.get(0).trim()); assertEquals("Town_8 via Road_9 to Town_10 2 mi",path.get(1).trim()); assertEquals("Town_10 via Road_11 to Town_11 3 mi",path.get(2).trim()); } else fail("Town names are not valid"); } @Test public void testTown_5ToTown_2STUDENT() { fail("Test not implemented yet"); } } Town Graph Manager.java import java.util.ArrayList; import java.util.Collections; import java.util.Set; public class TownGraphManager implements TownGraphManagerInterface{ private Graph actorGraph; /** * Constructor */ public TownGraphManager(){ actorGraph = new Graph(); ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
