Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the tree resulting from Kruskal is different from Dijkstra?

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?

like image 860
HMdeveloper Avatar asked Dec 05 '13 19:12

HMdeveloper


People also ask

What is the difference between minimum spanning tree and Dijkstra's algorithm?

Prim's purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only two nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual path. "Dijkstra is concerned with only two nodes" is bunk.

Can Prim's and Kruskal's algorithms yield different minimum spanning trees explain why or why not?

In general: If the edge weights in your graph are all different from each other, then your graph has a unique minimum spanning tree, so Kruskal's and Prim's algorithms are guaranteed to return the same tree.

Can Dijkstra's algorithm be used on a tree?

With Dijkstra's Algorithm, you can find the shortest path between nodes in a graph. Particularly, you can find the shortest path from a node (called the "source node") to all other nodes in the graph, producing a shortest-path tree.

Do you get same spanning tree when you apply Kruskal's and Prim's algorithm on same graph?

Prim's and Kruskal's algorithms will always return the same Minimum Spanning tree (MST).


3 Answers

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.

like image 81
Varaquilex Avatar answered Nov 15 '22 10:11

Varaquilex


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.

like image 25
chill Avatar answered Nov 15 '22 10:11

chill


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.

like image 22
ile Avatar answered Nov 15 '22 10:11

ile