|
1 | 1 | package com.thealgorithms.graph; |
2 | 2 |
|
3 | | -import java.util.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.PriorityQueue; |
4 | 5 |
|
5 | 6 |
|
| 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 | +} |
6 | 26 |
|
7 | 27 | /** |
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. |
10 | 30 | */ |
11 | 31 | public class PrismAlgorithm { |
12 | 32 |
|
13 | 33 | /** |
14 | 34 | * 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> |
16 | 40 | * |
17 | 41 | * @param V Number of vertices in the graph. |
18 | 42 | * @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}. |
20 | 45 | * @return The sum of the edge weights in the MST. |
21 | 46 | * |
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> |
24 | 50 | */ |
25 | 51 | static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj) { |
26 | 52 |
|
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); |
29 | 55 |
|
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)); |
32 | 61 |
|
33 | | - // Start from node 0, with weight = 0 |
34 | | - pq.add(new int[]{0, 0}); |
| 62 | + int mstWeightSum = 0; |
35 | 63 |
|
| 64 | + // Process nodes until the priority queue is empty |
36 | 65 | 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; |
40 | 69 |
|
| 70 | + // Skip if the node is already included in MST |
41 | 71 | if (visited[node] == 1) continue; |
42 | 72 |
|
| 73 | + // Include the node in MST |
43 | 74 | visited[node] = 1; |
44 | 75 | mstWeightSum += weight; |
45 | 76 |
|
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); |
50 | 81 |
|
| 82 | + // Only consider unvisited nodes |
51 | 83 | if (visited[adjNode] == 0) { |
52 | | - pq.add(new int[]{edgeWeight, adjNode}); |
| 84 | + pq.add(new Pair(edgeWeight, adjNode)); |
53 | 85 | } |
54 | 86 | } |
55 | 87 | } |
|
0 commit comments