Minimum spanning tree cost of given Graphs

08 Jun, 2022
Given an undirected graph of V nodes (V > 2) named V1, V2, V3, ..., Vn. Two nodes Vi and Vj are connected to each other if and only if 0 < | i – j | ≤ 2. Each edge between any vertex pair (Vi, Vj) is assigned a weight 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: 13Input: 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, ith node can only be connected to (i – 1)th and (i – 2)th node and the minimum spanning tree will only include the node with the minimum weight so the newly added edge will have the weight 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 nth node, weight = weight + (n + (n – 2)).  
This can be generalized as weight = V2 – V + 1 where V is the total nodes in the graph.Below is the implementation of the above approach:  
C++
Java
C#
Python3
PHP
Javascript
// 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 graphint getMinCost(int Vertices){    int cost = 0;     // Calculating cost of MST    cost = (Vertices * Vertices) - Vertices + 1;     return cost;} // Driver codeint main(){    int V = 5;    cout << getMinCost(V);     return 0;}
// Java implementation of the approachclass GfG{ // Function that returns the minimum cost// of the spanning tree for the required graphstatic int getMinCost(int Vertices){    int cost = 0;     // Calculating cost of MST    cost = (Vertices * Vertices) - Vertices + 1;     return cost;} // Driver codepublic static void main(String[] args){    int V = 5;    System.out.println(getMinCost(V));}} // This code is contributed by// Prerna Saini.
// C# implementation of the above approachusing 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
# python3 implementation of the approach  # Function that returns the minimum cost# of the spanning tree for the required graphdef getMinCost( Vertices):    cost = 0      # Calculating cost of MST    cost = (Vertices * Vertices) - Vertices + 1      return cost  # Driver codeif __name__ == "__main__":     V = 5    print (getMinCost(V))
<?php// PHP implementation of the approach// Function that returns the minimum cost// of the spanning tree for the required graphfunction 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..?>
<script> // Javascript implementation of the approach // Function that returns the minimum cost// of the spanning tree for the required graphfunction getMinCost(Vertices){    var cost = 0;     // Calculating cost of MST    cost = (Vertices * Vertices) - Vertices + 1;     return cost;} // Driver codevar V = 5;document.write( getMinCost(V)); // This code is contributed by rrrtnx.</script>
21
 
Time Complexity: O(1)Auxiliary Space: O(1)
prerna saini
ankthon
jit_t
ukasp
rrrtnx
pushpeshrajdx01
Graph Minimum Spanning Tree
math
Minimum Spanning Tree
Graph
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.