Docsity

Minimum Spanning Trees: CS231 Algorithms Handout #25, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
CS231 Algorithms Handout #25
Prof Lyn Turbak April 18, 2001
Wellesley College
Minimum Spanning Trees
Reading: CLR Sections 5.4 -- 5.5; Chapter 24
---------------------------------------------------------------------------------------------------------------------
General Graph Terminology
A graph is a pair (V,E) of vertices V and edges E โІ V x V (all pairs of vertices). We only
consider graphs where there is at most one edge between any two vertices. For any vertex v, a
possible edge is (v,v) (a self-edge).
The adjacency list Adj[v] of a vertex v in G is the set of all edges of the form (v,w) in G.
A graph is directed if the each edge (a,b) is interpreted as going from a to b. It is undirected if
(a,b) and (b,a) are considered equivalent edges.
A path in a graph (V,E) is a sequence of vertices <v0, v1, ..., vn> such that each vi โˆˆ Vand each
(vi-1, vi) โˆˆ E. Such a path has length n. The singleton sequence (v0) is a length 0 path. The
sequence ,<v,v> is only a path if E contains the self-edge (v,v).
A path <v0, v1, ..., vn> is a cycle if n >= 1 and v0 = vn. A cycle is simple if v1, ..., vn are distinct
and no edge is repeated. (The latter condition prevents <a,b,a> from being considered a cycle in
an undirected graph.) A graph is acylic if it contains no simple cycles.
An undirected graph is connected if there is a path between any two vertices.
A directed acyclic graph is a DAG. A connected acyclic undirected graph is a tree. An acyclic
undirected graph is a forest.
A subgraph of G = (V,E) is a graph G' = (V',E') where V' โІ V and E' โІ E.
pf3
pf4

Partial preview of the text

Download Minimum Spanning Trees: CS231 Algorithms Handout #25 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout # Prof Lyn Turbak April 18, 2001 Wellesley College

Minimum Spanning Trees

Reading: CLR Sections 5.4 -- 5.5; Chapter 24

--------------------------------------------------------------------------------------------------------------------- General Graph Terminology

A graph is a pair (V,E) of vertices V and edges E โІ V x V (all pairs of vertices). We only consider graphs where there is at most one edge between any two vertices. For any vertex v, a possible edge is (v,v) (a self-edge ).

The adjacency list Adj[v] of a vertex v in G is the set of all edges of the form (v,w) in G.

A graph is directed if the each edge (a,b) is interpreted as going from a to b. It is undirected if (a,b) and (b,a) are considered equivalent edges.

A path in a graph (V,E) is a sequence of vertices <v 0 , v 1 , ..., vn> such that each vi โˆˆ Vand each

(vi-1, vi) โˆˆ E. Such a path has length n. The singleton sequence (v 0 ) is a length 0 path. The sequence ,<v,v> is only a path if E contains the self-edge (v,v).

A path <v 0 , v 1 , ..., vn> is a cycle if n >= 1 and v 0 = vn. A cycle is simple if v 1 , ..., vn are distinct and no edge is repeated. (The latter condition prevents <a,b,a> from being considered a cycle in an undirected graph.) A graph is acylic if it contains no simple cycles.

An undirected graph is connected if there is a path between any two vertices.

A directed acyclic graph is a DAG. A connected acyclic undirected graph is a tree. An acyclic undirected graph is a forest.

A subgraph of G = (V,E) is a graph G' = (V',E') where V' โІ V and E' โІ E.

Minimum Spanning Trees

A weighted graph is a graph (V, E) together with a weighting function w: E --> Real.

The weight of a weighted graph is (^) โˆ‘

e โˆˆ E

w(e).

A spanning tree of a graph (V, E) is a tree (V, E') where E' โІ E.

A sub-spanning tree (my terminology) of (V, E) is a tree (V', E') where V' โІ V and E' โІ E.

A minimum (weight) spanning tree (MST) of a connected, undirected graph G is a spanning tree of G with minimal weight. (It may not be unique.)

--------------------------------------------------------------------------------------------------------------------- Skeleton of Greedy MST Algorithm

The following is the skeleton of a greedy MST algorithm. The skeleton can be instantiated to both Prim's algorithm and Kruskal's algorithm, discussed later.

Idea: Grow a set of edges ST that is a subset of a spanning tree of G. At each step, extend ST by the "best" safe edge -- i.e., the "best" edge that maintains the invariant that ST is a subset of a minimum spanning tree of G.

MST(G, w) ST โ† {} D โ† Init-Data(G) {Initialize auxiliary data structure D.} while not Is-Spanning-Tree?(ST, D) do {Invariant: ST is the subset of a spanning tree.} (a,b) โ† Find-Safe-Edge(ST, w, D) ST โ† ST โˆช {(a, b)} return ST

Note: The spanning tree is represented by the edge set ST, from which the vertices can be unambiguously derived.

Kruskal's Algorithm

Idea: Grow a spanning forest --- a set of sub-spanning trees whose vertex sets are disjoint. Initially, each vertex is a trivial sub-spanning tree. At each step, add the minimum-weight edge between two distinct sub-spanning trees. This "glues" the two trees into a single tree. Eventually there will be a single spanning tree.

In this case, the auxiliary data structure D is a pair of (1) as-yet unprocessed edges of G, sorted by increasing weight; and (2) a partition of the vertices in G, where each set in the partition has the vertices in one tree of the current forest.

Init-Data(G) sorted-edges โ† sort(edges[G])) {sorted by increasing weight} partitions โ† {} for v in vertices(G) do partitions โ† partitions U Singleton-Partition(v) return <sorted-edges, partitions> {returns a pair}

Is-Spanning-Tree?(ST, <edges, partitions>) return Is-Singleton?(partitions)

Find-Safe-Edge(ST, w, <edges, partitions>) (a, b) โ† Least(sorted-edges) {first element of sorted edges} if not Same-Partition?(partition(a), partition(b)) then partitions โ† Union-Partitions(a, b, partitions) return (a, b) else return Find-Safe-Edge(ST, w, <edges - (a,b), partitions>)

Analysis:

  • Initialization: (1) sorting edges: O(E lg(E)) (2) initializing partitions: O(V). In a connected graph, O(V) <= O(E) < O(E lg(E)).
  • At most |E| calls to Same-Partition and Union, each of which costs O(lg(E)). This gives a total time of O(E lg(E)). (Actually, the time per operation is the inverse Ackerman function of E and V, which grows far more slowly than a logarithm.)
  • Total: O(E lg(E)), which = O(E lg(V)) since E = O(V2 ) and O(lg(V2 )) = O(lg(V))