簡單的高階實現
按值排序邊緣,如果不建立迴圈,則按排序順序將每個邊緣新增到 MST。
algorithm kruskalMST(G: a graph)
sort G's edges by their value
MST = an empty graph
for each edge e in G:
if adding e to MST does not create a cycle:
add e to MST
return MST