Java Implementation of Dijkstra's Alogrith
Java Implementation of Dijkstra's Alogrith
import java.util.*;
import java.lang.*;
import java.io.*;
class ShortestPath {
// from the set of vertices not yet included in shortest path tree
min = dist[v];
min_index = v;
return min_index;
}
// A utility function to print the constructed distance array
// representation
int dist[] = new int[V]; // The output array. dist[i] will hold
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// iteration.
sptSet[u] = true;
// picked vertex.
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] <
dist[v])
}
// print the constructed distance array
printSolution(dist);
// Driver method
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
t.dijkstra(graph, 0);