githubEdit

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. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

You are given an undirected graph with V vertices numbered from 0 to V-1 and E edges. Each edge is represented as [u, v, w], where u and v are connected vertices with weight w. Your task is to find the weight of the second best minimum spanning tree of the given graph.

A second best MST is defined as a minimum-weight spanning tree whose total weight is strictly greater than the weight of the minimum spanning tree (MST). If no such second best MST exists, return -1.

πŸ“˜ 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

  • $1 \le V \le 100$

  • $V-1 \le E \le V \times (V-1)/2$

  • $0 \le \text{edges}[i][2] \le 10^3$

βœ… My Approach

The optimal solution uses Kruskal's Algorithm with Union-Find (Disjoint Set Union) to systematically find the second best MST:

MST Edge Removal with Kruskal's Algorithm

  1. Sort Edges by Weight:

    • Sort all edges in ascending order of their weights for greedy MST construction.

  2. Build the First MST:

    • Use Union-Find with path compression and union by rank.

    • Apply Kruskal's algorithm to construct the MST.

    • Store all edges that form the MST and calculate its total weight.

  3. Find Second Best MST:

    • For each edge in the original MST:

      • Temporarily exclude that edge from consideration.

      • Rebuild a spanning tree using remaining edges with Kruskal's algorithm.

      • Check if the new tree is valid (has exactly V-1 edges).

      • If valid and weight is strictly greater than original MST, update the minimum among these candidates.

  4. Union-Find Operations:

    • Find with Path Compression: Recursively finds the root and compresses the path.

    • Union by Rank: Merges two sets by attaching the smaller rank tree under the larger rank tree.

  5. Return Result:

    • Return the minimum weight found that is greater than the original MST.

    • If no such tree exists, return -1.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(V Γ— E log E), where we sort edges once in O(E log E), build the initial MST in O(E log V), and then for each of the V-1 MST edges, we rebuild the spanning tree in O(E log V). Since V-1 < E in most cases, the dominant term is O(V Γ— E log E).

  • Expected Auxiliary Space Complexity: O(V + E), where O(V) is used for the Union-Find data structure (parent and rank arrays) and O(E) for storing the MST edges. The space for sorting is typically O(log E) for the recursion stack.

πŸ§‘β€πŸ’» Code (C++)

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated