Difference between Prim's and Dijkstra's algorithms?

前端 未结 15 1875
既然无缘
既然无缘 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:23

    The only difference I see is that Prim's algorithm stores a minimum cost edge whereas Dijkstra's algorithm stores the total cost from a source vertex to the current vertex.

    Dijkstra gives you a way from the source node to the destination node such that the cost is minimum. However Prim's algorithm gives you a minimum spanning tree such that all nodes are connected and the total cost is minimum.

    In simple words:

    So, if you want to deploy a train to connecte several cities, you would use Prim's algo. But if you want to go from one city to other saving as much time as possible, you'd use Dijkstra's algo.

提交回复
热议问题