# Minimum spanning tree cost of given Graphs

Given an undirected graph of **V** nodes (V > 2) named V_{1}, V_{2}, V_{3}, …, V_{n}. Two nodes **V _{i}** and

**V**are connected to each other if and only if

_{j}**0 < | i – j | ≤ 2**. Each edge between any vertex pair

**(V**is assigned a weight

_{i}, V_{j})**i + j**. The task is to find the cost of the minimum spanning tree of such graph with

**V**nodes.

**Examples:**

Input:V = 4

Output:13

Input:V = 5

Output:21

**Approach:** Starting with a graph with minimum nodes (i.e. 3 nodes), the cost of the minimum spanning tree will be 7. Now for every node **i** starting from the fourth node which can be added to this graph, **i ^{th}** node can only be connected to

**(i – 1)**and

^{th}**(i – 2)**node and the minimum spanning tree will only include the node with the minimum weight so the newly added edge will have the weight

^{th}**i + (i – 2)**.

So addition of fourth node will increase the overall weight as 7 + (4 + 2) = 13

Similarly adding fifth node, weight = 13 + (5 + 3) = 21

…

For n^{th}node,weight = weight + (n + (n – 2)).

This can be generalized as **weight = V ^{2} – V + 1** where

**V**is the total nodes in the graph.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the minimum cost ` `// of the spanning tree for the required graph ` `int` `getMinCost(` `int` `Vertices) ` `{ ` ` ` `int` `cost = 0; ` ` ` ` ` `// Calculating cost of MST ` ` ` `cost = (Vertices * Vertices) - Vertices + 1; ` ` ` ` ` `return` `cost; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 5; ` ` ` `cout << getMinCost(V); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GfG ` `{ ` ` ` `// Function that returns the minimum cost ` `// of the spanning tree for the required graph ` `static` `int` `getMinCost(` `int` `Vertices) ` `{ ` ` ` `int` `cost = ` `0` `; ` ` ` ` ` `// Calculating cost of MST ` ` ` `cost = (Vertices * Vertices) - Vertices + ` `1` `; ` ` ` ` ` `return` `cost; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `V = ` `5` `; ` ` ` `System.out.println(getMinCost(V)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Prerna Saini. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// Function that returns the minimum cost ` ` ` `// of the spanning tree for the required graph ` ` ` `static` `int` `getMinCost(` `int` `Vertices) ` ` ` `{ ` ` ` `int` `cost = 0; ` ` ` ` ` `// Calculating cost of MST ` ` ` `cost = (Vertices * Vertices) - Vertices + 1; ` ` ` ` ` `return` `cost; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `V = 5; ` ` ` `Console.WriteLine(getMinCost(V)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python3

`# python3 implementation of the approach ` ` ` `# Function that returns the minimum cost ` `# of the spanning tree for the required graph ` `def` `getMinCost( Vertices): ` ` ` `cost ` `=` `0` ` ` ` ` `# Calculating cost of MST ` ` ` `cost ` `=` `(Vertices ` `*` `Vertices) ` `-` `Vertices ` `+` `1` ` ` ` ` `return` `cost ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `V ` `=` `5` ` ` `print` `(getMinCost(V)) ` ` ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` `// Function that returns the minimum cost ` `// of the spanning tree for the required graph ` `function` `getMinCost(` `$Vertices` `) ` `{ ` ` ` `$cost` `= 0; ` ` ` ` ` `// Calculating cost of MST ` ` ` `$cost` `= (` `$Vertices` `* ` `$Vertices` `) - ` `$Vertices` `+ 1; ` ` ` ` ` `return` `$cost` `; ` `} ` ` ` `// Driver code ` `$V` `= 5; ` `echo` `getMinCost(` `$V` `); ` ` ` `#This Code is contributed by ajit.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

21

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Kruskal's Minimum Spanning Tree using STL in C++
- Minimum Product Spanning Tree
- Applications of Minimum Spanning Tree Problem
- Boruvka's algorithm for Minimum Spanning Tree
- Find the weight of the minimum spanning tree
- Algorithms | Graph Minimum Spanning Tree | Question 7
- Reverse Delete Algorithm for Minimum Spanning Tree
- Algorithms | Graph Minimum Spanning Tree | Question 8
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Algorithms | Graph Minimum Spanning Tree | Question 6
- Algorithms | Graph Minimum Spanning Tree | Question 4
- Algorithms | Graph Minimum Spanning Tree | Question 1
- Algorithms | Graph Minimum Spanning Tree | Question 3
- Algorithms | Graph Minimum Spanning Tree | Question 5
- Algorithms | Graph Minimum Spanning Tree | Question 2
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Find the minimum spanning tree with alternating colored edges
- Minimum Spanning Tree using Priority Queue and Array List
- Uniform-Cost Search (Dijkstra for large Graphs)
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.