VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-edge-weights-in-a-spanning-tree-with-edge-u-v/

⇱ Find Edge Weights in a Spanning Tree with Edge (u, v) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Edge Weights in a Spanning Tree with Edge (u, v)

Last Updated : 23 Jul, 2025

Given an undirected weighted graph with N nodes and M edges, the task is for each edge (u, v) find the minimum possible weight of the Spanning Tree which contains the edge (u, v). The edges are given in the form of a 2D array edges[][], such that edges[i] = {u, v, w} denotes that there is a directed edge from node u to node v with weight w.

Examples:

Input: N = 5, M = 7, edges = {{1, 2, 3}, {1, 3, 1}, {1, 4, 5}, {2, 3, 2}, {2, 5, 3}, {3, 4, 2}, {4, 5, 4}}
Output: 9 8 11 8 8 8 9
Explanation:
👁 tree

  • For edge (1, 2), the MST will have edges: (1, 2), (1, 3), (3, 4) and (2, 5), weight = 3 + 1 + 2 + 3 = 9
  • For edge (1, 3), the MST will have edges: (1, 3), (2, 3), (3, 4) and (2, 5), weight = 1 + 2 + 2 + 3 = 8
  • For edge (1, 4), the MST will have edges: (1, 3), (2, 3), (1, 4) and (2, 5), weight = 1 + 2 + 5 + 3 = 11
  • For edge (2, 3), the MST will have edges: (1, 3), (2, 3), (3, 4) and (2, 5), weight = 1 + 2 + 2 + 3 = 8
  • For edge (2, 5), the MST will have edges: (1, 3), (2, 3), (3, 4) and (2, 5), weight = 1 + 2 + 2 + 3 = 8
  • For edge (3, 4), the MST will have edges: (1, 3), (2, 3), (3, 4) and (2, 5), weight = 1 + 2+ 2 + 3 = 8
  • For edge (4, 5), the MST will have edges: (1, 3), (2, 3), (3, 4) and (4, 5), weight = 1 + 2 + 2 + 4 = 9

Input: N =2, M = 1, edges = {1, 2, 42}
Output: 42

Approach: The problem can be solved using the following approach:

We first build the Minimum Spanning Tree (MST) to ensure the overall minimum connection weight. Then, for all the edges which are in the MST, the answer will be the total weight of the MST. For edges which are not part of MST, say (u, v), we will remove the edge with maximum weight on the path from node u to node v and add edge (u, v).

To find the heaviest edge on the path from node u to node v, we find the heaviest edge from node u to node l and from node v to node l, where l is the Lowest Common Ancestor of node u and node v. We pre-compute ancestor and maximum weight information using Binary Lifting for efficient queries. This allows us to analyze individual edges by quickly finding the Lowest Common Ancestor (LCA) and adjusting the MST weight considering the maximum weight already covered, ultimately providing the minimum spanning tree weight with each edge included.

Steps to solve the problem:

  • Construct the MST using Kruskal's Algorithm.
  • Preprocessing for Efficient Queries:
    • Perform a Depth First Search (DFS) starting from any node.
    • For each visited node, update the ancestor and maxWeight[] arrays at different levels for all its descendants.
    • Use Binary Lifting to calculate ancestor and maxWeight[] for each child node.
  • Processing Edge Queries:
    • For each edge, check if the edge is already included in the MST using the included array.
    • If the edge is included in the MST, the answer will be the weight of MST
    • Else, find the LCA of the edge endpoints and calculate the adjusted MST weight by adding the current edge's weight and subtracting the maximum weight on the path to the LCA stored in the maxWeight[] array.
    • Print the adjusted MST weight.

Below is the implementation of the above approach:


Output
9 8 11 8 8 8 9 

Time Complexity: O(M log M + M log N), where N is the number of nodes and M is the number of edges.
Auxiliary Space: O(N log N + N + M)

Comment