Not Frequent
0/14

Minimum Spanning Trees

Authors: Benjamin Qi, Andrew Wang

Contributor: Neo Wang

A subset of the edges of a connected, undirected, edge-weighted graph that connects all the vertices to each other of minimum total weight, where no cycles are allowed.

To review a couple of terms:

• An undirected edge is an edge that goes both ways
• A connected graph is a graph of vertices such that each vertex can reach every other vertex using undirected edges.
• A spanning tree is a set of edges that forms a tree and contains every vertex in the original graph
• A minimum spanning tree is a spanning tree such that the sum of edge weights are minimized

Focus Problem – read through this problem before continuing!

Kruskal's

Kruskal's Algorithm finds the MST by greedily adding edges. For all edges not yet in the MST, we can repeatedly add the edge of minimum weight to the MST except when adding edges that would forms a cycle. This can be done by sorting the edges in order of non-decreasing weight. Furthermore, we can easily determine whether adding an edge will create a cycle in constant time using Union Find. Note that since the most expensive operation is sorting the edges, the computational complexity of Kruskal's Algorithm is $\mathcal{O}(E \log E)$.

Implementation

C++

Resources
Benq (from KACTL)

Disjoint Set Union + Kruskal

#include "DSU.h"
template<class T> T kruskal(int N, vector<pair<T,pi>> ed) {	sort(all(ed));	T ans = 0; DSU D; D.init(N); // edges that unite are in MST	trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f;	return ans;}

Java

public static HashMap<Integer, ArrayList<Integer>> MST;public static PriorityQueue<Edge> pq; //contains all edges
//Assumes that DSU code is includedpublic static void kruskal() {	while (!pq.isEmpty()) {		Edge e = pq.poll();		if (find(e.start) != find(e.end)) {			MST.get(e.start).add(e.end);			MST.get(e.end).add(e.start);

Notice that the road that allows for a "decent route between any two cities," with cost "as small as possible" is the definition of a minimum spanning tree. Thus, we can use our favorite minimum spanning tree algorithm to determine the cost of such a tree by calculating $\sum c$ for all edges included in the tree.

However, we must also account for the impossible case, which occurs when any nodes cannot be connected to the tree. Recall that the minimum spanning tree must contain a total of $n-1$ edges, so we can use a variable $cnt$ that is incremented every time we add an edge to the minimum spanning tree. After running Kruskal's, if $cnt \ne n-1$, then we know that we failed to built the tree properly. Furthermore, since our minimum spanning tree algorithm gurantees no edges are counted twice, we cannot "accidentally" count $n-1$ edges.

C++

#include <iostream>#include <vector>#include <algorithm>using namespace std;
typedef long long ll;typedef pair<int, int> pi;typedef vector<int> vi;
#define trav(a,x) for (auto& a: x)

Java

import java.io.*;import java.util.*;
class kruskal {	static int comp;	static int disjoint[];	static int size[];	public static void main(String[] args) throws IOException {		BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));		PrintWriter out = new PrintWriter(System.out);

Prim's

Resources
CPH
cp-algo
CP2

Similar to Dijkstra's, Prim's algorithm greedily adds vertices. On each iteration, we add the vertex that is closest to the current MST (instead of closest to the source in Dijkstra's) until all vertices have been added.

The process of finding the closest vertex to the MST can be done efficiently using a priority queue. After removing a vertex, we add all of its neighbors that are not yet in the MST to the priority queue and repeat. To begin the algorithm, we simply add any vertex to the priority queue.

Complexity

Our implementation has complexity $\mathcal{O}(E \log E)$ since in the worst case every edge will be checked and its corresponding vertex will be added to the priority queue.

Alternatively, we may linearly search for the closest vertex instead of using a priority queue. Each linear pass runs in time $\mathcal{O}(V)$, and this must be repeated $V$ times. Thus, this version of Prim's algorithm has complexity $\mathcal{O}(V^2)$. As with Dijkstra, this complexity is preferable for dense graphs (in which $E \approx V^2$).

C++

#include <iostream>#include <vector>#include <set>using namespace std;
typedef long long ll;typedef pair<ll, int> pl;typedef vector<int> vi;
#define pb push_back

Java

import java.io.*;import java.util.*;
class prim {	static Map<Integer, ArrayList<Edge>> tree;	static int N, ct;	static long[] dist;	static long max = 10000000000000000 L;	public static void main(String[] args) throws IOException {		BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));

Problems

StatusSourceProblem NameDifficultyTags
Old SilverEasy
Show TagsMST
GoldEasy
Show TagsMST
KSEasy
Show TagsMST
CFEasy
Show TagsMST
CFNormal
Show TagsMST, Math
GoldNormal
Show TagsMST
HRNormal
Show TagsBinary Search, MST
JOINormal
Show TagsBinary Search, MST
GoldNormal
Show TagsMST
PlatHard
Show TagsMST
COCIHard
Show TagsMST, NT
APIOInsane