9

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

Death_by_Ch0colate
  • 339
  • 1
  • 2
  • 7

3 Answers3

14

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.

Death_by_Ch0colate
  • 339
  • 1
  • 2
  • 7
7

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.

  • 1
    They certainly might, but do they? – Raphael Nov 19 '17 at 22:23
  • 4
    @Raphael: I answered that. –  Nov 19 '17 at 22:23
  • 3
    @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. –  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
  • @luk32: review the details of these algorithms. You will see where there is room for non-determinism. Also note that "stability" is not defined for graphs. –  Nov 20 '17 at 15:45
3

To add upon Yves Daoust's answer, the following graph

Triangle

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!

Cort Ammon
  • 3,351
  • 13
  • 16