Skip to content

Conversation

@Erennn7
Copy link
Contributor

@Erennn7 Erennn7 commented Oct 18, 2025

PR: Prim's Algorithm for Minimum Spanning Tree (MST)

This PR introduces a fully documented implementation of Prim's Algorithm in R, designed to compute the Minimum Spanning Tree (MST) of a connected, weighted, undirected graph. The algorithm efficiently grows the MST by repeatedly adding the minimum-weight edge connecting the already included vertices to a new vertex.

Overview

Prim's algorithm operates as follows:

  • Start from an arbitrary vertex.
  • Repeatedly select the smallest edge connecting a vertex in the MST to a vertex outside the MST.
  • Add the selected edge and vertex to the MST.
  • Continue until all vertices are included.

The implementation supports both adjacency matrix and edge list representations, includes input validation, and provides formatted MST output.

Features

  • Computes the MST of connected, weighted, undirected graphs.
  • Handles edge cases like disconnected graphs with appropriate warnings.
  • Outputs MST edges, total weight, and parent array.
  • Includes helper functions for printing MST and constructing adjacency matrices from edge lists.
  • Multiple examples provided: basic graphs, complete graphs, edge-list graphs, and disconnected graphs.
  • Comparison notes with Kruskal's algorithm for educational purposes.

Complexity

  • Time Complexity:
    • O(V²) with adjacency matrix (simple implementation)
    • O((V + E) log V) with priority queue (optimized, not implemented here)
  • Space Complexity: O(V) for key, parent, and visited arrays

Applications

  • Network design (telecommunications, computer networks)
  • Circuit design and electrical grids
  • Clustering algorithms in machine learning
  • Transportation and logistics planning
  • Approximation algorithms for NP-hard problems

Demonstration

Run the included examples to see Prim's Algorithm in action:

# Example: Basic 5-vertex graph
graph <- matrix(c(
  0, 2, 0, 6, 0,
  2, 0, 3, 8, 5,
  0, 3, 0, 0, 7,
  6, 8, 0, 0, 9,
  0, 5, 7, 9, 0
), nrow = 5, byrow = TRUE)

mst <- prim_mst(graph)
print_mst(mst, graph)

@Erennn7 Erennn7 requested review from acylam and siriak as code owners October 18, 2025 11:17
Copilot AI review requested due to automatic review settings October 18, 2025 11:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive implementation of Prim's algorithm for computing the Minimum Spanning Tree (MST) of weighted, undirected graphs in R. The implementation includes input validation, helper functions, and multiple examples demonstrating various use cases.

  • Introduces prim_mst() function with O(V²) adjacency matrix implementation
  • Adds utility functions for MST visualization and graph construction from edge lists
  • Provides five detailed examples covering basic graphs, edge lists, disconnected graphs, and complete graphs

@Erennn7
Copy link
Contributor Author

Erennn7 commented Oct 19, 2025

✅ Updated Prim’s MST R implementation
• Fixed roxygen2 documentation syntax: removed colons after @param names for prim_mst(), print_mst(), and create_graph_from_edges() functions.
• No functional changes were made; all examples, logic, and outputs remain unchanged.
• This ensures documentation is compatible with standard roxygen2 processing and avoids warnings/errors when generating Rd files.

@Erennn7
Copy link
Contributor Author

Erennn7 commented Oct 20, 2025

@siriak please have a look

siriak
siriak previously approved these changes Oct 24, 2025
@siriak siriak requested a review from Copilot October 24, 2025 16:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 24, 2025 16:37
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

@siriak siriak enabled auto-merge (squash) October 24, 2025 16:37
@siriak siriak disabled auto-merge October 24, 2025 16:38
@siriak siriak merged commit ac2f0c8 into TheAlgorithms:master Oct 24, 2025
2 checks passed
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