java - Trying to properly implement Dijkstra's Shortest Path algorithm with 2 short arrays and 1 boolean array -


i trying implement dijkstra's shortest path algorithm.

i'm having hard time understanding how test every possible path inside while loop.

i've looked specific examples of proper implementation cant find can use code working :\

any pointers appreciated

here code

    package userapplication;  `enter code here`import java.util.arraylist;      public class shortestpath {         private ui ui;         private map map;         private boolean included[];         private short distance[];         private short path[];         private arraylist<short> trace;         private short n;         boolean flag = false;          // ***************************************************************//         public shortestpath(ui ui, map map, short n) {              initialize(ui, map, n);          }          // ***************************************************************//         // ***************************************************************//         private void initialize(ui ui, map map, short n) {              this.n = n;              initializeresources(ui, map);          }          // ***************************************************************//         // ***************************************************************//         private void initializeresources(ui ui, map map) {              this.ui = ui;              this.map = map;          }          // ***************************************************************//         // ***************************************************************//         private void initializestorage(short current) {              included = new boolean[n];              path = new short[n];              distance = new short[n];              trace = new arraylist<short>();              (short = 0; < n - 1; i++) {                  distance[i] = map.getroaddistance(current, i);                  if ((distance[i] != short.max_value) && (distance[i] > 0)) {                      path[i] = current;                  } else {                      path[i] = -1;                  }              }              distance[current] = 0;              included[current] = true;          }          // ***************************************************************//         // ***************************************************************//         public short search(short start, short destination) {              initializestorage(start);             int edge = 0;             int target = 0;              short total = 0;              // trace.add(start);              while (!included(destination)) {                  (int = 0; < n; i++) {                      if (!trace.contains(destination)&!included((short)i)) {                          target = i;                          included[target] = true;                          trace.add((short) i);                          (int r = 0; r < n; r++) {                              if (!included[i]) {                                  edge = map.getroaddistance(target, destination);                                  if (edge > -1 && edge != short.max_value) {                                      if (distance[target] + edge < distance[i]) {                                          distance[i] = (short) (distance[target] + edge);                                          path[i] = (short) target;                                      }                                 }                             }                          }                      }                  }             }             return total;          }          // ***************************************************************//         // ***************************************************************//         public void findpath(short start, short end) {              short distance = 0;              if ((start > -1) && (end > -1)) {                  ui.writethis("# # # # # # # # # # # # # # # # # # # # # # # \n");                  ui.writethis(map.whatscityname(start) + " (" + start + ")" + " "                         + map.whatscityname(end).trim() + "(" + end + ")\n");                  if (start == end) {                      initializestorage(start);                      trace.add(start);                      path[start] = 0;                      included[start] = true;                      distance = 0;                  } else {                      distance = search(start, end);                 }                  reportanswer(distance);                  reporttraceoftargets(distance);              } else {                  ui.writethis("# # # # # # # # # # # # # # # # # # # # # # # \n");                  ui.writethis(ui.getstartcityname() + " (" + start + ")" + " "                         + ui.getdestinationcityname().trim() + " (" + end + ")\n");                  ui.writethis("error 1 of cities not on map\n\n");             }          }          // ***************************************************************//         // ***************************************************************//         public void reportanswer(short distance) {              if (distance > 0) {                  ui.writethis("distance  :  " + distance + "\n");              } else {                  ui.writethis("distance  : ?\n");             }         }          // ***************************************************************//         // ***************************************************************//         public void reporttraceoftargets(short distance) {              if (distance > -1) {                  ui.writethis("path: ");                  (int = n - 1; >= 0; i--) {                      if ((path[i] != -1) && (included[i])) {                          ui.writethis(" > " + (map.whatscityname((short) i)));                     }                 }              } else {                  ui.writethis("path: sorry - cant reach destination city start city\n");              }             ui.writethis("\ntrace of targets: ");              (int = 0; < trace.size(); i++) {                  ui.writethis(" > " + (map.whatscityname(trace.get(i))));              }              ui.writethis("\n" + "# targets: " + trace.size() + "\n\n");          }          // ***************************************************************//         // ***************************************************************//         public boolean included(short destination) {             if (trace.contains(destination)) {                 return true;             }             return false;          }      } 


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -