From 6f9084ccc83d1b75fddcaf316ab1c39bd35cb766 Mon Sep 17 00:00:00 2001 From: Shristi Choudhary <148475072+ShristiC7@users.noreply.github.com> Date: Fri, 10 Oct 2025 06:38:50 +0530 Subject: [PATCH 1/4] Create Prim's_algorithm.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Prim’s Algorithm - **Language:** C++ - **Description:** Constructs a Minimum Spanning Tree (MST) by starting with one vertex and repeatedly adding the smallest possible edge connecting a vertex inside the MST to one outside it. - **Complexity:** O(E log V) - **Use Case:** Network design, cost minimization --- greedy_methods/Prim's_algorithm.py | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 greedy_methods/Prim's_algorithm.py diff --git a/greedy_methods/Prim's_algorithm.py b/greedy_methods/Prim's_algorithm.py new file mode 100644 index 000000000000..a9bdec4824c7 --- /dev/null +++ b/greedy_methods/Prim's_algorithm.py @@ -0,0 +1,64 @@ +// Prim's Algorithm - Minimum Spanning Tree (MST) +// Language: C++ +// Category: Greedy Algorithms + +#include +#include +#include +#include + +using namespace std; + +void primMST(int V, vector>> &adj) { + vector key(V, INT_MAX); + vector parent(V, -1); + vector inMST(V, false); + + priority_queue, vector>, greater>> pq; + + // Start from vertex 0 + key[0] = 0; + pq.push({0, 0}); + + while (!pq.empty()) { + int u = pq.top().second; + pq.pop(); + + inMST[u] = true; + + for (auto &[v, weight] : adj[u]) { + if (!inMST[v] && weight < key[v]) { + key[v] = weight; + pq.push({key[v], v}); + parent[v] = u; + } + } + } + + cout << "Edges in MST:\n"; + int totalWeight = 0; + for (int i = 1; i < V; ++i) { + cout << parent[i] << " - " << i << " (" << key[i] << ")\n"; + totalWeight += key[i]; + } + cout << "Total Weight = " << totalWeight << endl; +} + +int main() { + int V, E; + cout << "Enter number of vertices and edges: "; + cin >> V >> E; + + vector>> adj(V); + cout << "Enter edges (u v w):\n"; + for (int i = 0; i < E; ++i) { + int u, v, w; + cin >> u >> v >> w; + adj[u].push_back({v, w}); + adj[v].push_back({u, w}); + } + + primMST(V, adj); + + return 0; +} From c17b229ff42d00d0f15d55ebbc72f23b31cf0ce2 Mon Sep 17 00:00:00 2001 From: Shristi Choudhary <148475072+ShristiC7@users.noreply.github.com> Date: Fri, 10 Oct 2025 06:48:07 +0530 Subject: [PATCH 2/4] Created Prim's_algorithm.cpp --- greedy_methods/{Prim's_algorithm.py => Prim's_algorithm.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_methods/{Prim's_algorithm.py => Prim's_algorithm.cpp} (100%) diff --git a/greedy_methods/Prim's_algorithm.py b/greedy_methods/Prim's_algorithm.cpp similarity index 100% rename from greedy_methods/Prim's_algorithm.py rename to greedy_methods/Prim's_algorithm.cpp From bab3b968db09c54ac4f643ae23cbc6a8829bb2d6 Mon Sep 17 00:00:00 2001 From: Shristi Choudhary <148475072+ShristiC7@users.noreply.github.com> Date: Fri, 17 Oct 2025 02:42:37 +0530 Subject: [PATCH 3/4] Update and rename Prim's_algorithm.cpp to Prim's_algorithm.py --- greedy_methods/Prim's_algorithm.cpp | 64 ----------------------------- greedy_methods/Prim's_algorithm.py | 46 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 64 deletions(-) delete mode 100644 greedy_methods/Prim's_algorithm.cpp create mode 100644 greedy_methods/Prim's_algorithm.py diff --git a/greedy_methods/Prim's_algorithm.cpp b/greedy_methods/Prim's_algorithm.cpp deleted file mode 100644 index a9bdec4824c7..000000000000 --- a/greedy_methods/Prim's_algorithm.cpp +++ /dev/null @@ -1,64 +0,0 @@ -// Prim's Algorithm - Minimum Spanning Tree (MST) -// Language: C++ -// Category: Greedy Algorithms - -#include -#include -#include -#include - -using namespace std; - -void primMST(int V, vector>> &adj) { - vector key(V, INT_MAX); - vector parent(V, -1); - vector inMST(V, false); - - priority_queue, vector>, greater>> pq; - - // Start from vertex 0 - key[0] = 0; - pq.push({0, 0}); - - while (!pq.empty()) { - int u = pq.top().second; - pq.pop(); - - inMST[u] = true; - - for (auto &[v, weight] : adj[u]) { - if (!inMST[v] && weight < key[v]) { - key[v] = weight; - pq.push({key[v], v}); - parent[v] = u; - } - } - } - - cout << "Edges in MST:\n"; - int totalWeight = 0; - for (int i = 1; i < V; ++i) { - cout << parent[i] << " - " << i << " (" << key[i] << ")\n"; - totalWeight += key[i]; - } - cout << "Total Weight = " << totalWeight << endl; -} - -int main() { - int V, E; - cout << "Enter number of vertices and edges: "; - cin >> V >> E; - - vector>> adj(V); - cout << "Enter edges (u v w):\n"; - for (int i = 0; i < E; ++i) { - int u, v, w; - cin >> u >> v >> w; - adj[u].push_back({v, w}); - adj[v].push_back({u, w}); - } - - primMST(V, adj); - - return 0; -} diff --git a/greedy_methods/Prim's_algorithm.py b/greedy_methods/Prim's_algorithm.py new file mode 100644 index 000000000000..c8fb51f0facb --- /dev/null +++ b/greedy_methods/Prim's_algorithm.py @@ -0,0 +1,46 @@ +from typing import Dict, Any +import heapq + +def prim_mst(graph: Dict[Any, Dict[Any, int]]) -> Dict[Any, Any]: + """ + Generate the Minimum Spanning Tree (MST) using Prim's algorithm. + + Args: + graph (Dict[Any, Dict[Any, int]]): Adjacency dictionary of the graph where keys are nodes and + values are dictionaries of neighbor nodes with edge weights. + + Returns: + Dict[Any, Any]: A dictionary representing the MST with each node pointing to its parent in the MST. + + Example: + >>> graph = { + ... 'A': {'B': 2, 'C': 3}, + ... 'B': {'A': 2, 'C': 1}, + ... 'C': {'A': 3, 'B': 1} + ... } + >>> prim_mst(graph) + {'A': 'B', 'B': 'C', 'C': 'B'} + """ + if not graph: + return {} + + start_node = next(iter(graph)) + visited = set([start_node]) + edges = [(weight, start_node, to) for to, weight in graph[start_node].items()] + heapq.heapify(edges) + mst = {} + + while edges: + weight, frm, to = heapq.heappop(edges) + if to not in visited: + visited.add(to) + mst[to] = frm + for next_to, next_weight in graph[to].items(): + if next_to not in visited: + heapq.heappush(edges, (next_weight, to, next_to)) + + return mst + +if __name__ == "__main__": + import doctest + doctest.testmod() From 3d1221f399ccbb998bb6e025e16b19fdad367661 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 21:13:46 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/Prim's_algorithm.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/greedy_methods/Prim's_algorithm.py b/greedy_methods/Prim's_algorithm.py index c8fb51f0facb..eaae76f6611f 100644 --- a/greedy_methods/Prim's_algorithm.py +++ b/greedy_methods/Prim's_algorithm.py @@ -1,6 +1,7 @@ from typing import Dict, Any import heapq + def prim_mst(graph: Dict[Any, Dict[Any, int]]) -> Dict[Any, Any]: """ Generate the Minimum Spanning Tree (MST) using Prim's algorithm. @@ -41,6 +42,8 @@ def prim_mst(graph: Dict[Any, Dict[Any, int]]) -> Dict[Any, Any]: return mst + if __name__ == "__main__": import doctest + doctest.testmod()