ArticleslgStudy

computer science

Kruskal's algorithm

Kruskal's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Kruskal's algorithm rather than just read about it. In short: Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree.

Kruskal's algorithm — main illustration
Kruskal's algorithm — illustration

Key takeaways

  • Kruskal's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Kruskal's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Kruskal's algorithm from memory before moving on to harder problems.

Reference excerpt

Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree. It is a greedy algorithm that in each step adds to the forest the lowest-weight edge that will not form a cycle. The key steps of the algorithm are sorting and the use of a disjoint-set data structure to detect cycles. Its running time is dominated by the time to sort all of the graph edges by their weight. A minimum spanning tree of a connected weighted graph is a connected subgraph, without cycles, for which the sum of the weights of all the edges in the subgraph is minimal. For a disconnected graph, a minimum spanning forest is composed of a minimum spanning tree for each connected component. This algorithm was first published by Joseph Kruskal in 1956, and was rediscovered soon afterward by Loberman & Weinberger (1957). Other algorithms for this problem include Prim's algorithm, Borůvka's algorithm, and the reverse-delete algorithm.

Algorithm The algorithm performs the following steps:

Create a forest (a set of trees) initially consisting of a separate single-vertex tree for each vertex in the input graph. Sort the graph edges by weight. Loop through the edges of the graph, in ascending sorted order by their weight. For each edge: Test whether adding the edge to the current forest would create a cycle. If not, add the edge to the forest, combining two trees into a single tree. At the termination of the algorithm, the forest forms a minimum spanning forest of the graph. If the graph is connected, the forest has a single component and forms a minimum spanning tree.

Pseudocode The following code is implemented with a disjoint-set data structure. It represents the forest F as a set of undirected edges, and uses the disjoint-set data structure to efficiently determine whether two vertices are part of the same tree.

function Kruskal(Graph G) is F:= ∅ for each v in G.Vertices do MAKE-SET(v)

for each {u, v} in G.Edges ordered by increasing weight({u, v}) do if FIND-SET(u) ≠ FIND-SET(v) then F := F ∪ { {u, v} } UNION(FIND-SET(u), FIND-SET(v)) return F

Complexity For a graph with E edges and V vertices, Kruskal's algorithm can be shown to run in time O(E log E) time, with simple data structures. This time bound is often written instead as O(E log V), which is equivalent for graphs with no isolated vertices, because for these graphs V / 2 ≤ E < V2 and the logarithms of V and V2 are again within a constant factor of each other. To achieve this bound, first sort the edges by weight using a comparison sort in O(E log E) time. Once sorted, it is possible to loop through the edges in sorted order in constant time per edge. Next, use a disjoint-set data structure, with a set of vertices for each component, to keep track of which vertices are in which components. Creating this structure, with a separate set for each vertex, takes V operations and O(V) time. The final iteration through all edges performs two find operations and possibly one union operation per edge. These operations take amortized time O(α(V)) time per operation, giving worst-case total time O(E α(V)) for this loop, where α is the extremely slowly growing inverse Ackermann function. This part of the time bound is much smaller than the time for the sorting step, so the total time for the algorithm can be simplified to the time for the sorting step. In cases where the edges are already sorted, or where they have small enough integer weight to allow integer sorting algorithms such as counting sort or radix sort to sort them in linear time, the disjoint set operations are the slowest remaining part of the algorithm and the total time is O(E α(V)).

Example

Proof of correctness The proof consists of two parts. First, it is proved that the algorithm produces a spanning tree. Second, it is proved that the constructed spanning tree is of minimal weight.

Spanning tree Let G {\displaystyle G} be a connected, weighted graph and let Y {\displaystyle Y} be the subgraph of G {\displaystyle G} produced by the algorithm. Y {\displaystyle Y} cannot have a cycle, as by definition an edge is not added if it results in a cycle. Y {\displaystyle Y} cannot be disconnected, since the first encountered edge that joins two components of Y {\displaystyle Y} would have been added by the algorithm. Thus, Y {\displaystyle Y} is a spanning tree of G {\displaystyle G} .

Minimality We show that the following proposition P is true by induction: If F is the set of edges chosen at any stage of the algorithm, then there is some minimum spanning tree that contains F and none of the edges rejected by the algorithm.

Clearly P is true at the beginning, when F is empty: any minimum spanning tree will do, and there exists one because a weighted connected graph always has a minimum spanning tree. Now assume P is true for some non-final edge set F and let T be a minimum spanning tree that contains F. If the next chosen edge e is also in T, then P is true for F + e. Otherwise, if e is not in T then T + e has a cycle C. The cycle C contains edges which do not belong to F + e, since e does not form a cycle when added to F but does in T. Let f be an edge which is in C but not in F + e. Note that f also belongs to T, since f belongs to T + e but not F + e. By P, f has not been considered by the algorithm. f must therefore have a weight at least as large as e. Then T − f + e is a tree, and it has the same or less weight as T. However since T is a minimum spanning tree then T − f + e has the same weight as T, otherwise we get a contradiction and T would not be a minimum spanning tree. So T − f + e is a minimum spanning tree containing F + e and again P holds. Therefore, by the principle of induction, P holds when F has become a spanning tree, which is only possible if F is a minimum spanning tree itself.

… excerpt ends here. Continue reading the full article.

Illustrations

Kruskal's algorithm illustration
Kruskal's algorithm illustration
Kruskal's algorithm illustration
Kruskal's algorithm illustration
Kruskal's algorithm illustration

Worked examples

Example 1 — a first encounter with Kruskal's algorithm

Start with the simplest possible case. Write down what Kruskal's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Kruskal's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Kruskal's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Kruskal's algorithm

In research
Kruskal's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Kruskal's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Kruskal's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, Greedy algorithms, Spanning tree, so understanding it makes those chapters shorter.
In everyday life
Look for Kruskal's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Kruskal's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Kruskal's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Kruskal's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Kruskal's algorithm in simple terms?

Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree.

Why does Kruskal's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Kruskal's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Kruskal's algorithm.

Tags

  • Graph algorithms
  • Greedy algorithms
  • Spanning tree

Keep exploring