forked from IamBisrutPyne/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimsAlgorithm.java
More file actions
75 lines (61 loc) · 2.01 KB
/
PrimsAlgorithm.java
File metadata and controls
75 lines (61 loc) · 2.01 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
/**
* Program Title: Prim's Algorithm for Minimum Spanning Tree (MST)
* Author: [@chaanakyaaM]
* Date: [2025-10-07]
*
* Description: Finds the Minimum Spanning Tree (MST) of a connected, undirected
* weighted graph using a Priority Queue (Min-Heap) to efficiently select the
* next cheapest edge.
* Time Complexity: O(E log V) (where E is edges, V is vertices)
* Space Complexity: O(V + E)
*/
import java.util.*;
class Edge {
int to;
int weight;
Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
public class PrimsAlgorithm {
public static int prim(int n, List<List<Edge>> adj) {
boolean[] visited = new boolean[n];
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
pq.offer(new Edge(0, 0)); // Start from node 0
int mstWeight = 0;
while (!pq.isEmpty()) {
Edge current = pq.poll();
if (visited[current.to]) continue;
visited[current.to] = true;
mstWeight += current.weight;
for (Edge neighbor : adj.get(current.to)) {
if (!visited[neighbor.to]) {
pq.offer(neighbor);
}
}
}
return mstWeight;
}
public static void main(String[] args) {
int n = 5; // number of nodes
List<List<Edge>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
// Adding undirected edges
addEdge(adj, 0, 1, 2);
addEdge(adj, 0, 3, 6);
addEdge(adj, 1, 2, 3);
addEdge(adj, 1, 3, 8);
addEdge(adj, 1, 4, 5);
addEdge(adj, 2, 4, 7);
addEdge(adj, 3, 4, 9);
int result = prim(n, adj);
System.out.println("Total weight of MST: " + result);
}
static void addEdge(List<List<Edge>> adj, int u, int v, int w) {
adj.get(u).add(new Edge(v, w));
adj.get(v).add(new Edge(u, w)); // Since the graph is undirected
}
}