Skip to content

Commit f01d7fa

Browse files
dijikstras algorithm
1 parent 9c663b4 commit f01d7fa

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

dijkstrasAlgorithm.java

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Java Program to Implement Dijkstra's Algorithm
2+
// Using Priority Queue
3+
4+
5+
import java.util.*;
6+
7+
8+
public class GFG {
9+
10+
11+
private int dist[];
12+
private Set<Integer> settled;
13+
private PriorityQueue<Node> pq;
14+
15+
private int V;
16+
List<List<Node> > adj;
17+
18+
// Constructor of this class
19+
public GFG(int V)
20+
{
21+
22+
// This keyword refers to current object itself
23+
this.V = V;
24+
dist = new int[V];
25+
settled = new HashSet<Integer>();
26+
pq = new PriorityQueue<Node>(V, new Node());
27+
}
28+
29+
// Method 1
30+
// Dijkstra's Algorithm
31+
public void dijkstra(List<List<Node> > adj, int src)
32+
{
33+
this.adj = adj;
34+
35+
for (int i = 0; i < V; i++)
36+
dist[i] = Integer.MAX_VALUE;
37+
38+
// Add source node to the priority queue
39+
pq.add(new Node(src, 0));
40+
41+
// Distance to the source is 0
42+
dist[src] = 0;
43+
44+
while (settled.size() != V) {
45+
46+
// Terminating ondition check when
47+
// the priority queue is empty, return
48+
if (pq.isEmpty())
49+
return;
50+
51+
// Removing the minimum distance node
52+
// from the priority queue
53+
int u = pq.remove().node;
54+
55+
// Adding the node whose distance is
56+
// finalized
57+
if (settled.contains(u))
58+
59+
// Continue keyword skips exwcution for
60+
// following check
61+
continue;
62+
63+
// We don't have to call e_Neighbors(u)
64+
// if u is already present in the settled set.
65+
settled.add(u);
66+
67+
e_Neighbours(u);
68+
}
69+
}
70+
71+
// Method 2
72+
// To process all the neighbours
73+
// of the passed node
74+
private void e_Neighbours(int u)
75+
{
76+
77+
int edgeDistance = -1;
78+
int newDistance = -1;
79+
80+
// All the neighbors of v
81+
for (int i = 0; i < adj.get(u).size(); i++) {
82+
Node v = adj.get(u).get(i);
83+
84+
// If current node hasn't already been processed
85+
if (!settled.contains(v.node)) {
86+
edgeDistance = v.cost;
87+
newDistance = dist[u] + edgeDistance;
88+
89+
// If new distance is cheaper in cost
90+
if (newDistance < dist[v.node])
91+
dist[v.node] = newDistance;
92+
93+
// Add the current node to the queue
94+
pq.add(new Node(v.node, dist[v.node]));
95+
}
96+
}
97+
}
98+
99+
// Main driver method
100+
public static void main(String arg[])
101+
{
102+
103+
int V = 5;
104+
int source = 0;
105+
106+
// Adjacency list representation of the
107+
// connected edges by declaring List class object
108+
// Declaring object of type List<Node>
109+
List<List<Node> > adj
110+
= new ArrayList<List<Node> >();
111+
112+
// Initialize list for every node
113+
for (int i = 0; i < V; i++) {
114+
List<Node> item = new ArrayList<Node>();
115+
adj.add(item);
116+
}
117+
118+
// Inputs for the GFG(dpq) graph
119+
adj.get(0).add(new Node(1, 9));
120+
adj.get(0).add(new Node(2, 6));
121+
adj.get(0).add(new Node(3, 5));
122+
adj.get(0).add(new Node(4, 3));
123+
124+
adj.get(2).add(new Node(1, 2));
125+
adj.get(2).add(new Node(3, 4));
126+
127+
// Calculating the single source shortest path
128+
GFG dpq = new GFG(V);
129+
dpq.dijkstra(adj, source);
130+
131+
// Printing the shortest path to all the nodes
132+
// from the source node
133+
System.out.println("The shorted path from node :");
134+
135+
for (int i = 0; i < dpq.dist.length; i++)
136+
System.out.println(source + " to " + i + " is "
137+
+ dpq.dist[i]);
138+
}
139+
}
140+
141+
// Class 2
142+
// Helper class implementing Comparator interface
143+
// Representing a node in the graph
144+
class Node implements Comparator<Node> {
145+
146+
// Member variables of this class
147+
public int node;
148+
public int cost;
149+
150+
// Constructors of this class
151+
152+
// Constructor 1
153+
public Node() {}
154+
155+
// Constructor 2
156+
public Node(int node, int cost)
157+
{
158+
159+
// This keyword refers to current instance itself
160+
this.node = node;
161+
this.cost = cost;
162+
}
163+
164+
// Method 1
165+
@Override public int compare(Node node1, Node node2)
166+
{
167+
168+
if (node1.cost < node2.cost)
169+
return -1;
170+
171+
if (node1.cost > node2.cost)
172+
return 1;
173+
174+
return 0;
175+
}
176+
}

0 commit comments

Comments
 (0)