-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathdjikstra.java
More file actions
164 lines (98 loc) · 3.83 KB
/
djikstra.java
File metadata and controls
164 lines (98 loc) · 3.83 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Scanner;
import java.util.Arrays;
class Node{
int val;
int weight;
Node next;
public Node(int val,int weight){
this.val = val;
this.weight = weight;
this.next = null;
}
}
class Graph{
public Node[] CreateGraph(int n){
Node[] Graph = new Node[n];
return Graph;
}
/* This Function adds Edge between the Source and Destination */
public void AddEdge(Node[] Graph,int src,int dest,int weight){
if(Graph[src] == null){
Graph[src] = new Node(dest,weight);
return;
}
else{
Node temp = new Node(dest,weight);
temp.next = Graph[src];
Graph[src] = temp;
}
}
/* This Function returns the Index of the Node that has the Least Shortest Distance and is UnVisited. */
public int MinIndex(int[] dist,boolean[] visited){
int min = Integer.MAX_VALUE;
int min_index = -1;
for(int i = 0;i<dist.length;i++){
if(!visited[i] && min > dist[i]){
min = dist[i];
min_index = i;
}
}
return min_index;
}
}
public class djikstra {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Graph g = new Graph();
System.out.print("Enter the Number of Nodes in the Graph : ");
int num = scan.nextInt();
Node[] Graph = g.CreateGraph(num);
System.out.print("Enter the Total Number of Edges in the Graph : ");
int edges = scan.nextInt();
System.out.println("Now Enter all the Edges as Source Destination Weight");
/* Storing the edges */
for(int i = 0;i<edges;i++){
int src = scan.nextInt();
int dest = scan.nextInt();
int weight = scan.nextInt();
g.AddEdge(Graph, src, dest, weight);
g.AddEdge(Graph, dest, src, weight);
}
System.out.print("Enter the Node from where you want to find the Shortest Distance to all the other Nodes : ");
int start = scan.nextInt();
/* Distance Array Which Stores the Shortest Distance from the Given Node to all other Nodes */
int[] dist = new int[num];
/* Boolean Array which stores whether a Node is Visited or Not */
boolean[] visited = new boolean[num];
/* Initialize all the Distances to MAX Values. */
Arrays.fill(dist, Integer.MAX_VALUE);
/* Initialize the Shortest Distance to reach the Start Node to 0 */
dist[start] = 0;
while(true){
/* At Every Iteration Find the Node that has the least Shortest Distance Value and is Unvisited. */
int min_index = g.MinIndex(dist, visited);
/* If you haven't found any Node that has Shortest Distance and Unvisted then exit the loop */
if(min_index == -1){
break;
}
/* Mark the Node as Visited. */
visited[min_index] = true;
Node temp = Graph[min_index];
/* Now Iterate over all it's Neighbours and update the values if the
(Shortest Distance to this Node) + (Edge Weight between this Node and Neighouring Node)
is less than the Shortest Distance to the neighbour in Distance Array */
while(temp != null){
if(dist[temp.val] > dist[min_index] + temp.weight && !visited[temp.val]){
dist[temp.val] = dist[min_index] + temp.weight;
}
temp = temp.next;
}
}
/* Printing the Shortest Distances from the Start Node to all the other Nodes */
for(int i = 0;i<num;i++){
System.out.print(dist[i] + " ");
}
System.out.println("");
scan.close();
}
}