title: Implementing Dijkstra algorithm in Java
body: <p>I'm trying to make a program that supports multiple operations on automata (graphs). 
  This is what my datasctructures look like: </p>
  
  <pre><code>public class Automaton {
  private ArrayList&lt;Node&gt; $Q; /* All the nodes */
  private Node $startingNode; /* The startingNode */
  private ArrayList&lt;Connection&gt; $delta; /* All the connections */
  private ArrayList&lt;Node&gt; $F; /* All the end nodes */
  private ArrayList&lt;Character&gt; $E; 
  
  
  public class Connection { 
  private Node $start; 
  private Node $end; 
  private char $condition; }
  
  
  public class Node { 
  private boolean $startingNode; 
  private boolean $endNode; 
  private String $name; }
  </code></pre>
  
  <p>One of the operations is the Dijkstra algorithm, here is my code so far, the problem is that it never goes into this if (D[v] >= (D[w] + value)), because 1 will never be smaller than 1, im looking for a way how i can count the length of my path so it's higher than 1. Eg : from A->D is length 3, since you go from A to B, from B to C, from C to D.</p>
  
  <pre><code>public String[] shortestPath() {
      boolean[] visited = new boolean[$Q.size()];
      String[] P = new String[$Q.size()];
      int[] D = new int[$Q.size()];
      char c;
      int w;
      int value = 0;
      Node source = null;
      boolean found = false;
  
      for (int i = 0; i &lt; visited.length; i++) {
          if ($Q.get(i).getStartingNode()) {
              visited[i] = true;
              source = $Q.get(i);
          } else
              visited[i] = false;
      }
  
      for (int i = 0; i &lt; $Q.size(); i++) {
          /* If the source and the node are not the same */
          if (!($Q.get(i).getName().equals(source.getName()))) {
              P[i] = source.getName();
              /* If there is a connection */
              c = checkConnection(source, $Q.get(i));
              if (c != '\u0000') {/* There is a connection */
                  if (c == '$')/* Epsilon transition */
                      D[i] = 0;
                  else
                      D[i] = 1;
              } else
                  /* There isnt a connection, put it on -1 */
                  D[i] = -1;
          }
  
      }
      for(int i = 0; i &lt; visited.length; i++)
          System.out.println(visited[i]);
  
      for (int i = 1; i &lt; $Q.size() &amp;&amp; !found; i++) {
          w = findMinimum(D, visited);
          visited[w] = true;
  
          System.out.println(w);
  
          for (int v = 0; v &lt; $Q.size() &amp;&amp; !found; v++) {
              if (!(visited[v])) {
                  c = checkConnection($Q.get(w), $Q.get(v));
                  if (c != '\u0000') {/* There is a connection */
                      if (c == '$')/* Epsilon transition */
                          value = 0;
                      else
                          value = 1;
                      /*Check if from source to v is shorter if you go via w*/
                      if (D[v] &gt;= (D[w] + value)) {
                          D[v] = D[w] + value;
                          P[v] = $Q.get(w).getName();
  
                          if ($Q.get(w).getEndNode())
                              found = true;
                      }
                  }/*Connection if*/
              }
          }
      }
      return P;
  
  }
  
  /** Returns the minimum cost of the connections from the source
   * @param d the array with the costs from the source to the other nodes
   * @param visited an array that remembers which node is already visited
   * @return returns the index of the position with the minimum cost
   */
  private int findMinimum(int[] d, boolean[] visited) {
      // TODO Auto-generated method stub
      int min = 2; /* Since the cost will be 0 of 1 we can use 2 as a higher number */
      int index = 0;
  
      for (int i = 0; i &lt; d.length; i++) {
          /* If we didnt visit the node yet, so a node outside s */
          if (!(visited[i])) {
              /* If there really is a connection */
              if (d[i] != -1) {
                  /* If the cost is lower than our current minimum */
                  if (d[i] &lt;= min){
                      index = i;
                      min = d[i];
                  }
  
              }
          }
      }
  
      /**
   * Checks if there is a connection between 2 nodes
   * @param start the startnode of the connection
   * @param end the endnode of the connection
   * @return returns the cost of the edge between the 2 nodes, else return null
   */
  private char checkConnection(Node start, Node end){
      for(int i = 0; i &lt; $delta.size(); i++){
          /*If the start node and the end node are the same*/
          if(($delta.get(i).getStart().getName().equals(start.getName())) &amp;&amp; $delta.get(i).getEnd().getName().equals(end.getName())){
              return $delta.get(i).getCondition();
          }
      }
      return '\u0000'; /*Empty char*/
  }
      return index;
  }
  </code></pre>