Anda di halaman 1dari 3

algorithm - Kruskal vs Prim - Stack Overow

http://stackoverow.com/questions/1195872/kr...

sign up

log in

tour

help
Dismiss

Announcing Stack Overflow Documentation


We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping

Learn more about Documentation

Kruskal vs Prim
I was wondering when one should use Prim's algorithm and when Kruskal's to find the minimum spanning tree? They both have easy logics,
same worst cases, and only difference is implementation which might involve a bit different data structures. So what is the deciding factor?
algorithm

graph-theory

minimum-spanning-tree

prims-algorithm

kruskals-algorithm
edited Jul 6 '12 at 20:20

asked Jul 28 '09 at 18:28

templatetypedef
194k

44

490

ebe
740

10 Answers
12 followers, 328 questions

Use Prim's algorithm when you have a graph with

rss

A minimum spanning tree (MST) or minimum weight spanning


tree is a spanning tree of a connected, undirected graph with the
lots
edges.
leastof
possible
weight.

frequentruns
info intop
users
For a graph with V vertices E edges, Kruskal's algorithm
O(E
log V) time and Prim's
algorithm can run in O(E + V log V) amortized time, if you use a Fibonacci Heap.

Prim's algorithm is significantly faster in the limit when you've got a really dense graph with
many more edges than vertices. Kruskal performs better in typical situations (sparse graphs)
because it uses simpler data structures.
edited Jul 30 '09 at 16:41

answered Jul 28 '09 at 18:36

tgamblin
35.9k

11

71

I would say "typical situations" instead of average.. I think it's an obscure term to use, for example what is
the "average size" of a hash table? no idea. yairchu Jul 29 '09 at 11:28

@SplittingField: I do believe you're comparing apples and oranges. Amortized analysis is simpy a way of
getting a measurement of the function (so to speak) --- whether it is the worst case or average case is
dependent on what you're proving. In fact (as I look it up now), the wiki article uses language that implies
that its only used for worst-case analysis. Now, using such an analysis means that you can't make as strong
promises about the cost of a particular operation, but by the time the algorithm is done, it will indeed by
O(E+VlogV), even worst-case. agorenst Jul 30 '09 at 16:49

That sounds good in theory, but I bet few people can implement a Fibonacci heap Alexandru Oct 29 '09 at
20:04

@tgamblin, there can be C(V,2) edges in worst case. So, doesn't the time compleixty of Prim's algorithm
boils down to O(V^2 + VlogV) i.e. O(V^2) in case of fibonacci heap? Green goblin Nov 9 '12 at 5:40

There is also another important factor: the output of Prims is a MST only if the graph is connected (output
seems to me of no use otherwise), but the Kruskal's output is the Minimum Spanning forests (with some
use). Andrei I Oct 23 '13 at 10:00

91

I found a very nice thread on the net that explains the difference in a very straightforward way :
http://www.thestudentroom.co.uk/showthread.php?t=232168.
Kruskal's algorithm will grow a solution from the cheapest edge by adding the next cheapest edge, provided that it
doesn't create a cycle.
Prim's algorithm will grow a solution from a random vertex by adding the next cheapest vertex, the vertex that is
not currently in the solution but connected to it by the cheapest edge.
Here attached is an interesting sheet on that topic.

1 of 3

Sunday 07 August 2016 10:25 AM

algorithm - Kruskal vs Prim - Stack Overow

http://stackoverow.com/questions/1195872/kr...

If you implement both Kruskal and Prim, in their optimal form : with a union find and a finbonacci heap
respectively, then you will note how Kruskal is easy to implement compared to Prim.
Prim is harder with a fibonacci heap mainly because you have to maintain a book-keeping table to record the
bi-directional link between graph nodes and heap nodes. With a Union Find, it's the opposite, the structure is
simple and can even produce directly the mst at almost no additional cost.
edited Jan 26 '14 at 15:42

answered

Snicolas
26.7k

Nitpick: Last 'slide' in each should read "repeat until you have a spanning tree"; not until MST, which is
something of a recursive task - how do I know it's minimal - that's why I'm following Prim's/Kruskal's to begin
with! Ollie Ford Jun 13 '15 at 23:17
@OllieFord I found this thread for having searched a simple illustration of Prim and Kruskal algorithms. The
algorithms guarantee that you'll find a tree and that tree is a MST. And you know that you have found a tree
when you have exactly V-1 edges. mikedu95 Jan 20 at 21:16
@mikedu95 You're correct, making the same point as my earlier comment from a different angle.
Ollie Ford Jan 20 at 23:17

Kruskal can have better performance if the edges can be sorted in linear time, or are
already sorted.
Prim's better if the number of edges to vertices is high.
edited Jul 29 '09 at 11:42

answered Jul 28 '09 at 18:43

Daniel C. Sobral
211k

56

378

588

I know that you did not ask for this, but if you have more processing units, you should always
consider Borvka's algorithm, because it might be easily parallelized - hence it has a
performance advantage over Kruskal and Jarnk-Prim algorithm.
edited Nov 29 '15 at 22:47

answered Mar 3 '12 at 22:14

malejpavouk
1,627

2 of 3

20

33

Sunday 07 August 2016 10:25 AM

algorithm - Kruskal vs Prim - Stack Overow

http://stackoverow.com/questions/1195872/kr...

If we stop the algorithm in middle prim's algorithm always generates connected tree, but
kruskal on the other hand can give disconnected tree or forest
answered Apr 3 '13 at 17:21

Prakhar
394

18

The best time for Kruskal's is O(E logV). For Prim's using fib heaps we can get O(E+V lgV).
Therefore on a dense graph, Prim's is much better.
answered Oct 14 '11 at 1:34

Leon Stenneth
31

One important application of Kruskal's algorithm is in single link clustering.


Consider n vertices and you have a complete graph.To obtain a k clusters of those n
points.Run Kruskal's algorithm over the first n-(k-1) edges of the sorted set of edges.You
obtain k-cluster of the graph with maximum spacing.
answered Sep 8 '13 at 5:45

Jaskaran
84

Kruskal time complexity worst case is O(E log E),this because we need to sort the edges.
Prim time complexity worst case is O(E log V) with priority queue or even better, O(E+V log
V) with Fibonacci Heap. We should use Kruskal when the graph is sparse, i.e.small number of
edges,like E=O(V),when the edges are already sorted or if we can sort them in linear time. We
should use Prim when the graph is dense, i.e number of edges is high ,like E=O(V).
answered Jun 6 at 12:34

Ghiurutan Alexandru
21

Prim's is better for more dense graphs, and in this we also do not have to pay much attention
to cycles by adding an edge, as we are primarily dealing with nodes. Prim's is faster than
Kruskal's in the case of complex graphs.
edited Dec 16 '15 at 0:40

answered Nov 23 '13 at 1:57

Community

Sakshi

11

In kruskal Algorithm we have number of edges and number of vertices on a given graph but on
each edge we have some value or weight on behalf of which we can prepare a new graph
which must be not cyclic or not close from any side For Example
graph like this _____________ | | | | | | |__________| | Give name to any vertex a,b,c,d,e,f .
answered Apr 17 at 1:56

Abhishek
11

3 of 3

Sunday 07 August 2016 10:25 AM

Anda mungkin juga menyukai