## Time Complexity of Kruskals AlgorithmWe 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.
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: - Sort all of the edges first from low weight to high weight.
- Add the edge with the lightest weight to the spanning tree at this point. Reject the edge if it would otherwise result in a cycle.
- Once we have included all of the edges, a minimal spanning tree will be produced.
The uses of Kruskal's algorithm include: - Wiring between cities may be laid out using Kruskal's method.
- It is possible to utilize it to install LAN connections.
## Kruskal's Algorithm ExampleLet'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.
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 AlgorithmThe 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 TreeThe following principles are held by a spanning tree: - The graph and spanning tree both have the same number of vertices (V).
- The spanning tree has a fixed number of edges, which is one fewer than the total number of vertices (E = V-1).
- There should only be one source of component, not more, so that the spanning tree is not disconnected.
- There should be no cycles in the spanning tree, which means it should be acyclic.
- The sum of the edge weights of all the spanning tree's edges is referred to as the overall cost (or weight) of the spanning tree.
- There are several spanning trees that might be used for a graph.
## Implementation of the Kruskal algorithmLet'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
- It takes O (E * logE) time to sort edges.
- After sorting, the find-union method is used to iteratively traverse all edges. The maximum time for the find and union procedures is O (logV).
- O (E * logE + E * logV) time is the measure of total complexity.
- O (logV) and O (logE) are same since the value of E can only be at most O (V2) in size.
- O (E* logE) or O (E*logV) is the general temporal complexity as a result.
Next TopicTravelling Salesman Problem |