Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to Find Out if an Edge is a Part of a Minimum Spanning Tree in Python
Suppose we have a 2D matrix named edges, that represents an undirected graph. Every item in the matrix edges represents an edge and is of the form (u, v, w). This means nodes u and v are connected and the edge has the weight w. We also have integers a and b, that represent an edge (a,b). We have to find out if the edge (a, b) is part of a minimum spanning tree.
Note ? the graph has to be connected and the edge (a, b) exists in the graph.
So, if the input is like edges =
[[0, 2, 100], [1, 2, 200], [1, 3, 100], [2, 3, 300]], a = 0 b = 2,
then the output will be True.
Algorithm
The approach is based on the cut property of minimum spanning trees. An edge is part of MST if removing all lighter edges doesn't disconnect the two vertices.
To solve this, we will follow these steps ?
Find the weight of edge (a, b)
Create a subgraph containing only edges with weights less than the target edge
Check if vertices a and b are still connected in this subgraph
If they are not connected, the edge (a, b) is part of MST
Implementation
class Solution:
def findPath(self, edges, a, b):
if a == b:
return True
if not edges:
return False
for x in edges:
if x[2] == -1:
continue
new_a = -1
if x[0] == a:
new_a = x[1]
elif x[1] == a:
new_a = x[0]
if new_a != -1:
edges.remove(x)
if self.findPath(edges, new_a, b):
return True
edges.append(x)
return False
def solve(self, edges, a, b):
# Find weight of edge (a, b)
weight = next(x for x in edges if (x[0] == a and x[1] == b) or (x[1] == a and x[0] == b))[2]
# Keep only edges with weight less than target edge
lighter_edges = [x for x in edges if x[2] < weight]
# If a and b are not connected with lighter edges,
# then edge (a, b) is part of MST
return not self.findPath(lighter_edges, a, b)
# Example usage
ob = Solution()
edges = [
[0, 2, 100],
[1, 2, 200],
[1, 3, 100],
[2, 3, 300]
]
result = ob.solve(edges, 0, 2)
print(f"Is edge (0, 2) part of MST? {result}")
Is edge (0, 2) part of MST? True
How It Works
In the given example:
Edge (0, 2) has weight 100
Lighter edges: [(1, 3, 100)] - no edges with weight < 100
Since vertices 0 and 2 are not connected through lighter edges, edge (0, 2) is essential for MST
Alternative Approach Using Union-Find
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px != py:
self.parent[px] = py
return True
return False
def connected(self, x, y):
return self.find(x) == self.find(y)
def is_edge_in_mst(edges, a, b):
# Find the weight of target edge
target_weight = None
for u, v, w in edges:
if (u == a and v == b) or (u == b and v == a):
target_weight = w
break
# Find all unique vertices
vertices = set()
for u, v, w in edges:
vertices.add(u)
vertices.add(v)
# Create Union-Find structure
uf = UnionFind(max(vertices) + 1)
# Connect vertices using edges with weight less than target
for u, v, w in edges:
if w < target_weight:
uf.union(u, v)
# If a and b are not connected, target edge is in MST
return not uf.connected(a, b)
# Example
edges = [
[0, 2, 100],
[1, 2, 200],
[1, 3, 100],
[2, 3, 300]
]
result = is_edge_in_mst(edges, 0, 2)
print(f"Is edge (0, 2) part of MST? {result}")
Is edge (0, 2) part of MST? True
Conclusion
An edge is part of MST if removing all lighter-weight edges leaves the two vertices disconnected. Both DFS path-finding and Union-Find approaches efficiently solve this problem using the cut property of minimum spanning trees.
