Skip to content

@graphty/algorithms / index / primMST

Function: primMST() ​

primMST(graph, startNode?): MSTResult

Defined in: algorithms/mst/prim.ts:15

Computes the minimum spanning tree of an undirected graph using Prim's algorithm. The algorithm grows the MST from a starting node by repeatedly adding the minimum weight edge that connects a node in the tree to a node outside the tree.

Parameters ​

graph ​

Graph

The undirected graph to compute the MST for

startNode? ​

NodeId

Optional starting node; if not provided, uses the first node in the graph

Returns ​

MSTResult

The minimum spanning tree as a set of edges and the total weight

Throws ​

Error if the graph is directed, the start node is not found, or the graph is not connected