Menu
CoddyTech

伪代码

Coddy Prim 算法:图算法 课程的第 4 课,共 9 课。

prim(n, edges):
   inTree[0] = true; everything else false
   total = 0
   repeat (n - 1) times:
      best = the crossing edge (one endpoint in, one out) with smallest weight
      if none exists: stop      # disconnected
      total += best.weight
      inTree[best.outsideEndpoint] = true
   return total
  • 当 inTree[u] 和 inTree[v] 不同时,边 (u, v, w) 会跨越这个割。
  • 每轮扫描所有边,找到权重最小的跨割边,这样代码更简单(对于小型图,不需要优先队列)。

亲手试试

这节课没有代码挑战。

quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Prim 算法:图算法中的所有课

自己练习: C 在线编译器