-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathPrims.java
More file actions
111 lines (96 loc) · 2.75 KB
/
Prims.java
File metadata and controls
111 lines (96 loc) · 2.75 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.thealgorithms.graph;
import java.util.*;
/**
* Implementation of Prim's Algorithm for finding the Minimum Spanning Tree (MST)
* of a connected, undirected, weighted graph.
*
* Prim’s algorithm builds the MST by always choosing the minimum weight edge
* that connects a vertex in the MST to a vertex outside it.
*
* Time Complexity: O(E log V)
* Space Complexity: O(V)
*
* https://en.wikipedia.org/wiki/Prim%27s_algorithm
*
* Example:
* Graph:
* A - B (4)
* A - C (3)
* B - C (1)
* B - D (2)
* C - D (4)
*
* MST edges:
* B - C (1)
* B - D (2)
* A - C (3)
*
* @author OpenAI
*/
public final class Prims {
private Prims() {
}
/**
* Represents an edge in the graph.
*/
private static class Edge {
String to;
int weight;
Edge(String to, int weight) {
this.to = to;
this.weight = weight;
}
}
/**
* Graph represented as adjacency list.
*/
public static class Graph {
private final Map<String, List<Edge>> adj = new LinkedHashMap<>();
/**
* Adds an undirected weighted edge between u and v.
*/
public void addEdge(String u, String v, int weight) {
adj.computeIfAbsent(u, k -> new ArrayList<>()).add(new Edge(v, weight));
adj.computeIfAbsent(v, k -> new ArrayList<>()).add(new Edge(u, weight));
}
}
/**
* Runs Prim’s Algorithm to compute the MST.
*
* @param graph the input graph
* @param start the starting vertex
* @return list of edges representing the MST
*/
public static List<String> primsMST(Graph graph, String start) {
List<String> mstEdges = new ArrayList<>();
Set<String> visited = new HashSet<>();
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
visited.add(start);
pq.addAll(graph.adj.get(start));
while (!pq.isEmpty()) {
Edge edge = pq.poll();
if (visited.contains(edge.to)) continue;
visited.add(edge.to);
mstEdges.add(edge.to);
for (Edge next : graph.adj.get(edge.to)) {
if (!visited.contains(next.to)) {
pq.offer(next);
}
}
}
return mstEdges;
}
/**
* Example usage.
*/
public static void main(String[] args) {
Graph graph = new Graph();
graph.addEdge("A", "B", 4);
graph.addEdge("A", "C", 3);
graph.addEdge("B", "C", 1);
graph.addEdge("B", "D", 2);
graph.addEdge("C", "D", 4);
List<String> mst = primsMST(graph, "A");
System.out.println("MST vertices (order added): " + mst);
}
}