Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs

Minimum Spanning Tree

O(E log V)

Kruskal: sort edges, add the cheapest that doesn't form a cycle (union-find). Prim: grow from a node via a heap.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn firstUnion-Find (Disjoint Set)Greedy
0%
mastery
0/12 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Connect all points/cities at minimum total cost, or the minimax (bottleneck) edge between two vertices — it lies on the MST.

  1. 1Brute force

    Try all spanning trees.

  2. 2The observation

    Exponential choices: the cut property makes the cheapest crossing edge always safe.

  3. 3Why it works

    Cut property: the edges chosen so far are always a subset of some MST.

Reach for it when

  • +minimax edge between two vertices (bottleneck path lies on MST)

Not when

  • ×The graph is directeduse Minimum arborescence (a different algorithm)
  • ×You want shortest distances from a source (a shortest-path tree is not an MST)use Dijkstra
  • ×You think MST minimises each pairwise distance — it only minimises the totaluse Shortest paths for pairwise distances
  • ×Only some vertices must be connected (Steiner tree)use Not an MST problem

Practice

Next · Foundation

Connecting Cities With Minimum Cost

Kruskal = sort edges + DSU.

0/12 solved
  • Connecting Cities With Minimum CostFoundationFoundationPremiumMedium
  • Road ReparationApply it · Same at CSES scale, report impossible.Apply itCSESMedium
  • Min Cost to Connect All PointsOne change · Complete graph on points.One changeGoogle, UberMedium
  • Optimize Water Distribution in a VillageTwists · Nodes also have a cost (build a well).TwistsPremiumHard
  • Find Critical and Pseudo-Critical Edges in Minimum Spanning TreeMastery checkMastery checkHard
  • New Roads QueriesCSESMedium
  • Transfer Speeds SumCSESHard
  • MST Edge CheckCSESHard
  • MST Edge Set CheckCSESHard
  • MST Edge CostCSESHard
  • Qpwoeirut and VerticesCodeforces2300
  • Graph and QueriesCodeforces2600

Pitfalls

Classic bugs

  • !Kruskal: sort edges ascending and add an edge only if find(u) != find(v) — otherwise it forms a cycle.
  • !Prim: grow from one node with a min-heap of crossing edges.
Go deepervariants and techniques · 3 combinations · resources

Variants and techniques

  • Kruskal Reconstruction TreeCP 2400+During Kruskal, every successful union creates a new node whose value is the edge weight and whose children are the two roots. The bottleneck weight between u and v is then their LCA's value, and 'vertices reachable from v using edges <= x' is a single subtree.

Combinations

  • Minimum Spanning Tree+Union-Find (Disjoint Set)+Greedy

    connect all nodes at minimum cost

    Minimum Spanning Tree:
    the goal (spanning tree)
    Union-Find (Disjoint Set):
    cycle check per edge
    Greedy:
    process edges by increasing weight (cut property)

    Connecting Cities With Minimum CostRoad ReparationMin Cost to Connect All Points

  • Kruskal Reconstruction Tree+Binary Lifting & LCA

    the minimum possible maximum edge between u and v (or the first time they connect) for many pairs

    Kruskal Reconstruction Tree:
    turns each union into a node valued by the edge weight
    Binary Lifting & LCA:
    LCA queries, or jumping to the highest ancestor with value <= x

    Qpwoeirut and VerticesNew Roads Queries

  • Kruskal Reconstruction Tree+Euler Tour / Tree Flattening+Segment Tree / Fenwick (BIT)

    edges deleted over time, plus queries for the max value in v's current component

    Kruskal Reconstruction Tree:
    the threshold-reachable set is a subtree
    Euler Tour / Tree Flattening:
    that subtree is a contiguous leaf range
    Segment Tree / Fenwick (BIT):
    range max with point delete over the leaf order

    Graph and Queries

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
Floyd-Warshall1D / Fibonacci-style
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial