Skip to content

Commit 54f7120

Browse files
Added Prim's Algorithm to searches
1 parent d5289b9 commit 54f7120

File tree

1 file changed

+111
-0
lines changed
  • src/main/java/com/thealgorithms/searches

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.graphs;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Implementation of Prim's Algorithm for finding the Minimum Spanning Tree (MST)
7+
* of a connected, undirected, weighted graph.
8+
*
9+
* Prim’s algorithm builds the MST by always choosing the minimum weight edge
10+
* that connects a vertex in the MST to a vertex outside it.
11+
*
12+
* Time Complexity: O(E log V)
13+
* Space Complexity: O(V)
14+
*
15+
* https://en.wikipedia.org/wiki/Prim%27s_algorithm
16+
*
17+
* Example:
18+
* Graph:
19+
* A - B (4)
20+
* A - C (3)
21+
* B - C (1)
22+
* B - D (2)
23+
* C - D (4)
24+
*
25+
* MST edges:
26+
* B - C (1)
27+
* B - D (2)
28+
* A - C (3)
29+
*
30+
* @author OpenAI
31+
*/
32+
public final class PrimsAlgorithm {
33+
34+
private PrimsAlgorithm() {
35+
}
36+
37+
/**
38+
* Represents an edge in the graph.
39+
*/
40+
private static class Edge {
41+
String to;
42+
int weight;
43+
44+
Edge(String to, int weight) {
45+
this.to = to;
46+
this.weight = weight;
47+
}
48+
}
49+
50+
/**
51+
* Graph represented as adjacency list.
52+
*/
53+
public static class Graph {
54+
private final Map<String, List<Edge>> adj = new LinkedHashMap<>();
55+
56+
/**
57+
* Adds an undirected weighted edge between u and v.
58+
*/
59+
public void addEdge(String u, String v, int weight) {
60+
adj.computeIfAbsent(u, k -> new ArrayList<>()).add(new Edge(v, weight));
61+
adj.computeIfAbsent(v, k -> new ArrayList<>()).add(new Edge(u, weight));
62+
}
63+
}
64+
65+
/**
66+
* Runs Prim’s Algorithm to compute the MST.
67+
*
68+
* @param graph the input graph
69+
* @param start the starting vertex
70+
* @return list of edges representing the MST
71+
*/
72+
public static List<String> primsMST(Graph graph, String start) {
73+
List<String> mstEdges = new ArrayList<>();
74+
Set<String> visited = new HashSet<>();
75+
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
76+
77+
visited.add(start);
78+
pq.addAll(graph.adj.get(start));
79+
80+
while (!pq.isEmpty()) {
81+
Edge edge = pq.poll();
82+
if (visited.contains(edge.to)) continue;
83+
84+
visited.add(edge.to);
85+
mstEdges.add(edge.to);
86+
87+
for (Edge next : graph.adj.get(edge.to)) {
88+
if (!visited.contains(next.to)) {
89+
pq.offer(next);
90+
}
91+
}
92+
}
93+
94+
return mstEdges;
95+
}
96+
97+
/**
98+
* Example usage.
99+
*/
100+
public static void main(String[] args) {
101+
Graph graph = new Graph();
102+
graph.addEdge("A", "B", 4);
103+
graph.addEdge("A", "C", 3);
104+
graph.addEdge("B", "C", 1);
105+
graph.addEdge("B", "D", 2);
106+
graph.addEdge("C", "D", 4);
107+
108+
List<String> mst = primsMST(graph, "A");
109+
System.out.println("MST vertices (order added): " + mst);
110+
}
111+
}

0 commit comments

Comments
 (0)