Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Program Help Please!!! Given Code: import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; import
Java Program Help Please!!!
Given Code:
import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; import java.util.TreeSet; public class Graph { public Graph(String fileName){ readGraph(fileName); } //Read an adjacency matrix from a file public void readGraph(String fileName){ //Assuming the input file is an ASCII file (not unreasonable in our example) //Below is the first pass of the file try (Scanner reader = new Scanner( Paths.get(fileName), FILE_ENCODING) ) { //Just read all of the letters right now and store it in a set. Using a TreeSet as it will store them in sorted order. TreeSet1. Write a C++/Java function(s) that modifies the book's recursive DFS function to do the following: (a) Given an array of vertices, your algorithm tries to visit each vertex in the order provided. For example, if the order is 4 5 3 0 21, the outer layer of DFS visits vertex 4 first. If it has to come back because dfs(v) didn't mark all vertices, it will visit 5 next (unless 5 is already marked) and so on. The default ordering is naturally 01 2 3 4 5. (b) It stores the vertex marks inside of an array (basically keeps track of counts). (c) It stores the order in which vertices become dead ends are removed from the stack in an array. (d) It represents the trees in the DFS forest as a list of sets. The last two features are important for another HWKset = new TreeSet (); while (reader.hasNext()){ set.add(reader.next()); } //Create a mapping now int vertexNumber = 0; for (String vertex: set){ vertexMapping.put(vertex, vertexNumber++); } } catch (IOException x) { System.err.format("IOException: %s%n", x); } //Now we know how many vertices we have. Allocate the necessary storage for our adjacency matrix adjMatrix = new boolean[vertexMapping.size()][vertexMapping.size()]; //Second pass of the file. Now we store the values into the file. try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName), Charset.forName(FILE_ENCODING))){ String line = null; while ((line = reader.readLine()) != null) { //See https://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters String[] tokens = line.split("\\s+"); //This code is definitely not robust and automatically assumes two tokens per line adjMatrix[vertexMapping.get(tokens[0])][vertexMapping.get(tokens[1])] = true; } } catch (IOException x) { System.err.format("IOException: %s%n", x); } //Convert the adjacency matrix to an adjacency list as well matToList(adjMatrix); } public void matToList(boolean[][] adjMatrix) { //We overwrite both our adjacency matrix and lists this.adjMatrix = adjMatrix; adjList = new ArrayList > (adjMatrix.length); //Loop through our adjacency matrix to add elements to our adjacency list for (int i = 0; i ()); for (int j = 0; j > adjList) { //We overwrite both our adjacency matrix and lists this.adjList = adjList; adjMatrix = new boolean[adjList.size()][adjList.size()]; //Loop through our adjacency list to add elements to our adjacency matrix for (int i = 0; i reverseVertexMapping = new HashMap (); for (String key: vertexMapping.keySet()){ reverseVertexMapping.put(vertexMapping.get(key), key); } for (int i = 0; i ", reverseVertexMapping.get(i)); for (int vertex : adjList.get(i)){ System.out.format("%s ", reverseVertexMapping.get(vertex)); } System.out.println(); /ew vertex } } //Maps between the vertices from the file to integers used by the adjacency matrix / adjacency list private HashMap vertexMapping = new HashMap (); private boolean[][] adjMatrix; //Can't be used for weighted graphs private ArrayList > adjList; //Using a straight array causes a variety of issues public static final String FILE_ENCODING = "US-ASCII"; public static void main(String[] args) { //Create a Graph object using the command line argument as the parameter Graph myGraph = new Graph(args[0]); //Print the graph contents myGraph.printAdjMatrix(); myGraph.printAdjList(); //Testing conversion methods //Create two graphs, an adjacency matrix and an adjacency list, representing graph as a path a -> b -> c -> d boolean[][] adjMatrix = { {false,true,false,false}, {false,false,true,false}, {false,false,false,true}, {false,false,false,false} }; ArrayList > adjList = new ArrayList>(4); for (int i = 0; i ()); adjList.get(0).add(1); adjList.get(1).add(2); adjList.get(2).add(3); //Stores the labels String[] vertexLabels = {"a", "b", "c", "d"}; //Update the vertexMapping myGraph.replaceVertexMapping(vertexLabels); //Convert the adjacency matrix to an adjacency list myGraph.matToList(adjMatrix); //Print the graph contents myGraph.printAdjMatrix(); myGraph.printAdjList(); //Convert the adjacency list to an adjacency matrix myGraph.listToMat(adjList); //Print the graph contents myGraph.printAdjMatrix(); myGraph.printAdjList(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started