-1
我有一个下面的邻接矩阵D.如何编写一个python函数,如果矩阵中的所有顶点都连接,则返回True;否则返回False?Python函数:检查邻接矩阵中的连通性
D = [['a', 'c', 'g', 'w', 'Q', 'f', 'Z', 't', 'R'], [0, 1, 2, 1, 9, 0, 0, 0, 0], [1, 0, 3, 4, 0, 0, 0, 0, 0], [2, 3, 0, 15, 2, 0, 0, 0, 0], [1, 4, 15, 0, 7, 0, 0, 0, 0], [9, 0, 2, 7, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 9, 0], [0, 0, 0, 0, 0, 2, 0, 0, 20], [0, 0, 0, 0, 0, 9, 0, 0, 0], [0, 0, 0, 0, 0, 0, 20, 0, 0]]
def connectivity(adjMatrix):
connected = True
while connected == True:
# some algorithm that checks that each vertex can be connected to any other vertex
# if connected -> remains True
# if not connected -> False
return connected
print(connectivity(D))
这是一个很好理解的话题。您应该能够通过快速搜索轻松找到一个有效的算法。 –