Sitemap

The Art of Building Bridges (Without Creating Loops)

Kruskal’s Algorithm

DSA for Android Developers Episode 3

5 min readAug 17, 2025

--

Picture this: You’re the mayor of an archipelago, seven islands scattered across a bay. Your citizens are demanding bridges to connect every island, but the city budget is tight. Each potential bridge has a different cost based on distance and engineering complexity. Your mission? Connect all islands with the minimum total cost, without creating any unnecessary loops.

Press enter or click to view image in full size

This isn’t just a thought experiment. It’s the exact challenge faced by city planners, network engineers, and even your favorite streaming service when building content delivery networks. And there’s an algorithm that solves it perfectly: Kruskal’s algorithm.

Let’s explore how this elegant algorithm thinks about connections differently than its cousin, Prim’s and why that difference matters.

The Minimum Spanning Tree: A Different Perspective

While Prim’s algorithm grows a tree like a spreading vine, Kruskal’s takes a completely different approach. It’s like a matchmaker at a speed-dating event, pairing up the most compatible couples first, making sure no one ends up in a love triangle.

The Core Insight

Kruskal’s algorithm has one brilliant idea: Sort all possible connections by cost, then add them one by one, skipping any that would create a cycle.
That’s it. The entire algorithm. But hidden in this simplicity is profound elegance.

How Kruskal’s Algorithm Thinks

The Mental Model: Building a Highway System

Imagine you’re connecting cities with highways:
1. List all possible highways with their construction costs
2. Sort them from cheapest to most expensive
3. For each highway, in order:
• Will it connect two previously unconnected regions? Build it!
• Will it create a redundant loop? Skip it!
4. Stop when all cities are connected
The magic? You’ll automatically end up with the cheapest possible network.

Visualizing the Process

Let’s watch Kruskal’s algorithm connect 6 cities:

Press enter or click to view image in full size

Notice how we built the network by always choosing the cheapest available connection, like a budget-conscious shopper at a sale!

The Secret Weapon: Union-Find

The trickiest part of Kruskal’s algorithm is quickly determining whether an edge would create a cycle. Enter the Union-Find (also called Disjoint Set Union) data structure, Kruskal’s secret weapon.

Understanding Union-Find

Think of Union-Find like managing friend groups at a party:
• Find: Which group does this person belong to?
• Union: These two groups just became friends — merge them!
With clever optimizations (path compression and union by rank), both operations become nearly O(1)!

The Union-Find Implementation

Let me show you this powerful data structure before we dive into Kruskal’s:

Press enter or click to view image in full size

This data structure is the reason Kruskal’s algorithm is so efficient — it can check and merge components in nearly constant time!

Kruskal’s Algorithm: The Complete Implementation

Now let’s see how Kruskal’s algorithm uses Union-Find to build the MST:

import java.util.*;

