# Algorithms | Greedy Algorithms | Question 5

Which of the following is true about Kruskal and Prim MST algorithms?

Assume that Prim is implemented for adjacency list representation using Binary Heap and Kruskal is implemented using union by rank.

(A)

Worst case time complexity of both algorithms is same.

(B)

Worst case time complexity of Kruskal is better than Prim

(C)

Worst case time complexity of Prim is better than Kruskal

(D)

None of these

Explanation:

Kruskal’s algorithm and Prim’s algorithm are both used to find the minimum spanning tree (MST) of a weighted graph, but they have different approaches.

Kruskal’s algorithm: Kruskal’s algorithm sorts the edges of the graph in the non-decreasing order of their weights and then iterates over the sorted edges, adding them to the MST if they do not create a cycle. It uses the disjoint-set data structure with the union by rank and path compression to efficiently detect cycles and maintain the disjoint sets.

The time complexity of Kruskal’s algorithm is primarily determined by the sorting step, which takes O(E log E) time, where E is the number of edges in the graph. The union-find operations for cycle detection take nearly constant time per operation, approximately O(α(V)), where α is the inverse Ackermann function and V is the number of vertices.

Prim’s algorithm: Prim’s algorithm starts with an arbitrary vertex and grows the MST by adding the minimum-weight edge that connects a vertex in the MST to a vertex outside the MST. It commonly uses a priority queue, often implemented with a binary heap, to efficiently select the minimum-weight edge at each step.

For Prim’s algorithm with a binary heap implementation, the time complexity is O((E + V) log V), where E is the number of edges and V is the number of vertices in the graph. This time complexity arises from performing V extract-min operations on the binary heap and updating the key values of adjacent vertices.

Comparing the time complexities of both algorithms:

• Kruskal’s algorithm: O(E log E + α(V))
• Prim’s algorithm (with binary heap): O((E + V) log V)

Overall, the Worst case time complexity of both algorithms is the same.