Time Complexity of Kruskals Algorithm
We shall talk about Kruskal's algorithm in this post. We shall also examine the Kruskal's algorithm's difficulty, functionality, example, and implementation here.
However, we need first comprehend the fundamental concepts, such as spanning tree and least spanning tree, before going on to the technique.
An undirected edge-weighted graph's smallest spanning forest is found via Kruskal's method. Finding a minimal spanning tree is done if the graph is linked. (A minimal spanning tree is a collection of edges that forms a tree with every vertex in a linked graph where the weighted average of all the edges in the tree is minimized. A minimal spanning forest for a disconnected graph is made up of a minimum spanning tree for each linked component.) In graph theory, it is known as a greedy method since it continuously adds the next lowest-weight edge that won't cycle to the minimal spanning forest.
An undirected connected graph's spanning tree is a sub graph of that graph.
Minimum Spanning Tree - A minimum spanning tree is one in which the total edge weights are at their lowest value. The weight of the spanning tree is the total of the weights assigned to its edges.
Let's get to the major point now.
For a linked weighted graph, the least spanning tree is determined using Kruskal's Algorithm. The algorithm's primary goal is to identify the subset of edges that will allow us to pass through each graph vertex. Instead of concentrating on a global optimum, it adopts a greedy strategy that seeks the best outcome at each step.
How is the Kruskal algorithm implemented?
With Kruskal's method, we begin with the edges that have the lowest weight and keep adding edges until we get the desired result. The following are the steps to apply Kruskal's algorithm:
The uses of Kruskal's algorithm include:
Kruskal's Algorithm Example
Let's now use an example to demonstrate how Kruskal's algorithm functions. An illustration will make it simpler to comprehend Kruskal's algorithm.
Let's say that a weighted graph is
The table below provides the edge weights for the graph above.
Sort the edges listed above now according to ascending weights.
Let's start building the least spanning tree right now.
Step 1: Add the edge AB with weight 1 to the MST in step 1 first.
Step 2: Since the edge DE with weight 2 isn't producing the cycle, add it to the MST.
Step 3: Add the edge BC with weight 3 to the MST in step 3 because it does not produce a cycle or loop.
Step 4: Choose the edge CD with weight 4 to the MST at this point since the cycle is not being formed.
Step 5 - Next, choose weight 5 on the edge AE. This edge must be eliminated since including it would start the cycle.
Step 6: Pick the edge of the AC using weight in step 6." This edge must be eliminated since including it would start the cycle.
Step 7: Pick the edge AD with weight 10 in step 7. Throwing away this edge will prevent the cycle from starting.
Step 8: As a result, the final minimal spanning tree created using Kruskal's approach from the provided weighted graph is -
The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.
In the above tree, the number of edges now matches the number of vertices minus 1. The algorithm so ends here.
Time Complexity of Kruskal's Algorithm
The Kruskal method has an O(E logE) or O(V logV) time complexity, where E is the number of edges and V is the number of vertices.
A connected, undirected graph with all of its vertices is described as a spanning tree, which is a tree-like sub graph of the graph. Or, to put it in Layman's terms, it is a subset of the graph's edges that together form an acyclic tree, of which the graph's nodes are all members.
With the additional restriction of having the lowest weights among all conceivable spanning trees, the minimal spanning tree has all the characteristics of a spanning tree. Similar to spanning trees, a graph may have several potential MSTs.
Properties of a Spanning Tree
The following principles are held by a spanning tree:
Implementation of the Kruskal algorithm
Let's now examine how Kruskal's method was put into practice.
Create a C++ program that implements Kruskal's algorithm.
Following are the edges in the constructed MST 2 -- 3 == 4 0 -- 3 == 5 0 -- 1 == 10 Minimum Cost Spanning Tree: 19
Time Complexity: Either O (E*logE) or O (E*logV)
Auxiliary Space: O (V + E), where V is the total number of edges in the graph and E is the total number of vertices.