public class KruskalMST {

static class Edge implements Comparable<Edge> {
int source, destination, weight;

public Edge(int source, int destination, int weight) {
this.source = source;
this.destination = destination;
this.weight = weight;
}

@Override
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}

static class Graph {
private int vertices;
private List<Edge> edges;

public Graph(int vertices) {
this.vertices = vertices;
this.edges = new ArrayList<>();
}

public void addEdge(int source, int destination, int weight) {
edges.add(new Edge(source, destination, weight));
}

public List<Edge> kruskalMST() {
// Step 1: Sort all edges by weight
Collections.sort(edges);

// Step 2: Initialize Union-Find
UnionFind uf = new UnionFind(vertices);

// Step 3: Process edges in order
List<Edge> mst = new ArrayList<>();

for (Edge edge : edges) {
// Try to union the two vertices
if (uf.union(edge.source, edge.destination)) {
mst.add(edge);

// Early termination: we need exactly V-1 edges
if (mst.size() == vertices - 1) {
break;
}
}
}

return mst;
}
}

public static void main(String[] args) {
// Example: Connecting data centers
Graph network = new Graph(6);
String[] datacenters = {"NYC", "LA", "Chicago", "Dallas", "Seattle", "Miami"};

// Add connections with costs (in thousands of dollars)
network.addEdge(0, 1, 2800); // NYC - LA
network.addEdge(0, 2, 790); // NYC - Chicago
network.addEdge(0, 5, 1280); // NYC - Miami
network.addEdge(1, 2, 2015); // LA - Chicago
network.addEdge(1, 3, 1435); // LA - Dallas
network.addEdge(1, 4, 1135); // LA - Seattle
network.addEdge(2, 3, 925); // Chicago - Dallas
network.addEdge(2, 4, 2070); // Chicago - Seattle
network.addEdge(3, 5, 1310); // Dallas - Miami
network.addEdge(4, 5, 3300); // Seattle - Miami

List<Edge> mst = network.kruskalMST();

System.out.println("Optimal fiber optic connections:");
int totalCost = 0;
for (Edge edge : mst) {
System.out.printf("%s - %s: $%dk\n",
datacenters[edge.source],
datacenters[edge.destination],
edge.weight);
totalCost += edge.weight;
}
System.out.printf("Total cost: $%dk\n", totalCost);
}
}

Time Complexity: O(E log E) where E is the number of edges. The sorting dominates the runtime.

Kruskal’s vs. Prim’s: When to Use Which?

Use Kruskal’s When:

  • Your graph is sparse (few edges relative to vertices.
  • You have all edges available upfront
  • Edges are already sorted or easy to sort
  • You’re working with edge lists
  • You need to process edges in parallel

Use Prim’s When:

  • Your graph is dense (many edges)
  • You’re starting from a specific vertex
  • You’re using adjacency lists
  • You need to grow the solution incrementally
  • Memory is a concern (Prim’s can be more memory-efficient)

Real-World Magic: Where Kruskal’s Excels

1. Telecommunications: The Backbone of the Internet

Internet Service Providers use Kruskal’s algorithm to design backbone networks. When connecting cities across continents, the edge list (city pairs with cable costs) is natural to work with.

2. Image Segmentation

In computer vision, Kruskal’s algorithm helps segment images:
1. Treat each pixel as a vertex
2. Connect neighboring pixels with edges weighted by color difference
3. Build an MST and remove heavy edges to separate regions
This technique powers some Instagram filters!

3. Clustering Algorithms

Single-linkage clustering uses Kruskal’s algorithm:
1. Build the complete MST
2. Remove the k-1 heaviest edges to create k clusters
This works beautifully for geographical clustering — like grouping customers by region for delivery optimization.

4. Approximation Algorithms

The Traveling Salesman Problem’s 2-approximation algorithm often uses Kruskal’s MST as a starting point. While TSP is NP-hard, the MST gives us a good foundation.

Practice Time: Level Up Your Skills

Ready to master Kruskal’s algorithm? Try these problems:

  1. LeetCode #1584: Min Cost to Connect All Points
  2. LeetCode #1489: Find Critical and Pseudo-Critical Edges
  3. SPOJ BLINNET: Optical Fiber Cable Network
  4. Codeforces: Various MST problems in competitions

Start with basic implementation, then try these variations:

  • Find the second-best MST
  • Handle edges being added/removed dynamically
  • Build an MST with constraints (e.g., must include certain edges)

The next time you see a network — whether it’s airlines routes, social connections, or neural pathways — remember Kruskal’s insight: start with the strongest connections, avoid redundancy, and you’ll build something both minimal and complete.

Found this helpful? Follow for more algorithms that bridge theory and practice. Coming next: “Dijkstra’s Algorithm: Teaching Computers to Navigate Like Humans”

--

--

Android Dev Nexus
Android Dev Nexus

Written by Android Dev Nexus

Your friendly neighbourhood developers working at PayPal, turning Android fundamentals into concepts you’ll actually understand. A new post every Tue and Fri.

No responses yet