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.
Input: V = 4
Input: V = 5
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Algorithms | Graph Minimum Spanning Tree | Question 1
- Algorithms | Graph Minimum Spanning Tree | Question 2
- Algorithms | Graph Minimum Spanning Tree | Question 3
- Algorithms | Graph Minimum Spanning Tree | Question 4
- Algorithms | Graph Minimum Spanning Tree | Question 5
- Algorithms | Graph Minimum Spanning Tree | Question 6
- Algorithms | Graph Minimum Spanning Tree | Question 7
- Algorithms | Graph Minimum Spanning Tree | Question 8
- Find the weight of the minimum spanning tree
- Find the minimum spanning tree with alternating colored edges
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Number of spanning trees of a weighted complete Graph
- Find sum of all nodes of the given perfect binary tree
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Minimum number of given operation required to convert n to m
- Generate two BSTs from the given array such that maximum height among them is minimum
- Minimum, maximum and average price values for all the items of given type
- Find minimum value of the expression by choosing K elements from given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.