33import java .util .ArrayList ;
44import java .util .PriorityQueue ;
55
6-
76/**
8- * A helper class representing an edge with its
9- * associated weight and the connected node .
7+ * This class provides a method to compute the weight of the
8+ * Minimum Spanning Tree (MST) of a graph using Prim's Algorithm .
109 */
11- class Pair {
12- int node ;
13- int weight ;
10+ public final class PrimAlgorithm {
11+
12+ private PrimAlgorithm () {
13+ throw new UnsupportedOperationException ("Utility class" );
14+ }
1415
1516 /**
16- * Constructs a Pair object to hold an edge's weight and node.
17+ * Helper record representing an edge with its associated weight and node.
1718 *
18- * @param weight The weight of the edge.
19- * @param node The target node connected by the edge.
19+ * @param node the target node connected by the edge
20+ * @param weight the weight of the edge
2021 */
21- public Pair (int weight , int node ) {
22- this .node = node ;
23- this .weight = weight ;
24- }
25- }
26-
27- /**
28- * This class provides a method to compute the weight of the
29- * Minimum Spanning Tree (MST) of a graph using Prim's Algorithm.
30- */
31- public class PrismAlgorithm {
22+ private record Pair (int node , int weight ) {}
3223
3324 /**
3425 * Computes the total weight of the Minimum Spanning Tree (MST)
@@ -38,23 +29,22 @@ public class PrismAlgorithm {
3829 * the edge with the smallest weight that connects a new vertex to
3930 * the growing MST. It ensures that no cycles are formed.</p>
4031 *
41- * @param V Number of vertices in the graph.
42- * @param adj Adjacency list representation of the graph.
43- * For each node, the adjacency list contains a list of
44- * {adjacentNode, edgeWeight}.
45- * @return The sum of the edge weights in the MST.
32+ * @param V number of vertices in the graph
33+ * @param adj adjacency list representation of the graph
34+ * for each node, the adjacency list contains a list of
35+ * {adjacentNode, edgeWeight}
36+ * @return the sum of the edge weights in the MST
4637 *
47- * <p>Time Complexity: O(E log V) where E is the number of edges
38+ * <p>Time Complexity: O(E log V), where E is the number of edges
4839 * and V is the number of vertices.</p>
4940 * <p>Space Complexity: O(V + E) due to adjacency list and visited array.</p>
5041 */
51- static int spanningTree (int V , ArrayList <ArrayList <ArrayList <Integer >>> adj ) {
52-
42+ public static int spanningTree (int V , ArrayList <ArrayList <ArrayList <Integer >>> adj ) {
5343 // Min-heap to pick edge with the smallest weight
5444 PriorityQueue <Pair > pq = new PriorityQueue <>((x , y ) -> x .weight - y .weight );
5545
5646 // Array to keep track of visited vertices
57- int [] visited = new int [V ];
47+ boolean [] visited = new boolean [V ];
5848
5949 // Start with node 0 (arbitrary choice), with edge weight = 0
6050 pq .add (new Pair (0 , 0 ));
@@ -64,24 +54,26 @@ static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj) {
6454 // Process nodes until the priority queue is empty
6555 while (!pq .isEmpty ()) {
6656 Pair current = pq .poll ();
67- int weight = current .weight ;
68- int node = current .node ;
57+ int node = current .node () ;
58+ int weight = current .weight () ;
6959
7060 // Skip if the node is already included in MST
71- if (visited [node ] == 1 ) continue ;
61+ if (visited [node ]) {
62+ continue ;
63+ }
7264
7365 // Include the node in MST
74- visited [node ] = 1 ;
66+ visited [node ] = true ;
7567 mstWeightSum += weight ;
7668
7769 // Traverse all adjacent edges
78- for (int i = 0 ; i < adj .get (node ). size (); i ++ ) {
79- int adjNode = adj . get ( node ). get ( i ) .get (0 );
80- int edgeWeight = adj . get ( node ). get ( i ) .get (1 );
70+ for (ArrayList < Integer > edge : adj .get (node )) {
71+ int adjNode = edge .get (0 );
72+ int edgeWeight = edge .get (1 );
8173
8274 // Only consider unvisited nodes
83- if (visited [adjNode ] == 0 ) {
84- pq .add (new Pair (edgeWeight , adjNode ));
75+ if (! visited [adjNode ]) {
76+ pq .add (new Pair (adjNode , edgeWeight ));
8577 }
8678 }
8779 }
0 commit comments