# Do Kruskal's and Prim's algorithms yield the same minimum spanning tree?

Assuming the edges are undirected, have unique weight, and no negative paths, do these algorithms produce the same Minimum Spanning Trees?

• Yes, and they seem to produce the same MST. But that's not definitive. – Death_by_Ch0colate Nov 19 '17 at 21:15
• Ok, that is true, just checking. – Evil Nov 19 '17 at 22:23
• The answer remains positive even if we remove the condition "no negative path" – Apass.Jack Aug 5 '18 at 14:47
• – Apass.Jack Aug 17 '18 at 19:26

Found this which states that if all the conditions I mentioned above are met, a graph necessarily has a unique MST. Therefore, in terms of my question, Kruskal's and Prim's algorithms necessarily produce the same result.

If the MST is unique, all algorithms will perforce produce it.

If the MST is not unique, the outputs might differ due to different node processing orders (even two distinct implementations of the same algorithm can), but the total weights will be identical. In this case, the MST is a misnomer.

• They certainly might, but do they? – Raphael Nov 19 '17 at 22:23
• @Raphael: I answered that. – Yves Daoust Nov 19 '17 at 22:23
• @Yves No, you didn't. You said maybe, "maybe" is not a guarantee. For example, if you have sorting algorithms, and they are stable, they do produce the same output, regardless of the algorithm used. If they are not stable they might produce same results. So the question is whether those algorithms have a sense of stability w.r.t the topic and exhibit it. – luk32 Nov 20 '17 at 15:18
• @luk32: did you read "even two distinct implementations of the same algorithm can" ? By the way, stable sorting algorithm produce the same output because it is uniquely defined, so they have no choice. If stability is not required, the solution is not unique and different implementations may behave differently. – Yves Daoust Nov 20 '17 at 15:20
• Fair enough, but that statement is not obvious. What makes the implementation possible to be not stable? Is there some kind of sorting involved and the stability depends on this? – luk32 Nov 20 '17 at 15:26

In this graph, we have 3 nodes and 3 edges, each has the same weight. Obviously any 2 edges will form a MST for this graph. However, which two edges are chosen will depend on not only the algorithm, but the implementation of the algorithm. For instance, if I store the nodes in a list, I may visit them in a different order than if I stored the nodes in a set, even if I use the same MST algorithm from that point on.

In fact, if my implementation relies on pointer arithmetic (which some containers in some languages do), I may even pick a different MST each time I run the algorithm!

• Note that the original post states that edges have unique weight; this of course guarantees a unique MST. – wchargin Nov 20 '17 at 3:45
• That last point also applies when using e.g. set or dict in Python 3.3+: hashes are salted with a value that is different for every run to make denial-of-service attacks harder. – Robin Nov 20 '17 at 9:39
• @YvesDaoust Terribly sorry about that! – Cort Ammon Nov 20 '17 at 16:27
• @CortAmmon: no worries, that happens. – Yves Daoust Nov 20 '17 at 17:21