def prim(graph,n):
state = [0 for i in range(n)]
dist = [float("inf") for _ in range(n)]
result = []
for i in range(n):
if dist[0] == float("inf"):
idx = 0
dist[0] = 0
else:
mx = float("inf")
for i in range(n):
if dist[i] < mx and not state[i]:
mx =dist[i]
idx = i
result.append(idx)
state[idx] = 1
for i in range(n):
if graph[i][idx] < dist[i] and not state[i]:
dist[i] = graph[i][idx]
return result
if __name__ == '__main__':
graph = [[1,5,3,6],
[5,8,9,6],
[3,9,8,6],
[6,6,6,3]]
print(prim(graph,4))
相关文章
- 02-14【思特奇杯•云上蓝桥---算法训练营】第1周作业 蓝桥杯真题-阶乘约数(Python)
- 02-14c – LLDB Python脚本中的指针算法
- 02-14二叉树最深层次元素求和(基于层序遍历算法)
- 02-14【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
- 02-14HDU 1233 还是畅通工程(最小生成树,prim)
- 02-14k-means算法学习1
- 02-14机器学习 K-均值算法
- 02-14字节算法面试的一道DP题
- 02-14零基础学习Kmeans聚类算法的原理与实现过程
- 02-14【机器学习】机器学习入门08 - 聚类与聚类算法K-Means