Question

Let's assume I am running Dijkstra Algorithm to visit all nodes (instead of original initial node and the destination node), i.e. I am checking to see if all nodes are visited or not, instead of the destination node. Will this algorithm generate an MST (Minimum Spanning Tree)? (and is it similar to Prim?)

Was it helpful?

Solution

No. Consider a graph that looks like a square, three edges cost 1, and the remaining one costs 2. The MST for this graph has cost 3, but if you start your Dijkstra algorithm on a vertex that contains the expensive edge, that one will be taken as it is the shortest path to the connected node.

Cool ASCII visualization:

    1
 A------B
 |      |
1|      |1
 |      |
 C------D
    2

If you start Dijkstra at C, CD is the shortest path from C to D but it cannot be contained in the MST.

OTHER TIPS

It will not generate a MST as demonstrated by this example graph:

              ... A
 .... 10 -''''    \
S                  2
 '''' 11 -....      \
              ''''' B

If we start Dijkstra's algorithm at node S the resulting tree will look like this:

              ... A
 .... 10 -''''    
S                 
 '''' 11 -....    
              ''''' B

Having a total edge length of 21, while the (in this case unique) MST would be:

              ... A
 .... 10 -''''    \
S                  2
                    \
                    B

Resulting in a total of 12.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top