Open In App

Time and Space Complexity Analysis of Prim’s Algorithm

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The time complexity of Prim’s algorithm is O(V2) using an adjacency matrix and O((V +E) log V) using an adjacency list, where V is the number of vertices and E is the number of edges in the graph. The space complexity is O(V+E) for the priority queue and O(V2) for the adjacency matrix representation. The algorithm’s time complexity depends on the data structure used for storing vertices and edges, impacting its efficiency in finding the minimum spanning tree of a graph.

Aspect Complexity
Time Complexity O((V + E) log V)
Space Complexity O(V + E)

Let’s explore the detailed time and space complexity of the Prim’s Algorithm:

Time Complexity Analysis of Prim’s Algorithm:

Best Case Time Complexity: O(E log V)

  • In the best-case scenario, the graph is already a minimum spanning tree (MST) or consists of disconnected components.
  • Each edge added to the MST is the smallest among all available edges.
  • The time complexity in this case is O(E log V), where E is the number of edges and V is the number of vertices.
  • The algorithm selects the minimum-weight edge at each step, and the priority queue operations are optimized.

Average Case Time Complexity: O((V + E) log V)

  • In the average case, the edges of the graph are randomly distributed, and the algorithm’s performance depends on the density of edges.
  • On average, each vertex has a constant number of edges adjacent to it.
  • The time complexity in the average case is typically O((V + E) log V), where V is the number of vertices and E is the number of edges.
  • Priority queue operations and updates (decrease-key operations) may vary, leading to an average logarithmic time complexity for each operation.

Worst Case Time Complexity: O((V + E) log V)

  • In the worst-case scenario, the graph is densely connected, and each edge added to the MST results in multiple updates in the priority queue.
  • The time complexity in the worst case is O((V + E) log V), where V is the number of vertices and E is the number of edges.
  • This worst-case complexity arises when each edge insertion or update operation in the priority queue takes logarithmic time

Auxiliary Space of Prim’s Algorithm:

The auxiliary space complexity of Prim’s Algorithm is O(V+E) for the priority queue used to store vertices and their key values during the algorithm’s execution.

Priority Queue/Min-Heap:

  • Prim’s Algorithm employs a priority queue or min-heap to efficiently select the minimum-weight edge at each step.
  • The space complexity of the priority queue depends on the number of edges in the graph.
  • In the worst-case scenario, where all edges are included in the priority queue, the space complexity of the priority queue can be O(E), where E is the number of edges.

Visited Set:

  • Prim’s Algorithm needs a data structure to track which vertices have already been included in the minimum spanning tree (MST).
  • This data structure (often a boolean array, set, or similar) keeps track of visited vertices.
  • The space complexity of this data structure would be O(V), where V is the number of vertices.

Overall Complexity:

  • Combining the space requirements of the priority queue and the visited set, the auxiliary space complexity of Prim’s Algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the graph.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads