# Minimum Spanning Tree in Mathematica

Published on 18 January 2023 (Updated: 18 January 2023)

Welcome to the Minimum Spanning Tree in Mathematica page! Here, you'll find the source code for this program as well as a description of how the program works.

## Current Solution

``````(* Code *)

(* This is easy to do using Mathematica graph operations: *)

minimumSpanningTreeWeight = am \[Function] Module[
{g = WeightedAdjacencyGraph[am /. 0 -> \[Infinity] (* needed to indicate no edge *)]},
Total[PropertyValue[
{g, EdgeList[FindSpanningTree[g, Method -> "Prim"]]},
EdgeWeight]]];

(* The outer function provides the 'user interface': *)

minimumSpanningTreeWeightMain = l \[Function]
Module[{e = "Usage: please provide a comma-separated list of integers"},
Catch[
minimumSpanningTreeWeight@
(* convert to square matrix *)
Partition[#,
(* compute square dimension *)
Module[{l = Sqrt[Length[#]]},
If[IntegerQ[l], l, Throw[e]]]] & @
Map[
(* convert string to integer, or throw *)
s \[Function] If[StringMatchQ[s, DigitCharacter ..],
FromDigits[s],
Throw[e]],
(* construct arguments to spanning tree:
StringSplit[If[StringLength[l] > 0, l, Throw[e]], ", "],
{-1} (* at each leaf *)]]];

(* Valid Tests *)

minimumSpanningTreeWeightMain["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"]

(* Invalid Tests *)

minimumSpanningTreeWeightMain[""]
minimumSpanningTreeWeightMain["1, 0, 3, 0, 5, 1"]

``````

Minimum Spanning Tree in Mathematica was written by:

• Ben Hekster

If you see anything you'd like to change or update, please consider contributing.

## How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

## How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.