Python 3D Academyবাংলা

Minimum Spanning Trees in Python

Chapter 55: Minimum Spanning Trees

4 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Connecting every village with the least cable

    The cheapest way to connect everything is a tree: it uses exactly V − 1 cables for V villages and never closes a loop, because a cable that closes a loop can be removed without disconnecting anyone.

  2. Kruskal's algorithm, with union-find

    Kruskal's algorithm tries the edges cheapest first and keeps each one that joins two different groups; union-find (Chapter 53) answers "same group?" in nearly constant time.

  3. Prim's algorithm, with a heap

    Prim's algorithm grows a single tree from a start, each step adding the cheapest edge that leaves the tree, found with a heap of the edges leaving it.

  4. Why the cheapest crossing edge is always safe

    For any split of the vertices into two sides, the cheapest edge crossing between the sides belongs to some minimum spanning tree (the cut property).