What is the Kruskal algorithm? Definition, use 2025

What is the Kruskal algorithm? Definition, use 2025

/ / Import the required packages

Import Java.util. *;

/ / Use the Kruskal algorithm to implement a minimum spanning tree.

Class KruskalAlgorithm {

/ / Create an Edge class to create a graph edge that implements the Comparable interface

implements class edge comparable {

int source, destination, weight;

public int to compare (edge edgeToCompare) {

Return weight – edgeToCompare. the weight;

}

};

/ / Creating a subclass union

class subgroup {

int parent, value;

};

/ / Start top, edge and borderArray

int corner, edge;

So, [];

/ / Using constructor to create graphs

Kruskal algorithm (int corner, int edge) {

this. vertices = corners;

this. edges = edges;

edgeArray = new edge [this. edges];

for (int i = 0; i < edge; + + i)

EdgeArray [i] = new edge () / / Create edges for all edges given by user

}

/ / applyKruskal () function to apply the Kruskal algorithm to find the minimum spanning tree.

null applyKruskal () {

/ / Initialize finalResult array to store the last MST

Edge final result [] = new edge [vertices];

int = 0;

int index = 0;

for (index = 0; index < corner; + + index)

final result [index] = new edge ()

/ / Using sort () method to sort edges for the Kruskal algorithm

Arrays.Sort

/ / Create an array of vertices of subset type for subsets of vertices

subsetArray [] = new subgroup [vertices];

/ / Allocate memory to create vertice subgroups

for (index = 0; index < corner; + + index)

subsetarray [index] = new subgroup

for (int vertex = 0; vertex < vertices; + + vertex) {

vertex.parent = top;

vertex.The value is 0;

}

index = 0.

/ / Use for loop to select the smaller edge from the edges and

Increase the index for the next iteration

while (newEdge < corner-1) {

/ / Create an example of an edge for the next edge

edge nextEdge = new edge ()

nextEdge = EdgeArray [index + +];

int nextSource = findSetOfElement (subsetArray,

nextEdge. Source)

int nextDestination = findSetOfElement (subsetArray, nextEdge. the destination)

/ / Add only if the edge does not form a circle in the minimum spanning tree.

If (the next source!) = next destination) {

final result [newEdge + +] = next edge;

PerformanceUnion (Substare, NextSource, NextDestination)

}

}

for (index = 0; index < newEdge; + + index)

System.out.println (end result [index]. source + “-” + end result [index]. destination + “:” + end result [index]. weight)

}

/ / Create a findSetOfElement () method to get a set of elements

int findSetOfElement (subset subsetArray [] int i) {

If (subgroup array [i]. Parents! = i)

SubsetArray [i]. parent = findSetOfElement (subsetArray, subsetArray [i]. parents).

Return the highest point [i]. The parents;.

}

/ / Create a performUnion () method to organize two sets

void performUnion (subset subsetArray [] int source root, int destination root) {

int nextSourceRoot = findSetOfElement (subsetArray, sourceRoot)

int nextDestinationRoot = findSetOfElement (subsetArray, destinationRoot)

If (subgroup array [nextSourceRoot].value < sub-array [nextDestinationRoot].the price)

Substare [nextSourceRoot].parent = next destination route;

Otherwise if (subgroup array [nextSourceRoot].value > Substare [nextDestinationRoot].the price)

Substare [nextDestinationRoot].parent = next source root;

Other {

Substare [nextDestinationRoot].parent = next source root;

Substare [nextSourceRoot]. Value + +;

}

}

public static zero main (string [] args) {

int v, e;

/ / Get input from users using the scanner class.

Scanner sc = new scanner (System. in)

/ / Show custom messages

System. out. println (Enter the number of vertices:)

/ / Store the value entered by the user in the variable v

v = sc. nextInt ()

/ / Show custom messages

System. out. println (Enter number of edges)

/ / Store the value entered by the user in variable e

E = sc. nextInt ()

Kruskal algorithm graph = new Kruskal algorithm (v, e)

for (int i = 0; i < e; i + +) {

System. out. println (Enter source value for edge [“+ i +”] “)

Graph. EdgeArray [i].source = sc. nextInt ()

System. out. println (Enter destination value for edge [“+ i +”] “)

Graph. EdgeArray [i].destination = sc. nextInt ()

System. out. println (Enter weight for edge [“+ i +”])

Graph. EdgeArray [i].Weight = sc. nextInt ()

}

/ / Call applyKruskal () method to get MST

Graph. applyKruskal ()

}

}

Conclusion

  • Kruskal’s algorithm is a fundamental graph algorithm used to find the minimum spanning tree (MST) of a connected, weighted graph. By utilizing a greedy approach, it efficiently selects edges in increasing order of weight while avoiding cycles. Kruskal’s algorithm is widely used in network design, clustering, and optimizing problems where minimum cost solutions are crucial. Its simplicity and effectiveness make it a valuable tool in both theoretical and practical applications in 2024.

YOU MAY BE INTERESTED IN

Do all ABAPers know Fixed Point Arithmetic?

Use of data elements in SAP ABAP

Transport ABAP Report Variants into a Work Bench Request

admin
admin
https://www.thefullstack.co.in

Leave a Reply