7

Can anyone explain why the tree resulting from Kruskal is different from Dijkstra ?

I know for the fact that kruskal works on nondescending order of edges but Dijkstra takes advantage of priority queue but still cannot understand why the resulted tree from them are different?

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
  • Kruskal's finds a minimum-spanning-tree, Dijkstra's finds a minimum-length-path, so the two are not directly compareable (unlike say, the A* algorithm vs. Dijkstra's), so I think your question is invalid. – Dai Dec 05 '13 at 19:46
  • 1
    I suggest you read up a bit more on both. The difference should become clear fairly quickly. – Bernhard Barker Dec 05 '13 at 19:51
  • Could he be thinking of [Prim's algorithm](http://en.wikipedia.org/wiki/Prim's_algorithm) instead of Dijkstra's? – Dennis Meng Dec 05 '13 at 19:52
  • For Prime and kruskal I think the tree would be the same – HMdeveloper Dec 05 '13 at 19:54
  • Actually this question was part of past final exam at university and that is why I asked it – HMdeveloper Dec 05 '13 at 19:54
  • as mentioned by chill, MST is usually not unique, and usually Prim's and Kruskal's algorithms find structurally different trees. – ile Dec 05 '13 at 22:27

3 Answers3

8

The basic difference, I would say, is that given a set of nodes, Dijkstra's algorithm finds the shortest path between 2 nodes. Which does not necessarily cover all the nodes in the graph.

However on Kruskal's case, the algorithm tries to cover all the nodes while keeping the edge cost minimum.

Consider this graph:

       E
   3 / |
    B  | 3
 5 /3\ |
  /    D
A      | 2
  \    F
 1 \  / 1
    C
   2 \
      G

Dijkstra's algorithm will return the path A-C-F-D-E for source and destination nodes A and E, at a total cost of 7. However Kruskal's algorithm should cover all the nodes, therefore it will consider edges [AC], [CG], [CF], [FD], [DB] and [DE] with a total cost of 12.

In Dijkstra, the irrelevant nodes (ones that are not in the path from source the destination) are ignored, e.g., G and B in this case. The resulting path, of course, is a tree but does not cover all the nodes. There might be millions of nodes connected to G (assuming they are not connected to other nodes) which would not be in the path of the Dijkstra's result. On the other hand, Kruskal had to add those nodes to the resulting tree.

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • 1
    I think OP is considering Dijkstra's version which finds all shortest paths from a single node, so the result generates a tree over all nodes. – ile Dec 05 '13 at 22:24
2

The minimum spanning tree is not unique.

The Kruskal's algorithm select a minimum length edge of all possible edges which connect two different disjoint MST components, found so far.

The Dijkstra/Prim/Jarnik's algorithm selects minimum length edge of all the edges, which extend the single MST component found so far.

At each step, in the general case the algorithms select a minimum edge from distinct sets of possiblities.

PS. Well, if the OP refers to the shortest path tree of the Dijkstra's shortest path algorithm the answer is that the shortest path tree is not necessarily a minimum spanning tree, the algorithms compute different things.

chill
  • 16,470
  • 2
  • 40
  • 44
  • 1
    Prim's algorithm isn't typically called Dijkstra's algorithm (which is good, because [there's already an algorithm with that name](http://en.wikipedia.org/wiki/Dijkstra's_algorithm)) (no idea which one the question is talking about). – Bernhard Barker Dec 05 '13 at 20:05
  • Yes, perhaps to avoid confusion with the shortest path, anyway, it seems it was re-discovered by Dijkstra in 1959 and assuming the OP refers to specifically this Dijkstra/Prim/Jarnik algorithm actually makes the question sensible. – chill Dec 05 '13 at 20:10
2

They solve different problems. It might be possible that they produce the same trees in some cases (e.g. the graph is already a tree), but in general the trees are optimized for different things: one finds the shortest paths from a single source, another minimizes the total weight of the tree.

For example, consider we built MST with Kruskall. Let's say all the edges in MST weight 1 and it looks more or less linear. Assume that to get from A to Z it takes 5 edges, so the path from A to Z in MST will cost 5. However, it might as well be possible that original graph had an edge from A to Z with cost 3 (< 5), which MST didn't include, yet Dijkstra will probably have the edge in its resulting tree.

ile
  • 339
  • 1
  • 7