-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))
這是一個很好理解的話題。您應該能夠通過快速搜索輕鬆找到一個有效的算法。 –