Question: NEED: HOW TO EXPLAIN THIS JAVA CODE AND HOW ITS WORK. import java.util.*; import java.lang.*; import java.io.*; public class ShortestDistances { static int n; static

NEED: HOW TO EXPLAIN THIS JAVA CODE AND HOW ITS WORK.

import java.util.*; import java.lang.*; import java.io.*; public class ShortestDistances { static int n; static String names[]; static int graph[][]; int minDistance(int dist[], Boolean sptSet[]) { // A utility function to find the vertex with minimum distance value, from the set of vertices not yet included in shortest path tree // Initialize min value int min = Integer.MAX_VALUE, min_index=-1; for (int v = 0; v < n; v++) if (sptSet[v] == false && dist[v] <= min) { min = dist[v]; min_index = v; } return min_index; } // A utility function to print the constructed distance array void printDistances(int dist[], int src) { for (int i = 0; i < n; i++) System.out.println("Distance From "+names[src]+" to "+names[i]+": "+dist[i]); } // Funtion that implements Dijkstra's single source shortest path // algorithm for a graph represented using adjacency matrix // representation void dijkstra(int src) { int distances[] = new int[n]; // The output array. distances[i] will hold // the shortest distance from src to i // spt[i] will true if vertex i is included in shortest // path tree or shortest distance from src to i is finalized Boolean spt[] = new Boolean[n]; // Initialize all distances as INFINITE and stp[] as false for (int i = 0; i < n; i++) { distances[i] = Integer.MAX_VALUE; spt[i] = false; } // Distance of source vertex from itself is always 0 distances[src] = 0; // Find shortest path for all vertices for (int count = 0; count < n-1; count++) { // Pick the minimum distance vertex from the set of vertices // not yet processed. u is always equal to src in first // iteration. int u = minDistance(distances, spt); // Mark the picked vertex as processed spt[u] = true; // Update dist value of the adjacent vertices of the // picked vertex. for (int v = 0; v < n; v++) // Update distances[v] only if is not in sptSet, there is an // edge from u to v, and total weight of path from src to // v through u is smaller than current value of distances[v] if (!spt[v] && graph[u][v]!=0 && distances[u] != Integer.MAX_VALUE && distances[u]+graph[u][v] < distances[v]) distances[v] = distances[u] + graph[u][v]; } // print the constructed distance array printDistances(distances, src); } // Driver method public static void main (String[] args) { /* Let us create the example graph discussed above */ Scanner s=new Scanner(System.in); ShortestDistances t = new ShortestDistances(); System.out.print("Enter the number of nodes: "); n=s.nextInt();//number of vertexs names=new String[n]; System.out.print(" Enter the names of nodes: "); for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!