Lecture 26: Minimum Spanning Trees

10/29/2020

Spanning Trees

Spanning Trees

MST Applications

MST vs. SPT

A Useful Tool for Finding the MST: Cut Property

Cut Property Proof

Generic MST Finding Algorithm

Prim's Algorithm

Prim's Algorithm

Prim's Algorithm Implementation

Prim's Demo

Prim's vs. Dijkstra's

Prim's Implementation (Psuedocode)

public class PrimMST { public PrimMST(EdgeWeightedGraph G) { edgeTo = new Edge[G.V()]; distTo = new double[G.V()]; marked = new boolean[G.V()]; fringe = new SpecialPQ<Double>(G.V()); distTo[s] = 0.0; setDistancesToInfinityExceptS(s); /* Fringe is ordered by distTo tree. Must be a specialPQ like Dijkstra's */ insertAllVertices(fringe); /* Get vertices in order of distance from tree */ while (!fringe.isEmpty()) { // Get vertex closest to tree that is unvisited int v = fringe.delMin(); // Scan means to consider all of a vertex's outgoing edges scan(G, v) } } private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; // Vertex is closest, so add to MST for (Edge e : G.adj(v)) { int w = e.other(v); /* Already in MST, so go to next edge */ if (marked[w]) {continue;} /* Better path to a particular vertex found, so update current best known for that vertex */ if (e.weight() < distTo[w]) { distTo[w] = e.weight(); edgeTo[w] = e; pq.decreasePriority(w, distTo[w]); } } } }

Prim's Runtime

Kruskal's Algorithm

Kruskal's Demo

Kruskal's Algorithm

Kruskal's Implementation (Psueocode)

public class KruskalMST { private List<edge> mst = new ArrayList<Edge>(); public KruskalMST(EdgeWeightedGraph G) { MinPQ<Edge> pq = new MinPQ<Edge>(); for (Edge e : G.edges()) { pq.insert(e); } WeightedQuickUnionPC uf = new WeightedQuickUnionPC(G.V()); while(!pq.isEmpty() && mst.size() < G.V() - 1) { Edge e = pq.delMin(); int v = e.from(); int w = e.to(); if (!uf.connected(v, w)) { uf.union(v, w); mst.add(e); } } } }

Kruskal's Runtime

Shortest Paths and MSt algorithms Summary

170 Spoiler: State of the Art Compare-Based MST Algorithms

CSM Summary: Graph Algorithms

Depth First Search (DFS)

Breadth First Search (BFS)

Shortest Paths Tree vs Minimum Spanning Tree

Shortest Paths Tree Algorithms

Dijkstra's/A*/Prim's Runtime

Cut Property

MST Algorithms