Kruskal's algorithm is a greedy algorithm that is used to find the minimum spanning tree (MST) of a connected, undirected graph. The algorithm starts by sorting all the edges in the graph by their weight, in ascending order. Then, it iterates through the sorted edges and adds them to the MST, if adding the edge doesn't form a cycle. The process continues until a MST is formed. Kruskal's algorithm can also be used to find the maximum spanning tree by iterating through the edges in descending order.
Kruskal's algorithm is different from Prim's algorithm as it starts with the edges and not a vertex, also it uses UnionFind data structures to check for cycles instead of keeping track of the vertices already in the tree.
Kruskal's algorithm is often used when the graph is sparse (having less edges) and the cost of adding an edge is very high, because it avoids adding edges that make a cycle and only adds edges that connect the graph together.
#kruskalalgorithm
#kruskal'salgorithminhindi