Module 9 · Graphs and Tries · Lesson 32 of 40

Task order, groups and cheap networks

Topological sort for dependencies, union-find for grouping, and minimum spanning trees with Kruskal and Prim.

In this lesson · about 25 minutes

  • Order tasks with dependencies using Kahn's algorithm
  • Group items with union-find
  • Explain what a minimum spanning tree is

Topological sort: doing things in the right order

To get dressed, socks come before shoes, and a shirt before a jacket. Tasks with "must come before" rules form a directed graph with no cycles (a DAG). A topological order lists the tasks so every rule is respected.

Kahn's algorithm uses each task's in-degree, the number of tasks it still waits for:

  1. Put every task with in-degree 0 (waiting for nothing) in a queue.
  2. Take one out, add it to the order, and lower the in-degree of each task that depended on it. Any that reach 0 join the queue.
  3. If some tasks never reach 0, there is a cycle: no valid order exists.

Used for course prerequisites, build systems and spreadsheets. Time: O(V + E).

Union-find: who is in which group?

Union-find (also called disjoint set union, DSU) keeps track of groups. Each item points to a "parent"; following parents leads to the group's leader.

  • find(x): follow parents up to the leader.
  • union(a, b): join two groups by pointing one leader at the other.

Two tricks make it almost O(1) per operation: path compression (point items straight at the leader after a find) and union by size (hang the smaller group under the bigger one). Union-find can also spot cycles: if an edge joins two items that already share a leader, it closes a loop.

Minimum spanning tree (MST)

You must connect several towns with cables, using as little cable as possible. A spanning tree uses edges to connect all vertices with no cycles. The minimum spanning tree has the smallest total weight.

  • Kruskal: sort all edges from cheapest; add each edge unless it makes a cycle (checked with union-find).
  • Prim: grow one tree from a start vertex, always adding the cheapest edge that reaches a new vertex (using a heap).

Both run in about O(E log E). Both are greedy: they always take the cheapest safe choice.

Try it: getting dressed in order

Exercise

Course order

Write course_order(graph). graph maps each course to the courses that need it first. Return a valid order using Kahn's algorithm, or None if there is a cycle. Start the queue with the zero in-degree courses in sorted order so the answer is predictable.

Code that runs before yours
from collections import deque

Exercise

Count friend groups with union-find

There are n people numbered 0 to n − 1, and a list of friendship pairs. Friends of friends are in the same group. Write groups(n, pairs) that returns the number of groups. find is written for you; finish union.

Check your understanding0 of 3 correct

Question 1

1.Kahn's algorithm can't place every task. What does that mean?

Question 2

2.What does find(x) return in union-find?

Question 3

3.Kruskal's algorithm adds edges in which order?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.