title: Undirected Graph add/remove Vertex; removeEdge methods
body: <p>Can anyone help with the following three methods?</p>
  
  <p>addVertex: adds a single vertex</p>
  
  <p>removeEdge: removes an edge between two vertices. </p>
  
  <p>removeVertex: removes a single vertex</p>
  
  <p>Using an adjacency list to represent an undirected graph. The data is in a .txt file (example below):</p>
  
  <p>1 2 3 4 5 6 7 8 9</p>
  
  <p>1 2</p>
  
  <p>1 4</p>
  
  <p>1 3</p>
  
  <p>2 4</p>
  
  <p>2 5</p>
  
  <p>3 6</p>
  
  <p>4 6</p>
  
  <p>5 7</p>
  
  <p>5 8</p>
  
  <p>6 9</p>
  
  <p>7 9</p>
  
  <p>8 9</p>
  
  <p>Code: </p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Graph&lt;T&gt; {
  
  private Map&lt;Integer, List&lt;Integer&gt;&gt; adjacencyList;
  protected int numVertices;
  protected T[] vertices;
  
  // AdjList &amp; parseFile
  public Graph(String fileName) throws FileNotFoundException {
      adjacencyList = new HashMap&lt;Integer, List&lt;Integer&gt;&gt;();
      buildGraphFromFile(fileName);
  
  }
  
  // addVertex
  public void addVertex(String[] vertexName) {
  
  }
  
  // removeVertex
  public void removeVertex(String[] vertexName) {
  
  }
  
  // addEdge
  // connects vertexA to vertexB &amp; vice versa
  public void addEdge(int vertexA, int vertexB) {
      edge(vertexA, vertexB);
      edge(vertexB, vertexA);
  }
  
  // Connect vertexA to VertexB. If VA already exists in AdjList return
  // edges-list &amp;
  // add VB to it. If not, create new ArrayList &amp; add VB then add all to
  // AdjList
  private void edge(int vertexA, int vertexB) {
      List&lt;Integer&gt; edges;
      if (adjacencyList.containsKey(vertexA)) {
          edges = adjacencyList.get(vertexA);
          edges.add(vertexB);
      } else {
          edges = new ArrayList&lt;Integer&gt;();
          edges.add(vertexB);
          this.adjacencyList.put(vertexA, edges);
      }
  }
  
  // RemoveEdge
  public void removeEdge(int vertexA, int vertexB) {
  
  }
  
  // Returns true if the graph is empty; false otherwise
  public boolean isEmpty() {
      return adjacencyList.isEmpty();
  }
  
  // Returns the size of the graph
  public int size() {
      int size = 0;
      Iterator&lt;Integer&gt; vertices = adjacencyList.keySet().iterator();
      while (vertices.hasNext()) {
          size++;
  
      }
      return size;
  }
  
  // Returns true is VA points to VB vice versa.
  public boolean isConnected(int vertexA, int vertexB) {
      List&lt;Integer&gt; edges = getEdges(vertexA);
      return edges.contains(vertexB);
  }
  
  // Returns all edges of each vertex.
  public List&lt;Integer&gt; getEdges(int vertexA) {
      List&lt;Integer&gt; edges = adjacencyList.get(vertexA);
      if (edges == null) {
          throw new RuntimeException(vertexA + " not present in the graph.");
      }
      return edges;
  }
  
  // Reads text file. Line one contains all vertices. Following lines contain
  // edges
  // (one edge per line).
  private void buildGraphFromFile(String fileName)
          throws FileNotFoundException {
      try {
          File file = new File("data.txt");
          InputStreamReader streamReader = new InputStreamReader(
                  new FileInputStream(file));
          BufferedReader br = new BufferedReader(streamReader);
          String line = br.readLine();
  
          // vertices
          if (line != null) {
              String[] vertexName = line.split(" ");
              int[] vertex = new int[vertexName.length];
              for (int i = 0; i &lt; vertex.length; ++i) {
                  vertex[i] = Integer.parseInt(vertexName[i]);
  
              }
  
              // edges
              while ((line = br.readLine()) != null) {
                  String[] tokens = line.split(" ");
                  int vertexA = Integer.parseInt(tokens[0]);
                  int vertexB = Integer.parseInt(tokens[1]);
                  addEdge(vertexA, vertexB);
  
              }
          }
          br.close();
  
          // catch exceptions &amp; errors
      } catch (FileNotFoundException ex) {
          ex.printStackTrace();
      } catch (IOException ex) {
          ex.printStackTrace();
      } catch (Exception e) {
          System.out.println("Error: " + e);
      }
  }
  
  // String representation
  public String toString() {
      StringBuilder builder = new StringBuilder();
      Iterator&lt;Integer&gt; vertices = adjacencyList.keySet().iterator();
      while (vertices.hasNext()) {
          Integer vertex = vertices.next();
          List&lt;Integer&gt; edges = adjacencyList.get(vertex);
          builder.append(vertex);
          builder.append(": ");
          builder.append(edges);
          builder.append('\n');
      }
      return builder.toString();
  }
  
  // Main method
  // Generates initial graph using buildGraphFromFile method
  public static void main(String[] args) {
      try {
          Graph initialGraph = new Graph(
                  "data.txt");
          System.out.println(initialGraph);
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>