检查边是否是任何最小生成树的一部分
给定一个二维数组形式的连通无向加权图,其中每一行的类型为[start node, end node, weight]描述一条边,还有两个整数(A, B) 。如果 (A, B) 之间形成的边是图的任何最小生成树 (MST) 的一部分,则返回。
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.
例子:
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:
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
方法:Kruskal 算法和 Prim 算法是两种最常用的算法,可用于查找任何图的 MST。在本文中,解决方案基于 Kruskal 算法。请按照下面提到的步骤使用此方法解决问题:
- 使用Kruskal 算法找到整个图的最小生成树成本。
- 在检查 MST 中是否包含边 (A, B)时,首先将该边包含在最小生成树中,然后再包含其他边。
- 最后检查包括边(A,B)在内的生成树的成本和计算出的 MST 权重是否相同。
- 如果成本相同,则边 (A, B)是图的某些 MST 的一部分,否则不是。
下面是上述方法的实现:
Python3
# Python program to implement above approach
# Class to implement disjoint set union
class dsu:
def __init__(self):
self.parent = {}
self.rank = {}
# Function to find parent of a node
def find(self, x):
if (x not in self.parent):
self.rank[x] = 1
self.parent[x] = x
if (self.parent[x] != x):
self.parent[x] = \
self.find(self.parent[x])
return (self.parent[x])
# Function to perform union
def union(self, u, v):
p1 = self.find(u)
p2 = self.find(v)
# If do not belong to same set
if (p1 != p2):
if (self.rank[p1]
< self.rank[p2]):
self.parent[p1] = p2
elif(self.rank[p1]
> self.rank[p1]):
self.parent[p2] = p1
else:
self.parent[p2] = p1
self.rank[p1] += 1
return (True)
# Belong to same set
else:
return False
class Solution:
# Find the MST weight
def kruskal(self, include, edges, a, b):
obj = dsu()
total = 0
# If include is True , then include
# edge (a,b) first
if (include):
for (u, v, wt) in edges:
# As graph is undirected so
# (a,b) or (b,a) is same
# If found break the for loop
if (u, v) == (a, b) or \
(b, a) == (u, v):
val = obj.union(a, b)
total += wt
break
# Go on adding edge to the disjoint set
for (u, v, wt) in edges:
# Nodes (u,v) not belong to
# same set include it
if (obj.union(u, v)):
total += wt
# Finally return total weight of MST
return (total)
# Function to find if edge (a, b)
# is part of any MST
def solve(self, edges, a, b):
# Sort edges according to weight
# in ascending order
edges.sort(key=lambda it: it[2])
# Not included edge (a,b)
overall = self.kruskal(False,
edges, a, b)
# Find mst with edge (a,b) included
inc = self.kruskal(True,
edges, a, b)
# Finally return True if same
# else False
if (inc == overall):
return (True)
else:
return (False)
# Driver code
if __name__ == "__main__":
obj = Solution()
graph = [[0, 1, 20], [0, 2, 5],
[0, 3, 10], [2, 3, 10]]
A, B = 2, 3
val = obj.solve(graph, A, B)
if (val):
print("True")
else:
print("False")
True
时间复杂度:O(E * logV)。其中 E 是边数,V 是顶点数。
辅助空间: O(V)