title: Shortest Path LinkedList
body: <p>I want to find the shortest path on a list of linked list, which represents a directed graph with cost per edge/path. </p>
  
  <p>The <strong>output</strong> would look something like this, It tells me the cost it would take me to get from vertex 0 to the other vertices:</p>
  
  <pre><code>d[0 to 0] = 0
  d[0 to 1] = 20
  d[0 to 2] = 10
  </code></pre>
  
  <p>This is how I <strong>populate my list</strong> for testing.</p>
  
  <pre><code>LinkedList&lt;GraphData&gt; g = new LinkedList[3];
  
  for (int i = 0; i &lt; 3; i++)
      weight[i] = new LinkedList&lt;GraphData&gt;();
  
  g[0].add(new GraphData(1, 20);
  g[0].add(new GraphData(2, 10);
  </code></pre>
  
  <p>The <strong>GraphData class</strong> looks something like this:</p>
  
  <pre><code>int vertex, int edgeCost;
  </code></pre>
  
  <p><strong>Now for my problem:</strong></p>
  
  <p>I want to find the shortest path from vertex v to all the others.</p>
  
  <pre><code> public static int[] shortestPaths(int v, LinkedList&lt;GraphData&gt;[] cost)
  {
      // get the set of vertices
      int n = cost.length;
  
      // dist[i] is the distance from v to i
      int[] dist = new int[n];
  
      // s[i] is true if there is a path from v to i
      boolean[] s = new boolean[n];
  
      // initialize dist
      for(int i = 0; i &lt; n; i++)
          dist[i] = cost[v].get(i).getCost();
  
      s[v] = true;
  
      // determine n-1 paths from v 
      for ( int j = 2 ; j &lt; n  ; j++ )
      {
          // choose u such that dist[u] is minimal for all w with s[w] = false
          // and dist[u] &lt; INFINITY
          int u = -1;
  
          for (int k = 0; k &lt; n; k++)
              if ( !s[k] &amp;&amp; dist[k] &lt; INFINITY)
                  // check if u needs updating
                  if ( u &lt; 0 || dist[k] &lt; dist[u])
                      u = k;
          if (u &lt; 0)
              break; 
  
          // set s[u] to true and update the distances
          s[u]=true;
  
          for (int k = 0; k &lt; n; k++)
              if ( !s[k] &amp;&amp; cost[u].get(k).getCost() &lt; INFINITY )
                  if( dist[k] &gt; dist[u] + cost[u].get(k).getCost())
                      dist[k] = dist[u] + cost[u].get(k).getCost();
  
          // at this point dist[k] is the smallest cost path from
          // v to k of length j.
      }       
      return dist;
  }
  </code></pre>
  
  <p>This line dist[i] = cost[v].get(i).getCost(); throws "IndexOutOfBoundsException" </p>
  
  <p>Any idea what I am doing wrong? Any help will be appreciated.</p>