Skip to content

Add Prims Algorithm using Greedy Technique to find Minimum Cost Spanning Trees #6069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

vijay227799
Copy link

This Java code implements Prim's Algorithm to find the Minimum Spanning Tree (MST) of a given weighted graph represented by a cost matrix. It reads the cost matrix and number of vertices from user input, then calculates and displays the minimum cost edges that form the MST along with the total minimum cost.

The algorithm uses a greedy approach, beginning from an initial vertex and progressively adding the shortest edge that connects a new vertex to the growing MST. The pr function performs the core operations, updating the minimum cost and the list of nearest vertices.

Test Case:
Input:
Number of vertices: 4
Cost matrix:
0 2 0 6
2 0 3 8
0 3 0 0
6 8 0 0
Output
Enter the number of vertices:
4
Enter the cost matrix:
Min Tree edges are
1: Minimum edge is <1, 2> Cost: 2
2: Minimum edge is <2, 3> Cost: 3
3: Minimum edge is <1, 4> Cost: 6
Minimum cost: 11

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@siriak
Copy link
Member

siriak commented Oct 30, 2024

It's already implemented here

@siriak siriak closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants