What's the difference between prim and dijkstra's algorithm?

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

Was this solution helpful to you?

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

Just Added Q & A:

Find solution

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.