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?)
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.