Minimal Spanning Tree Calculator (for a 3-Vertex Graph)
A Minimal Spanning Tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. It's a fundamental concept in graph theory with applications in network design, cluster analysis, and more.
This calculator demonstrates the concept of an MST for a simple graph consisting of three vertices (let's call them A, B, and C) connected by three possible edges: A-B, B-C, and C-A. You can input the "cost" or "distance" (weight) for each of these edges, and the calculator will determine which two edges form the Minimal Spanning Tree and what its total weight is.
For a graph with only three vertices forming a triangle, the Minimal Spanning Tree will always consist of the two edges with the smallest weights, as this connects all three vertices without forming a cycle.
Minimal Spanning Tree Result:
Enter edge weights and click "Calculate MST" to see the result.
How it Works:
The calculator takes the three edge weights you provide. It then identifies the two edges with the lowest weights. These two edges will connect all three vertices (A, B, and C) without creating a cycle, thus forming the Minimal Spanning Tree. The total weight of the MST is simply the sum of the weights of these two selected edges.
Example:
- If Edge A-B has a weight of 10
- Edge B-C has a weight of 5
- Edge C-A has a weight of 8
The two smallest weights are 5 (B-C) and 8 (C-A). Therefore, the MST consists of edges B-C and C-A, and its total weight is 5 + 8 = 13.
.minimal-spanning-tree-calculator {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 700px;
margin: 20px auto;
padding: 25px;
border: 1px solid #e0e0e0;
border-radius: 10px;
background-color: #f9f9f9;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.minimal-spanning-tree-calculator h2,
.minimal-spanning-tree-calculator h3 {
color: #333;
text-align: center;
margin-bottom: 20px;
}
.minimal-spanning-tree-calculator p {
color: #555;
line-height: 1.6;
margin-bottom: 15px;
}
.calculator-form .form-group {
margin-bottom: 15px;
display: flex;
align-items: center;
}
.calculator-form label {
flex: 1;
margin-right: 15px;
color: #444;
font-weight: bold;
}
.calculator-form input[type="number"] {
flex: 2;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
width: calc(100% – 120px); /* Adjust based on label width */
}
.calculator-form button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding: 20px;
border: 1px solid #dcdcdc;
border-radius: 8px;
background-color: #eaf6ff;
}
.calculator-result h3 {
color: #007bff;
margin-top: 0;
margin-bottom: 15px;
}
.calculator-result #mstResult p {
font-size: 17px;
color: #333;
margin-bottom: 8px;
}
.calculator-result #mstResult strong {
color: #0056b3;
}
.minimal-spanning-tree-calculator ul {
list-style-type: disc;
margin-left: 20px;
color: #555;
}
.minimal-spanning-tree-calculator li {
margin-bottom: 5px;
}
function calculateMST() {
var edgeABWeight = parseFloat(document.getElementById('edgeABWeight').value);
var edgeBCWeight = parseFloat(document.getElementById('edgeBCWeight').value);
var edgeCAWeight = parseFloat(document.getElementById('edgeCAWeight').value);
var resultDiv = document.getElementById('mstResult');
resultDiv.innerHTML = "; // Clear previous results
// Input validation
if (isNaN(edgeABWeight) || isNaN(edgeBCWeight) || isNaN(edgeCAWeight) ||
edgeABWeight < 0 || edgeBCWeight < 0 || edgeCAWeight < 0) {
resultDiv.innerHTML = 'Please enter valid non-negative numbers for all edge weights.';
return;
}
// Create an array of edge objects
var edges = [
{ name: 'A-B', weight: edgeABWeight },
{ name: 'B-C', weight: edgeBCWeight },
{ name: 'C-A', weight: edgeCAWeight }
];
// Sort edges by weight in ascending order
edges.sort(function(a, b) {
return a.weight – b.weight;
});
// For a 3-vertex graph (a triangle), the MST will always consist of the two edges with the smallest weights.
var mstEdges = [edges[0], edges[1]];
var totalMSTWeight = edges[0].weight + edges[1].weight;
resultDiv.innerHTML += 'The Minimal Spanning Tree consists of the following edges:';
resultDiv.innerHTML += '
' + mstEdges[0].name + ' (Weight: ' + mstEdges[0].weight.toFixed(1) + ')';
resultDiv.innerHTML += '
' + mstEdges[1].name + ' (Weight: ' + mstEdges[1].weight.toFixed(1) + ')';
resultDiv.innerHTML += 'Total Minimal Spanning Tree Weight:
' + totalMSTWeight.toFixed(1) + '';
}