Java Program to Find Single Source Shortest Path in DAG

This Java program,to find the single source shortest path in directed acyclic graph by Dijkstra’s algorithm.Dijkstra’s algorithm is a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree.

Here is the source code of the Java program to find the single source shortest path in directed acyclic graph. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class DijkstraShortestPath
  5. {
  6.     private boolean settled[];
  7.     private boolean unsettled[];
  8.     private int distances[];
  9.     private int adjacencymatrix[][];
  10.     private int numberofvertices;
  11.  
  12.     public DijkstraShortestPath(int numberofvertices)
  13.     {
  14.         this.numberofvertices = numberofvertices;
  15.         this.settled = new boolean[numberofvertices + 1];
  16.         this.unsettled = new boolean[numberofvertices + 1]; 
  17.         this.distances = new int[numberofvertices + 1];
  18.         this.adjacencymatrix = new int[numberofvertices + 1][numberofvertices + 1];
  19.     }
  20.  
  21.     public void dijkstraShortestPath(int source, int adjacencymatrix[][])
  22.     {
  23.         int evaluationnode; 
  24.         for (int vertex = 1; vertex <= numberofvertices; vertex++)
  25.         {
  26.             distances[vertex] = Integer.MAX_VALUE;
  27.         }
  28.  
  29.         for (int sourcevertex = 1; sourcevertex <= numberofvertices; sourcevertex++)
  30.         {
  31.             for (int destinationvertex = 1; destinationvertex <= numberofvertices; destinationvertex++)
  32.             {
  33.                 this.adjacencymatrix[sourcevertex][destinationvertex] 
  34.                         = adjacencymatrix[sourcevertex][destinationvertex];  			              
  35.             }
  36.         }
  37.  
  38.         unsettled[source] = true;
  39.         distances[source] = 0;
  40.         while (getUnsettledCount(unsettled) != 0)
  41.         {
  42.             evaluationnode = getNodeWithMinimumDistanceFromUnsettled(unsettled);
  43.             unsettled[evaluationnode] = false;
  44.             settled[evaluationnode] = true;
  45.             evaluateNeighbours(evaluationnode);
  46.         }
  47.     }  
  48.  
  49.     public int getUnsettledCount(boolean unsettled[])
  50.     {
  51.         int count = 0;
  52.         for (int vertex = 1; vertex <= numberofvertices; vertex++)
  53.         {
  54.             if (unsettled[vertex] == true)
  55.             {
  56.                 count++;
  57.             }
  58.         }
  59.         return count;
  60.     }
  61.  
  62.     public int getNodeWithMinimumDistanceFromUnsettled(boolean unsettled[])
  63.     {
  64.         int min = Integer.MAX_VALUE;
  65.         int node = 0;
  66.         for (int vertex = 1; vertex <= numberofvertices; vertex++)
  67.         {
  68.             if (unsettled[vertex] == true && distances[vertex] < min)
  69.             {
  70.                 node = vertex;
  71.                 min = distances[vertex];
  72.             }
  73.         }
  74.         return node;
  75.     }
  76.  
  77.     public void evaluateNeighbours(int evaluationNode)
  78.     {
  79.         int edgeDistance = -1;
  80.         int newDistance = -1;
  81.  
  82.         for (int destinationNode = 1; destinationNode <= numberofvertices; destinationNode++)
  83.         {
  84.             if (settled[destinationNode] == false)
  85.             {
  86.                 if (adjacencymatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
  87.                 {
  88.                     edgeDistance = adjacencymatrix[evaluationNode][destinationNode];
  89.                     newDistance = distances[evaluationNode] + edgeDistance;
  90.                     if (newDistance < distances[destinationNode])
  91.                     {
  92.                         distances[destinationNode] = newDistance;	
  93.                     }
  94.                     unsettled[destinationNode] = true;
  95.                 }
  96.             }
  97.         }
  98.     }
  99.  
  100.     public static void main(String... arg)
  101.     {
  102.         int adjacency_matrix[][];
  103.         int number_of_vertices;
  104.         int source = 0;
  105.         Scanner scan = new Scanner(System.in);
  106.         try
  107.         {
  108.             System.out.println("Enter the number of vertices");
  109.             number_of_vertices = scan.nextInt();
  110.             adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
  111.  
  112.             System.out.println("Enter the Weighted Matrix for the graph");
  113.             for (int i = 1; i <= number_of_vertices; i++)
  114.             {
  115.                 for (int j = 1; j <= number_of_vertices; j++)
  116.                 {
  117.                     adjacency_matrix[i][j] = scan.nextInt();
  118.                     if (i == j)
  119.                     {
  120.                         adjacency_matrix[i][j] = 0;
  121.                         continue;
  122.                     }
  123.                     if (adjacency_matrix[i][j] == 0)
  124.                     {
  125.                         adjacency_matrix[i][j] =  Integer.MAX_VALUE;
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             System.out.println("Enter the source ");
  131.             source = scan.nextInt();
  132.  
  133.             DijkstraShortestPath dijkstrasAlgorithm = new DijkstraShortestPath(number_of_vertices);
  134.             dijkstrasAlgorithm.dijkstraShortestPath(source, adjacency_matrix);
  135.  
  136.             System.out.println("The Shorted Path to all nodes are ");
  137.             for (int i = 1; i <= dijkstrasAlgorithm.distances.length - 1; i++)
  138.             {
  139.                 System.out.println(source + " to " + i + " is "+ dijkstrasAlgorithm.distances[i]);
  140. 	    }
  141.         } catch (InputMismatchException inputMismatch)
  142.         {
  143.             System.out.println("Wrong Input Format");
  144.         }
  145.         scan.close();
  146.     }
  147. }

$javac DijkstraShortestPath.java
$java DijkstraShortestPath
Enter the number of vertices
5
Enter the Weighted Matrix for the graph
0 9 6 5 3 
0 0 0 0 0
0 2 0 4 0
0 0 0 0 0
0 0 0 0 0
Enter the source 
1
The Shorted Path to all nodes are 
1 to 1 is 0
1 to 2 is 8
1 to 3 is 6
1 to 4 is 5
1 to 5 is 3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.