title: DV router algorithm implementation using java
body: <p>I am trying to implement DV routing algorithm in Java. I have used the bellman ford approach. I have provided an input and using that input I am able to compute the correct output. So I am able to compute the shortest path between each node. Now I am trying to print the shortest path from source to destination node for all the nodes present in the file. I have done the following till now. I am unable to understand how can I print the shortest path between source node(scnode) and destination node(dnode). This is what I have done till now.</p>
  
  <pre><code>    public class DVAlgorithm
   {
      private static int distances[][];
      private static  int numberofvertices;
      public static final int MAX_VALUE = 999;
      static int n = 0;
      static int scnode; //scnode - source node to find shortest path from
      static int dnode; // dnode - destination node to find shortest path to
  
      public static void initialize(int numberofvertices1)
      {       
          numberofvertices = numberofvertices1;
          distances = new int[numberofvertices1 + 1][numberofvertices1 + 1];      
      }
  
      public static void Calculation(int adjacencymatrix[][])
      {
          int index;
          for (index = 1; index &lt;= numberofvertices; index++)
          for (int node = 1; node &lt;= numberofvertices; node++)
          {
              distances[index][node] = MAX_VALUE;
          }
  
          for(index = 1; index &lt;= numberofvertices; index++)
          {
              distances[index][index] = 0;
              for (int node = 1; node &lt;= numberofvertices - 1; node++)
              {
                  for (int sourcenode = 1; sourcenode &lt;= numberofvertices; sourcenode++)
                  {
                      for (int destinationnode = 1; destinationnode &lt;= numberofvertices; destinationnode++)
                      {
                          if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
                          {
                              if (distances[index][destinationnode] &gt; distances[index][sourcenode] + adjacencymatrix[sourcenode][destinationnode])
                                  distances[index][destinationnode] = distances[index][sourcenode] + adjacencymatrix[sourcenode][destinationnode];
                              System.out.println("Index: " +index);
                              System.out.print("\tSource node: " +sourcenode);
                              System.out.print("\tDesination node: " +destinationnode);
                              System.out.println("Distance between index and destination node inside if loop" +distances[index][destinationnode]);
                          }
                      }
                  }
              }
          }
      }
  
      public static void printMatrix()
      {
          int index;
          System.out.println("The final matrix after DV algorithm is: ");
          for (index = 1; index &lt;= numberofvertices; index++)
          {
              System.out.print("\n");
                  for (int vertex = 1; vertex &lt;= numberofvertices; vertex++)
                  {
                      System.out.print(" "+ distances[index][vertex]+" ");    
                  }
          }
      }
  
      public static void main(String... arg) throws IOException
      {
          int numberofvertices = 0;
          int source;
          Scanner scanner = new Scanner(System.in);
  
          System.out.println("Enter the number of vertices");
          numberofvertices = scanner.nextInt();
  
          int adjacencymatrix[][] = new int[numberofvertices + 1][numberofvertices + 1];
  
          System.out.println("Enter the adjacency matrix");
          for (int sourcenode = 1; sourcenode &lt;= numberofvertices; sourcenode++)
          {
              for (int destinationnode = 1; destinationnode &lt;= numberofvertices; destinationnode++)
              {
                  adjacencymatrix[sourcenode][destinationnode] = scanner.nextInt();
              if (sourcenode == destinationnode)
                  {
                      adjacencymatrix[sourcenode][destinationnode] = 0;
                      continue;
                  }
              if (adjacencymatrix[sourcenode][destinationnode] == 0)
                  {
                      adjacencymatrix[sourcenode][destinationnode] = MAX_VALUE;
                  }
              }
          }
  
          initialize(numberofvertices);
          Calculation(adjacencymatrix);
  
          System.out.println("1. Print Matrix ");
          System.out.println("2. Failure Simulation ");
          System.out.println("3. Display shortest path between specific nodes: ");
          System.out.println("Enter choice: ");
          source = scanner.nextInt();
          int Fnode;
      //    String ValContinue="n";
      //do{
         //     printMatrix();
  
              switch(source)
              {
                  case 1:
                      printMatrix();
                  break;
  
                  case 2:
                      System.out.println("Enter the number of the router whose failure is to be simulated : ");
                      Fnode = scanner.nextInt();
                      for(int i=1;i&lt;numberofvertices;i++)
                          adjacencymatrix[Fnode][i]=MAX_VALUE;
  
                      for(int i=1;i&lt;numberofvertices;i++)
                          adjacencymatrix[i][Fnode]=MAX_VALUE;
  
                      initialize(numberofvertices);
                      Calculation(adjacencymatrix);                              
                      printMatrix();
  
                  break;              
  
                  case 3:
                      System.out.println("Enter the source node: ");
                      scnode = scanner.nextInt();
                      System.out.println("Enter the destination node: ");
                      dnode = scanner.nextInt();
  
  
                      break;
  
                  default:
                      System.out.println("Invalid choice ");
              }
  
   /*        System.out.println("\nDo you want to Continue (y/n) : ");
           ValContinue = scanner.next();            
          }while(ValContinue.equals("n"));
    */       scanner.close(); 
  
  //        printMatrix();
      }
  }`
  </code></pre>