Skip to content

Conversation

ShristiC7
Copy link

@ShristiC7 ShristiC7 commented Oct 16, 2025

Describe your change:

  • Add an algorithm — Prim’s Minimum Spanning Tree (MST) algorithm in Python.

This implementation demonstrates how to construct a MST for a connected, undirected, weighted graph using Prim’s algorithm. The code is accompanied by doctests to verify correctness and includes type hints for all functions and parameters.

Reference: Prim’s algorithm - Wikipedia


Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work — I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Added Prim's Algorithm #13539 .

Example Doctest:

>>> graph = {
...     'A': {'B': 2, 'C': 3},
...     'B': {'A': 2, 'C': 1},
...     'C': {'A': 3, 'B': 1}
... }
>>> prim_mst(graph)
{'B': 'C', 'C': 'B', 'A': 'B'}

### 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
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 16, 2025
@ShristiC7
Copy link
Author

ShristiC7 commented Oct 16, 2025

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 16, 2025
@cclauss
Copy link
Member

cclauss commented Oct 16, 2025

  • graphs/minimum_spanning_tree_prims.py
  • graphs/minimum_spanning_tree_prims2.py
  • graphs/prim.py

@cclauss cclauss closed this Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants