title: Adjacency List for Undirected Graph
body: <p>Anyone know where I can obtain generic sample code for using an adjacency list to represent an undirected graph?</p>
  
  <p>The graph data would be from a .txt file: The nodes are specified on the first line, separated by spaces. The edges are specified on the following lines, each edge on a separate line.</p>
  
  <p>Like this... </p>
  
  <pre><code>1 2 3 4 5 6 7 8 9
  
  1 2
  
  1 4
  
  1 3
  
  2 4
  
  2 5
  
  3 6
  
  4 6
  
  5 7
  
  5 8
  
  6 9
  
  7 9
  
  8 9
  </code></pre>
  
  <p>My .txt file isn't connecting with the graph methods. I'm also getting the following NPE error: </p>
  
  <pre><code>Error: java.lang.NullPointerException
  </code></pre>
  
  <p>Creating an adjacency list &amp; performing standard Graph ADT methods for the undirected graph. </p>
  
  <pre><code>//TON of imports up here (removed for now)
  
  class Graph&lt;V&gt; implements GraphADT1 &lt;V&gt;{
  
  // Map of adjacency lists for each node
  private HashMap&lt;Integer, LinkedList&lt;Integer&gt;&gt; adj;
  private ArrayList&lt;V&gt; vertices;
  private HashMap&lt;V, LinkedHashSet&lt;V&gt;&gt; neighbors;
  private HashMap&lt;V, Set&lt;V&gt;&gt; neighborsView; 
  private int edgeCount; 
  
  
  public Graph(int[] nodes) {
  
      vertices = new ArrayList&lt;V&gt;();
      neighbors = new HashMap&lt;V, LinkedHashSet&lt;V&gt;&gt;();
      neighborsView = new HashMap&lt;V, Set&lt;V&gt;&gt;();
      adj = new HashMap&lt;Integer, LinkedList&lt;Integer&gt;&gt;();
  
      for (int i = 0; i &lt; nodes.length; ++i) {
          adj.put(i, new LinkedList&lt;Integer&gt;());
      }
  }
  
  //Add Vertex Method
  public void addVertex(V vid)    {
      vertices.add(vid);
      LinkedHashSet&lt;V&gt; neighborV = new LinkedHashSet&lt;V&gt;();
      neighbors.put(vid, neighborV);
      neighborsView.put(vid, Collections.unmodifiableSet(neighborV));
  
  }
  
  // Removes Vertex Method
  public void removeVertex(V vid) {
      if(vertices.remove(vid)) {
          LinkedHashSet&lt;V&gt; neighborV = neighbors.remove(vid);
          for(V uid : neighborV) {
              LinkedHashSet&lt;V&gt; neighborU = neighbors.get(uid);
              neighborU.remove(vid);
              --edgeCount;
          }
          neighborsView.remove(vid);
  
      } else {
          throw new NoSuchElementException("no such vertex");
      }
  }
  
  //Add Edge Method 
  public void addEdge(V uid, V vid) {
      LinkedHashSet&lt;V&gt; neighborU = neighbors.get(uid);
      LinkedHashSet&lt;V&gt; neighborV = neighbors.get(vid);
      if(neighborU == null) throw new NoSuchElementException("first argument not in graph");
      if(neighborV == null) throw new NoSuchElementException("second argument not in graph");
      if(neighborU.add(vid) &amp;&amp; neighborV.add(uid)) {
          ++edgeCount;
      }
  }
  
  //Remove Edge Method
  public void removeEdge(V uid, V vid) {
      LinkedHashSet&lt;V&gt; neighborU = neighbors.get(uid);
      LinkedHashSet&lt;V&gt; neighborV = neighbors.get(vid);
      if(neighborU == null) throw new NoSuchElementException("first argument not in graph");
      if(neighborV == null) throw new NoSuchElementException("second argument not in graph");
      if(neighborU.remove(vid) &amp;&amp; neighborV.remove(uid)) {
          --edgeCount;
      } else {
          throw new NoSuchElementException("no edge between vertices");
      }
  }
  
  
  
  public void addNeighbor(int neighborV, int neighborU) {
      adj.get(neighborV).add(neighborU);
  }
  
  public List&lt;Integer&gt; getNeighbors(int v) {
      return adj.get(v);
  }
  
  public static void main(String[] args) {
      try {
  
          File file = new File("data.txt");
          InputStreamReader streamReader = new InputStreamReader(
                  new FileInputStream(file));
          BufferedReader br = new BufferedReader(streamReader);
          String line = br.readLine();
  
          if (line != null) {
  
              // read nodes
              String[] nodeNames = line.split(" ");
              int[] nodes = new int[nodeNames.length];
              for (int i = 0; i &lt; nodes.length; ++i) {
                  nodes[i] = Integer.parseInt(nodeNames[i]);
              }
  
              // create graph
                  Graph V = new Graph(nodes);
  
  
              // read edges
              while ((line = br.readLine()) != null) {
                  String[] tokens = line.split(" ");
                  int neighborV = Integer.parseInt(tokens[0]);
                  int neighborU = Integer.parseInt(tokens[1]);
  
                  // we add neighbor to each node in both directions.
                  V.addNeighbor(neighborV, neighborU);
                  V.addNeighbor(neighborU, neighborV);
              }
  
          }
          br.close();
      } catch (FileNotFoundException ex) {
          ex.printStackTrace();
      } catch (IOException ex) {
          ex.printStackTrace();
      } catch (Exception e) {
          System.out.println("Error: " + e);
      }
  }
  
  
  
  
  public Iterator&lt;?&gt; iteratorBFS(Object startVertex) {
  
      return null;
  }
  
  public Iterator&lt;?&gt; iteratorDFS(Object startVertex) {
  
      return null;
  }
  
  public boolean isEmpty() {
      return size() == 0;
  }
  
  public boolean isConnected() {
  
      return false;
  }
  
  public int size() {
  
      return 0;
  }
  }
  </code></pre>