title: Doubts about how to represent a graph with adjacency lists
body: <p>I have to write a class that implements the following interface:</p>
  
  <pre><code>public interface Graph&lt;V&gt; {
      /**
       * Adds vertex to the graph.
       */
      boolean addVertex(V vertex);
  
      /**
       * Adds the edge (vertex1, vertex2) to the graph.
       */
      boolean addEdge(V vertex1, V vertex2);
  
      /**
       * Return true if graph contains vertex.
       */ 
      boolean hasVertex(V vertex);
  
      /**
       * Return true if graph contains the edge (vertex1, vertex2).
       */
      boolean hasEdge(V vertex1, V vertex2);
  
      /** 
       * Returns the line of vertices of the graph.
       */
      ArrayList&lt;V&gt; vertices();
  
      /** 
       * Returns the line adjacency of vertex.
       */
      ArrayList&lt;V&gt; neighbors(V vertex);
  }
  </code></pre>
  
  <p>So I have to create a class that allows you to create and edit a directed-weighted graphwith adjacency-list. </p>
  
  <p>My problem is that I don't know how to start. 
  I thought I would create a class Edge made ​​in this way:</p>
  
  <pre><code>public class Edge { 
  
      public V v1; //first node
      public V v2; //second node
      public int weight; //edge's weight
  
      public Edge() {
          v1 = "";
          v2 = "";
          peso = 0;
      }
  
      public Edge(V v1, V v2, int weight) {
          this.v1 = v1;
          this.v2 = v2;
          this.weight = weight;
      }   
  
  }
  </code></pre>
  
  <p>Then I started to write a class that implements SparseGraph Graph:</p>
  
  <pre><code>public class SparseGraph&lt;V&gt; implements Graph&lt;V&gt; {
  
      public List&lt;V&gt; vertices = new ArrayList&lt;V&gt;(); //contains all the vertices (nodes)
      public List&lt;???&gt; neighbors = new ArrayList&lt;???&gt;(); //adjacency lists
  
      public SparseGraph&lt;V&gt;() {
          //empty
      } 
  
      public boolean addVertex(V vertex) {
          return true;
      }
  
      public boolean addEdge(V vertex1, V vertex2) {
          return true;
      }
  
      public boolean hasVertex(V vertex) {
          return true;
      }
  
      public boolean hasEdge(V vertex1, V vertex2) {
          return true;
      }
  
      public ArrayList&lt;V&gt; vertices() {
          return null;
      }
  
      public ArrayList&lt;V&gt; neighbors(V vertex) {
          return null;
      }
  
  }
  </code></pre>
  
  <p>How can I represent the data type node? The delivery says: "<em>The graph must be defined on the nodes of a generic type V</em>". 
  So I have created a separate class Vertex. But now how do I create a graph whose nodes contain (for example) a number? 
  That is, I wish I could give an identifier to the nodes of the graph. 
  I do it in the time of testing when I write:</p>
  
  <pre><code>SparseGraph&lt;Integer&gt; g = new SparseGraph&lt;Integer&gt;(); 
  </code></pre>
  
  <p>Am I wrong? 
  <strong>Now how do I represent the adjacency lists?</strong> </p>
  
  <p>I'm very confused... </p>
  
  <p>Thanks to anyone who wants to help me! 
  <em>PS:</em> I don't want the code of the methods, I would just like some advice on how to create the adjacency lists.. and would like to know if the rest of the code is right.. The class Edge might work done in that way? The class SparseGraph? </p>
  
  <p>Thank you</p>