DAA - Kruskal's algorithm(BCA-IV sem KSWU)

Kruskal's algorithm


        The Kruskal's algorithm is a algorithm used to find the minimum spanning tree (MST) of a given graph. 
    A tree that connects all the vertices of the graph with the minimum total edge weight is called minimum spanning tree.

    Here's a step-by-step explanation of Kruskal's algorithm in an easy way:

1. Start with the given graph and sort all the edges in increasing order of their weights.
2. Create an empty set to store the MST.
3. Iterate through the sorted edges, starting from the one with the smallest weight.
4. For each edge, check if adding it to the MST will create a cycle. If adding the edge doesn't form a cycle, add it to the MST.
5. To check for cycles, you can use a disjoint set data structure. Maintain a set for each vertex, and merge the sets when adding an edge to the MST.
6. Repeat steps 4 and 5 until all the edges have been processed or the MST has the desired number of edges (equal to the number of vertices minus one).
7. Once the algorithm finishes, the MST will contain the minimum set of edges that connect all the vertices with the minimum total weight.

     Kruskal's algorithm works by iteratively adding edges with the smallest weights to the MST, making sure that adding an edge doesn't create a cycle. It guarantees that the resulting tree will be a minimum spanning tree.

advantages and disadvantages of kruskal algorithm

Advantages:
1. Efficiency: Kruskal's algorithm has a time complexity of O(E log E), where E is the number of edges. This makes it efficient for large graphs with a large number of edges.
2. Guaranteed Minimum Spanning Tree: Kruskal's algorithm always produces a minimum spanning tree, which is the tree with the minimum total edge weight. It guarantees an optimal solution.

Disadvantages:
1. Slower for Dense Graphs: Kruskal's algorithm is less efficient for dense graphs, where the number of edges is close to the maximum possible number of edges. In such cases, other algorithms like Prim's algorithm may be more efficient.
2. Requires Sorting: Kruskal's algorithm requires sorting the edges based on their weights. This additional sorting step can increase the time complexity of the algorithm.
3. May Not Handle Disconnected Graphs: Kruskal's algorithm assumes the given graph is connected. If the graph is disconnected, the algorithm will find a minimum spanning forest instead of a single spanning tree.



No comments:

Post a Comment