Functions
prims_mst
Computes the minimum spanning tree of a weighted graph using Prim’s algorithm.The weighted graph to find the MST from. Can be either an AdjacencyListWeightedGraph or AdjacencyMatrixWeightedGraph
The starting vertex label for building the MST
An empty graph object to output the MST into. If None, a new AdjacencyListWeightedGraph is created
The minimum spanning tree of the input graph
mst_weight
Calculates the total weight of all edges in a graph (typically used for MST graphs).The graph to calculate the total edge weight of (must have an
edges() method)The total weight of all edges in the graph
Algorithm Details
Algorithm Details
How Prim’s Algorithm Works:
- Start with a single vertex and mark it as visited
- Add all edges from the visited vertex to the priority queue
- Pop the edge with minimum weight from the priority queue
- If the destination vertex hasn’t been visited:
- Add the edge to the MST
- Mark the destination as visited
- Add all edges from the new vertex to the priority queue
- Repeat steps 3-4 until all vertices are visited