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.
Worst case time complexity of both algorithms is same.
Worst case time complexity of Kruskal is better than Prim
Worst case time complexity of Prim is better than Kruskal
None of these
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.
For more reference Please visit:
Hence Option (A) is correct.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!