08 Minumum Spanning Tree
08 Minumum Spanning Tree
OF ALGORITHMS
1
Minumum Cost Spanning Trees (MCST)
Definition: A spanning tree of a connected graph is its connected acyclic subgraph (i.e., a tree)
that contains all the vertices of the graph.
A minimum spanning tree of a weighted connected graph is its spanning tree of the smallest
weight, where the weight of a tree is defined as the sum of the weights on all its edges.
The minimum spanning tree problem is the problem of finding a minimum spanning tree for a
given weighted connected graph.
Main Definations
Graph
A graph G=(V,E) consists of a set of vertices V (or nodes) and a set of edges E. Each edge e∈E connects
two vertices and may have an associated nonnegative weight w(e).
Connected Graph
A graph is called connected if, for every pair of vertices u,v∈V, there exists at least one path in G that leads
from u to v.
Tree
A tree is a connected acyclic graph. Equivalently, a tree with ∣V∣ vertices has exactly ∣V∣−1edges and
contains no cycles.
Spanning Tree
Given a connected graph 𝐺, a spanning tree is a subgraph T⊆G that includes all vertices of G, is connected,
and has no cycles. Therefore T also has ∣𝑉∣−1edges.
Minimum Spanning Tree (MST)
Among all spanning trees of a weighted, connected graph G, a minimum spanning tree is one whose total
edge weight sum,
is minimum. The goal is to find such a tree T.
Examples of MST
Types of MST Algorithms
Below are the three most common greedy algorithms for finding a Minimum Spanning Tree:
Algorithm Key Idea Time Complexity
Sort all edges in increasing weight
Kruskal order, then add each edge if it does O(ElogE) (using Union-Find)
not form a cycle.
Grow the tree one vertex at a time:
at each step, add the smallest-weight
Prim O(ElogV) (using a min-heap)
edge connecting the tree to a new
vertex.
In parallel, for each component select
its cheapest outgoing edge, merge
Borůvka O(ElogV)
components, and repeat until one
tree remains.
Goal: Build a spanning tree by “growing” outward from a starting vertex, always adding the cheapest edge
that connects the current tree to a new vertex.
Key Idea: Maintain a set T of vertices that are already in the MST.
At each step, among all edges with one endpoint in T and the other outside T, choose the edge of minimum
weight—and add its outside endpoint to T. Repeat until T contains every vertex.
Why It Works: By the Cut Property, at each iteration the cheapest edge crossing the cut (𝑇,𝑉∖𝑇) must
belong to some MST. Adding that edge cannot create a cycle, because its outside endpoint was not yet in T.
Data Structures: A min-heap (or priority queue) keyed by the current best connection weight for each
outside vertex.An array key[v] holding the weight of the lightest edge known so far that connects 𝑣v to the
tree.An array parent[v] recording which tree-vertex provides that best connection.
Example of Prim’s Algorithm:
Kruskal's algorithm looks at a minimum spanning tree for a weighted connected graph G = (V, E) as
an acyclic sub graph with |V | - 1 edges for which the sum of the edge weights is the smallest.
Consequently, the algorithm constructs a minimum spanning tree as an expanding sequence of sub
graphs, which are always acyclic but are not necessarily connected on the intermediate stages of
the algorithm.
Working: The algorithm begins by sorting the graph's edges in non-decreasing order of their
weights. Then, starting with the empty subgraph, it scans this sorted list adding the next edge on
the list to the current sub graph if such an inclusion does not create a cycle and simply skipping the
edge otherwise.
In Shortly: There is no shorter path to every corner according to Kruksal's algorithm
Example
Example
In Kruskal’s algorithm, all paths are listed and sorted from smallest to largest. This list is given below for the
above graph:
x-v: 1
w-v: 1
w-u: 1
x-w: 2
u-s: 2
x-y: 3
t-u: 3
u-v: 3
y-v: 4
s-t: 4
y-z: 5
y-t: 5
z-t: 10
The cost of Kruksal’s algorithm is 16.
Let’s create the Kruksal’sAlgortithm using Psuedo code.
Let’s examine the graph step by step with Kruksal Algorithm.
Similarties and diffarences of Prim and Kruksal
Plot nodes S–G and all 15 edges with their costs on paper or using a drawing tool.
Initialize key[S] = 0; all others = ∞.Maintain a min-heap of vertices by key[v].Extract-min, update neighbors’
keys/parents, repeat until all 8 nodes are in the tree.List the resulting edges and compute total cost.
Kruskal first examines the edge S–A, S–B, then A–B (all 10s);
depending on the loop control (union-find), he can skip or
choose A–B (here he can choose and construct a different
path connecting S to A, B, A to C, then C to D).
Prim can start from S and first choose S–A and S–B, then
directly A–C or B–C according to the min-heap order, and
finally B–D or C–D.
Since the “first seen” choices they make during the tie-break
(equal cost edges) are different, the resulting tree structures
are also different.