Skip to content

Commit 255a57a

Browse files
Merge pull request #418 from guptushar27/patch-4
Create DijkstraAlgorithm2.java
2 parents a9f9899 + a56c385 commit 255a57a

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
2+
import java.util.*;
3+
4+
/**
5+
* Algorithm Name : Dijkstra Algorithm
6+
* Programming Language: Java
7+
* Category: Graph Algorithms
8+
* Difficulty Level: Medium (Intermediate)
9+
*
10+
* Problem Statement:
11+
* Implement Dijkstra’s Algorithm to find the shortest path from a single
12+
* source vertex to all other vertices in a weighted graph (non-negative edge weights)
13+
* using a priority queue (min-heap) for optimization.
14+
*
15+
* Requirements:
16+
* - Input:
17+
* Number of vertices: V
18+
* Number of edges: E
19+
* Edges with weights (u, v, w)
20+
* Starting vertex: s
21+
* - Output:
22+
* Shortest distance from source to all vertices
23+
*
24+
* Complexity:
25+
* - Time Complexity: O((V + E) * log V)
26+
* - Space Complexity: O(V + E)
27+
*
28+
* Additional Notes:
29+
* - Negative edge weights are not supported (Dijkstra assumes non-negative edges).
30+
*/
31+
public class DijkstraAlgorithm2 {
32+
33+
// Inner class to represent a graph edge (destination vertex and weight)
34+
static class Edge {
35+
int vertex, weight;
36+
Edge(int vertex, int weight) {
37+
this.vertex = vertex;
38+
this.weight = weight;
39+
}
40+
}
41+
42+
/**
43+
* Dijkstra’s Algorithm implementation.
44+
* Uses a PriorityQueue (min-heap) to efficiently get the vertex with the smallest distance.
45+
*/
46+
public static int[] dijkstra(List<List<Edge>> graph, int source, int V) {
47+
// Initialize distance array with "infinity" (here, Integer.MAX_VALUE)
48+
int[] dist = new int[V];
49+
Arrays.fill(dist, Integer.MAX_VALUE);
50+
dist[source] = 0;
51+
52+
// Min-heap: stores pairs (distance, vertex)
53+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
54+
pq.offer(new int[]{0, source}); // distance = 0, vertex = source
55+
56+
while (!pq.isEmpty()) {
57+
int[] current = pq.poll();
58+
int currDist = current[0];
59+
int u = current[1];
60+
61+
// Skip if we have already found a better distance
62+
if (currDist > dist[u]) continue;
63+
64+
// Traverse all adjacent vertices
65+
for (Edge edge : graph.get(u)) {
66+
int v = edge.vertex;
67+
int weight = edge.weight;
68+
69+
// Relaxation step: if found shorter path to v, update it
70+
if (dist[u] + weight < dist[v]) {
71+
dist[v] = dist[u] + weight;
72+
pq.offer(new int[]{dist[v], v});
73+
}
74+
}
75+
}
76+
return dist;
77+
}
78+
79+
/**
80+
* Utility function to add an edge to the adjacency list.
81+
* Since this example is for a directed graph — remove one addEdge call below for undirected.
82+
*/
83+
public static void addEdge(List<List<Edge>> graph, int u, int v, int w) {
84+
graph.get(u).add(new Edge(v, w));
85+
}
86+
87+
/**
88+
* Sample test cases:
89+
* - Small connected graph
90+
* - Graph with multiple paths
91+
* - Single-node graph
92+
*/
93+
public static void main(String[] args) {
94+
// Example 1: Small connected graph
95+
int V1 = 5;
96+
List<List<Edge>> graph1 = new ArrayList<>();
97+
for (int i = 0; i < V1; i++) graph1.add(new ArrayList<>());
98+
99+
addEdge(graph1, 0, 1, 10);
100+
addEdge(graph1, 0, 4, 5);
101+
addEdge(graph1, 1, 2, 1);
102+
addEdge(graph1, 4, 1, 3);
103+
addEdge(graph1, 4, 2, 9);
104+
addEdge(graph1, 2, 3, 4);
105+
addEdge(graph1, 4, 3, 2);
106+
107+
int source1 = 0;
108+
int[] dist1 = dijkstra(graph1, source1, V1);
109+
System.out.println("Shortest distances from vertex " + source1 + ": " + Arrays.toString(dist1));
110+
System.out.println();
111+
112+
// Example 2: Graph with multiple paths
113+
int V2 = 4;
114+
List<List<Edge>> graph2 = new ArrayList<>();
115+
for (int i = 0; i < V2; i++) graph2.add(new ArrayList<>());
116+
117+
addEdge(graph2, 0, 1, 1);
118+
addEdge(graph2, 0, 2, 4);
119+
addEdge(graph2, 1, 2, 2);
120+
addEdge(graph2, 1, 3, 6);
121+
addEdge(graph2, 2, 3, 3);
122+
123+
int source2 = 0;
124+
int[] dist2 = dijkstra(graph2, source2, V2);
125+
System.out.println("Shortest distances from vertex " + source2 + ": " + Arrays.toString(dist2));
126+
System.out.println();
127+
128+
// Example 3: Single-node graph
129+
int V3 = 1;
130+
List<List<Edge>> graph3 = new ArrayList<>();
131+
for (int i = 0; i < V3; i++) graph3.add(new ArrayList<>());
132+
133+
int source3 = 0;
134+
int[] dist3 = dijkstra(graph3, source3, V3);
135+
System.out.println("Shortest distances from vertex " + source3 + ": " + Arrays.toString(dist3));
136+
}
137+
}

0 commit comments

Comments
 (0)