Advertisement
Guest User

Startimes_Test4

a guest
Jul 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.85 KB | None | 0 0
  1. public class PathFinder {
  2.  
  3.     public static void main(String[] args) {
  4.        
  5.         int[][] tab =   { //    the example from the topic
  6.             {0, 7, 2, 0, 0, 0},
  7.             {0, 0, 2, 1, 0, 0},
  8.             {0, 3, 0, 8, 5, 0},
  9.             {0, 0, 0, 0, 4, 3},
  10.             {0, 0, 0, 5, 0, 1},
  11.             {0, 0, 0, 0, 0, 0}
  12.         };
  13.  
  14.         System.out.println(getShortestPath(tab, 1, 6));
  15.     }
  16.     public static int getShortestPath(int [][] tab,int source,int destination)  {
  17.  
  18.         int sourceIndex = source - 1;
  19.         int destinationIndex = destination - 1;
  20.         final int noNodes = tab.length;
  21.  
  22.         Node[] nodesTab = new Node[noNodes];
  23.         for (int i = 0; i < noNodes; i++)   {
  24.  
  25.             nodesTab[i] = new Node();
  26.         }
  27.         nodesTab[sourceIndex].setWeight(0); //  because nodesTab[0] is the source node
  28.         nodesTab[sourceIndex].setVisited(true);
  29.         int parentNode = 0, childNode = 0;
  30.         while (parentNode != destinationIndex)  {
  31.  
  32.             for (int i = 0; i < tab[parentNode].length; i++)    {
  33.  
  34.                 if (tab[parentNode][i] != 0)    {
  35.  
  36.                     childNode = i;
  37.                     int weightFromSourceToChild = (nodesTab[parentNode].getWeight() + tab[parentNode][childNode]);
  38.                     if (!nodesTab[childNode].isVisited() &&
  39.                         weightFromSourceToChild < nodesTab[childNode].getWeight()
  40.                         || nodesTab[childNode].getWeight() == -1)   {
  41.  
  42.                         nodesTab[childNode].setVisited(true);
  43.                         nodesTab[childNode].setWeight(weightFromSourceToChild);
  44.                     }
  45.                 }
  46.             }
  47.             parentNode = childNode;
  48.            
  49.         }
  50.  
  51.         int shortestPath = nodesTab[destinationIndex].getWeight();
  52.  
  53.         return shortestPath;
  54.     }
  55. }
  56. class Node  {
  57.  
  58.     private int weight;
  59.     private boolean visited;
  60.  
  61.     public Node()   {
  62.  
  63.         setWeight(-1);
  64.         setVisited(false);
  65.     }
  66.     public int getWeight()  {
  67.  
  68.         return this.weight;
  69.     }
  70.     public void setWeight(int weight)   {
  71.  
  72.         this.weight = weight;
  73.     }
  74.     public boolean isVisited()  {
  75.  
  76.         return this.visited;
  77.     }
  78.     public void setVisited(boolean visited) {
  79.  
  80.         this.visited = visited;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement