Kruskal's algorithm
1. Original procedure of the algorithm
Kruskal's algorithm is used to find the minimal spanning tree of a graph.
We work with a weighted undirected graph where
is a set of
nodes,
is a set of undirected edges and
is
a positive weight of any edge.
- At the beginning of the algorithm initialize a subgraph
containing all the nodes of the graph
and an empty set of edges (At the termination of the algorithm the subgraph
becomes a minimum spanning tree of the graph
.).
- Sort the edges of the graph
according to their weight and process them consecutively from the shortest one.
- Check if addition of the current edge to the subgraph
establishes a cycle.
- If not, add the edge to the subgraph
.
- Otherwise, do not add the edge to the subgraph
.
- If not, add the edge to the subgraph
-
After processing the last edge the subgraph
becomes the minimum spanning tree of the graph
.
When processing the algorithm in the standard way we work with a visual representation
of a graph. When adding an edge to the subgraph
we highlight it directly in the graph (using a different color or any other means of highlighting).
There is a graph
with seven nodes in
Image 1 demonstrating the situation before processing
the fourth edge between the nodes
and
.
After the first three steps of the algorithm the edges
,
,
were added to the subgraph
.
The next one
does not establish a cycle and therefore we can add it to the
subgraph
as well.
Example 1: Weighted undirected graph 
We cannot add the next two edges of the weight
to the subgraph
because they would both establish a cycle of the size
(tripple of the nodes
or
).
To demonstrate Kruskal's algorithm we come with an Animation 2 of the computation and use a concrete weighted undirected graph.
Example 2: Weighted undirected graph 
-
- Start of the algorithm - undirected graph
with seven nodes
,
,
,
,
,
,
Edges of the spanning tree: - Still not processed edges:
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
,
–
–
Do not display this label