title: Dijkstra Algorithm with Adjaceny matrix
body: <p>I am trying to implement Dijkstra Algorithm using priority queue given an adjaceny matrix.
  I know the problem is probably at where I add vertices to the priority queue but I can't figure out how to fix it! </p>
  
  <pre><code>static int dijkstra(int[][] g, int i, int j) {
      // Get the number of vertices in G
      int n = g.length;
      int counter = 0;
  
      PriorityQueue&lt;Vertex&gt; q = new PriorityQueue&lt;Vertex&gt;(n, new Comparator&lt;Vertex&gt;() {       
          public int compare(Vertex a, Vertex b) {
              Vertex v1 = (Vertex) a;
              Vertex v2 = (Vertex) b;
              if (v1.getD() &gt; v2.getD()) {
                  return 1;
              } else if (v1.getD() &lt; v2.getD()) {
                  return -1;
              } else {
                  return 0;
              }
          }
      });
      int[] distance = new int[n];
      for (int l = 0; l &lt; n; l++) {
          distance[l] = 99999;
      }
      distance[i] = 0;
  
      for (int l = 0; l &lt; n/2; l++) {
          for (int m = 0; m &lt; n; m++) {
              if (g[l][m] &gt; 1) {
                  System.out.printf("%d was added \n", g[l][m]);
                  q.add(new Vertex(l, g[l][m]));
              }
          }
      }
      while (!q.isEmpty()) {
          int u = 0;
          for (int z = 0; z &lt; n; z++) {
              if (distance[z] &lt; distance[u]) {
                  u = z;
              }
          }
          if (distance[u] == 99999) { break; }
          q.remove();
          for (int l = 0; l &lt; n; l++) {
              if (g[u][l] &gt; 1) {
                  int alt = distance[u] + g[u][l];
                  if (alt &lt; distance[l]) {
                      distance[l] = alt;
                      q.remove();
                      q.add(new Vertex(u, distance[l]));
                  }
              }
          }
      }
  
      for (int k = 0; k &lt; n; k++) {
          System.out.printf("==&gt;%d", distance[j]);
      }
      return distance[j];
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>class Vertex {
      int v,d;
      public Vertex(int num, int dis) {
          v = num;
          d = dis;
      }
      public int getV() {
          return v;
      }
      public int getD() {
          return d;
      }
  }
  </code></pre>
  
  <p>I am testing with the following matrix:</p>
  
  <pre><code> 0  0  0  0  0  0  0 38  0  0  0  0  0  0  0  0
   0  0  0  0 65  0 64  0  6  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0  6  8  0  0  0 62
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0 65  0  0  0  0  0  0  0  6  0  0  0  0 55  0
   0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
   0 64  0  0  0  0  0 53  0  0 36  0 45  0  0  0
  38  0  0  0  0  0 53  0  0  0  0 91  0 29  0  0
   0  6  0  0  0  0  0  0  0  0  0 95 55  0  0  0
   0  0  0  0  6  0  0  0  0  0  0  0  0  0  0  0
   0  0  6  0  0  0 36  0  0  0  0  0  0  0  0  0
   0  0  8  0  0  0  0 91 95  0  0  0 60  0  0  0
   0  0  0  0  0  0 45  0 55  0  0 60  0  0  0  0
   0  0  0  0  0  0  0 29  0  0  0  0  0  0  0  0
   0  0  0  0 55  0  0  0  0  0  0  0  0  0  0  0
   0  0 62  0  0  0  0  0  0  0  0  0  0  0  0  0
  </code></pre>
  
  <p>And start is <code>0</code>, end is <code>n - 1</code>. I should be getting <code>195</code> but it seems like none of the distances are being changed!</p>