Skip to content

Commit 01637e9

Browse files
committed
Updated PrismAlgorithm
1 parent e385da2 commit 01637e9

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

src/main/java/com/thealgorithms/graph/PrismAlgorithm.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,87 @@
11
package com.thealgorithms.graph;
22

3-
import java.util.*;
3+
import java.util.ArrayList;
4+
import java.util.PriorityQueue;
45

56

7+
/**
8+
* A helper class representing an edge with its
9+
* associated weight and the connected node.
10+
*/
11+
class Pair {
12+
int node;
13+
int weight;
14+
15+
/**
16+
* Constructs a Pair object to hold an edge's weight and node.
17+
*
18+
* @param weight The weight of the edge.
19+
* @param node The target node connected by the edge.
20+
*/
21+
public Pair(int weight, int node) {
22+
this.node = node;
23+
this.weight = weight;
24+
}
25+
}
626

727
/**
8-
* This class provides a method to compute the Minimum Spanning Tree (MST)
9-
* weight using Prim's Algorithm without any custom helper class.
28+
* This class provides a method to compute the weight of the
29+
* Minimum Spanning Tree (MST) of a graph using Prim's Algorithm.
1030
*/
1131
public class PrismAlgorithm {
1232

1333
/**
1434
* Computes the total weight of the Minimum Spanning Tree (MST)
15-
* for a given undirected, weighted graph using Prim's Algorithm.
35+
* for a given undirected, weighted graph.
36+
*
37+
* <p>The algorithm uses a PriorityQueue (min-heap) to always pick
38+
* the edge with the smallest weight that connects a new vertex to
39+
* the growing MST. It ensures that no cycles are formed.</p>
1640
*
1741
* @param V Number of vertices in the graph.
1842
* @param adj Adjacency list representation of the graph.
19-
* Each entry: {adjacentNode, edgeWeight}.
43+
* For each node, the adjacency list contains a list of
44+
* {adjacentNode, edgeWeight}.
2045
* @return The sum of the edge weights in the MST.
2146
*
22-
* <p>Time Complexity: O(E log V)</p>
23-
* <p>Space Complexity: O(V + E)</p>
47+
* <p>Time Complexity: O(E log V) where E is the number of edges
48+
* and V is the number of vertices.</p>
49+
* <p>Space Complexity: O(V + E) due to adjacency list and visited array.</p>
2450
*/
2551
static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj) {
2652

27-
// Min-heap storing {weight, node}
28-
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
53+
// Min-heap to pick edge with the smallest weight
54+
PriorityQueue<Pair> pq = new PriorityQueue<>((x, y) -> x.weight - y.weight);
2955

30-
int[] visited = new int[V]; // visited array
31-
int mstWeightSum = 0;
56+
// Array to keep track of visited vertices
57+
int[] visited = new int[V];
58+
59+
// Start with node 0 (arbitrary choice), with edge weight = 0
60+
pq.add(new Pair(0, 0));
3261

33-
// Start from node 0, with weight = 0
34-
pq.add(new int[]{0, 0});
62+
int mstWeightSum = 0;
3563

64+
// Process nodes until the priority queue is empty
3665
while (!pq.isEmpty()) {
37-
int[] current = pq.poll();
38-
int weight = current[0];
39-
int node = current[1];
66+
Pair current = pq.poll();
67+
int weight = current.weight;
68+
int node = current.node;
4069

70+
// Skip if the node is already included in MST
4171
if (visited[node] == 1) continue;
4272

73+
// Include the node in MST
4374
visited[node] = 1;
4475
mstWeightSum += weight;
4576

46-
// Explore adjacent edges
47-
for (ArrayList<Integer> edge : adj.get(node)) {
48-
int adjNode = edge.get(0);
49-
int edgeWeight = edge.get(1);
77+
// 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);
5081

82+
// Only consider unvisited nodes
5183
if (visited[adjNode] == 0) {
52-
pq.add(new int[]{edgeWeight, adjNode});
84+
pq.add(new Pair(edgeWeight, adjNode));
5385
}
5486
}
5587
}

0 commit comments

Comments
 (0)