Sunday, 23 November 2014

The basic difference is in which edge you choose to add next to the spanning tree in each step.

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.

Diff. b/w Prim's & Kruskal's Alogorithm.

Diff. b/w Prim's & Kruskal's Alogorithm.

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).