如何将Kruskal算法与文件中的邻接矩阵结合在一起?

时间:2019-05-03 04:34:22

标签: python algorithm graph

N, M = map(int, input().split())
Edges = []
for i in range(M):
    start, end, weight = map(int, input().split())
    Edges.append([weight, start, end])
Edges.sort()
Comp = [i for i in range(N)]
Ans = 0
for weight, start, end in Edges:
    if Comp[start] != Comp[end]:
        Ans += weight
        a = Comp[start]
        b = Comp[end]
        for i in range(N):
            if Comp[i] == b:
                Comp[i] = a

该算法(以上)如何与包含邻接矩阵的文件组合?

从文件读取矩阵的代码:

with open('in.txt') as f:
    matrix = [list(map(int, row.split())) for row in f.readlines()]

0 个答案:

没有答案