Hi peers,
We know Prim’s algorithm can correctly find the minimum spanning tree (MST) in a given graph. Kruskal algorithm (“the algorithm” in the following context) is yet another simple but powerful way to find MST in a graph. In this short essay, I will provide the pseudocode and the proof for the algorithm in two parts.
Pseudocode
Setup:
Given a graph, G (V, E), V: {v1, v2, …, v_n} is the set of vertices in the graph and E: {e1, e2, …, e_n} is the set of edges.
∀ e_i ϵ E,∃ a corresponding cost,c_i; i ∈{1,2,3,…,n}. C:{c1,c2,…,c_n } denotes the set of costs associated with E.
T is the set of edges that have been visited by the algorithm.
m denotes the number of edges, while n denotes the number of vertices.
Initialization: T= ∅
Steps:
- Sort E in order of increasing cost.
- For the counter k = 1 to m:
If ( T ∪{k} has no cycles)
Add k to T.
-Return T as MST.
Proof of Correctness:
To prove, we