When should I use Kruskal as opposed to Prim (and vice versa)?

后端 未结 10 1327
小鲜肉
小鲜肉 2020-12-04 04:27

I was wondering when one should use Prim\'s algorithm and when Kruskal\'s to find the minimum spanning tree? They both have easy logics, same worst cases, and only differenc

相关标签:
10条回答
  • 2020-12-04 04:56

    Use Prim's algorithm when you have a graph with lots of edges.

    For a graph with V vertices E edges, Kruskal's algorithm runs in O(E log V) time and Prim's algorithm can run in O(E + V log V) amortized time, if you use a Fibonacci Heap.

    Prim's algorithm is significantly faster in the limit when you've got a really dense graph with many more edges than vertices. Kruskal performs better in typical situations (sparse graphs) because it uses simpler data structures.

    0 讨论(0)
  • 2020-12-04 04:57

    Kruskal can have better performance if the edges can be sorted in linear time, or are already sorted.

    Prim's better if the number of edges to vertices is high.

    0 讨论(0)
  • 2020-12-04 05:06

    I found a very nice thread on the net that explains the difference in a very straightforward way : http://www.thestudentroom.co.uk/showthread.php?t=232168.

    Kruskal's algorithm will grow a solution from the cheapest edge by adding the next cheapest edge, provided that it doesn't create a cycle.

    Prim's algorithm will grow a solution from a random vertex by adding the next cheapest vertex, the vertex that is not currently in the solution but connected to it by the cheapest edge.

    Here attached is an interesting sheet on that topic.enter image description hereenter image description here

    If you implement both Kruskal and Prim, in their optimal form : with a union find and a finbonacci heap respectively, then you will note how Kruskal is easy to implement compared to Prim.

    Prim is harder with a fibonacci heap mainly because you have to maintain a book-keeping table to record the bi-directional link between graph nodes and heap nodes. With a Union Find, it's the opposite, the structure is simple and can even produce directly the mst at almost no additional cost.

    0 讨论(0)
  • 2020-12-04 05:10

    Prim's is better for more dense graphs, and in this we also do not have to pay much attention to cycles by adding an edge, as we are primarily dealing with nodes. Prim's is faster than Kruskal's in the case of complex graphs.

    0 讨论(0)
  • 2020-12-04 05:10

    In kruskal Algorithm we have number of edges and number of vertices on a given graph but on each edge we have some value or weight on behalf of which we can prepare a new graph which must be not cyclic or not close from any side For Example

    graph like this _____________ | | | | | | |__________| | Give name to any vertex a,b,c,d,e,f .

    0 讨论(0)
  • 2020-12-04 05:12

    One important application of Kruskal's algorithm is in single link clustering.

    Consider n vertices and you have a complete graph.To obtain a k clusters of those n points.Run Kruskal's algorithm over the first n-(k-1) edges of the sorted set of edges.You obtain k-cluster of the graph with maximum spacing.

    0 讨论(0)
提交回复
热议问题