Skip to content

Commit d9b8924

Browse files
authored
Merge pull request #70 from gandharva-gk/main
Djikstra Algorithm in Java
2 parents e0d3cc0 + fa97914 commit d9b8924

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

djikstra.java

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import java.util.Scanner;
2+
import java.util.Arrays;
3+
4+
class Node{
5+
int val;
6+
int weight;
7+
Node next;
8+
9+
public Node(int val,int weight){
10+
this.val = val;
11+
this.weight = weight;
12+
this.next = null;
13+
}
14+
}
15+
16+
class Graph{
17+
18+
public Node[] CreateGraph(int n){
19+
Node[] Graph = new Node[n];
20+
return Graph;
21+
}
22+
23+
/* This Function adds Edge between the Source and Destination */
24+
25+
public void AddEdge(Node[] Graph,int src,int dest,int weight){
26+
27+
28+
if(Graph[src] == null){
29+
30+
Graph[src] = new Node(dest,weight);
31+
return;
32+
33+
}
34+
35+
else{
36+
37+
Node temp = new Node(dest,weight);
38+
39+
temp.next = Graph[src];
40+
41+
Graph[src] = temp;
42+
43+
}
44+
45+
}
46+
47+
/* This Function returns the Index of the Node that has the Least Shortest Distance and is UnVisited. */
48+
49+
public int MinIndex(int[] dist,boolean[] visited){
50+
int min = Integer.MAX_VALUE;
51+
int min_index = -1;
52+
53+
for(int i = 0;i<dist.length;i++){
54+
55+
if(!visited[i] && min > dist[i]){
56+
min = dist[i];
57+
min_index = i;
58+
}
59+
}
60+
61+
return min_index;
62+
}
63+
64+
}
65+
66+
67+
public class djikstra {
68+
public static void main(String[] args) {
69+
70+
Scanner scan = new Scanner(System.in);
71+
72+
Graph g = new Graph();
73+
74+
75+
System.out.print("Enter the Number of Nodes in the Graph : ");
76+
77+
int num = scan.nextInt();
78+
79+
Node[] Graph = g.CreateGraph(num);
80+
81+
System.out.print("Enter the Total Number of Edges in the Graph : ");
82+
83+
int edges = scan.nextInt();
84+
85+
System.out.println("Now Enter all the Edges as Source Destination Weight");
86+
87+
/* Storing the edges */
88+
89+
for(int i = 0;i<edges;i++){
90+
int src = scan.nextInt();
91+
int dest = scan.nextInt();
92+
int weight = scan.nextInt();
93+
94+
g.AddEdge(Graph, src, dest, weight);
95+
g.AddEdge(Graph, dest, src, weight);
96+
97+
}
98+
99+
System.out.print("Enter the Node from where you want to find the Shortest Distance to all the other Nodes : ");
100+
101+
int start = scan.nextInt();
102+
103+
/* Distance Array Which Stores the Shortest Distance from the Given Node to all other Nodes */
104+
105+
int[] dist = new int[num];
106+
107+
/* Boolean Array which stores whether a Node is Visited or Not */
108+
109+
boolean[] visited = new boolean[num];
110+
111+
/* Initialize all the Distances to MAX Values. */
112+
113+
Arrays.fill(dist, Integer.MAX_VALUE);
114+
115+
/* Initialize the Shortest Distance to reach the Start Node to 0 */
116+
117+
dist[start] = 0;
118+
119+
while(true){
120+
121+
/* At Every Iteration Find the Node that has the least Shortest Distance Value and is Unvisited. */
122+
123+
int min_index = g.MinIndex(dist, visited);
124+
125+
/* If you haven't found any Node that has Shortest Distance and Unvisted then exit the loop */
126+
127+
if(min_index == -1){
128+
break;
129+
}
130+
131+
/* Mark the Node as Visited. */
132+
133+
visited[min_index] = true;
134+
135+
Node temp = Graph[min_index];
136+
137+
/* Now Iterate over all it's Neighbours and update the values if the
138+
(Shortest Distance to this Node) + (Edge Weight between this Node and Neighouring Node)
139+
is less than the Shortest Distance to the neighbour in Distance Array */
140+
141+
while(temp != null){
142+
143+
if(dist[temp.val] > dist[min_index] + temp.weight && !visited[temp.val]){
144+
dist[temp.val] = dist[min_index] + temp.weight;
145+
}
146+
147+
temp = temp.next;
148+
149+
}
150+
}
151+
152+
/* Printing the Shortest Distances from the Start Node to all the other Nodes */
153+
154+
for(int i = 0;i<num;i++){
155+
System.out.print(dist[i] + " ");
156+
}
157+
158+
System.out.println("");
159+
160+
161+
scan.close();
162+
163+
}
164+
}

0 commit comments

Comments
 (0)