githubEdit

Minimum Spanning Tree

Minimum Spanning Tree (MST)

A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that includes all the vertices of the graph. Or, to say in Laymanโ€™s words, it is a subset of the edges of the graph that forms a tree (acyclic) where every node of the graph is a part of the tree.

The minimum spanning tree has all the properties of a spanning tree, with an added constraint of having the minimum possible weights among all possible spanning trees. Like a spanning tree, there can also be many possible MSTs for a graph.

Minimum Spanning Tree (MST)

Above texts are taken from Geek for geeksarrow-up-right

Now, let's dive deeper with two different algorithms for getting your MST ๐Ÿ™ƒ.

Kruskals

Disjoint Set

Before kruskals let's consider disjoint sets. More information is available here,

Disjoint sets

Here, we are separating each set from other by setting a root for a certain part.

Actual kruskal

If you have the disjoint, you can sort your data and use the set to do the MST!

Prim

Adjacency matrix

For prim, you can do with adjacency list without any priority queue,

Priority Queue

But the more efficient way is to go ahead and use priority queue!

Above code is directly taken from Geek for Geeks.

Last updated

Was this helpful?