title: Prim's Algorithm Implementation in Java
body: <p>I'm trying to implement Prim's Algorithm in Java using for my graph HashMap + LinkedList and a class Edge which contains the connected vertex and the weight:</p>
  
  <pre><code>adjacencyList = new HashMap&lt;T, LinkedList&lt;Edge&lt;T&gt;&gt;&gt;();
  </code></pre>
  
  <p>My idea was, starting from a given vertex:
  1)save all vertices into a LinkedList so I can remove them every time they've been visisted
  2)saving the path into another LinkedList so I can have my final MST
  3)using a PriorityQueue to find MIN weight</p>
  
  <p>In the end I'll need MST, number of edges and total weight.
  I'm very confused about how to return MST and I have few errors on my code which I have no idea on how to fix them!</p>
  
  <p>First of all I get this error:</p>
  
  <pre><code>    Prim.java:21: error: no suitable method found for addAll(LinkedList&lt;Edge&lt;T&gt;&gt;)
          unvisited.addAll(graph.getVertices());
                   ^
      method Collection.addAll(Collection&lt;? extends T&gt;) is not applicable
        (argument mismatch; LinkedList&lt;Edge&lt;T&gt;&gt; cannot be converted to Collection&lt;? extends T&gt;)
      method List.addAll(Collection&lt;? extends T&gt;) is not applicable
        (argument mismatch; LinkedList&lt;Edge&lt;T&gt;&gt; cannot be converted to Collection&lt;? extends T&gt;)
      method AbstractCollection.addAll(Collection&lt;? extends T&gt;) is not applicable
        (argument mismatch; LinkedList&lt;Edge&lt;T&gt;&gt; cannot be converted to Collection&lt;? extends T&gt;)
      method LinkedList.addAll(Collection&lt;? extends T&gt;) is not applicable
        (argument mismatch; LinkedList&lt;Edge&lt;T&gt;&gt; cannot be converted to Collection&lt;? extends T&gt;)
    where T is a type-variable: T extends Comparable&lt;T&gt; declared in class Prim
  </code></pre>
  
  <p>It seems the problem is in my getVertices() method (which return all the vertices of the graph) as it returns a <code>Set&lt;T&gt;</code>; I tried to put everything inside a LinkedList using addAll and get as return this LinkedList but it gives me the same error. What am I doing wrong?</p>
  
  <pre><code>public class Graph&lt;T extends Comparable&lt;T&gt;&gt; {
      .
      .
      public Set&lt;T&gt; getVertices() {
              if (adjacencyList.isEmpty()) {
                  System.out.println("Error message.\n");
                  return null;
              } else
                  return adjacencyList.keySet();
          }
      }
  </code></pre>
  
  <p>The second error is:</p>
  
  <pre><code>   Prim.java:28: error: incompatible types: T cannot be converted to Edge&lt;T&gt;
              for (Edge&lt;T&gt; edge : graph.getAdjacentVertices(source)) {
                                                            ^
    where T is a type-variable:
      T extends Comparable&lt;T&gt; declared in class Prim
  Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
  </code></pre>
  
  <p>I can fix it by casting <code>Edge&lt;T&gt;</code> to <code>source</code> but I think it doesn't make any sense at all because the idea was that I give a vertex which can contain any type, not only Edges.</p>
  
  <p>Prim's:</p>
  
  <pre><code>public class Prim&lt;T extends Comparable&lt;T&gt;&gt; {
      public &lt;SHOULD I RETURN graph ?&gt; minimumSpanningTree(Graph&lt;Edge&lt;T&gt;&gt; graph, T vertex) {
          //ArgumentException
  
          double weight = 0;
          T source = vertex;
  
          LinkedList&lt;T&gt; vertexSet = new LinkedList&lt;&gt;();
          LinkedList&lt;Edge&lt;T&gt;&gt; path = new LinkedList&lt;&gt;();
  
          vertexSet.addAll(graph.getVertices()); //unvisited ERROR HERE
          vertexSet.remove(source);
  
          double numberOfVertices = graph.getVertices().size();
          PriorityQueue&lt;Edge&lt;T&gt;&gt; priorityQueue = new PriorityQueue&lt;&gt;();
  
          while (!vertexSet.isEmpty()) {
              for (Edge&lt;T&gt; edge : graph.getAdjacentVertices(source)) {//ERROR
                 if (vertexSet.contains(edge.getDestination())) 
                      priorityQueue.insert(edge);
              }
              Edge&lt;T&gt; minWeight = priorityQueue.extractMin();
              weight += minWeight.getWeight();
              path.add(HERE I SHOULD PUT MST PATH BUT I DON'T KNOW HOW!!);
  
              source = minWeight.getDestination();
              vertexSet.remove(source);
          }
          return &lt;graph??&gt;;
      }
  }
  </code></pre>
  
  <p>As I said before, I don't know if I should return a graph as MST (maybe the one I give as input deleting the most expensive edges) or the LinkedList called path which saves the path with lowest weight.
  I also don't know how to find the number of edges in the MST; should I for every vertex (keySet of my HashMap) find the size of the LinkedList (which is its value) and sum them all?</p>
  
  <p><strong>EDIT: getAdjacentVertices method</strong></p>
  
  <pre><code>public LinkedList&lt;T&gt; getAdjacentVertices(T vertex) {
      if (adjacencyList.isEmpty() || adjacencyList.containsKey(vertex) == false) {
          System.out.println("Error msg.\n");
          return null;
      } else {
          LinkedList&lt;T&gt; allAdjacents = new LinkedList&lt;&gt;();
          for (Edge&lt;T&gt; edge : adjacencyList.get(vertex)) {
              allAdjacents.add(edge.getDestination());
          }
          return allAdjacents;
      }
  }
  </code></pre>