Question: Below is the following study question, with the book example, along with my attempt at getting something to work. Modify the Graph program given in

Below is the following study question, with the book example, along with my attempt at getting something to work.

Modify the Graph program given in the textbook (program 4.5.1) to create a program, SubGraph, by adding a method subgraph() that takes a SET as its argument and returns the induced subgraph. [MO7.2]

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.

Also modify the main method so that you would get the following sample runs. >more graph.txt A B A C C G A G H A B C B H >more subgraph.txt A C G >java SubGraph subgraph.txt < graph.txt 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: A C

 public class Graph { // symbol table: key = string vertex, value = set of neighboring vertices private ST> st; // number of edges private int E; /**  * Initializes an empty graph with no vertices or edges.  */ public Graph() { st = new ST>(); } /**  * Initializes a graph from the specified file, using the specified delimiter.  *  * @param filename the name of the file  * @param delimiter the delimiter  */ public Graph(String filename, String delimiter) { st = new ST>(); In in = new In(filename); while (in.hasNextLine()) { String line = in.readLine(); String[] names = line.split(delimiter); for (int i = 1; i < names.length; i++) { addEdge(names[0], names[i]); } } } /**  * Returns the number of vertices in this graph.  *  * @return the number of vertices in this graph  */ public int V() { return st.size(); } /**  * Returns the number of edges in this graph.  *  * @return the number of edges in this graph  */ public int E() { return E; } // throw an exception if v is not a vertex private void validateVertex(String v) { if (!hasVertex(v)) throw new IllegalArgumentException(v + " is not a vertex"); } /**  * Returns the degree of vertex v in this graph.  *  * @param v the vertex  * @return the degree of {@code v} in this graph  * @throws IllegalArgumentException if {@code v} is not a vertex in this graph  */ public int degree(String v) { validateVertex(v); return st.get(v).size(); } /**  * Adds the edge v-w to this graph (if it is not already an edge).  *  * @param v one vertex in the edge  * @param w the other vertex in the edge  */ public void addEdge(String v, String w) { if (!hasVertex(v)) addVertex(v); if (!hasVertex(w)) addVertex(w); if (!hasEdge(v, w)) E++; st.get(v).add(w); st.get(w).add(v); } /**  * Adds vertex v to this graph (if it is not already a vertex).  *  * @param v the vertex  */ public void addVertex(String v) { if (!hasVertex(v)) st.put(v, new SET()); } /**  * Returns the vertices in this graph.  *  * @return the set of vertices in this graph  */ public Iterable vertices() { return st.keys(); } /**  * Returns the set of vertices adjacent to v in this graph.  *  * @param v the vertex  * @return the set of vertices adjacent to vertex {@code v} in this graph  * @throws IllegalArgumentException if {@code v} is not a vertex in this graph  */ public Iterable adjacentTo(String v) { validateVertex(v); return st.get(v); } /**  * Returns true if v is a vertex in this graph.  *  * @param v the vertex  * @return {@code true} if {@code v} is a vertex in this graph,  * {@code false} otherwise  */ public boolean hasVertex(String v) { return st.contains(v); } /**  * Returns true if v-w is an edge in this graph.  *  * @param v one vertex in the edge  * @param w the other vertex in the edge  * @return {@code true} if {@code v-w} is a vertex in this graph,  * {@code false} otherwise  * @throws IllegalArgumentException if either {@code v} or {@code w}  * is not a vertex in this graph  */ public boolean hasEdge(String v, String w) { validateVertex(v); validateVertex(w); return st.get(v).contains(w); } /**  * Returns a string representation of this graph.  *  * @return string representation of this graph  */ public String toString() { StringBuilder s = new StringBuilder(); for (String v : st) { s.append(v + ": "); for (String w : st.get(v)) { s.append(w + " "); } s.append(' '); } return s.toString(); } /**  * Unit tests the {@code Graph} data type.  */ public static void main(String[] args) { // create graph Graph graph = new Graph(); while (!StdIn.isEmpty()) { String v = StdIn.readString(); String w = StdIn.readString(); graph.addEdge(v, w); } // print out graph StdOut.println(graph); // print out graph again by iterating over vertices and edges for (String v : graph.vertices()) { StdOut.print(v + ": "); for (String w : graph.adjacentTo(v)) { StdOut.print(w + " "); } StdOut.println(); } } }

My current attempt:

public class Graph { private ST> st; public Graph(){ st = new ST>(); } public void addEdge(String v, Strin w){ //Put v in w's SET and w in 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(w); st.get(w).add(v); } public Iterable adjacentTo(String v){ return st.get(v); } public Iterable vertices(){ return st.keys(); }

public static void main(String[] args) { In in = new In(args[0]); String[] data = in.readAllLines(); String[] tokens = data[0].split(" "); SET set = new SET();

for(int i = 0; i < tokens.length; i++) set.add(tokens[i]);

subgraph(set);

}

public static void subgraph(SET set) { Graph graph = new Graph(), subGraph = new Graph();

while (!StdIn.isEmpty()) { String v = StdIn.readString(); String w = StdIn.readString(); if(set.contains(v) && set.contains(w)) subGraph.addEdge(v, w); graph.addEdge(v, w); }

// print out graph StdOut.println("The graph is: "); StdOut.println(graph);

StdOut.println("The subgraph is: "); StdOut.println(subGraph); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!