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?)
Asked
Active
Viewed 180 times
2 Answers
0
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.

us2012
- 16,083
- 3
- 46
- 62
-
My goal is not to go from C to D, I am going to start from C, and while there is a node that is not seen, I am going to continue adding nodes. So, I am going to select A, then B, then D. Which is an MST – sheidaei Feb 10 '13 at 02:47
-
That's not Dijkstra then. Dijkstra in general is an algorithm that finds shortest paths from a distinguished node to all other nodes. You should make the description of the algorithm you are actually talking about more precise in that case. – us2012 Feb 10 '13 at 02:48
-
I am guessing you are right, the one I had in mind is more like Prime than Dijkstra, since Dijkstra calculate from one node the shortest path, it is not creating the MST – sheidaei Feb 10 '13 at 03:03
0
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.

jix
- 329
- 3
- 5