Time and Space Complexity Analysis of Kruskal Algorithm
Last Updated :
19 Feb, 2024
Kruskal's algorithm is a popular algorithm for finding the Minimum Spanning Tree (MST) of a connected, undirected graph. The time complexity of Kruskal's algorithm is O(E log E), where E is the number of edges in the graph. The space complexity of Kruskal's algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Complexity | Kruskal's Algorithm |
---|
Time Complexity | O(E log E) |
---|
Space Complexity | O(V + E) |
---|
Let’s explore the detailed time and space complexity of the Prim’s Algorithm:
Time Complexity of Kruskal’s Algorithm:
Best Case Time Complexity: O(E log E)
In the best case scenario, the graph's edges are already sorted in non-decreasing order of weights. As a result, the sorting step can be performed in linear time. Hence, the time complexity remains O(E log E) due to the union-find operations and the space complexity is O(V + E).
Average Case Time Complexity: O(E log E)
The average case complexity of Kruskal's algorithm is the same as the best and worst cases, O(E log E) for time complexity and O(V + E) for space complexity. This is because the algorithm's performance is mainly determined by the sorting step, which dominates the overall complexity.
Worst Case Time Complexity: O(E log E)
In the worst case, where the edges are sorted in non-increasing order of weights, the sorting step will take O(E log E) time. The union-find operations also contribute to the time complexity. The space complexity remains O(V + E) regardless of the case due to the data structures used.
Auxiliary Space of Kruskal Algorithm:
The auxiliary space of Kruskal's algorithm is the additional space required by the algorithm beyond the space needed to store the input graph. This space is used for data structures such as the priority queue and the disjoint-set data structure.
- The priority queue is used to store the edges of the graph, sorted by their weight. The disjoint-set data structure is used to keep track of which vertices are in the same connected component.
- The size of the priority queue is O(E), where E is the number of edges in the graph. The size of the disjoint-set data structure is O(V), where V is the number of vertices in the graph.
Therefore, the auxiliary space of Kruskal's algorithm is O(E + V).
Similar Reads
Time and Space Complexity Analysis of Prim's Algorithm 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
3 min read
Time and Space Complexity of DFS and BFS Algorithm The time complexity of both Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms is O(V + E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of DFS is O(V), where V represents the number of vertices in the graph, and for BFS, it is O(V
2 min read
Time and Space Complexity of Dijkstraâs Algorithm The time complexity of Dijkstra's Algorithm is typically O(V2) when using a simple array implementation or O((V + E) log V) with a priority queue, where V represents the number of vertices and E represents the number of edges in the graph. The space complexity of the algorithm is O(V) for storing th
3 min read
Time and Space Complexity Analysis of Binary Search Algorithm Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. Example of Binary Search AlgorithmAspectComplexityTime ComplexityO(log n)Space ComplexityO(1)
3 min read
Time and Space Complexity Analysis of Tree Traversal Algorithms Let us discuss the Time and Space complexity of different Tree Traversal techniques, such as Inorder Traversal, Preorder Traversal, Postorder Traversal, etc. Time Complexity of Tree Traversal AlgorithmsLet us see different corner cases: Complexity function T(n) â for all problems where tree traversa
2 min read