Difference between Prim's and Dijkstra's algorithms?

前端 未结 15 1863
既然无缘
既然无缘 2020-12-07 06:53

What is the exact difference between Dijkstra\'s and Prim\'s algorithms? I know Prim\'s will give a MST but the tree generated by Dijkstra will also be a MST. Then what is

15条回答
  •  生来不讨喜
    2020-12-07 07:40

    Dijsktra's algorithm finds the minimum distance from node i to all nodes (you specify i). So in return you get the minimum distance tree from node i.

    Prims algorithm gets you the minimum spaning tree for a given graph. A tree that connects all nodes while the sum of all costs is the minimum possible.

    So with Dijkstra you can go from the selected node to any other with the minimum cost, you don't get this with Prim's

提交回复
热议问题