24. Second Best Minimum Spanning Tree
β GFG solution to the Second Best Minimum Spanning Tree problem: find the second minimum weight spanning tree using MST edge removal technique with Kruskal's algorithm and union-find. π
π§© Problem Description
π Examples
Example 1
Input: V = 5, E = 7, edges[][] = [[0, 1, 4], [0, 2, 3], [1, 2, 1], [1, 3, 5], [2, 4, 10], [2, 3, 7], [3, 4, 2]]
Output: 12
Explanation: The MST has weight 11 (edges with weights 1, 3, 2, 5). By removing one MST edge and
adding another, we can form a spanning tree with weight 12, which is the second best MST.Example 2
Input: V = 5, E = 4, edges[][] = [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]]
Output: -1
Explanation: This graph forms a linear tree structure. There's only one unique spanning tree
possible, so no second best MST exists.π Constraints
β
My Approach
MST Edge Removal with Kruskal's Algorithm
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated