Skip to content

Commit 188630d

Browse files
Merge pull request #97 from sanghaisubham/shortestpath
Implemented Dijkstra Algorithm in Java
2 parents a2c0982 + fac25b9 commit 188630d

File tree

1 file changed

+114
-0
lines changed
  • Algorithms/Graph Algorithms/Shortest Path Algorithm/Dijkstra's Algorithm/Java

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* Author :SUBHAM SANGHAI
2+
A Java program for Dijkstra's single source shortest path algorithm,
3+
where given a graph and a source vertex in graph, we have to find
4+
shortest paths from source to all vertices in the given graph.
5+
The program is for adjacency matrix representation of the graph*/
6+
import java.util.*;
7+
import java.lang.*;
8+
import java.io.*;
9+
class Dijkstra
10+
{
11+
12+
private static final int V=5;
13+
14+
/*A utility function to find the vertex with minimum distance value,
15+
from the set of vertices not yet included in shortest path tree*/
16+
int minDistance(int dist[], Boolean sptSet[])
17+
{
18+
// Initialize min value
19+
int min = Integer.MAX_VALUE, min_index=-1;
20+
21+
for (int v = 0; v < V; v++)
22+
if (sptSet[v] == false && dist[v] <= min)
23+
{
24+
min = dist[v];
25+
min_index = v;
26+
}
27+
28+
return min_index;
29+
}
30+
31+
32+
33+
// A utility function to print the constructed distance array
34+
void printSolution(int dist[], int n)
35+
{
36+
System.out.println("Vertex | Distance from Source");
37+
System.out.println("------- | ---------------------");
38+
for (int i = 0; i < V; i++)
39+
System.out.println(i+" | "+dist[i]);
40+
}
41+
42+
43+
44+
45+
/*Function that implements Dijkstra's single source shortest path
46+
algorithm for a graph represented using adjacency matrix
47+
representation*/
48+
void dijkstra(int graph[][], int src)
49+
{
50+
int dist[] = new int[V];
51+
/*The output array. dist[i] will hold
52+
the shortest distance from src to i*/
53+
/* sptSet[i] will true if vertex i is included in shortest
54+
path tree or shortest distance from src to i is finalized*/
55+
Boolean sptSet[] = new Boolean[V];
56+
// Initialize all distances as INFINITE and stpSet[] as false
57+
for (int i = 0; i < V; i++)
58+
{
59+
dist[i] = Integer.MAX_VALUE;
60+
sptSet[i] = false;
61+
}
62+
// Distance of source vertex from itself is always 0
63+
dist[src] = 0;
64+
// Find shortest path for all vertices
65+
for (int count = 0; count < V-1; count++)
66+
{
67+
/* Pick the minimum distance vertex from the set of vertices
68+
not yet processed. u is always equal to src in first
69+
iteration.*/
70+
int u = minDistance(dist, sptSet);
71+
// Mark the picked vertex as processed
72+
sptSet[u] = true;
73+
/*Update dist value of the adjacent vertices of the
74+
picked vertex.*/
75+
for (int v = 0; v < V; v++)
76+
/* Update dist[v] only if is not in sptSet, there is an
77+
edge from u to v, and total weight of path from src to
78+
v through u is smaller than current value of dist[v]*/
79+
if (!sptSet[v] && graph[u][v]!=0 &&
80+
dist[u] != Integer.MAX_VALUE &&
81+
dist[u]+graph[u][v] < dist[v])
82+
dist[v] = dist[u] + graph[u][v];
83+
}
84+
85+
// print the constructed distance array
86+
printSolution(dist, V);
87+
}
88+
89+
90+
91+
92+
// Driver method
93+
public static void main (String[] args)
94+
{
95+
/* Let us create the following graph
96+
2 3
97+
(0)--(1)--(2)
98+
| / \ |
99+
6| 8/ \5 |7
100+
| / \ |
101+
(3)-------(4)
102+
9 */
103+
int graph[][] = new int[][] {{0, 2, 0, 6, 0},
104+
{2, 0, 3, 8, 5},
105+
{0, 3, 0, 0, 7},
106+
{6, 8, 0, 0, 9},
107+
{0, 5, 7, 9, 0},
108+
};
109+
Dijkstra t = new Dijkstra();
110+
t.dijkstra(graph, 0);
111+
}
112+
113+
114+
}

0 commit comments

Comments
 (0)