Prim's Algorithm & Kruskal's algorithm

1. Problem

These two algorithm are all used to find a minimum spanning tree for a weighted undirected graph.

2.Kruskal's algorithm

2.1 Pseudocode
A = ∅
foreach v ∈ G.V:
  MAKE-SET(v)
foreach (u, v) in G.E ordered by weight(u, v), increasing:
  if FIND-SET(u) ≠ FIND-SET(v):
  A = A ∪ {(u, v)}
  UNION(u, v)
return A
2.2 Complexity

O(E logE) , equivalently, O(E log V) time ,because
\[ E \le V^2\ and\ logV^2=2logV \]

3. Prim's algorithm

3.1 Pseudocode
Remove all loops and parallel edges
Choose any arbitrary node as root node
Check outgoing edges and select the one with less cost
Repeat step 3 (until all vertices are in the tree).
3.2 Complexity

\(O(V^2)\) [adjacency matrix]

\(O(ElogV)\) [adjacency list]

4.Summary

Both of the algorithms are greedy algorithms and aim to find a subset of the edges which forms a tree that contains every vertex. However, Kruskal's algorithm chooses a node, whereas Prim's algorithm chooses an edge at each time.

5.Reference

Prim's algorithm

Kruskal's algorithm

转载于:https://www.cnblogs.com/xiaofulan/p/10371714.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值