-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraAlgo.java
60 lines (48 loc) · 1.67 KB
/
DijkstraAlgo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* The DijkstraAlgo class implements Dijkstra's algorithm to find the shortest path from a source
* vertex to all other vertices in a graph.
*/
import java.util.*;
class DijkstraAlgo {
static final int V = 5;
int minDistance(int dist[], Boolean sptSet[]) {
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
void printSolution(int dist[]) {
System.out.println("Vertex \t\t Distance from Source");
for (int i = 0; i < V; i++)
System.out.println(i + " \t\t " + dist[i]);
}
void dijkstra(int graph[][], int src) {
int dist[] = new int[V];
Boolean sptSet[] = new Boolean[V];
for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
public static void main(String[] args) {
int graph[][] = new int[][] { { 0, 2, 0, 3, 0 },
{ 2, 0, 3, 0, 2 },
{ 0, 3, 0, 2, 0 },
{ 3, 0, 2, 0, 3 },
{ 0, 2, 0, 3, 0 } };
DijkstraAlgo t = new DijkstraAlgo();
t.dijkstra(graph, 0);
}
}