/**
* 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> adj) {
boolean[] visited = new boolean[n];
PriorityQueue
> 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
> 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
}
}