Modify the Graph program given in the textbook (program 4.5.1) to create a program, SubGraph. It would take a filename and vertices as input arguments on the command line. It would create and print out a graph using the data specified in the file (as is done by the "Graph" program). And then it would also print out the subgraph of the graph formed by the vertices that are given on the command line. Please add Commenting (code documentation)
Note: The induced subgraph is the graph comprised of the specified vertices together with all edges from the original graph that connect any two of them.
- The input and output of the program should be similar to, or as specified by, the following sample run.
>more graph.txt A B A C C G A G H A B C B H
>java SubGraph graph.txt A C G The graph is A: B C G H B: A C H C: A B G G: A C H: A B The subgraph is A: C G C: A G G: A C
le Program 4.5.1 Graph data type Click here to view code image public class Graph private St
> st; public Graph() { st = new ST>(); } public void addEdge (String v, String w) { // Put vin w's SET and win v's SET. if (!st.contains (v)) st. put (v, new SET()); if (!st.contains (w)) st.put (w, new SET()); st.get (v).add(); st.get (w).add(v); La public Iterable adjacent to (String v) { return st.get (v); } public Iterable vertices() { return st.keys (); } // See Exercises 4.5.1-4 for VO, E(), degree(), // hasVertex(), and hasEdge (). public static void main(String[] args) { // Read edges from standard input; print resulting graph. Graph G = new Graph(); while (!StdIn.isEmpty()) G.addEdge (StdIn.readString(), stdin.readstring()); Stdout.print (G); st symbol table of vertex neighbor sets This implementation uses ST and SET (see SECTION 4.4) to implement the graph data type. Clients build graphs by adding edges and process them by iterating over the vertices and then over the set of vertices adjacent to each vertex. See the text for toString() and a matching constructor that reads a graph from a file. more tinyGraph.txt Umm TOP java Graph >(); In in = new In (filename); while (in.hasNextLine()) String line = in.readLine(); String[] names = line.split(delimiter); for (int i = 1; i > st; public Graph() { st = new ST>(); } public void addEdge (String v, String w) { // Put vin w's SET and win v's SET. if (!st.contains (v)) st. put (v, new SET()); if (!st.contains (w)) st.put (w, new SET()); st.get (v).add(); st.get (w).add(v); La public Iterable adjacent to (String v) { return st.get (v); } public Iterable vertices() { return st.keys (); } // See Exercises 4.5.1-4 for VO, E(), degree(), // hasVertex(), and hasEdge (). public static void main(String[] args) { // Read edges from standard input; print resulting graph. Graph G = new Graph(); while (!StdIn.isEmpty()) G.addEdge (StdIn.readString(), stdin.readstring()); Stdout.print (G); st symbol table of vertex neighbor sets This implementation uses ST and SET (see SECTION 4.4) to implement the graph data type. Clients build graphs by adding edges and process them by iterating over the vertices and then over the set of vertices adjacent to each vertex. See the text for toString() and a matching constructor that reads a graph from a file. more tinyGraph.txt Umm TOP java Graph >(); In in = new In (filename); while (in.hasNextLine()) String line = in.readLine(); String[] names = line.split(delimiter); for (int i = 1; i