VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-an-edge-is-a-part-of-any-minimum-spanning-tree/

⇱ Check if an edge is a part of any Minimum Spanning Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if an edge is a part of any Minimum Spanning Tree

Last Updated : 23 Jul, 2025

Given a connected undirected weighted graph in the form of a 2D array where each row is of the type [start node, end node, weight] describing an edge, and also two integers (A, B). Return if the edge formed between (A, B) is a part of any of the Minimum Spanning Tree (MST) of the graph.

Minimum Spanning Tree (MST): This is a special subgraph of the graph, such that each and every vertex is connected and the overall sum of the weights of the edges of this subgraph is as minimum as possible. A graph can have multiple minimum spanning trees.

Examples:

Input: graph = [[0 ,1, 20] , [0 , 2 , 5] , [ 0, 3, 10 ] , [ 2, 3, 10]], A = 2, B = 3
Output:  True
Explanation : 2 minimum spanning trees with can be generated which will have weight 35. The connections of the trees are 
1st: [ (0,1) , (0,3) , (0,2)] => 20 + 10 + 5 = 35
2nd: [ ( 0 , 1) , ( 0 , 2 ) , ( 2 , 3) ] => 20 + 5 + 10 = 35
As it can be seen , the edge ( 2, 3) is present in second MST.

The graph is shown in image:

👁 Image

Input: graph = [[0 ,1, 20] , [0 , 2 , 5] , [ 0, 3, 10 ] , [ 2, 3, 20]], A = 2, B = 3
Output: False
Explanation: Only 1 minimum spanning trees with weight 35 can be generated,
but edge (2, 3) is not included.
[(0,1) , (0,3) , (0,2)] => 20 + 10 + 5 = 35

The graph is given in the image

👁 Image

Approach : Kruskal Algorithm and Prim's Algorithm are the two most used algorithms that can be used to find MST of any graph. In this article, the solution is based on the Kruskal algorithm. Follow the steps mentioned below to solve the problem using this approach:

  1. Find the minimum spanning tree cost of the entire graph, using the Kruskal algorithm.
  2. As the inclusion of the edge (A, B) in the MST is being checked, include this edge first in the minimum spanning tree and then include other edges subsequently.
  3. Finally check if the cost is the same for both the spanning trees including the edge(A, B) and the calculated weight of the MST.
  4. If cost is the same, then edge (A, B) is a part of some MST of the graph otherwise it is not.

Below is the implementation of the above approach: 


Output
True

Time Complexity: O(E * logV). where E is the number of edges and V is the number of vertices.
Auxiliary Space: O(V)

Comment
Article Tags: