What is the difference in Kruskal's and Prim's algorithm?
-
-
Answer:
The basic difference is in which edge you choose to add next to the spanning tree in each step. In Prim's, you always keep a connected component, starting with a single vertex. You look at all edges from the current component to other vertices and find the smallest among them. You then add the neighbouring vertex to the component, increasing its size by 1. In N-1 steps, every vertex would be merged to the current one if we have a connected graph. In Kruskal's, you do not keep one connected component but a forest. At each stage, you look at the globally smallest edge that does not create a cycle in the current forest. Such an edge has to necessarily merge two trees in the current forest into one. Since you start with N single-vertex trees, in N-1 steps, they would all have merged into one if the graph was connected.
Raziman T.V. at Quora Visit the source
Other answers
Both Prim's algorithm and Kruskal's algorithm are greedy algorithms for finding the Minimum Spanning Tree. For Prim's algorithm, the graph has to be connected, but that is not true in the case of Kruskal's algorithm. In Prim's algorithm, the next edge in the MST shall be the cheapest edge in the current vertex. In Kruskal's algorithm, we shall choose the cheapest edge, but it may not be in the current vertex. Prim's algorithm is found to run faster in dense graphs with more number of edges than vertices, whereas Kruskal's algorithm is found to run faster in sparse graphs.
Robin Thomas
When to use each of these algorithms? Answer: 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 http://en.wikipedia.org/wiki/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. Source: http://stackoverflow.com/questions/1195872/kruskal-vs-prim
Prabhakar Bikkaneti
Both Prim's algorithm and Kruskal's algorithm are greedy algorithmsfor finding the Minimum Spanning Tree. For Prim's algorithm, the graph has to be connected, but that is not true in the case of Kruskal's algorithm. In Prim's algorithm, the next edge in the MST shall be the cheapest edge in the current vertex. What is the difference between Kruskalâs and Primâs Algorithm? ⢠Primâs algorithm initializes with a node, whereas Kruskalâs algorithm initiates with an edge. ⢠Primâs algorithms span from one node to another while Kruskalâs algorithm select the edges in a way that the position of the edge is not based on the last step. ⢠In primâs algorithm, graph must be a connected graph while the Kruskalâs can function on disconnected graphs too. ⢠Primâs algorithm has a time complexity of O(V2), and Kruskalâs time complexity is O(logV). Here is the code for implementation of prim's algorithm: Here is the code for implementation of prim's algorithm:
Ashutosh Pawar
In terms of visualizing the process of construction the difference is.... In Prim's algorithm at any point of time, the set of selected edges will form a single tree. In Kruskal's algorithm at any point of time, the set of selected edges need not belong to the same tree. But at the end we will have a single spanning tree. In terms of implementation the difference is... In Kruskal's every edge is considered only once. Either it is selected or rejected. In Prim's certain edges are considered more than once. So in Kruskal's implementation we can use heap structure for selecting the next best edge efficiently where as in Prim's it is not possible.
Gadiraju Venkata Padma Raju
Almost every difference between the two is already mentioned in the other answers.One advantage of Prim's algorithm is that it has a version which runs in O(V^2). Consider a graph with V vertices and V*(V-1)/2 edges (complete graph). Then Kruskal's runs in O(ElogV) = O(V^2logV), while Prim's runs in O(V^2) when we don't use binary heap. So if E ~ V^2 (the graph is dense) then this version of Prim's algorithm which is O(V^2) can be used.Also, at any instant, prim's algorithm gives connected component as well as it works only on connected graph in its basic form but kruskal's algorithm can generate forest(disconnected components) at any instant as well as it can work on disconnected components without any modification. Prim's can also be easily modified to work for disconnected graph, but Kruskal works without modifications. Minimum spanning tree of the entire graph is equal to sum of minimum spanning tree's of each of it's disconnected components, therefore Prim's works in this case.
Priyanka Mishra
In Kruskalâs algorithm it begins with an edge, but in Prim's algorithm it start with a node. In Kruskal's Algorithm select the next edge in a haphazard way but in increasing order while in Prim's algorithm it move from one node to another. Kruskal's algorithm works on both connected and disconnected graph while in Prim's algorithm restricted on connected graph. Kruskal's has a time complexity of O(logV) while Prim's time complexity is O(V2).
Vishal Varshney
Related Q & A:
- What is the difference between M.Tech and M.S?Best solution by Quora
- What is the difference between a Computer science (A.S.) and Computer Information Systems (A.S?Best solution by Yahoo! Answers
- What is the difference between a B.S. and B.A. in psychology?Best solution by Yahoo! Answers
- What is the difference between B.Tech I.T and C.S.E?Best solution by quora.com
- Is there a big difference between men's and women's snowboard gear?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.