From e385da26d9ea2936ef24a5e361c6d19596ad1247 Mon Sep 17 00:00:00 2001 From: Richa Kiran <64418209+richk21@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:29:15 +0530 Subject: [PATCH 1/4] Implemented PrismAlgorithm --- .../thealgorithms/graph/PrismAlgorithm.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/PrismAlgorithm.java diff --git a/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java b/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java new file mode 100644 index 000000000000..584dfd6ee965 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java @@ -0,0 +1,59 @@ +package com.thealgorithms.graph; + +import java.util.*; + + + +/** + * This class provides a method to compute the Minimum Spanning Tree (MST) + * weight using Prim's Algorithm without any custom helper class. + */ +public class PrismAlgorithm { + + /** + * Computes the total weight of the Minimum Spanning Tree (MST) + * for a given undirected, weighted graph using Prim's Algorithm. + * + * @param V Number of vertices in the graph. + * @param adj Adjacency list representation of the graph. + * Each entry: {adjacentNode, edgeWeight}. + * @return The sum of the edge weights in the MST. + * + *

Time Complexity: O(E log V)

+ *

Space Complexity: O(V + E)

+ */ + static int spanningTree(int V, ArrayList>> adj) { + + // Min-heap storing {weight, node} + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + + int[] visited = new int[V]; // visited array + int mstWeightSum = 0; + + // Start from node 0, with weight = 0 + pq.add(new int[]{0, 0}); + + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int weight = current[0]; + int node = current[1]; + + if (visited[node] == 1) continue; + + visited[node] = 1; + mstWeightSum += weight; + + // Explore adjacent edges + for (ArrayList edge : adj.get(node)) { + int adjNode = edge.get(0); + int edgeWeight = edge.get(1); + + if (visited[adjNode] == 0) { + pq.add(new int[]{edgeWeight, adjNode}); + } + } + } + + return mstWeightSum; + } +} From 01637e95a21d0224842ae8a41db7951baf1e8b2f Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Thu, 2 Oct 2025 00:27:20 +0530 Subject: [PATCH 2/4] Updated PrismAlgorithm --- .../thealgorithms/graph/PrismAlgorithm.java | 74 +++++++++++++------ 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java b/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java index 584dfd6ee965..b3be56265ffa 100644 --- a/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java +++ b/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java @@ -1,55 +1,87 @@ package com.thealgorithms.graph; -import java.util.*; +import java.util.ArrayList; +import java.util.PriorityQueue; +/** + * A helper class representing an edge with its + * associated weight and the connected node. + */ +class Pair { + int node; + int weight; + + /** + * Constructs a Pair object to hold an edge's weight and node. + * + * @param weight The weight of the edge. + * @param node The target node connected by the edge. + */ + public Pair(int weight, int node) { + this.node = node; + this.weight = weight; + } +} /** - * This class provides a method to compute the Minimum Spanning Tree (MST) - * weight using Prim's Algorithm without any custom helper class. + * This class provides a method to compute the weight of the + * Minimum Spanning Tree (MST) of a graph using Prim's Algorithm. */ public class PrismAlgorithm { /** * Computes the total weight of the Minimum Spanning Tree (MST) - * for a given undirected, weighted graph using Prim's Algorithm. + * for a given undirected, weighted graph. + * + *

The algorithm uses a PriorityQueue (min-heap) to always pick + * the edge with the smallest weight that connects a new vertex to + * the growing MST. It ensures that no cycles are formed.

* * @param V Number of vertices in the graph. * @param adj Adjacency list representation of the graph. - * Each entry: {adjacentNode, edgeWeight}. + * For each node, the adjacency list contains a list of + * {adjacentNode, edgeWeight}. * @return The sum of the edge weights in the MST. * - *

Time Complexity: O(E log V)

- *

Space Complexity: O(V + E)

+ *

Time Complexity: O(E log V) where E is the number of edges + * and V is the number of vertices.

+ *

Space Complexity: O(V + E) due to adjacency list and visited array.

*/ static int spanningTree(int V, ArrayList>> adj) { - // Min-heap storing {weight, node} - PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + // Min-heap to pick edge with the smallest weight + PriorityQueue pq = new PriorityQueue<>((x, y) -> x.weight - y.weight); - int[] visited = new int[V]; // visited array - int mstWeightSum = 0; + // Array to keep track of visited vertices + int[] visited = new int[V]; + + // Start with node 0 (arbitrary choice), with edge weight = 0 + pq.add(new Pair(0, 0)); - // Start from node 0, with weight = 0 - pq.add(new int[]{0, 0}); + int mstWeightSum = 0; + // Process nodes until the priority queue is empty while (!pq.isEmpty()) { - int[] current = pq.poll(); - int weight = current[0]; - int node = current[1]; + Pair current = pq.poll(); + int weight = current.weight; + int node = current.node; + // Skip if the node is already included in MST if (visited[node] == 1) continue; + // Include the node in MST visited[node] = 1; mstWeightSum += weight; - // Explore adjacent edges - for (ArrayList edge : adj.get(node)) { - int adjNode = edge.get(0); - int edgeWeight = edge.get(1); + // Traverse all adjacent edges + for (int i = 0; i < adj.get(node).size(); i++) { + int adjNode = adj.get(node).get(i).get(0); + int edgeWeight = adj.get(node).get(i).get(1); + // Only consider unvisited nodes if (visited[adjNode] == 0) { - pq.add(new int[]{edgeWeight, adjNode}); + pq.add(new Pair(edgeWeight, adjNode)); } } } From 2a4cff0a46e8486cda5ee239a14611f43741a245 Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Thu, 2 Oct 2025 00:27:20 +0530 Subject: [PATCH 3/4] Updated PrismAlgorithm-II --- ...PrismAlgorithm.java => PrimAlgorithm.java} | 68 ++++++++----------- 1 file changed, 30 insertions(+), 38 deletions(-) rename src/main/java/com/thealgorithms/graph/{PrismAlgorithm.java => PrimAlgorithm.java} (52%) diff --git a/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java b/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java similarity index 52% rename from src/main/java/com/thealgorithms/graph/PrismAlgorithm.java rename to src/main/java/com/thealgorithms/graph/PrimAlgorithm.java index b3be56265ffa..6f6e27a2469f 100644 --- a/src/main/java/com/thealgorithms/graph/PrismAlgorithm.java +++ b/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java @@ -3,32 +3,23 @@ import java.util.ArrayList; import java.util.PriorityQueue; - /** - * A helper class representing an edge with its - * associated weight and the connected node. + * This class provides a method to compute the weight of the + * Minimum Spanning Tree (MST) of a graph using Prim's Algorithm. */ -class Pair { - int node; - int weight; +public final class PrimAlgorithm { + + private PrimAlgorithm() { + throw new UnsupportedOperationException("Utility class"); + } /** - * Constructs a Pair object to hold an edge's weight and node. + * Helper record representing an edge with its associated weight and node. * - * @param weight The weight of the edge. - * @param node The target node connected by the edge. + * @param node the target node connected by the edge + * @param weight the weight of the edge */ - public Pair(int weight, int node) { - this.node = node; - this.weight = weight; - } -} - -/** - * This class provides a method to compute the weight of the - * Minimum Spanning Tree (MST) of a graph using Prim's Algorithm. - */ -public class PrismAlgorithm { + private record Pair(int node, int weight) {} /** * Computes the total weight of the Minimum Spanning Tree (MST) @@ -38,23 +29,22 @@ public class PrismAlgorithm { * the edge with the smallest weight that connects a new vertex to * the growing MST. It ensures that no cycles are formed.

* - * @param V Number of vertices in the graph. - * @param adj Adjacency list representation of the graph. - * For each node, the adjacency list contains a list of - * {adjacentNode, edgeWeight}. - * @return The sum of the edge weights in the MST. + * @param V number of vertices in the graph + * @param adj adjacency list representation of the graph + * for each node, the adjacency list contains a list of + * {adjacentNode, edgeWeight} + * @return the sum of the edge weights in the MST * - *

Time Complexity: O(E log V) where E is the number of edges + *

Time Complexity: O(E log V), where E is the number of edges * and V is the number of vertices.

*

Space Complexity: O(V + E) due to adjacency list and visited array.

*/ - static int spanningTree(int V, ArrayList>> adj) { - + public static int spanningTree(int V, ArrayList>> adj) { // Min-heap to pick edge with the smallest weight PriorityQueue pq = new PriorityQueue<>((x, y) -> x.weight - y.weight); // Array to keep track of visited vertices - int[] visited = new int[V]; + boolean[] visited = new boolean[V]; // Start with node 0 (arbitrary choice), with edge weight = 0 pq.add(new Pair(0, 0)); @@ -64,24 +54,26 @@ static int spanningTree(int V, ArrayList>> adj) { // Process nodes until the priority queue is empty while (!pq.isEmpty()) { Pair current = pq.poll(); - int weight = current.weight; - int node = current.node; + int node = current.node(); + int weight = current.weight(); // Skip if the node is already included in MST - if (visited[node] == 1) continue; + if (visited[node]) { + continue; + } // Include the node in MST - visited[node] = 1; + visited[node] = true; mstWeightSum += weight; // Traverse all adjacent edges - for (int i = 0; i < adj.get(node).size(); i++) { - int adjNode = adj.get(node).get(i).get(0); - int edgeWeight = adj.get(node).get(i).get(1); + for (ArrayList edge : adj.get(node)) { + int adjNode = edge.get(0); + int edgeWeight = edge.get(1); // Only consider unvisited nodes - if (visited[adjNode] == 0) { - pq.add(new Pair(edgeWeight, adjNode)); + if (!visited[adjNode]) { + pq.add(new Pair(adjNode, edgeWeight)); } } } From dd01551c4d7234cb3db7fbc208926c7e1d229a91 Mon Sep 17 00:00:00 2001 From: Shubham Kumar Date: Fri, 3 Oct 2025 19:18:40 +0530 Subject: [PATCH 4/4] Updated PrismAlgorithm-II --- src/main/java/com/thealgorithms/graph/PrimAlgorithm.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java b/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java index 6f6e27a2469f..fe7a70a51a4f 100644 --- a/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java +++ b/src/main/java/com/thealgorithms/graph/PrimAlgorithm.java @@ -19,7 +19,8 @@ private PrimAlgorithm() { * @param node the target node connected by the edge * @param weight the weight of the edge */ - private record Pair(int node, int weight) {} + private record Pair(int node, int weight) { + } /** * Computes the total weight of the Minimum Spanning Tree (MST) @@ -71,7 +72,6 @@ public static int spanningTree(int V, ArrayList>> a int adjNode = edge.get(0); int edgeWeight = edge.get(1); - // Only consider unvisited nodes if (!visited[adjNode]) { pq.add(new Pair(adjNode, edgeWeight)); }