import java.util.LinkedList; import java.util.Vector; import java.util.Scanner; import java.io.File; import java.io.IOException; import java.io.PrintWriter; /** * AdjListsDiGraph.java * Implements the DiGraph Interface. * Uses a Vector of LinkedLists to keep track of the adjacent vertices. * * @author (SK) * @version 2022.11.15 * SK: fixed removeVertex() ConcurrentModificationEception * */ public class AdjListsDiGraph //implements DiGraph { private Vector vertices; //Vector to hold the vertices in the graph private Vector> arcs; // Lists of adjacent vertices /** * Constructor. Creates an empty graph. * */ public AdjListsDiGraph() { this.arcs = new Vector>(); this.vertices = new Vector(); } /** * Creates and returns a new graph of Strings using the data found * in the input tgf file. * If the file does not exist, a message is printed. * * @param String The name of the tgf file to read the graph from * @return AdjListsDiGraph the graph of Strings created */ public static AdjListsDiGraph AdjListsGraphFromFile(String tgf_fName) { //create an empty graph of Strings AdjListsDiGraph g = new AdjListsDiGraph(); try{ Scanner scanner = new Scanner(new File(tgf_fName)); //read vertices while (!scanner.next().equals("#")){ //discard special symbol (#) String token = scanner.next(); //read next String g.vertices.add(token); //add it a vertex to the vertices g.arcs.add(new LinkedList()); } //read arcs while (scanner.hasNext()){ int sourceIndex = scanner.nextInt() -1; //correct to start at 0 int destinationIndex = scanner.nextInt() - 1; //locate the LinkedList corresponding to the source LinkedList list = g.arcs.get(sourceIndex); //find the destination that corresponds to the destinationIndex String destination = g.vertices.get(destinationIndex); //add destination to the list list.add(destination); } scanner.close(); } catch (IOException ex) { System.out.println(" ***ERROR*** " + tgf_fName + " file could not be read. "); } return g; } /** * Saves the current graph into a .tgf file. * @param the name of the file to write to */ public void saveToTGF(String fName) { try { PrintWriter writer = new PrintWriter(new File(fName)); //notice that indexing in the tgf format starts at 1 (not 0) //write vertices by iterating through vector "vertices" for (int i = 0; i < vertices.size(); i++) { writer.print((i+1) + " " + vertices.get(i)); writer.println(""); } writer.println("#"); // # symbol separates the vertices from the arcs //write arcs by iterating through arcs vector for (int i = 0; i < arcs.size(); i++){ //for each adjacent list for (T destinationVertex :arcs.get(i)) { //for each destination vertex in that list int destinationIndex = vertices.indexOf(destinationVertex); //find the index of that vertex writer.println((i+1) + " " + (destinationIndex+1)); } } writer.close(); } catch (IOException ex) { System.out.println("***ERROR***" + fName + " could not be written"); } } /** * Basic Driver program. */ public static void main (String args[]){ System.out.println("TESTING METHODS"); System.out.println("_________________"); AdjListsDiGraph g = new AdjListsDiGraph(); System.out.println("New graph created."); g.saveToTGF("out.tgf"); } }