From 54f7120e0d9edec634840570fb8efeb8f2b38d77 Mon Sep 17 00:00:00 2001 From: jyothsnasharma7 Date: Mon, 20 Oct 2025 00:41:25 +0530 Subject: [PATCH 1/3] Added Prim's Algorithm to searches --- .../com/thealgorithms/searches/Prims.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/Prims.java diff --git a/src/main/java/com/thealgorithms/searches/Prims.java b/src/main/java/com/thealgorithms/searches/Prims.java new file mode 100644 index 000000000000..7ca93dd3339d --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/Prims.java @@ -0,0 +1,111 @@ +package com.thealgorithms.graphs; + +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 PrimsAlgorithm { + + private PrimsAlgorithm() { + } + + /** + * 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> 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 primsMST(Graph graph, String start) { + List mstEdges = new ArrayList<>(); + Set visited = new HashSet<>(); + PriorityQueue 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 mst = primsMST(graph, "A"); + System.out.println("MST vertices (order added): " + mst); + } +} From f5b0ba8a85832283c803a778ab39802fe36a9e0c Mon Sep 17 00:00:00 2001 From: jyothsnasharma7 Date: Mon, 20 Oct 2025 01:10:35 +0530 Subject: [PATCH 2/3] Adding Prim's Algorithm to searches --- src/main/java/com/thealgorithms/searches/Prims.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/Prims.java b/src/main/java/com/thealgorithms/searches/Prims.java index 7ca93dd3339d..76a9ed68b2cb 100644 --- a/src/main/java/com/thealgorithms/searches/Prims.java +++ b/src/main/java/com/thealgorithms/searches/Prims.java @@ -29,9 +29,9 @@ * * @author OpenAI */ -public final class PrimsAlgorithm { +public final class Prims { - private PrimsAlgorithm() { + private Prims() { } /** From c881f64e6d42c224f863729cbc18a62d4465d2c7 Mon Sep 17 00:00:00 2001 From: jyothsnasharma7 Date: Mon, 20 Oct 2025 01:25:47 +0530 Subject: [PATCH 3/3] Added Prim's Algorithm to graphs --- src/main/java/com/thealgorithms/{searches => graph}/Prims.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/com/thealgorithms/{searches => graph}/Prims.java (98%) diff --git a/src/main/java/com/thealgorithms/searches/Prims.java b/src/main/java/com/thealgorithms/graph/Prims.java similarity index 98% rename from src/main/java/com/thealgorithms/searches/Prims.java rename to src/main/java/com/thealgorithms/graph/Prims.java index 76a9ed68b2cb..90d0a5dca894 100644 --- a/src/main/java/com/thealgorithms/searches/Prims.java +++ b/src/main/java/com/thealgorithms/graph/Prims.java @@ -1,4 +1,4 @@ -package com.thealgorithms.graphs; +package com.thealgorithms.graph; import java.util.*;