forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimAlgorithm.java
More file actions
83 lines (69 loc) · 2.69 KB
/
PrimAlgorithm.java
File metadata and controls
83 lines (69 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.thealgorithms.graph;
import java.util.ArrayList;
import java.util.PriorityQueue;
/**
* This class provides a method to compute the weight of the
* Minimum Spanning Tree (MST) of a graph using Prim's Algorithm.
*/
public final class PrimAlgorithm {
private PrimAlgorithm() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Helper record representing an edge with its associated weight and node.
*
* @param node the target node connected by the edge
* @param weight the weight of the edge
*/
private record Pair(int node, int weight) {
}
/**
* Computes the total weight of the Minimum Spanning Tree (MST)
* for a given undirected, weighted graph.
*
* <p>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.</p>
*
* @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
*
* <p>Time Complexity: O(E log V), where E is the number of edges
* and V is the number of vertices.</p>
* <p>Space Complexity: O(V + E) due to adjacency list and visited array.</p>
*/
public static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj) {
// Min-heap to pick edge with the smallest weight
PriorityQueue<Pair> pq = new PriorityQueue<>((x, y) -> x.weight - y.weight);
// Array to keep track of visited vertices
boolean[] visited = new boolean[V];
// Start with node 0 (arbitrary choice), with edge weight = 0
pq.add(new Pair(0, 0));
int mstWeightSum = 0;
// Process nodes until the priority queue is empty
while (!pq.isEmpty()) {
Pair current = pq.poll();
int node = current.node();
int weight = current.weight();
// Skip if the node is already included in MST
if (visited[node]) {
continue;
}
// Include the node in MST
visited[node] = true;
mstWeightSum += weight;
// Traverse all adjacent edges
for (ArrayList<Integer> edge : adj.get(node)) {
int adjNode = edge.get(0);
int edgeWeight = edge.get(1);
if (!visited[adjNode]) {
pq.add(new Pair(adjNode, edgeWeight));
}
}
}
return mstWeightSum;
}
}