Minimum Spanning Tree - MST
Definitions
- Spanning tree: Acyclical connected subgraph that contains all the nodes of the graph.
- Weight of a tree: sum of the weights of all the contained edges.
- MST: a spanning tree wherein we have the minimum weight possible
Goal
Create a subgraph containing all the nodes with the smallest weights possible. It's to minimize the total weight unlike dijkstra which aims to minimize the distance to a target.
Prim's Algorithm
Goal
Prim's algorithm works by expanding the MST one node at a time, by way of the smallest weighted edge.
- Start with one given node. Mark it as visited.
- Look at the visited nodes' univsited neighbours, select the edge with the smallest weight and mark that node as visited.
- Repeat step two until all the nodes are marked as visited.
Complexity
, where m is the number of edges and n is the number of nodes
Kruskal's Algorithm
Goal
Keep filling up the sorted edges until you reach an MST.
- Start by sorting all the edges by ascending weightage.
- Go down the list adding the edges as long as we don't achieve a cycle.
Complexity
, where m is the number of edges