The Minimum Spanning Tree (MST) algorithm computes a spanning tree with the minimum total edge weight for each connected component in a graph.
The MST has a wide range of applications, including network design, clustering, and other optimization problems where minimizing overall cost or weight is essential.
A spanning tree is a connected subgraph that includes all the nodes of a connected graph G = (V, E) (or of a connected component) and forms a tree (i.e., a graph with no circles). A graph may have multiple spanning trees, but each spanning tree must contain (|V| - 1) edges.
In the example below, the 11 nodes of the graph and the 10 edges highlighted in red form a spanning tree:

An MST is a spanning tree with the minimum total sum of edge weights. A graph may have multiple valid MSTs if some edges share the same weight.
This implementation constructs the MST by:
GQLINSERT (A:default {_id: "A"}), (B:default {_id: "B"}), (C:default {_id: "C"}), (D:default {_id: "D"}), (E:default {_id: "E"}), (F:default {_id: "F"}), (G:default {_id: "G"}), (H:default {_id: "H"}), (A)-[:default {distance: 1}]->(B), (A)-[:default {distance: 2.4}]->(C), (A)-[:default {distance: 1.2}]->(D), (A)-[:default {distance: 0.7}]->(E), (A)-[:default {distance: 2.2}]->(F), (A)-[:default {distance: 1.6}]->(G), (A)-[:default {distance: 0.4}]->(H), (B)-[:default {distance: 1.3}]->(C), (C)-[:default {distance: 1}]->(D), (D)-[:default {distance: 1.65}]->(H), (E)-[:default {distance: 1.27}]->(F), (E)-[:default {distance: 0.9}]->(G), (F)-[:default {distance: 0.45}]->(G)
This algorithm accepts no parameters.
Returns:
| Column | Type | Description |
|---|---|---|
sourceId | STRING | Source node identifier (_id) of MST edge |
targetId | STRING | Target node identifier (_id) of MST edge |
weight | FLOAT | Weight of MST edge |
totalWeight | FLOAT | Total weight of the MST |
GQLCALL algo.mst() YIELD sourceId, targetId, weight, totalWeight
Returns the same columns as run mode, streamed for memory efficiency.
GQLCALL algo.mst.stream() YIELD sourceId, targetId, weight RETURN sourceId, targetId, weight
Returns:
| Column | Type | Description |
|---|---|---|
edgeCount | INT | Number of edges in the MST |
totalWeight | FLOAT | Total weight of the MST |
GQLCALL algo.mst.stats() YIELD edgeCount, totalWeight